-
Notifications
You must be signed in to change notification settings - Fork 615
fix(FormControl): allow required check boxes #5027
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
Changes from all commits
2f36653
86aa75b
002f965
fc03b09
83595a7
de2566f
812bc6d
ccd6082
f7433a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@primer/react": patch | ||
--- | ||
|
||
fix(FormControl): allow required check boxes in CheckboxGroup |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,9 @@ import axe from 'axe-core' | |
import { | ||
Autocomplete, | ||
Checkbox, | ||
CheckboxGroup, | ||
FormControl, | ||
Radio, | ||
Select, | ||
Textarea, | ||
TextInput, | ||
|
@@ -376,20 +378,67 @@ describe('FormControl', () => { | |
consoleSpy.mockRestore() | ||
}) | ||
|
||
it('should warn users if they pass `required` to a checkbox or radio', async () => { | ||
it('should warn users if they pass `required` to a radio', async () => { | ||
const consoleSpy = jest.spyOn(global.console, 'warn').mockImplementation() | ||
|
||
render( | ||
<FormControl required> | ||
<FormControl.Label>{LABEL_TEXT}</FormControl.Label> | ||
<Checkbox required /> | ||
<Radio value="radio" name="radio" required /> | ||
<FormControl.Caption>{CAPTION_TEXT}</FormControl.Caption> | ||
</FormControl>, | ||
) | ||
|
||
expect(consoleSpy).toHaveBeenCalled() | ||
consoleSpy.mockRestore() | ||
}) | ||
|
||
it('should allow required prop to individual checkbox', async () => { | ||
const {getByRole} = render( | ||
<FormControl required> | ||
<FormControl.Label>{LABEL_TEXT}</FormControl.Label> | ||
<Checkbox /> | ||
<FormControl.Caption>{CAPTION_TEXT}</FormControl.Caption> | ||
</FormControl>, | ||
) | ||
|
||
expect(getByRole('checkbox')).toBeRequired() | ||
}) | ||
|
||
it('should not add required prop to individual radio', async () => { | ||
const {getByRole} = render( | ||
<FormControl required> | ||
<FormControl.Label>{LABEL_TEXT}</FormControl.Label> | ||
<Radio value="radio" name="radio" /> | ||
<FormControl.Caption>{CAPTION_TEXT}</FormControl.Caption> | ||
</FormControl>, | ||
) | ||
|
||
expect(getByRole('radio')).not.toBeRequired() | ||
}) | ||
|
||
it('should allow required prop on checkbox if part of CheckboxGroup', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a test for "should allow required prop on radio button if part of RadioButtonGroup"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's my understanding from this that a radio cannot be required, not even when within a RadioButtonGroup, just the group itself can be required (at the top level). I can add a test for "does not allow required prop on radio button when part of RadioButtonGroup" if you think that'd be worth it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No worries, all good! |
||
const {getByTestId} = render( | ||
<CheckboxGroup> | ||
<CheckboxGroup.Label>Checkboxes</CheckboxGroup.Label> | ||
<FormControl required> | ||
<Checkbox value="checkOne" data-testid="checkbox-1" /> | ||
<FormControl.Label>Checkbox one</FormControl.Label> | ||
</FormControl> | ||
<FormControl> | ||
<Checkbox value="checkTwo" data-testid="checkbox-2" /> | ||
<FormControl.Label>Checkbox two</FormControl.Label> | ||
</FormControl> | ||
<FormControl> | ||
<Checkbox value="checkThree" /> | ||
<FormControl.Label>Checkbox three</FormControl.Label> | ||
</FormControl> | ||
</CheckboxGroup>, | ||
) | ||
|
||
expect(getByTestId('checkbox-1')).toBeRequired() | ||
expect(getByTestId('checkbox-2')).not.toBeRequired() | ||
}) | ||
}) | ||
|
||
it('should have no axe violations', async () => { | ||
|
Uh oh!
There was an error while loading. Please reload this page.