Skip to content

Fix <AutocompleteInput> field value becomes "@@ra-create" when onCreate returns nothing - #11444

Merged
fzaninotto merged 1 commit into
masterfrom
fix/oncreate-returns-nothing
Nov 9, 2026
Merged

fzaninotto merged 1 commit into
masterfrom
fix/oncreate-returns-nothing

Conversation

@erwanMarmelab

@erwanMarmelab erwanMarmelab commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Choice inputs (<AutocompleteInput>, <SelectInput>, <AutocompleteArrayInput>, <SelectArrayInput>) accept an onCreate prop to let users create a new option on the fly. A common implementation asks for the name with a prompt, and returns nothing when the user cancels:

<AutocompleteInput
    source="author"
    choices={authors}
    onCreate={filter => {
        const name = prompt('New author name', filter);
        if (!name) return; // user cancelled
        const author = { id: authors.length + 1, name };
        authors.push(author);
        return author;
    }}
/>

Scenario: the field shows "Leo Tolstoy". The user types "Zola", clicks "Create Zola", then cancels the prompt.

  • Expected: the field keeps its previous value, "Leo Tolstoy", and the form stays pristine.
  • Actual: the field value becomes "@@ra-create", the internal id of the "Create" option. The form becomes dirty and, if submitted, saves author: "@@ra-create".

Cause: in useSupportCreateSuggestion, handleChange only returns early when onCreate returns a value. Otherwise it falls through to the regular handleChange(eventOrValue) with the "Create" option as the selected value.

Also reported in shadcn-admin-kit, which uses the same hook: marmelab/shadcn-admin-kit#179.

Solution

When onCreate returns nothing, return without calling handleChange, so the field value is left unchanged. All four inputs use this hook, so one change fixes them all.

How To Test

  1. Run Storybook (make storybook) and open ra-ui-materialui/input/AutocompleteInput > OnCreatePrompt. It's an edit form for a book whose Author field shows "Leo Tolstoy".
  2. Type "Zola" in the Author field: a "Create Zola" option appears in the list.
  3. Click it: a prompt asks for the author name. Click Cancel.
  4. The list closes. The Author field still shows the text you typed, "Zola", but the form value is unchanged, so the Save button stays disabled.

Before this fix, step 4 cleared the field and enabled the Save button. Submitting the form sent author: "@@ra-create" to the data provider.

Additional Checks

  • The PR targets master for a bugfix or a documentation fix, or next for a feature
  • The PR includes unit tests (if not possible, describe why)
  • The PR includes one or several stories (if not possible, describe why)
  • The documentation is up to date

@erwanMarmelab erwanMarmelab changed the title Fix inputs saving @@ra-create on cancelled create Fix inputs saving @@ra-create on cancelled create Nov 9, 2026
@erwanMarmelab erwanMarmelab added the RFR Ready For Review label Nov 9, 2026
@fzaninotto
fzaninotto merged commit c99aef2 into master Nov 9, 2026
15 checks passed
@fzaninotto
fzaninotto deleted the fix/oncreate-returns-nothing branch November 9, 2026 15:57
@fzaninotto fzaninotto added this to the 5.15.7 milestone Nov 9, 2026
@fzaninotto fzaninotto changed the title Fix inputs saving @@ra-create on cancelled create Fix <AutocompleteInput> field value becomes "@@ra-create" when onCreate returns nothing Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

RFR Ready For Review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants