Skip to content

Commit

Permalink
fix: clearedColor should be changed when initial value is undefined (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MadCcc committed Apr 23, 2024
1 parent 09cb3f4 commit ce176a8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
22 changes: 19 additions & 3 deletions components/color-picker/__tests__/index.test.tsx
Expand Up @@ -700,21 +700,37 @@ describe('ColorPicker', () => {
});

describe('default clearValue should be changed', () => {
const Demo = () => {
const [color, setColor] = useState<string>('');
const Demo = ({ defaultValue }: { defaultValue?: string }) => {
const [color, setColor] = useState<string | undefined>(defaultValue);
useEffect(() => {
setColor('#1677ff');
}, []);
return <ColorPicker value={color} allowClear />;
};

it('normal', () => {
const { container } = render(<Demo />);
const { container } = render(<Demo defaultValue="" />);

expect(container.querySelector('.ant-color-picker-clear')).toBeFalsy();
});

it('strict', () => {
const { container } = render(
<React.StrictMode>
<Demo defaultValue="" />
</React.StrictMode>,
);

expect(container.querySelector('.ant-color-picker-clear')).toBeFalsy();
});

it('default undefined, normal', () => {
const { container } = render(<Demo />);

expect(container.querySelector('.ant-color-picker-clear')).toBeFalsy();
});

it('default undefined, strict', () => {
const { container } = render(
<React.StrictMode>
<Demo />
Expand Down
10 changes: 4 additions & 6 deletions components/color-picker/hooks/useColorState.ts
Expand Up @@ -43,13 +43,11 @@ const useColorState = (
return;
}
prevValue.current = value;
if (hasValue(value)) {
const newColor = generateColor(value || '');
if (prevColor.current.cleared === true) {
newColor.cleared = 'controlled';
}
setColorValue(newColor);
const newColor = generateColor(hasValue(value) ? value || '' : prevColor.current);
if (prevColor.current.cleared === true) {
newColor.cleared = 'controlled';
}
setColorValue(newColor);
}, [value]);

return [colorValue, setColorValue, prevColor] as const;
Expand Down

0 comments on commit ce176a8

Please sign in to comment.