-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Input required and validation #4327
Comments
As far as I'm concerned, I don't think that this is much of an issue. I think this should be left for the dev to solve and keep it out of react-select' scope. For anyone wondering how I solved this "issue":
The code in question: interface CustomSelectProps extends ReactSelectProps, SharedSelectProps {
native: false;
control: Control<any>;
rules: RegisterOptions;
/**
* The error object
*/
error?: unknown;
/**
* The associated error message
*/
errorMessage?: string;
}
// ...
<Controller
name={name ?? ""}
control={control}
rules={rules}
defaultValue={defaultValue}
as={
<ReactSelect
{...props}
id={normalizedId}
instanceId={normalizedId}
className={cn({
"rounded-md shadow-md": true,
"border-gray-200 ": !error,
"border-red-500": error
})}
/>
}
/> |
In some ways I agree and I do love RHF, but in other ways it feels problematic to tell users to adopt an entire form management library or write your own validation wrapper when there is already built-in native support input and select DOM elements. If react-select is going to position itself as a react based replacement for a select input, it should also support native validation or at least enable its users to do so easily. |
Somebody on the holy internet has solved this, until we have isRequired prop. |
One of the ideas I started thinking about was playing around with the idea of using Constraint Validation by applying a custom validation to the input. The idea is that the required/validation rules would exist on the hidden input, and then by passing a ref to that hidden field, it should be possible to pass the custom validity of the hidden field to the input... at least in theory. This would eliminate the need to add another input to the page and also should play nice with form managers like React Hook Form, Formik, etc.. as it would allow the validation to naturally flow to the named field which is then passed to the input. |
any updates? |
Hi, you mentioned this was a high priority over a year ago but I don't see anything in progress. Is this going to happen? |
2022 and we are still here from 2017 😄 , Folks, have you all thought about implementing this? |
I fully agree with @ebonow that having to use react-hook-form to power the errors as a workaround is undesirable, definitely to tell your users as such. In my usecase we actually already use react-select in conjunction with react-hook-form, but for our form inputs we use the default browser behaviour for validation, this means all of our inputs are great and easy to be validated, but on react-select (and therefore the select component we use) is not that trivial. |
For those of you working with typescript / functional components, here is a modified version of the solution post above: /** react-select-wrapper.less */
/** react-select-wrapper.tsx */
|
still don't have any suitable solution? |
@osvaldokalvaitir |
I was happy with the update, but I couldn't get it to work. You got it? |
Haven't tested it yet. That's a bummer. |
still a wanted feature after 7 years... |
bump here as well - I'm on |
Creating this as a round up of existing open validation issues as of now. Hoping to create some kind of proposed solution and seeking feedback before creating a PR though others are welcome to try as well so long as we all know the challenges and impact of solving for this.
Use-case:
As a developer working within in a form, I would like to apply native html validation to my react-select component.
Duplicates:
#2751 - Required attribute should be passed down...
#3625 - Using Required attribute
#1453 - Add validation support
#4416 - Obtaining focus from the hidden input inside react-select
#3140 - How to make react-select required in a form?
#4450 - How to make reactselect get focus when required and empty
Challenges
1. Single Input
Possible proposal: Introduce a visibly hidden text input which renders the selected value
2. Multi Input
Possible proposal: Introduce a visibly hidden text/select input which concatenates all of the values together and apply a
multiple
attribute3.
isSearchable=false
Possible proposal: Introduce a visibly hidden select input. Apply
multiple
attribute if it is a Multi Input.4. Visibly hidden input
Notes: This visually hidden element cannot be readonly, disabled, have style
display: none
, and would likely havetabindex="-1"
to ensure it does not interfere with focus order.Also currently (and curiously), the name prop is passed to a generated "FormField". My proposition would be to rewrite this formally as a component which also serves the benefit of decomposing Select.js.
5. Styling since anything other than a single searchable Select requires a visibly hidden input (type text or select or anything else a user suggests), then how would styling this element be possible?
Possible proposal:
element.addEventListener('invalid', handleInvalid)
handleInvalid
to set new internal stateisInvalid
isValid
and expose to other components (to style borders, colors, icons, etc)--is-invalid
to this element to be specified in CSS (though:invalid
is always available to the user as well)element.removeEventListener('invalid', handleInvalid)
6. Breaking changes
Would changing the behavior of the FormField be a breaking change? I suppose it is very possible depending on test suites and formalizing this as a component could have other impacts.
Most everything seems additive especially since adding validation would likely be additive, however, due to the nature of this application, there exists the possibility that either
required
orisRequired
could both already be pre-existing props so it is possibleThere is slight possibility of introducing conflicting css names. Introducing
InvalidInput
as a stylable component which is applied asclassNamePrefix__invalid-input
could help avoid these collisions.Perhaps greater likelihood is introducing css to apply to containers. It's possible
classNamePrefix__control--is-invalid
is already used, but I also believe it should be discouraged from applying internal naming conventions.Maybe, we consider adding another className to the control styled element called
classNamePrefix__validated-control
and styles can cascade fromclassNamePrefix__validated-control--is-invalid
to avoid naming conflicts.That said, any feedback? If the changes are breaking then we would want to target version 4 rather than 3.2, but wanted to get community buy-in to better understand how to solve this issue given the flexibility and current functionality of react-select.
The text was updated successfully, but these errors were encountered: