Browse Source

style: optimize ColorPicker code (#42326)

* chore: fix ColorPicker parameters' type

* chore: improve code

* chore: update
pull/41208/merge
qmhc 2 years ago
committed by GitHub
parent
commit
828db96962
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      components/color-picker/ColorPicker.tsx
  2. 2
      components/color-picker/ColorPickerPanel.tsx
  3. 6
      components/color-picker/components/ColorPresets.tsx
  4. 5
      components/color-picker/hooks/useColorState.ts
  5. 2
      components/color-picker/index.en-US.md
  6. 0
      components/color-picker/index.ts
  7. 2
      components/color-picker/index.zh-CN.md

2
components/color-picker/ColorPicker.tsx

@ -28,7 +28,7 @@ export interface ColorPickerProps
> { > {
value?: Color | string; value?: Color | string;
defaultValue?: Color | string; defaultValue?: Color | string;
children?: React.ReactElement; children?: React.ReactNode;
open?: boolean; open?: boolean;
disabled?: boolean; disabled?: boolean;
placement?: TriggerPlacement; placement?: TriggerPlacement;

2
components/color-picker/ColorPickerPanel.tsx

@ -17,7 +17,7 @@ const ColorPickerPanel: FC<ColorPickerPanelProps> = (props) => {
const { prefixCls, allowClear, presets, onChange, onClear, color, ...injectProps } = props; const { prefixCls, allowClear, presets, onChange, onClear, color, ...injectProps } = props;
const colorPickerPanelPrefixCls = `${prefixCls}-inner-panel`; const colorPickerPanelPrefixCls = `${prefixCls}-inner-panel`;
const extraPanelRender = (panel: React.ReactElement) => ( const extraPanelRender = (panel: React.ReactNode) => (
<div className={colorPickerPanelPrefixCls}> <div className={colorPickerPanelPrefixCls}>
{allowClear && ( {allowClear && (
<ColorClear <ColorClear

6
components/color-picker/components/ColorPresets.tsx

@ -19,7 +19,7 @@ interface ColorPresetsProps extends Pick<ColorPickerBaseProps, 'prefixCls'> {
const genPresetColor = (list: PresetsItem[]) => const genPresetColor = (list: PresetsItem[]) =>
list.map((value) => { list.map((value) => {
value.colors = value.colors.map((color) => generateColor(color)); value.colors = value.colors.map(generateColor);
return value; return value;
}); });
@ -35,7 +35,7 @@ const ColorPresets: FC<ColorPresetsProps> = ({ prefixCls, presets, value: color,
const [locale] = useLocale('ColorPicker'); const [locale] = useLocale('ColorPicker');
const [presetsValue] = useMergedState(genPresetColor(presets), { const [presetsValue] = useMergedState(genPresetColor(presets), {
value: genPresetColor(presets), value: genPresetColor(presets),
postState: (item) => genPresetColor(item), postState: genPresetColor,
}); });
const colorPresetsPrefixCls = `${prefixCls}-presets`; const colorPresetsPrefixCls = `${prefixCls}-presets`;
@ -57,7 +57,7 @@ const ColorPresets: FC<ColorPresetsProps> = ({ prefixCls, presets, value: color,
key={`panel-${preset?.label}`} key={`panel-${preset?.label}`}
> >
<div className={`${colorPresetsPrefixCls}-items`}> <div className={`${colorPresetsPrefixCls}-items`}>
{Array.isArray(preset?.colors) && preset?.colors.length > 0 ? ( {Array.isArray(preset?.colors) && preset?.colors.length ? (
preset.colors.map((presetColor: Color) => ( preset.colors.map((presetColor: Color) => (
<ColorBlock <ColorBlock
key={`preset-${presetColor.toHexString()}`} key={`preset-${presetColor.toHexString()}`}

5
components/color-picker/hooks/useColorState.ts

@ -15,7 +15,7 @@ const useColorState = (
): [Color, React.Dispatch<React.SetStateAction<Color>>] => { ): [Color, React.Dispatch<React.SetStateAction<Color>>] => {
const { defaultValue, value } = option; const { defaultValue, value } = option;
const [colorValue, setColorValue] = useState(() => { const [colorValue, setColorValue] = useState(() => {
let mergeState; let mergeState: string | Color | undefined;
if (hasValue(value)) { if (hasValue(value)) {
mergeState = value; mergeState = value;
} else if (hasValue(defaultValue)) { } else if (hasValue(defaultValue)) {
@ -23,8 +23,7 @@ const useColorState = (
} else { } else {
mergeState = defaultStateValue; mergeState = defaultStateValue;
} }
const genColor = generateColor(mergeState || ''); return generateColor(mergeState || '');
return genColor;
}); });
useEffect(() => { useEffect(() => {

2
components/color-picker/index.en-US.md

@ -41,7 +41,7 @@ Used when the user needs to customize the color selection
| onChange | Callback when `value` is changed | `(value: Color, hex: string) => void` | - | | onChange | Callback when `value` is changed | `(value: Color, hex: string) => void` | - |
| allowClear | Allow clearing color selected | boolean | false | | allowClear | Allow clearing color selected | boolean | false |
| presets | Preset colors | `{ label: ReactNode, colors: Array<string \| Color> }[]` | - | | presets | Preset colors | `{ label: ReactNode, colors: Array<string \| Color> }[]` | - |
| children | Trigger of ColorPicker | ReactElement | - | | children | Trigger of ColorPicker | React.ReactNode | - |
| trigger | ColorPicker trigger mode | `hover` \| `click` | `click` | | trigger | ColorPicker trigger mode | `hover` \| `click` | `click` |
| open | Whether to show popup | boolean | - | | open | Whether to show popup | boolean | - |
| onOpenChange | Callback when `open` is changed | `(open: boolean) => void` | - | | onOpenChange | Callback when `open` is changed | `(open: boolean) => void` | - |

0
components/color-picker/index.tsx → components/color-picker/index.ts

2
components/color-picker/index.zh-CN.md

@ -42,7 +42,7 @@ group:
| onChange | 颜色变化的回调 | `(value: Color, hex: string) => void` | - | | onChange | 颜色变化的回调 | `(value: Color, hex: string) => void` | - |
| allowClear | 允许清除选择的颜色 | boolean | false | | allowClear | 允许清除选择的颜色 | boolean | false |
| presets | 预设的颜色 | `{ label: ReactNode, colors: Array<string \| Color> }[]` | - | | presets | 预设的颜色 | `{ label: ReactNode, colors: Array<string \| Color> }[]` | - |
| children | 颜色选择器的触发器 | ReactElement | - | | children | 颜色选择器的触发器 | React.ReactNode | - |
| trigger | 颜色选择器的触发模式 | `hover` \| `click` | `click` | | trigger | 颜色选择器的触发模式 | `hover` \| `click` | `click` |
| open | 是否显示弹出窗口 | boolean | - | | open | 是否显示弹出窗口 | boolean | - |
| onOpenChange | 当 `open` 被改变时的回调 | `(open: boolean) => void` | - | | onOpenChange | 当 `open` 被改变时的回调 | `(open: boolean) => void` | - |

Loading…
Cancel
Save