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

Add required prop #4882

Merged
merged 7 commits into from
Nov 2, 2022
Merged

Add required prop #4882

merged 7 commits into from
Nov 2, 2022

Conversation

Rall3n
Copy link
Collaborator

@Rall3n Rall3n commented Oct 28, 2021

This PR adds the new required prop to allow for required validation in forms.

This also adds a new prop requiredMessage to customize the content of the HTML5 validation message. I would have wanted to use a default browser message, but there is no API to get them, and the default for <input type='text'> is imo not applicable for a dropdown component.

@ebonow I don't know if and how this should be implemented into our LiveRegion component. Maybe you can find a solution.

@changeset-bot
Copy link

changeset-bot bot commented Oct 28, 2021

🦋 Changeset detected

Latest commit: 7cc7001

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

This PR includes changesets to release 1 package
Name Type
react-select Minor

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 28, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 7cc7001:

Sandbox Source
react-codesandboxer-example Configuration

@Rall3n
Copy link
Collaborator Author

Rall3n commented Oct 28, 2021

Thinking about using <select> instead of an <input type='text'> for the RequiredInput component (would make requiredMessage obsolete as it would use appropriate strings for dropdowns).

test('form validation with `required` prop', () => {
const components = (value?: Option) => (
<form id="formTest">
<Select {...BASIC_PROPS} required value={value || null} />
Copy link
Collaborator

Choose a reason for hiding this comment

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

For testing, it might be better to test that the value is explicitly passed as null instead of coalescing in case the behavior differs between undefined and null.

Not that it would here, but it's possible some future use-case changes the default behavior and it gets missed.

@Rall3n
Copy link
Collaborator Author

Rall3n commented Jun 29, 2022

Removed the requiredMessage prop. Can be achieved using onInvalid on a <form> element.

@Rall3n Rall3n force-pushed the required-prop branch 3 times, most recently from 998153e to e865624 Compare October 21, 2022 06:26
@lukebennett88 lukebennett88 added the pr/enhancement PRs that are specifically enhancing the project label Oct 21, 2022
@lukebennett88 lukebennett88 removed this from the 6.0 milestone Oct 25, 2022
@Rall3n Rall3n force-pushed the required-prop branch 2 times, most recently from b874afe to 40e26f0 Compare October 27, 2022 06:35
@@ -1992,7 +2009,7 @@ export default class Select<
/>
))
) : (
<input name={name} type="hidden" />
<input name={name} type="hidden" value="" />
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just wondering, does setting value="" mean that it can't be used as an uncontrolled component anymore?

Copy link
Collaborator Author

@Rall3n Rall3n Oct 28, 2022

Choose a reason for hiding this comment

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

It would prevent React from throwing a "Switching from uncontrolled to controlled" error because React reuses the <input> element for RequiredInput component and if a value is set.

I don't know if it was really applicable here anymore, but better safe than sorry.

PS: I added a small comment regarding this in the RequiredInput component.

@lukebennett88 lukebennett88 merged commit c37e86d into JedWatson:master Nov 2, 2022
@github-actions github-actions bot mentioned this pull request Nov 2, 2022
@srinivasmerugu
Copy link

srinivasmerugu commented Dec 10, 2022

@Rall3n @ebonow With this requiredInput component changes we see an accessibility issue on this new tag .

Screenshot 2022-12-10 at 10 09 36 PM

I think on the below element we may need to pass the type="hidden" similar to other elements in the Select.tsx file since its has tabindex="-1".

<input required="" name="licenseOrCertification" tabindex="-1" class="css-5kkxb2-requiredInput-RequiredInput" value="">

could you please add this in the next minor release as axe dev tools (tool which is used to test the accessibility) is throwing a critical error.

@Rall3n Rall3n deleted the required-prop branch December 10, 2022 17:58
@osvaldokalvaitir
Copy link

@Rall3n @ebonow
I tested it and the required property didn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/enhancement PRs that are specifically enhancing the project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants