mirror of
https://github.com/aphrody-labs/base-ui.git
synced 2026-10-17 12:46:28 +00:00
[checkbox group] Fix parent checkbox with custom validate fn (#4912)
This commit is contained in:
1 parent
3869e3bd1e
commit
aa8d9d56af
2 files changed
+25
-1
No files matched your search
@@ -326,6 +326,30 @@ describe('<CheckboxGroup />', () => {
|
||||
checkboxes.forEach((checkbox) => expect(checkbox).toHaveAttribute('aria-invalid', 'true'));
|
||||
});
|
||||
|
||||
it('validates with the group value when toggling the parent checkbox', async () => {
|
||||
const validateSpy = vi.fn((_value: unknown) => null);
|
||||
|
||||
const { user } = render(
|
||||
<Field.Root validationMode="onChange" validate={validateSpy} name="fruits">
|
||||
<CheckboxGroup allValues={['apple', 'orange']}>
|
||||
<Checkbox.Root parent data-testid="parent" />
|
||||
<Checkbox.Root value="apple" />
|
||||
<Checkbox.Root value="orange" />
|
||||
</CheckboxGroup>
|
||||
</Field.Root>,
|
||||
);
|
||||
|
||||
const parent = screen.getByTestId('parent');
|
||||
|
||||
await user.click(parent);
|
||||
expect(validateSpy).toHaveBeenCalledTimes(1);
|
||||
expect(validateSpy.mock.lastCall?.[0]).toEqual(['apple', 'orange']);
|
||||
|
||||
await user.click(parent);
|
||||
expect(validateSpy).toHaveBeenCalledTimes(2);
|
||||
expect(validateSpy.mock.lastCall?.[0]).toEqual([]);
|
||||
});
|
||||
|
||||
it('revalidates when the controlled value changes externally', async () => {
|
||||
const validateSpy = vi.fn((value: unknown) => {
|
||||
const values = value as string[];
|
||||
|
||||
@@ -191,7 +191,7 @@ export const CheckboxRoot = React.forwardRef(function CheckboxRoot(
|
||||
}, [checked, groupIndeterminate, setFilled]);
|
||||
|
||||
useValueChanged(checked, () => {
|
||||
if (groupContext && !parent) {
|
||||
if (groupContext) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user