Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clearedColor should be changed when initial value is undefined #48584

Merged
merged 1 commit into from Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
MadCcc marked this conversation as resolved.
Show resolved Hide resolved
if (prevColor.current.cleared === true) {
newColor.cleared = 'controlled';
}
setColorValue(newColor);
}, [value]);

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