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

`AutocompleteInput` `emptyValue` prop is ignored

Open slax57 opened this issue 3 years ago • 4 comments

What you were expecting: To be able to change the emptyValue of a AutocompleteInput to anything (especially to null), and this value to be sent to the dataProvider when I don't select anything from the AutocompleteInput

What happened instead: The emptyValue is ignored, and the dataProvider always receives ''.

Steps to reproduce:

  1. Go to https://stackblitz.com/edit/github-xyrrnf?file=src/posts/PostCreate.tsx
  2. Make sure to log in as Admin (admin/password)
  3. Create a new Post, where you have added an Author but left the User field empty
  4. Notice the dataProvider's create function is called, and authors[0].user_id='' instead of expected authors[0].user_id='emptyValue'

Related code: PostCreate.tsx line 156

Other information:

This issue was discovered while trying to set emptyValue to null. We should also make sure this is allowed and does not get replaced by '' because it is falsy.

Environment

  • React-admin version: 4.2.7
  • Last version that did not exhibit the issue (if applicable):
  • React version: 18
  • Browser: chrome
  • Stack trace (in case of a JS error):

slax57 avatar Aug 22 '22 15:08 slax57

or you can use validate={required()} to make it highlight when not selected a value

tulequ avatar Aug 23 '22 17:08 tulequ

@tulequ This will not help if the field is not required.

elya-adzz avatar Aug 29 '22 07:08 elya-adzz

This applies to the SelectInput as well. In your codesandbox, if you change the AutocompleteInput by a SelectInput, you get the same result.

WiXSL avatar Sep 08 '22 13:09 WiXSL

as a temporary workaround you can use the transform property of the <Edit/>, <Create/> or <SaveButton/> component, see this section from docs.

It can look like this:

  <Edit 
    title="Edit the author"
    transform={author => author.user_id === '' ? {...author, user_id: null} : author}
  >
    <SimpleForm>
      <TextInput source="name"/>
      <ReferenceInput source="user_id" reference="users">
        <AutocompleteInput optionText="name"></AutocompleteInput>
      </ReferenceInput>
    </SimpleForm>
  </Edit>

JankesJanco avatar Sep 21 '22 13:09 JankesJanco