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][Autocomplete] onInputChange called with undefined event contrary to type definition #43899

Open
sydneyjodon-wk opened this issue Sep 26, 2024 · 2 comments
Assignees
Labels
component: autocomplete This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material typescript

Comments

@sydneyjodon-wk
Copy link
Contributor

sydneyjodon-wk commented Sep 26, 2024

Steps to reproduce

Link to live example: (required) https://stackblitz.com/edit/react-ouzbqw?file=Demo.tsx

Steps:

  1. Set the onInputChange prop
  2. Check what is passed in for the event param

Current behavior

Hello 👋

I'm noticing that the onInputChange is typed as:

onInputChange?: (
    event: React.SyntheticEvent,
    value: string,
    reason: AutocompleteInputChangeReason,
) => void;

with a non-null event param, but null seems to be coming through for the param on mount. See demo:

onInputChange-null-event

Expected behavior

I'm not sure if onInputChange is supposed to be called on mount or not, but if it is and it's expected that null is an option for event, would it be possible to update the typing of onInputChange to reflect that?

Thanks!

Context

We are trying to use strict null safety

Your environment

See https://stackblitz.com/edit/react-ouzbqw?file=Demo.tsx - also reproduce-able in this docs demo

Search keywords: onInputChange undefined event

@sydneyjodon-wk sydneyjodon-wk added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 26, 2024
@zannager zannager added the component: autocomplete This is the name of the generic UI component, not the React module! label Sep 27, 2024
@sai6855 sai6855 assigned sai6855 and unassigned sai6855 Sep 27, 2024
@mj12albert
Copy link
Member

mj12albert commented Oct 23, 2024

@sydneyjodon-wk Thanks for the repro, in your sandbox I think onInputChange is called because the initial value and inputValue are different, and inputValue is updated to match value
If you initialize the values like this:

const [value, setValue] = React.useState<string | null>(options[0]);
const [inputValue, setInputValue] = React.useState(options[0]);

the console.log does not get called

Here's a working sandbox (forked from yours): https://stackblitz.com/edit/react-ouzbqw-6hz33h?file=Demo.tsx

@mj12albert mj12albert added 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 Oct 23, 2024
@sydneyjodon-wk
Copy link
Contributor Author

@mj12albert thank you! I'm still wondering though if the prop type can be changed because my original sandbox is still evidence that it is possible for null to be passed in as an argument for onInputChange contrary to what the type says. Either that or don't call onInputChange at all in those cases?

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 typescript
Projects
None yet
Development

No branches or pull requests

4 participants