-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Allow to disable empty item option for SelectInput #7632
Comments
That's because react-hook-form requires a default value to be set, which is an empty string. Adding this option would mean to check that when the |
To be honest I find the current behavior pretty confusing. It's completely unexpected that I get an additional (empty) entry in the select, in addition to the ones I specify as In order to not make it more complicated than necessary, you could simply use the first element of the choices as default, if no default is given explicitly. That way, you don't need to check if a valid default value has been given. |
I agree with @weissu42 about the non-sens part of having the possibility to select an empty value when the form is required. What we add before 4.x: What we have now:
@djhi This may be doable with conditional typing. However, I don't think we should require a default value when we don't want an empty option. For our use case, we have to ensure the value of the select is selected by a human to avoid mistakes because of the default value. In a nutshell, the same behavior as for 3.x. |
I agree with @weissu42 and @ghost91- and @soullivaneuh that there are many situations where having a spurious blank value makes no sense. There are many scenarios and common ones where one must pick from a set of options and a non-choice is not an acceptable option and the added empty entry (especially as the default) makes for a confusing interface which creates erroneous data by default. Adding a blank entry is inappropriate, confusing and problematic for many scenarios. Furthermore, requiring the developer to set parameters like "EmptyText" and "EmptyValue" to override a blank default is a solution which introduces problems of its own. When one has a field that requires a value from a referenced column one an implementer is forced to choose between hardcoding a foreign key value or dealing with the case where a user selects an unacceptable blank value. Either case foists problems on an implementer to deal with an undesired artifact of react-admin's behavior. |
If the field isn't required, the empty option is necessary to empty it. We'll go with removing the empty option if the field is required. |
Fixed by #8039 |
Is your feature request related to a problem? Please describe.
I use the SelectInput for a required property, thus it makes no sense to allow to choose the empty option.
I checked the docs and finally also the code and while it is possible to customize the emptyText and emptyValue, it seems there is no way to not render it at all (SelectInput.tsx). It looks like this should be pretty easy to add (however I am of course by no means familiar with the code).
Describe the solution you'd like
Add an option to disable rendering the empty option, something like:
<SelectInput source="foo" choices={fooChoices} showEmptyOption={false} />
Of course this can simply default to
true
to ensure it's not a breaking changeDescribe alternatives you've considered
validate={[required('Foo cannot be empty')]}
to at least prevent the user from saving the form without picking a value, but it would be much nicer to not give users the opportunity to empty the input in the first placeThe text was updated successfully, but these errors were encountered: