Skip to content

Fix AutocompleteInput clearing mouse selections on blur - #11364

Merged
fzaninotto merged 2 commits into
marmelab:masterfrom
lprnmns:codex/fix-autocomplete-oncreate-autoselect
Sep 9, 2026
Merged

fzaninotto merged 2 commits into
marmelab:masterfrom
lprnmns:codex/fix-autocomplete-oncreate-autoselect

Conversation

@lprnmns

@lprnmns lprnmns commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Problem

Fixes #9813.

When AutocompleteInput uses both onCreate and autoSelect, clicking an existing choice and then blurring the input clears the selection. MUI emits the selected label again as a free-solo string during blur, and the string has no choice id.

Solution

Ignore that duplicate blur change only when its string already matches the selected choice. Unmatched free-solo values and normal choice changes keep their existing behavior.

How To Test

  • corepack yarn jest packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx --runInBand — 75 tests passed
  • corepack yarn eslint packages/ra-ui-materialui/src/input/AutocompleteInput.tsx packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx — passed
  • ./node_modules/.bin/prettier --config ./.prettierrc.js --check packages/ra-ui-materialui/src/input/AutocompleteInput.tsx packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx — passed
  • corepack yarn workspace ra-ui-materialui build — passed

Additional Checks

  • The PR targets master for a bugfix
  • The PR includes a regression test
  • No new story is needed; the regression uses the existing OnCreate story
  • No documentation change is needed; the public API is unchanged

@fzaninotto fzaninotto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!


describe('onCreate', () => {
it('should keep a clicked choice on blur when autoSelect is enabled', async () => {
render(<OnCreate autoSelect autoHighlight />);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please extract that into its own Story, so that it's testable by hand? It would also help reproduce the issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — I added a dedicated OnCreateWithAutoSelect Story and updated the regression test to render that Story. This keeps the scenario manually reproducible in Storybook. Local validation: the focused suite passes (75/75), ESLint and Prettier pass, and the root yarn typecheck build completes for all 20 projects.

@fzaninotto
fzaninotto merged commit 77fc18c into marmelab:master Sep 9, 2026
14 checks passed
@fzaninotto

Copy link
Copy Markdown
Member

Thanks!

@fzaninotto fzaninotto added this to the 5.15.4 milestone Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AutocompleteInput with Create, autoSelect not working

2 participants