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

[Autocomplete] freeSolo prop makes onChange value type become T | string #33093

Closed
2 tasks done
MarksCode opened this issue Jun 10, 2022 · 5 comments
Closed
2 tasks done
Labels
component: autocomplete This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material status: expected behavior Does not imply the behavior is intended. Just that we know about it and can't work around it

Comments

@MarksCode
Copy link

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

When passing in a custom option type Option to the Autocomplete component and freeSolo={true}, the onChange says the value can be of type Option | string instead of just Option.

type Option = { label: string; value: string };
return (
  <MuiAutocomplete<Option, true, any, true>
    freeSolo={true}
    onChange   // says value can be type Option | string
  />
);

Expected behavior 🤔

Type of value in onChange should just be Option, not Option | string.

Steps to reproduce 🕹

https://codesandbox.io/s/freesolo-demo-material-ui-forked-sn8l5i?file=/demo.tsx

Context 🔦

I want the onChange to only be type Option

Your environment 🌎

Latest mui and typescript. See codesandbox.

@MarksCode MarksCode added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jun 10, 2022
@hbjORbj hbjORbj added package: material-ui Specific to @mui/material component: autocomplete This is the name of the generic UI component, not the React module! labels Jun 15, 2022
@hbjORbj hbjORbj changed the title Autocomplete freeSolo prop makes onChange value type become T | string [Autocomplete] freeSolo prop makes onChange value type become T | string Jun 20, 2022
@michaldudak
Copy link
Member

This is by design. When you type a new value in a text input in the freeSolo mode, it's a string - it can't be anything else.

@michaldudak michaldudak added status: expected behavior Does not imply the behavior is intended. Just that we know about it and can't work around it and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jun 20, 2022
@tnoetzel
Copy link

tnoetzel commented Aug 4, 2022

@michaldudak - Doesn't actually seem to be the case that it's ever a string when you're also using filterOptions as described here.

If you play with that example by simply adding a console.log under if (typeof newValue === 'string') {, you'll note that it's never a string in this case.

Ideally, onChange would be aware of this, or there'd be some shorter way to indicate it, rather than creating a code block that never actually gets triggered.

@michaldudak
Copy link
Member

The type system is not aware of the logic you put in filterOptions, so it can't automatically exclude string from onChange's parameter. You can always manually type the parameter (onChange={(event, newValue: FilmOptionType)... and the additional code block won't be necessary.

@michaldudak michaldudak removed their assignment Oct 11, 2022
@bagelbits
Copy link

@michaldudak onChange={(event, newValue: FilmOptionType)... is still a type error though. And if you use multiple, you can't use if (typeof newValue === 'string') { as a check anymore because the type becomes (T | string)[]

@michaldudak
Copy link
Member

Yeah, sorry, I was wrong. In this case you can resort to TypeScript's as - you know better than the compiler what's the real type of the argument.

We are going to redesign the Autocomplete in Base UI some time this year, and we will consider this when designing the API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: autocomplete This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material status: expected behavior Does not imply the behavior is intended. Just that we know about it and can't work around it
Projects
None yet
Development

No branches or pull requests

5 participants