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

Select value prop #41302

Closed
fraidoonk opened this issue Feb 28, 2024 · 1 comment
Closed

Select value prop #41302

fraidoonk opened this issue Feb 28, 2024 · 1 comment
Labels
component: select This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material typescript

Comments

@fraidoonk
Copy link

fraidoonk commented Feb 28, 2024

Steps to reproduce

Link to live example: (required)

Steps:

  1. Set a state with type of number | string:
    const [category, setCategory] = useState<string | number>("");
  2. use the state as value to your select component:
    <Select value={category} disableUnderline onChange={(e) => { setCategory(e.target.value ?? 0); }} > -- map your MenuItems here with values being numerical </Select>
  3. Typescript throws error: Type 'string | number' is not assignable to type 'string | undefined'.

Current behavior

Even though the documentation says it is type of '' | any, it does not work

Expected behavior

No response

Context

No response

Your environment

npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Search keywords: select value prop

@fraidoonk fraidoonk added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Feb 28, 2024
@brijeshb42
Copy link
Contributor

You can use it like this -

<Select<string | number>
  value={c}
  onChange={(e) => {
    setC(e.target.value ?? 0);
  }}
>
  <MenuItem value={10}>Ten</MenuItem>
  <MenuItem value={20}>Twenty</MenuItem>
  <MenuItem value={30}>Thirty</MenuItem>
</Select>

Notice the generic parameter in Select tag.

Feel free to re-open if this doesn't work.

@brijeshb42 brijeshb42 added typescript component: select This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: select This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material typescript
Projects
None yet
Development

No branches or pull requests

2 participants