Skip to content

[useAutocomplete] Improve isOptionEqualToValue value argument type#47801

Open
silviuaavram wants to merge 3 commits intomui:masterfrom
silviuaavram:fix/autocomplete-free-solo-prop-type
Open

[useAutocomplete] Improve isOptionEqualToValue value argument type#47801
silviuaavram wants to merge 3 commits intomui:masterfrom
silviuaavram:fix/autocomplete-free-solo-prop-type

Conversation

@silviuaavram
Copy link
Member

@silviuaavram silviuaavram commented Feb 17, 2026

It's this PR #37291 from @newsiberian but the files have changed since then so created a new PR with the changes on the updated file structure. Same changes, but:

  • docs/data/base/components/autocomplete/AutocompleteIntroduction/tailwind/index.js
    • removed, not found anymore.
  • docs/pages/base-ui/api/use-autocomplete.json
    • removed, not found anymore
  • packages/mui-base/src/useAutocomplete/useAutocomplete.d.ts
    • changed to packages/mui-material/src/useAutocomplete/useAutocomplete.d.ts

Fixes #38322

@silviuaavram silviuaavram self-assigned this Feb 17, 2026
Copilot AI review requested due to automatic review settings February 17, 2026 14:50
@silviuaavram silviuaavram added type: bug It doesn't behave as expected. breaking change Introduces changes that are not backward compatible. typescript scope: autocomplete Changes related to the autocomplete. This includes ComboBox. v9.x labels Feb 17, 2026
@mui-bot
Copy link

mui-bot commented Feb 17, 2026

Netlify deploy preview

Bundle size report

Bundle Parsed size Gzip size
@mui/material 0B(0.00%) 0B(0.00%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes

Generated by 🚫 dangerJS against 62319d5

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the TypeScript type definitions for the isOptionEqualToValue and getOptionLabel props in the Autocomplete component to correctly reflect that in freeSolo mode, the value parameter can be either the option type or a string. This addresses a type safety issue where developers using freeSolo mode were not getting accurate type information.

Changes:

  • Introduced a new AutocompleteValueOrFreeSoloValueMapping type helper that resolves to Value | string when FreeSolo is true, otherwise just Value
  • Updated type definitions and documentation for getOptionLabel and isOptionEqualToValue to use this new type helper
  • Added demo examples showing proper usage of isOptionEqualToValue with freeSolo mode

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/mui-material/src/useAutocomplete/useAutocomplete.d.ts Added new type helper and updated type definitions for getOptionLabel and isOptionEqualToValue
packages/mui-material/src/Autocomplete/Autocomplete.js Updated JSDoc comments to reflect that parameters can be Value or string
packages/mui-joy/src/Autocomplete/AutocompleteProps.ts Updated import to use new type helper and applied it to getOptionLabel
packages/mui-joy/src/Autocomplete/Autocomplete.tsx Updated JSDoc comments to reflect that parameters can be Value or string
docs/pages/material-ui/api/autocomplete.json Updated API documentation signatures to show Value or string types
docs/pages/joy-ui/api/autocomplete.json Updated API documentation signatures to show Value or string types
docs/data/material/components/autocomplete/FreeSolo.tsx Added example demonstrating isOptionEqualToValue with freeSolo mode
docs/data/material/components/autocomplete/FreeSolo.js Added example demonstrating isOptionEqualToValue with freeSolo mode

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

AutocompleteFreeSoloValueMapping,
AutocompleteInputChangeReason,
AutocompleteValue,
AutocompleteValueOrFreeSoloValueMapping,
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

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

This import references AutocompleteValueOrFreeSoloValueMapping from the external @mui/base package. Please verify that this type has been added to @mui/base (version 7.0.0-beta.4 or the version being used) before merging this PR. If this type doesn't exist in the current version of @mui/base, this will cause a TypeScript compilation error.

Copilot uses AI. Check for mistakes.
Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

same here, need to add to upgrading guide

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Feb 19, 2026
@silviuaavram silviuaavram force-pushed the fix/autocomplete-free-solo-prop-type branch from d5ffd74 to 0a76519 Compare February 19, 2026 11:03
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Feb 19, 2026
@silviuaavram silviuaavram force-pushed the fix/autocomplete-free-solo-prop-type branch from 108071c to 5ef4df5 Compare February 19, 2026 14:39
Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

When it's related to types, I recommend adding .spec.tsx to confirm the behavior.
In this case, let's add a fail case (missing typeof check) and a success case (similar to the demo but can be shorter).

Take a look at packages/mui-material/src/Autocomplete/Autocomplete.spec.tsx

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Feb 20, 2026
@silviuaavram silviuaavram force-pushed the fix/autocomplete-free-solo-prop-type branch from 5ef4df5 to 62319d5 Compare February 20, 2026 09:15
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Feb 20, 2026

return option === value;
}}
/>;
Copy link
Member

@siriwatknp siriwatknp Feb 20, 2026

Choose a reason for hiding this comment

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

Suggested change
/>;
/>;
<Autocomplete
id="free-solo-demo3"
freeSolo
options={top100Films} // array of object
renderInput={() => null}
// @ts-expect-error option could be a string
getOptionLabel={(option) => option.title}
isOptionEqualToValue={(option, value) => {
// @ts-expect-error option could be a string
return option.title === value?.title;
}}
/>

If my understanding is correct, this should be added to ensure typesafety right?

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

👍 Please check my last comment related to the spec.

if (typeof value === 'string') {
return option.title === value;
}
return option.title === value?.title;
Copy link
Member

Choose a reason for hiding this comment

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

Is this optional or no?

Suggested change
return option.title === value?.title;
return option.title === value.title;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change Introduces changes that are not backward compatible. scope: autocomplete Changes related to the autocomplete. This includes ComboBox. type: bug It doesn't behave as expected. typescript v9.x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[material-ui][Autocomplete] Component with freeSolo and isOptionEqualToValue doesn't use string for type of value

4 participants

Comments