react-admin
react-admin copied to clipboard
SelectInput ignores emptyValue property
What you were expecting: The emptyValue is sent when the empty option is selected in a SelectInput.
What happened instead: An empty string is sent.
Steps to reproduce: Add emptyValue={null} property to a SelectInput.
Related code:
<ReferenceInput source="assignedToId" reference="users">
<SelectInput optionText="username" emptyValue={null} />
</ReferenceInput>
When the form loads, {assignedToId: null, ...} is received, but when the user clicks save then the form sends {assignedToId: '', ...}
Environment
- React-admin version: 4.2.6
- Last version that did not exhibit the issue (if applicable):
- React version: 18.2.0
- Browser: Chrome
- Stack trace (in case of a JS error):
Thanks for your report. Would you mind adding a reproduction based on the simple example codesandbox?
https://codesandbox.io/s/github/marmelab/react-admin/tree/master/examples/simple
https://codesandbox.io/s/lucid-haibt-b0p0zm?file=/src/posts/PostEdit.tsx
See the example with editing a post. A user select input is at the bottom of the first tab of the edit form. In the console, the object is seen to have { user_id: "", ... }. This happens regardless of the value received from the data provider.
I encount this problem too.
I tried some cases.
emptyValue="foo" => got "foo"
emptyValue="null" => ignored, got ""
emptyValue={null} => ignored, got ""
emptyValue="1" => got "1"
emptyValue={1} => ignored, got ""
emptyValue="true" => ignored, got ""
emptyValue=true => got true
A code sandbox was added to this ticket 2 months ago can the tag "needs more info" be removed, please?
The empty value of a SelectInput inside ReferenceInput is set to null by default since version 4.4.0, see #8188. So using emptyValue shouldn't be necessary in this case.
There is a remaining bug, which is that if a record has no value for a reference and the user doesn't update it, <SelectInput> and <ReferenceInput> still turn the reference value to empty string.
So I'm marking this a s a bug.
Besides, <SelectInput> shouldn't accept null as a possible emptyValue - see #8235.