react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

SelectInput ignores emptyValue property

Open dylanlt opened this issue 3 years ago • 3 comments

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):

dylanlt avatar Aug 10 '22 05:08 dylanlt

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

fzaninotto avatar Aug 10 '22 14:08 fzaninotto

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.

dylanlt avatar Aug 11 '22 01:08 dylanlt

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

takayamaki avatar Aug 16 '22 00:08 takayamaki

A code sandbox was added to this ticket 2 months ago can the tag "needs more info" be removed, please?

dylanlt avatar Oct 04 '22 23:10 dylanlt

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.

fzaninotto avatar Oct 05 '22 04:10 fzaninotto

Besides, <SelectInput> shouldn't accept null as a possible emptyValue - see #8235.

fzaninotto avatar Oct 05 '22 05:10 fzaninotto