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] TypeError: (intermediate value)(intermediate value)(intermediate value).filter is not a function #24785

Closed
bipindubey-technoark opened this issue Feb 5, 2021 · 16 comments · Fixed by #27925
Labels
component: autocomplete This is the name of the generic UI component, not the React module! new feature New feature or request ready to take Help wanted. Guidance available. There is a high chance the change will be accepted

Comments

@bipindubey-technoark
Copy link

bipindubey-technoark commented Feb 5, 2021

I am integrating autocomplete component in my website

it is crashing when we type anything in the autocomplete field

it is giving following error

Uncaught TypeError: (intermediate value)(intermediate value)(intermediate value).filter is not a function in autocomplete

My code:

<Autocomplete
            multiple
            id="checkboxes-tags-demo"
            options={users}
            disableCloseOnSelect
            size="small"
            value={values.userIds}
            getOptionSelected={(option, value) => option.id == value.id}
            getOptionLabel={(option) => option.firstName + ' ' + option.lastName}
            renderOption={(option, { selected }) => (
              <React.Fragment>
                <Checkbox
                  icon={icon}
                  checkedIcon={checkedIcon}
                  style={{ marginRight: 8 }}
                  checked={selected}
                  // onChange={(e) => console.log('called12', e.target.value)}
                />
                {option.firstName + ' ' + option.lastName}
              </React.Fragment>
            )}
            style={{ width: '100%' }}
            renderInput={(params) => (
              <Field {...params} label="All Users" fullWidth name="userIds" component={TextField} />
            )}
            onChange={(event, val) => {
              if (val !== null) {
                const items = [];
                val.map((v) => items.push(v));
                setFieldValue('userIds', items);
              }
            }}
          />

my values:

values.userIds = [1,2];
users = [
{id:1,firstName:'A', lastName:'B'},
{id:2,firstName:'P', lastName:'Q'},
{id:3,firstName:'X', lastName:'Y'},];

it is working fine on every other case like clicking on dropdown icon and then choosing the option or removing it by clicking on close icon

but crashing when we start to type anything in the field
Capture222

Thanks

@bipindubey-technoark bipindubey-technoark added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Feb 5, 2021
@oliviertassinari oliviertassinari added component: autocomplete This is the name of the generic UI component, not the React module! status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Feb 7, 2021
@oliviertassinari
Copy link
Member

Please provide a minimal 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!

@bipindubey-technoark
Copy link
Author

bipindubey-technoark commented Feb 9, 2021

@oliviertassinari
Here is the codesandbox eg have a look into it.
https://codesandbox.io/s/material-ui-issue-forked-uzk9l?file=/src/Demo.js

when you search in autocomplete by typing it it will give error inother cases works fine

@oliviertassinari Thanks for help

@oliviertassinari oliviertassinari added support: question Community support but can be turned into an improvement and removed status: waiting for author Issue with insufficient information labels Feb 9, 2021
@oliviertassinari
Copy link
Member

@bipindubey-technoark Thanks for providing a reproduction. I have updated to v5: https://codesandbox.io/s/material-ui-issue-forked-s0m4o?file=/src/Demo.js

We now have a proper error message:

Capture d’écran 2021-02-09 à 18 06 58

@oliviertassinari oliviertassinari changed the title Uncaught TypeError: (intermediate value)(intermediate value)(intermediate value).filter is not a function in autocomplete [Autocomplete] TypeError: (intermediate value)(intermediate value)(intermediate value).filter is not a function in autocomplete Feb 9, 2021
@oliviertassinari oliviertassinari changed the title [Autocomplete] TypeError: (intermediate value)(intermediate value)(intermediate value).filter is not a function in autocomplete [Autocomplete] TypeError: (intermediate value)(intermediate value)(intermediate value).filter is not a function Feb 9, 2021
@bipindubey-technoark
Copy link
Author

I didnt understood the exact issue??
what was the issue and how to solve it

@bipindubey-technoark
Copy link
Author

bipindubey-technoark commented Feb 10, 2021

@bipindubey-technoark Thanks for providing a reproduction. I have updated to v5: https://codesandbox.io/s/material-ui-issue-forked-s0m4o?file=/src/Demo.js

We now have a proper error message:

Capture d’écran 2021-02-09 à 18 06 58

this is giving this error because you have provided ff to the value but i provided an array which should work

@oliviertassinari
Copy link
Member

@bipindubey-technoark in your codesandbox, if you upgrade it to v5, too, you will see that the value becomes a string.

@bipindubey-technoark
Copy link
Author

bipindubey-technoark commented Feb 11, 2021

@bipindubey-technoark in your codesandbox, if you upgrade it to v5, too, you will see that the value becomes a string.

But in my case value in not string. it is an array.
refre to my codesandbox code ...i have provided an array to value.
but you have provided static string ff .

I am definitely sure that i have not provided a string ..

@bipindubey-technoark
Copy link
Author

bipindubey-technoark commented Feb 11, 2021

@oliviertassinari What you have done in v5 and why you put static value ff i dont know..

@bipindubey-technoark
Copy link
Author

bipindubey-technoark commented Feb 12, 2021

I fixed the issue
actually formik textfield was assigning a value string to the variable.
I am now using material ui textfield and it is working fine
For people who might get this issue in future

in renderInput function of autocomplete just change

    <Field
                 {...params}
                 label="All Users"
                 fullWidth
                 name="userIds"
                component={TextField}
               />

to

<MuiTextField {...params} label="All Users" fullWidth />

Note: MuiTextfield is material ui textfield and Textfield is formik Textfield

import {
  TextField as MuiTextField,
} from '@material-ui/core';

import { TextField } from 'formik-material-ui';

Thanks

@ImranHaider313
Copy link

In my case I was passing string as default value defaultValue={selectedDiagnosis} while using multiple variant. I just changed the selectedDiagnosis as array and all went well.

@oliviertassinari
Copy link
Member

@ImranHaider313 Interesting feedback. We could apply the prop-types logic of the value to the defaultValue prop too:

diff --git a/packages/material-ui/src/Autocomplete/Autocomplete.js b/packages/material-ui/src/Autocomplete/Autocomplete.js
index 0b3f81981c..c7a25a9a4d 100644
--- a/packages/material-ui/src/Autocomplete/Autocomplete.js
+++ b/packages/material-ui/src/Autocomplete/Autocomplete.js
@@ -770,7 +770,17 @@ Autocomplete.propTypes /* remove-proptypes */ = {
    * The default value. Use when the component is not controlled.
    * @default props.multiple ? [] : null
    */
-  defaultValue: PropTypes.any,
+  defaultValue: chainPropTypes(PropTypes.any, (props) => {
+    if (props.multiple && props.defaultValue !== undefined && !Array.isArray(props.defaultValue)) {
+      return new Error(
+        [
+          'Material-UI: The Autocomplete expects the `defaultValue` prop to be an array or undefined.',
+          `However, ${props.defaultValue} was provided.`,
+        ].join('\n'),
+      );
+    }
+    return null;
+  }),
   /**
    * If `true`, the input can't be cleared.
    * @default false

@oliviertassinari oliviertassinari added new feature New feature or request ready to take Help wanted. Guidance available. There is a high chance the change will be accepted and removed support: question Community support but can be turned into an improvement labels May 18, 2021
@ImranHaider313
Copy link

ImranHaider313 commented May 19, 2021

@oliviertassinari can I work on this (over the weekend) ?

@oliviertassinari oliviertassinari added good first issue Great for first contributions. Enable to learn the contribution process. and removed ready to take Help wanted. Guidance available. There is a high chance the change will be accepted labels Jun 9, 2021
@mnajdova mnajdova added OCD21 and removed good first issue Great for first contributions. Enable to learn the contribution process. labels Jun 30, 2021
@oliviertassinari oliviertassinari added the ready to take Help wanted. Guidance available. There is a high chance the change will be accepted label Jul 16, 2021
@paultrimor
Copy link

paultrimor commented Sep 23, 2021

I had the same issue and was able to fix by making use useState initialized to an array .i.e useState([])

@jbham
Copy link

jbham commented Jan 20, 2022

My error was apparently happening because of missing value prop. I added it as follows and it worked:

value={undefined}

Note: I am using mui-rff ("mui-rff": "^5.0.0")

<Autocomplete
                        multiple
                        value={undefined}
                        disablePortal
                        name="AutoComplete"
                        id="combo-box-demo"
                        options={cats}
                        getOptionLabel={(option) => option.name}
                        // defaultValue={[cats[0]]}
                        renderInput={(params) => (
                          <MuiTextField
                            {...params}
                            label="Credit Category(s)"
                            name="cats"
                            margin="normal"
                            size="small"
                            required
                          />

@cemalokten
Copy link

@jbham Setting value={undefined} worked for me, thank you! 🥳

@Shemrock3267
Copy link

I'm using react-hook-form with Autocomplete multiple.
Also I had an outer function that just loop over form fields and "clears" them when I press specific button. I have no default form values btw, because fields are dynamic depending on selected Card.
So in order for it to work (the clearing part & not getting that error) I had to do this:

<Controller
        control={control}
        name={name}
        render={({ field }) => (
          <Autocomplete
            {...field}
            multiple
            defaultValue={[]}
            value={field.value ? field.value : []}
            filterSelectedOptions
            getOptionLabel={option => option.label}
            disablePortal
            PaperComponent={AutocompletePaper}
            options={options as Array<Record<string, string>>}
            onChange={(e, val) => {
              field.onChange(val);
              onChange();
            }}
            fullWidth
            renderInput={params => (
              <TextField
                type={type}
                label={label}
                variant="outlined"
                {...params}
                value={params.inputProps.value ?? ''}
              />
            )}
          />
        )}
      />

Clear function

const handleClear = () => {
    removeDynamicParam();
    Object.keys(form.getValues()).forEach(formField => {
      form.setValue(formField, '');
    });
    refetch();
  };

The main attention is on these fields:

defaultValue={[]}
value={field.value ? field.value : []}

This way I'm not getting that error and Autocomplete getting cleared.

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! new feature New feature or request ready to take Help wanted. Guidance available. There is a high chance the change will be accepted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants