marmelab / react-admin

A frontend Framework for single-page applications on top of REST/GraphQL APIs, using TypeScript, React and Material Design
http://marmelab.com/react-admin
MIT License
24.77k stars 5.22k forks source link

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

Closed Guy-Adler closed 2 years ago

Guy-Adler commented 2 years ago

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 https://github.com/marmelab/react-admin/blob/58f9841d1fd7dbb3132376b6c50a2b0dd3ca4b8b/packages/ra-ui-materialui/src/input/useSupportCreateSuggestion.tsx#L69-L77 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

Guy-Adler commented 2 years ago

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 commented 2 years ago

Bug reproduced, thanks for your report 👍