diff --git a/components/color-picker/ColorPicker.tsx b/components/color-picker/ColorPicker.tsx index 0d70e445c1..56a7dbf006 100644 --- a/components/color-picker/ColorPicker.tsx +++ b/components/color-picker/ColorPicker.tsx @@ -6,7 +6,7 @@ import type { import classNames from 'classnames'; import useMergedState from 'rc-util/lib/hooks/useMergedState'; import type { CSSProperties } from 'react'; -import React, { useContext, useState } from 'react'; +import React, { useContext, useEffect, useState } from 'react'; import genPurePanel from '../_util/PurePanel'; import type { ConfigConsumerProps } from '../config-provider/context'; import { ConfigContext } from '../config-provider/context'; @@ -89,7 +89,7 @@ const ColorPicker: CompoundedComponent = (props) => { postState: (openData) => !disabled && openData, onChange: onOpenChange, }); - const [clearColor, setClearColor] = useState(false); + const [colorCleared, setColorCleared] = useState(false); const prefixCls = getPrefixCls('color-picker', customizePrefixCls); @@ -101,8 +101,8 @@ const ColorPicker: CompoundedComponent = (props) => { const handleChange = (data: Color) => { const color: Color = generateColor(data); - if (clearColor && color.toHsb().a > 0) { - setClearColor(false); + if (colorCleared && color.toHsb().a > 0) { + setColorCleared(false); } if (!value) { setColorValue(color); @@ -111,7 +111,7 @@ const ColorPicker: CompoundedComponent = (props) => { }; const handleClear = (clear: boolean) => { - setClearColor(clear); + setColorCleared(clear); }; const popoverProps: PopoverProps = { @@ -128,13 +128,19 @@ const ColorPicker: CompoundedComponent = (props) => { prefixCls, color: colorValue, allowClear, - clearColor, + colorCleared, disabled, presets, format, onFormatChange, }; + useEffect(() => { + if (colorCleared) { + setPopupOpen(false); + } + }, [colorCleared]); + return wrapSSR( { style={style} color={colorValue} prefixCls={prefixCls} - clearColor={clearColor} disabled={disabled} + colorCleared={colorCleared} /> )} , diff --git a/components/color-picker/__tests__/index.test.tsx b/components/color-picker/__tests__/index.test.tsx index 9c0b328d7d..8aac0784f9 100644 --- a/components/color-picker/__tests__/index.test.tsx +++ b/components/color-picker/__tests__/index.test.tsx @@ -83,8 +83,11 @@ describe('ColorPicker', () => { const { container } = render(); fireEvent.click(container.querySelector('.ant-color-picker-trigger')!); await waitFakeTimer(); + expect(container.querySelector('.ant-popover-hidden')).toBeFalsy(); expect(container.querySelector('.ant-color-picker-clear')).toBeTruthy(); fireEvent.click(container.querySelector('.ant-color-picker-clear')!); + await waitFakeTimer(); + expect(container.querySelector('.ant-popover-hidden')).toBeTruthy(); expect( container.querySelector('.ant-color-picker-alpha-input input')?.getAttribute('value'), ).toEqual('0%'); diff --git a/components/color-picker/components/ColorTrigger.tsx b/components/color-picker/components/ColorTrigger.tsx index bb7880c8cb..5170b4d1e6 100644 --- a/components/color-picker/components/ColorTrigger.tsx +++ b/components/color-picker/components/ColorTrigger.tsx @@ -6,7 +6,7 @@ import type { ColorPickerBaseProps } from '../interface'; import ColorClear from './ColorClear'; interface colorTriggerProps - extends Pick { + extends Pick { color: Exclude; open?: boolean; className?: string; @@ -17,17 +17,17 @@ interface colorTriggerProps } const ColorTrigger = forwardRef((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 containerNode = useMemo( () => - clearColor ? ( + colorCleared ? ( ) : ( ), - [color, clearColor, prefixCls], + [color, colorCleared, prefixCls], ); return ( diff --git a/components/color-picker/interface.ts b/components/color-picker/interface.ts index 37dce01490..e366156047 100644 --- a/components/color-picker/interface.ts +++ b/components/color-picker/interface.ts @@ -18,7 +18,7 @@ export interface ColorPickerBaseProps { prefixCls: string; format?: keyof typeof ColorFormat; allowClear?: boolean; - clearColor?: boolean; + colorCleared?: boolean; disabled?: boolean; presets?: PresetsItem[]; onFormatChange?: ColorPickerProps['onFormatChange'];