From 3ee394870e71c71c7d266101035cd4cf806d8334 Mon Sep 17 00:00:00 2001 From: unix Date: Sun, 29 Mar 2020 05:07:46 +0800 Subject: [PATCH 1/2] feat(loading): add component --- components/index.ts | 1 + components/loading/index.ts | 3 + components/loading/loading.tsx | 138 ++++++++++++++++++++++++++++++ pages/docs/components/loading.mdx | 91 ++++++++++++++++++++ 4 files changed, 233 insertions(+) create mode 100644 components/loading/index.ts create mode 100644 components/loading/loading.tsx create mode 100644 pages/docs/components/loading.mdx diff --git a/components/index.ts b/components/index.ts index 8f877f9..5b28be6 100644 --- a/components/index.ts +++ b/components/index.ts @@ -39,3 +39,4 @@ export { default as Tree } from './file-tree' export { default as Badge } from './badge' export { default as AutoComplete } from './auto-complete' export { default as Collapse } from './collapse' +export { default as Loading } from './loading' diff --git a/components/loading/index.ts b/components/loading/index.ts new file mode 100644 index 0000000..0f2714d --- /dev/null +++ b/components/loading/index.ts @@ -0,0 +1,3 @@ +import Loading from './loading' + +export default Loading diff --git a/components/loading/loading.tsx b/components/loading/loading.tsx new file mode 100644 index 0000000..fe439a3 --- /dev/null +++ b/components/loading/loading.tsx @@ -0,0 +1,138 @@ +import React, { useMemo } from 'react' +import useTheme from '../styles/use-theme' +import withDefaults from '../utils/with-defaults' +import { NormalSizes, NormalTypes } from 'components/utils/prop-types' +import { ZeitUIThemesPalette } from 'components/styles/themes' + +interface Props { + size?: NormalSizes + type?: NormalTypes + color?: string + width?: string + height?: string +} + +const defaultProps = { + size: 'medium' as NormalSizes, + type: 'default' as NormalTypes, +} + +export type LoadingProps = Props & typeof defaultProps & React.HTMLAttributes + +const getIconSize = (size: NormalSizes) => { + const sizes: { [key in NormalSizes]: string } = { + mini: '2px', + small: '3px', + medium: '4px', + large: '5px', + } + return sizes[size] +} + +const getIconBgColor = ( + type: NormalTypes, + palette: ZeitUIThemesPalette, + color?: string, +) => { + const colors: { [key in NormalTypes]: string } = { + default: palette.accents_6, + secondary: palette.secondary, + success: palette.success, + warning: palette.warning, + error: palette.error, + } + + return color ? color : colors[type] +} + +const Loading: React.FC> = React.memo(({ + children, size, type, color, +}) => { + const theme = useTheme() + const width = useMemo(() => getIconSize(size), [size]) + const bgColor = useMemo( + () => getIconBgColor(type, theme.palette, color), + [type, theme.palette, color], + ) + + return ( +
+ + + {children && ( + + )} + + + + + +
+ ) +}) + +export default withDefaults(Loading, defaultProps) diff --git a/pages/docs/components/loading.mdx b/pages/docs/components/loading.mdx new file mode 100644 index 0000000..1f5fb47 --- /dev/null +++ b/pages/docs/components/loading.mdx @@ -0,0 +1,91 @@ +import { Layout, Playground, Attributes } from 'lib/components' +import { Loading, Spacer, Row } from 'components' + +export const meta = { + title: 'Loading', + description: 'Loading', +} + +## Loading + +Indicate an action running in the background. + + + + +`} /> + + + Loading + +`} /> + + + + + + + + + + + + + + + + + + +`} /> + + + + + + + + + + + + + + + + + + + +`} /> + + +Loading.Props + +| Attribute | Description | Type | Accepted values | Default +| ---------- | ---------- | ---- | -------------- | ------ | +| **type** | bg-color type | `NormalTypes` | `'default', 'secondary', 'success', 'warning', 'error'` | `default` | +| **size** | loading size | `NormalSizes` | `'mini', 'small', 'medium', 'large'` | `medium` | +| **color** | custom bg color | `string` | - | - | +| **width** | custom width | `string` | - | `100%` | +| **height** | custom height | `string` | - | `100%` | +| ... | native props | `HTMLAttributes` | `'id', 'title', 'className', ...` | - | + + + +export default ({ children }) => {children} From 904e8e7b15108ae12165a999d82445c3cbda969f Mon Sep 17 00:00:00 2001 From: unix Date: Sun, 29 Mar 2020 05:08:50 +0800 Subject: [PATCH 2/2] feat: migrate to standard loading component --- components/auto-complete/auto-complete.tsx | 10 ++- .../button-dropdown/button-dropdown-item.tsx | 4 +- components/button/button.loading.tsx | 69 ------------------- components/button/button.tsx | 2 +- components/input/input-icon.tsx | 4 +- pages/docs/components/spinner.mdx | 5 +- 6 files changed, 17 insertions(+), 77 deletions(-) delete mode 100644 components/button/button.loading.tsx diff --git a/components/auto-complete/auto-complete.tsx b/components/auto-complete/auto-complete.tsx index 43eb4b8..6cd4c24 100644 --- a/components/auto-complete/auto-complete.tsx +++ b/components/auto-complete/auto-complete.tsx @@ -6,7 +6,7 @@ import AutoCompleteSearching from './auto-complete-searching' import AutoCompleteEmpty from './auto-complete-empty' import { AutoCompleteContext, AutoCompleteConfig } from './auto-complete-context' import { NormalSizes, NormalTypes } from '../utils/prop-types' -import ButtonLoading from '../button/button.loading' +import Loading from '../loading' import { pickChild } from 'components/utils/collections' export type AutoCompleteOption = { @@ -62,7 +62,7 @@ const childrenToOptionsNode = (options: AutoCompleteOptions) => { // When the search is seted, at least one element should exist to avoid re-render. const getSearchIcon = (searching?: boolean) => { if (searching === undefined) return null - return searching ? : + return searching ? : } const AutoComplete: React.FC> = ({ @@ -146,6 +146,12 @@ const AutoComplete: React.FC> = ({ .auto-complete { width: ${width || 'max-content'}; } + + .auto-complete :global(.loading) { + left: -3px; + right: -3px; + width: max-content; + } `} diff --git a/components/button-dropdown/button-dropdown-item.tsx b/components/button-dropdown/button-dropdown-item.tsx index 8b3d145..e73ed3e 100644 --- a/components/button-dropdown/button-dropdown-item.tsx +++ b/components/button-dropdown/button-dropdown-item.tsx @@ -4,7 +4,7 @@ import withDefaults from '../utils/with-defaults' import { getColor } from './styles' import { useButtonDropdown } from './button-dropdown-context' import { getButtonSize } from '../button/styles' -import ButtonLoading from '../button/button.loading' +import Loading from '../loading' import { NormalTypes } from '../utils/prop-types' interface Props { @@ -43,7 +43,7 @@ const ButtonDropdownItem: React.FC - {loading ? : children} + {loading ? : children} - - ) -}) - -export default ButtonLoading diff --git a/components/button/button.tsx b/components/button/button.tsx index 3b18f0a..55cb7c2 100644 --- a/components/button/button.tsx +++ b/components/button/button.tsx @@ -3,7 +3,7 @@ import withDefaults from '../utils/with-defaults' import useTheme from '../styles/use-theme' import { ButtonTypes, NormalSizes } from '../utils/prop-types' import ButtonDrip from './button.drip' -import ButtonLoading from './button.loading' +import ButtonLoading from '../loading' import { getButtonColors, getButtonCursor, getButtonHoverColors, getButtonSize } from './styles' interface Props { diff --git a/components/input/input-icon.tsx b/components/input/input-icon.tsx index a9fa558..2a1f38f 100644 --- a/components/input/input-icon.tsx +++ b/components/input/input-icon.tsx @@ -18,10 +18,10 @@ const InputIcon: React.FC = React.memo(({ }, [theme.layout.gap, ratio]) return ( - + {icon}