Skip to content

Commit 4b97149

Browse files
authored
fix(S2): clear isRequired context for CheckboxGroup in Form (#7522)
* clear isRequired context * add test * remove typescript
1 parent adae13c commit 4b97149

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

packages/@react-spectrum/s2/src/CheckboxGroup.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export const CheckboxGroup = forwardRef(function CheckboxGroup(props: CheckboxGr
120120
columnGap: 16,
121121
flexWrap: 'wrap'
122122
})({orientation})}>
123-
<FormContext.Provider value={{...formContext, size}}>
123+
<FormContext.Provider value={{...formContext, size, isRequired: undefined}}>
124124
<CheckboxContext.Provider value={{isEmphasized}}>
125125
{children}
126126
</CheckboxContext.Provider>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2024 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
13+
import {act, pointerMap, render} from '@react-spectrum/test-utils-internal';
14+
import {Checkbox, CheckboxGroup, Form} from '../src';
15+
import React from 'react';
16+
import userEvent from '@testing-library/user-event';
17+
18+
describe('CheckboxGroup', () => {
19+
let user;
20+
beforeAll(() => {
21+
user = userEvent.setup({delay: null, pointerMap});
22+
});
23+
24+
it('should not require all checkboxes to be checked when Form has isRequired', async () => {
25+
let {getByRole, getAllByRole, getByTestId} = render(
26+
<Form data-testid="form" isRequired>
27+
<CheckboxGroup label="Favorite sports">
28+
<Checkbox value="soccer">Soccer</Checkbox>
29+
<Checkbox value="baseball">Baseball</Checkbox>
30+
<Checkbox value="basketball">Basketball</Checkbox>
31+
</CheckboxGroup>
32+
</Form>
33+
);
34+
35+
36+
let group = getByRole('group');
37+
let checkbox = getAllByRole('checkbox')[0];
38+
39+
await user.click(checkbox);
40+
act(() => {getByTestId('form').checkValidity();});
41+
expect(group).not.toHaveAttribute('aria-describedby');
42+
expect(group).not.toHaveAttribute('data-invalid');
43+
44+
await user.click(checkbox);
45+
act(() => {getByTestId('form').checkValidity();});
46+
expect(group).toHaveAttribute('data-invalid');
47+
expect(group).toHaveAttribute('aria-describedby');
48+
let errorMsg = document.getElementById(group.getAttribute('aria-describedby'));
49+
expect(errorMsg).toHaveTextContent('Constraints not satisfied');
50+
});
51+
});
52+
53+

0 commit comments

Comments
 (0)