mui / material-ui

Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.
https://mui.com/material-ui/
MIT License
93.96k stars 32.27k forks source link

[Autocomplete] Uncaught TypeError: Cannot read property 'length' of null #20152

Closed aaronhayes closed 4 years ago

aaronhayes commented 4 years ago

TypeError · Uncaught TypeError: Cannot read property 'length' of null

https://github.com/mui-org/material-ui/blob/701e3ad76b788a50ea83aeb8516ed303c2435bd0/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js#L821

If value is null and multiple is true, we run into this error. Should add a check for value being null.

Version: "@material-ui/lab": "^4.0.0-alpha.45"

eps1lon commented 4 years ago

@aaronhayes Please provide a full reproduction test case. This would help a lot 👷 . A live example would be perfect. This codesandbox.io template may be a good starting point. Thank you!

aaronhayes commented 4 years ago

Yep will see what I can put together @eps1lon

ohlr commented 4 years ago

@eps1lon This should be a sufficient test case

        <Autocomplete
            options={options}
            getOptionLabel={option => option.label}
            value={null}
            onChange={(event: any, newValue: any) => {
                handleOptionsIdChange(newValue);
            }}
            multiple={true}
            renderInput={params => (
                <TextField {...params} label="test" variant="standard" fullWidth />
            )}
        />
dfyz011 commented 3 years ago

I still have problem here in combination with react-form-hooks: https://github.com/mui-org/material-ui/blob/42954448866f204b0cf7e35127d5d0af9c24d6fb/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js#L896

Version: "@material-ui/lab": "^4.0.0-alpha.56"

oliviertassinari commented 3 years ago

@ohlr This is not valid, https://github.com/mui-org/material-ui/issues/20152#issuecomment-600271717 throw types and prop-types warnings. null is not a valid value

parksj10 commented 3 years ago

Why is null not a valid value? the standard autcomplete (I.e. no multiple) accepts null values... Shouldn't we be able to set null (I.e. no values) for a multiple autocomplete?

oliviertassinari commented 3 years ago

@parksj10 you can provide an empty array instead.

frontendDevResearcher commented 2 years ago

if you provide an empty array, the autocomplete multiple selection will stop working.

frontendDevResearcher commented 2 years ago

@oliviertassinari

frontendDevResearcher commented 2 years ago

I was trying to use this hack as I was also getting [Autocomplete] TypeError: (intermediate value)(intermediate value)(intermediate value).filter is not a function error my this line solved the issue value={undefined} but then I started getting this error for that, I tried this value={item.type === "multiSelect" ? [] : undefined} now , no errors but the values are not coming on the autoComplete selection

<Autocomplete
                        multiple={item.type === "multiSelect"}
                        css={`
                          margin-top: 4px;
                        `}
                        value={item.type === "multiSelect" ? [] : undefined}
                        fullWidth
                        options={item.values || []}
                        getOptionLabel={(option) => option.label || ""}
                        renderInput={(params) => (
                          <TextField {...params} variant="standard" />
                        )}
                      />