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

Allow to disable empty item option for SelectInput #7632

Closed
weissu42 opened this issue May 4, 2022 · 6 comments
Closed

Allow to disable empty item option for SelectInput #7632

weissu42 opened this issue May 4, 2022 · 6 comments
Assignees

Comments

@weissu42
Copy link

weissu42 commented May 4, 2022

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 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
@djhi
Copy link
Collaborator

djhi commented May 5, 2022

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 showEmptyOption prop is false, developers also provided a valid default value. I don't think we should as it make the SelectInput API more complicated both for us and for developpers.

@ghost91-
Copy link

ghost91- commented May 18, 2022

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 choices. The fact that there currently is no way to remove that empty additional choice just makes it even worse.

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.

@soullivaneuh
Copy link
Contributor

soullivaneuh commented May 19, 2022

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:

image

What we have now:

image

Adding this option would mean to check that when the showEmptyOption prop is false, developers also provided a valid default value. I don't think we should as it make the SelectInput API more complicated both for us and for developpers.

@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.

@ralfarama
Copy link

ralfarama commented Jul 12, 2022

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.

@fzaninotto
Copy link
Member

If the field isn't required, the empty option is necessary to empty it.
If the field is required, the empty option will always lead to an invalid form.

We'll go with removing the empty option if the field is required.

@fzaninotto
Copy link
Member

Fixed by #8039

JinParc pushed a commit to JinParc/react-admin that referenced this issue Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants