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(FormControl): allow required check boxes #5027

Conversation

francinelucca
Copy link
Member

@francinelucca francinelucca commented Sep 25, 2024

Relates to https://github.com/github/primer/issues/3470

FormControl by design does not allow individual Checkbox/Radio to be required. As per:

Changelog

New

  • Adds FormControl tests for required prop used with individual and group checkboxes

Changed

  • Updates choice group checks in FormControl to determine if the input is a Checkbox and pass down the required prop in which case
  • Updates CheckboxGroup Playground story to showcase one required Checkbox within a CheckboxGroup
  • Updates CheckboxGroup story snapshots to account for required change

Rollout strategy

  • Patch release
  • Minor release
  • Major release; if selected, include a written rollout or migration plan
  • None; if selected, include a brief description as to why

Testing & Reviewing

  • Verify required checkbox within CheckboxGroup playground story, notice required prop on input in DOM.
  • There should be no visual changes to FormControl, Checkbox or CheckboxGroup
  • Verify Default FormControl story now properly marks required attribute on checkbox

Merge checklist

Copy link

changeset-bot bot commented Sep 25, 2024

🦋 Changeset detected

Latest commit: f7433a2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

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

Copy link
Contributor

github-actions bot commented Sep 25, 2024

size-limit report 📦

Path Size
packages/react/dist/browser.esm.js 97.31 KB (-0.31% 🔽)
packages/react/dist/browser.umd.js 97.51 KB (-0.29% 🔽)

…ice-use-required-instead-of-an-asterisk-for-required-fields
Copy link
Member

@broccolinisoup broccolinisoup left a 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 () => {
Copy link
Member

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.

Copy link
Member Author

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?

Copy link

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?

Copy link
Member Author

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!

Copy link
Contributor

github-actions bot commented Oct 8, 2024

👋 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!

@github-actions github-actions bot added the integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm label Oct 8, 2024
@github-actions github-actions bot temporarily deployed to storybook-preview-5027 October 8, 2024 20:24 Inactive
@primer-integration
Copy link

👋 Hi from github/github! Your integration PR is ready: https://github.com/github/github/pull/346259

@primer-integration
Copy link

🟢 golden-jobs completed with status success.

@francinelucca francinelucca changed the title fix(FormControl): allow required check boxes in CheckboxGroup fix(FormControl): allow required check boxes Oct 9, 2024
// 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.')
Copy link
Member

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 🤗

Copy link
Member Author

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 () => {
Copy link
Member

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"?

Copy link
Member Author

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

Copy link
Member

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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

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

Copy link
Member

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
Copy link
Member

@broccolinisoup broccolinisoup left a 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 😊

@francinelucca francinelucca added this pull request to the merge queue Oct 21, 2024
@francinelucca francinelucca added integration-tests: passing Changes in this PR do NOT cause breaking changes in gh/gh and removed integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm labels Oct 21, 2024
Merged via the queue into main with commit 9a30c63 Oct 21, 2024
49 checks passed
@francinelucca francinelucca deleted the francinelucca/3470-prcformcontrol-best-practice-use-required-instead-of-an-asterisk-for-required-fields branch October 21, 2024 19:18
@primer primer bot mentioned this pull request Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: FormControl integration-tests: passing Changes in this PR do NOT cause breaking changes in gh/gh staff Author is a staff member status: review needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants