diff --git a/components/color-picker/ColorPicker.tsx b/components/color-picker/ColorPicker.tsx index 697af63d5a..ea8083c1ba 100644 --- a/components/color-picker/ColorPicker.tsx +++ b/components/color-picker/ColorPicker.tsx @@ -1,4 +1,7 @@ -import type { ColorPickerProps as RcColorPickerProps } from '@rc-component/color-picker'; +import type { + HsbaColorType, + ColorPickerProps as RcColorPickerProps, +} from '@rc-component/color-picker'; import classNames from 'classnames'; import useMergedState from 'rc-util/lib/hooks/useMergedState'; import type { CSSProperties } from 'react'; @@ -95,10 +98,16 @@ const ColorPicker: CompoundedComponent = (props) => { }); const mergeCls = classNames(mergeRootCls, className, hashId); - const handleChange = (data: Color) => { - const color: Color = generateColor(data); - if (colorCleared && color.toHsb().a > 0) { + const handleChange = (data: Color, type?: HsbaColorType) => { + let color: Color = generateColor(data); + if (colorCleared) { setColorCleared(false); + const hsba = color.toHsb(); + // ignore alpha slider + if (colorValue.toHsb().a === 0 && type !== 'alpha') { + hsba.a = 1; + color = generateColor(hsba); + } } if (!value) { setColorValue(color); diff --git a/components/color-picker/ColorPickerPanel.tsx b/components/color-picker/ColorPickerPanel.tsx index 8a4115cdd2..c6841c2dae 100644 --- a/components/color-picker/ColorPickerPanel.tsx +++ b/components/color-picker/ColorPickerPanel.tsx @@ -1,3 +1,4 @@ +import type { HsbaColorType } from '@rc-component/color-picker'; import RcColorPicker from '@rc-component/color-picker'; import type { FC } from 'react'; import React from 'react'; @@ -9,7 +10,7 @@ import ColorPresets from './components/ColorPresets'; import type { ColorPickerBaseProps } from './interface'; interface ColorPickerPanelProps extends ColorPickerBaseProps { - onChange?: (value?: Color) => void; + onChange?: (value?: Color, type?: HsbaColorType) => void; onClear?: (clear?: boolean) => void; } diff --git a/components/color-picker/__tests__/index.test.tsx b/components/color-picker/__tests__/index.test.tsx index 8aac0784f9..7f9083d061 100644 --- a/components/color-picker/__tests__/index.test.tsx +++ b/components/color-picker/__tests__/index.test.tsx @@ -86,6 +86,7 @@ describe('ColorPicker', () => { 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( @@ -94,12 +95,19 @@ describe('ColorPicker', () => { expect( container.querySelector('.ant-color-picker-trigger .ant-color-picker-clear'), ).toBeTruthy(); - fireEvent.change(container.querySelector('.ant-color-picker-alpha-input input')!, { - target: { value: 1 }, + + fireEvent.click(container.querySelector('.ant-color-picker-trigger')!); + await waitFakeTimer(); + expect( + container.querySelector('.ant-color-picker-alpha-input input')?.getAttribute('value'), + ).toEqual('0%'); + + fireEvent.change(container.querySelector('.ant-color-picker-hex-input input')!, { + target: { value: '#273B57' }, }); expect( - container.querySelector('.ant-color-picker-trigger .ant-color-picker-clear'), - ).toBeFalsy(); + container.querySelector('.ant-color-picker-alpha-input input')?.getAttribute('value'), + ).toEqual('100%'); }); it('Should render trigger work', async () => { diff --git a/package.json b/package.json index 0d8c245223..2c7f728ca1 100644 --- a/package.json +++ b/package.json @@ -113,7 +113,7 @@ "@ant-design/react-slick": "~1.0.0", "@babel/runtime": "^7.18.3", "@ctrl/tinycolor": "^3.6.0", - "@rc-component/color-picker": "~1.1.0", + "@rc-component/color-picker": "~1.1.1", "@rc-component/mutate-observer": "^1.0.0", "@rc-component/tour": "~1.8.0", "@rc-component/trigger": "^1.13.0", @@ -323,4 +323,4 @@ "*.{ts,tsx,js,jsx}": "prettier --ignore-unknown --write", "*.{json,less,md}": "prettier --ignore-unknown --write" } -} +} \ No newline at end of file