feat(ColorPicker): close panel when click clear button (#42406)

* feat: close panel when click clear button

* feat: optimize code

* feat: optimize code

* feat: optimize code
This commit is contained in:
kiner-tang(文辉) 2023-05-17 09:58:41 +08:00 committed by GitHub
parent d7de0d846e
commit 7183eab0a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 12 deletions

View File

@ -6,7 +6,7 @@ import type {
import classNames from 'classnames'; import classNames from 'classnames';
import useMergedState from 'rc-util/lib/hooks/useMergedState'; import useMergedState from 'rc-util/lib/hooks/useMergedState';
import type { CSSProperties } from 'react'; import type { CSSProperties } from 'react';
import React, { useContext, useState } from 'react'; import React, { useContext, useEffect, useState } from 'react';
import genPurePanel from '../_util/PurePanel'; import genPurePanel from '../_util/PurePanel';
import type { ConfigConsumerProps } from '../config-provider/context'; import type { ConfigConsumerProps } from '../config-provider/context';
import { ConfigContext } from '../config-provider/context'; import { ConfigContext } from '../config-provider/context';
@ -89,7 +89,7 @@ const ColorPicker: CompoundedComponent = (props) => {
postState: (openData) => !disabled && openData, postState: (openData) => !disabled && openData,
onChange: onOpenChange, onChange: onOpenChange,
}); });
const [clearColor, setClearColor] = useState(false); const [colorCleared, setColorCleared] = useState(false);
const prefixCls = getPrefixCls('color-picker', customizePrefixCls); const prefixCls = getPrefixCls('color-picker', customizePrefixCls);
@ -101,8 +101,8 @@ const ColorPicker: CompoundedComponent = (props) => {
const handleChange = (data: Color) => { const handleChange = (data: Color) => {
const color: Color = generateColor(data); const color: Color = generateColor(data);
if (clearColor && color.toHsb().a > 0) { if (colorCleared && color.toHsb().a > 0) {
setClearColor(false); setColorCleared(false);
} }
if (!value) { if (!value) {
setColorValue(color); setColorValue(color);
@ -111,7 +111,7 @@ const ColorPicker: CompoundedComponent = (props) => {
}; };
const handleClear = (clear: boolean) => { const handleClear = (clear: boolean) => {
setClearColor(clear); setColorCleared(clear);
}; };
const popoverProps: PopoverProps = { const popoverProps: PopoverProps = {
@ -128,13 +128,19 @@ const ColorPicker: CompoundedComponent = (props) => {
prefixCls, prefixCls,
color: colorValue, color: colorValue,
allowClear, allowClear,
clearColor, colorCleared,
disabled, disabled,
presets, presets,
format, format,
onFormatChange, onFormatChange,
}; };
useEffect(() => {
if (colorCleared) {
setPopupOpen(false);
}
}, [colorCleared]);
return wrapSSR( return wrapSSR(
<Popover <Popover
style={styles?.popup} style={styles?.popup}
@ -152,8 +158,8 @@ const ColorPicker: CompoundedComponent = (props) => {
style={style} style={style}
color={colorValue} color={colorValue}
prefixCls={prefixCls} prefixCls={prefixCls}
clearColor={clearColor}
disabled={disabled} disabled={disabled}
colorCleared={colorCleared}
/> />
)} )}
</Popover>, </Popover>,

View File

@ -83,8 +83,11 @@ describe('ColorPicker', () => {
const { container } = render(<ColorPicker allowClear />); const { container } = render(<ColorPicker allowClear />);
fireEvent.click(container.querySelector('.ant-color-picker-trigger')!); fireEvent.click(container.querySelector('.ant-color-picker-trigger')!);
await waitFakeTimer(); await waitFakeTimer();
expect(container.querySelector('.ant-popover-hidden')).toBeFalsy();
expect(container.querySelector('.ant-color-picker-clear')).toBeTruthy(); expect(container.querySelector('.ant-color-picker-clear')).toBeTruthy();
fireEvent.click(container.querySelector('.ant-color-picker-clear')!); fireEvent.click(container.querySelector('.ant-color-picker-clear')!);
await waitFakeTimer();
expect(container.querySelector('.ant-popover-hidden')).toBeTruthy();
expect( expect(
container.querySelector('.ant-color-picker-alpha-input input')?.getAttribute('value'), container.querySelector('.ant-color-picker-alpha-input input')?.getAttribute('value'),
).toEqual('0%'); ).toEqual('0%');

View File

@ -6,7 +6,7 @@ import type { ColorPickerBaseProps } from '../interface';
import ColorClear from './ColorClear'; import ColorClear from './ColorClear';
interface colorTriggerProps interface colorTriggerProps
extends Pick<ColorPickerBaseProps, 'prefixCls' | 'clearColor' | 'disabled'> { extends Pick<ColorPickerBaseProps, 'prefixCls' | 'colorCleared' | 'disabled'> {
color: Exclude<ColorPickerBaseProps['color'], undefined>; color: Exclude<ColorPickerBaseProps['color'], undefined>;
open?: boolean; open?: boolean;
className?: string; className?: string;
@ -17,17 +17,17 @@ interface colorTriggerProps
} }
const ColorTrigger = forwardRef<HTMLDivElement, colorTriggerProps>((props, ref) => { const ColorTrigger = forwardRef<HTMLDivElement, colorTriggerProps>((props, ref) => {
const { color, prefixCls, open, clearColor, disabled, className, ...rest } = props; const { color, prefixCls, open, colorCleared, disabled, className, ...rest } = props;
const colorTriggerPrefixCls = `${prefixCls}-trigger`; const colorTriggerPrefixCls = `${prefixCls}-trigger`;
const containerNode = useMemo<React.ReactNode>( const containerNode = useMemo<React.ReactNode>(
() => () =>
clearColor ? ( colorCleared ? (
<ColorClear prefixCls={prefixCls} /> <ColorClear prefixCls={prefixCls} />
) : ( ) : (
<ColorBlock prefixCls={prefixCls} color={color.toRgbString()} /> <ColorBlock prefixCls={prefixCls} color={color.toRgbString()} />
), ),
[color, clearColor, prefixCls], [color, colorCleared, prefixCls],
); );
return ( return (

View File

@ -18,7 +18,7 @@ export interface ColorPickerBaseProps {
prefixCls: string; prefixCls: string;
format?: keyof typeof ColorFormat; format?: keyof typeof ColorFormat;
allowClear?: boolean; allowClear?: boolean;
clearColor?: boolean; colorCleared?: boolean;
disabled?: boolean; disabled?: boolean;
presets?: PresetsItem[]; presets?: PresetsItem[];
onFormatChange?: ColorPickerProps['onFormatChange']; onFormatChange?: ColorPickerProps['onFormatChange'];