-
Notifications
You must be signed in to change notification settings - Fork 536
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(FormControl): allow required check boxes #5027
fix(FormControl): allow required check boxes #5027
Conversation
🦋 Changeset detectedLatest commit: f7433a2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
size-limit report 📦
|
…ice-use-required-instead-of-an-asterisk-for-required-fields
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR! I left a question.
I also think we should document these accessibility rules about required checkboxes/radio buttons maybe on the form overview page? - I find them a bit complex 😬
@tbenning might help with this I think if you think documenting them is a good idea, too.
Thanks again!!
@@ -390,6 +391,41 @@ describe('FormControl', () => { | |||
expect(consoleSpy).toHaveBeenCalled() | |||
consoleSpy.mockRestore() | |||
}) | |||
|
|||
it('should not add required prop to individual checkbox', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other cases when signing up on a site, there may be some options like marketing emails and terms and conditions, where terms and conditions is required but marketing emails is not.
It makes me think what if the terms and conditions option is required (which is in most cases) and there are no other options. Does that mean individual checkboxes can also be required even if there are not a part of group?
Let me know if I am missing something here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point, @ichelsea thoughts on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say yes to that; Indvidual checkboxes can be required if not a part of a group of checkboxes. Are there technical limitations to this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No technical limitations, just wanted to confirm since that wasn't previously allowed. thanks for the input!
👋 Hi, this pull request contains changes to the source code that github/github depends on. If you are GitHub staff, we recommend testing these changes with github/github using the integration workflow. Thanks! |
👋 Hi from github/github! Your integration PR is ready: https://github.com/github/github/pull/346259 |
🟢 golden-jobs completed with status |
// eslint-disable-next-line no-console | ||
console.warn('An individual checkbox or radio cannot be a required field.') | ||
console.warn('An individual radio cannot be a required field.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know console.warn
was already here so no need to change - I just wanted to mention that there is a utility function for these cases. And it only runs in the dev environment 🤗
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored to use warning function, thanks for pointing that out 🙏
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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, all good!
@@ -99,9 +100,9 @@ const FormControl = React.forwardRef<HTMLDivElement, FormControlProps>( | |||
) | |||
} | |||
|
|||
if (childrenWithoutSlots.find(child => React.isValidElement(child) && child.props?.required)) { | |||
if (isRadioInput && childrenWithoutSlots.find(child => React.isValidElement(child) && child.props?.required)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we also cover the case of "Radio groups can only have the group receive a required setting because you can only select one choice."
Sorry if I missed it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah so that'd be handled at the RadioGroup level. The FormControl will be contained within the RadioGroup in this case (RadioGroup -> FormControl -> Radio) and doesn't need to be concerned about required being on the parent. Radios will not receive required
from the FromControl regardless
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good! Thanks for clarifying 🙌🏻
…ice-use-required-instead-of-an-asterisk-for-required-fields
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!! 🔥
Thanks for addressing the feedback and working on this 😊
Relates to https://github.com/github/primer/issues/3470
FormControl
by design does not allow individualCheckbox
/Radio
to berequired
. As per:Checkbox
can be required individually or within aCheckboxGroup
(since multiple checkboxes can be checked at once, opposed to radio buttons). This PR introduces functionality toFormControl
allow that behavior to happen.Changelog
New
FormControl
tests for required prop used with individual and group checkboxesChanged
FormControl
to determine if the input is aCheckbox
and pass down therequired
prop in which caseCheckboxGroup
Playground story to showcase one required Checkbox within a CheckboxGroupCheckboxGroup
story snapshots to account forrequired
changeRollout strategy
Testing & Reviewing
FormControl
,Checkbox
orCheckboxGroup
required
attribute on checkboxMerge checklist