Allow to disable empty item option for SelectInput
See original GitHub issueIs 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 change
Describe alternatives you’ve considered
- I can add
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 place
Issue Analytics
- State:
- Created a year ago
- Reactions:13
- Comments:6 (4 by maintainers)
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.
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.