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

[material-ui][FormControl] Typescript error: Cannot access e.target.checked #41843

Closed
marcellino-ornelas opened this issue Apr 10, 2024 · 2 comments
Labels
component: FormControl The React component package: material-ui Specific to @mui/material typescript

Comments

@marcellino-ornelas
Copy link

marcellino-ornelas commented Apr 10, 2024

Steps to reproduce

Link to live example: https://stackblitz.com/edit/react-lt4tqf?file=Demo.tsx%3AL184

Current behavior

No response

Expected behavior

No response

Context

Im trying to figure out how to type FormControlLabel.onChange so we can access e.target.checked?

using React.SyntheticEvent<HTMLInputElement> doesnt let us access .checked
Screenshot 2024-04-10 at 12 39 44 PM

and when I change it to React.ChangeEvent<HTMLInputElement> it works but then I get a different type error when passing the function to FormControlLabel.onChange

Screenshot 2024-04-10 at 12 41 22 PM

I could pass this to Switch.onChange but in my actual code, Im passing name="<some-name>" to the FormControlLabel which lets me access e.target.name in the event handler so I can reuse state logic.

Your environment

npx @mui/envinfo

These might not be the latest but using latest in stackblitz
https://stackblitz.com/edit/react-lt4tqf?file=Demo.tsx,node_modules%2F%40mui%2Fmaterial%2FFormControlLabel%2FFormControlLabel.d.ts

  Browser: Chrome
  System:
    OS: Linux 6.5 Debian GNU/Linux 11 (bullseye) 11 (bullseye)
  Binaries:
    Node: 20.5.1 - /usr/local/bin/node
    npm: 10.5.1 - /usr/local/bin/npm
    pnpm: Not Found
  Browsers:
    Chrome: Not Found
  npmPackages:
    @emotion/react: ^11.11.1 => 11.11.3 
    @emotion/styled: ^11.11.0 => 11.11.0 
    @mui/base:  5.0.0-beta.36 
    @mui/core-downloads-tracker:  5.15.14 
    @mui/icons-material: ^5.14.0 => 5.15.10 
    @mui/joy: ^5.0.0-alpha.87 => 5.0.0-beta.28 
    @mui/lab: 5.0.0-alpha.136 => 5.0.0-alpha.136 
    @mui/material: ^5.14.0 => 5.15.14 
    @mui/private-theming:  5.15.14 
    @mui/styled-engine:  5.15.14 
    @mui/system:  5.15.14 
    @mui/types:  7.2.14 
    @mui/utils:  5.15.14 
    @mui/x-data-grid: ^7.1.0 => 7.1.0 
    @mui/x-date-pickers: ^6.10.0 => 6.19.4 
    @types/react: 18.2.15 => 18.2.15 
    react: ^18.2.0 => 18.2.0 
    react-dom: ^18.2.0 => 18.2.0 
    styled-components: ^6.0.4 => 6.1.8 
    typescript: 5.1.6 => 5.1.6

Search keywords: e.target.checked

@marcellino-ornelas marcellino-ornelas added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Apr 10, 2024
@danilo-leal danilo-leal changed the title Typescript error: Cannot access e.target.checked for FormControlLabel [material-ui][FormControl] Typescript error: Cannot access e.target.checked Apr 11, 2024
@danilo-leal danilo-leal added typescript package: material-ui Specific to @mui/material component: FormControl The React component labels Apr 11, 2024
@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Apr 14, 2024

You can utilize the checked state provided in the second parameter of the onChange callback. Check out the updated StackBlitz: https://stackblitz.com/edit/react-lt4tqf-iywzq3?file=Demo.tsx%3AL184.

Actually, I see that it should be removed in v6 and event.target.checked should be used: https://github.com/mui/material-ui/blob/next/packages/mui-material/src/internal/SwitchBase.js#L116-L119. This will require update in onChange callback type definition as well. CC @DiegoAndai so that it can be tracked.

@ZeeshanTamboli ZeeshanTamboli removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Apr 14, 2024
@DiegoAndai
Copy link
Member

Thanks for the callout @ZeeshanTamboli. I created an issue to track this: #41925

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: FormControl The React component package: material-ui Specific to @mui/material typescript
Projects
None yet
Development

No branches or pull requests

5 participants