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

AutocompleteInput create text is undefined when using a function as option text #7902

Closed
Guy-Adler opened this issue Jun 27, 2022 · 2 comments · Fixed by #7908
Closed

AutocompleteInput create text is undefined when using a function as option text #7902

Guy-Adler opened this issue Jun 27, 2022 · 2 comments · Fixed by #7908

Comments

@Guy-Adler
Copy link
Contributor

Guy-Adler commented Jun 27, 2022

What you were expecting:

When passing the create property to AutocompleteInput, the default text for the creation option should be ra.action.create_item.
image

What happened instead:

The text was undefined.
image

Steps to reproduce:

  1. Create an AutocompleteInput where the optionText prop is a function.
  2. Start typing a text which isn't an option.

Related code:

Code sandbox: https://codesandbox.io/s/fervent-breeze-muqxj4?file=/src/posts/TagReferenceInput.tsx

The change was made on lines 48:

optionText={(choice) => choice.name[locale]}

Other information:

This is probably caused by line 84 of

return {
id: createValue,
name:
filter && createItemLabel
? translate(createItemLabel, {
item: filter,
_: createItemLabel,
})
: translate(createLabel, { _: createLabel }),

when passing optionText as a function, the string is always saved under name, which makes it impossible to use a combination of fields as the option text.

Environment

  • React-admin version: 4.2.0
  • Last version that did not exhibit the issue (if applicable):
  • React version: 17
  • Browser: Google Chrome 102.0.5005.115
  • Stack trace (in case of a JS error):
@Guy-Adler
Copy link
Contributor Author

As a workaround, one can use

const buildOption = (choice: any) => {
  if (choice.name !== undefined) {
    return choice.name;
  }
  return whatever you want;
};

however, this is very confusing

@antoinefricker
Copy link
Contributor

Bug reproduced, thanks for your report 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants