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] Async server filtering not working #41629

Open
1 task done
RoseannePeeters opened this issue Mar 24, 2024 · 0 comments
Open
1 task done

[material-ui][Autocomplete] Async server filtering not working #41629

RoseannePeeters opened this issue Mar 24, 2024 · 0 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 status: waiting for maintainer These issues haven't been looked at yet by a maintainer

Comments

@RoseannePeeters
Copy link

RoseannePeeters commented Mar 24, 2024

Search keywords

Autocomplete Async server filtering not working.

Latest version

  • I have tested the latest version

Steps to reproduce

Link to live example: I cannot provide this since it requires a backend, and I didn't find a free testing one that met my description for the test.

Steps:

  1. Click on autocomplete
  2. You see 14 items in there
  3. Type in it to filter (server side)
    4). It does the request but old data stays, while only the filtered requests should show.

Current behavior

Old data is showing with new data on input.

Expected behavior

It should only show new data, aka the current state of customers

Context

I want this autocomplete to be able to filter server side.

Your environment

Hi all, I am making a Autocomplete component that will do server side filtering, so far I got all working but there is one problem

My code:

{customers && (
                <Autocomplete
                  multiple
                  id="async-customers"
                  onInputChange={handleCustomerSearchTermChange}
                  isOptionEqualToValue={(option, value) =>
                    option.username === value.username
                  }
                  getOptionSelected={(option, value) =>
                    option.username === value.username
                  }
                  getOptionLabel={(option) => option.username}
                  options={customers}
                  loading={isLoadingChangeCustomers}
                  filterOptions={(customers) => customers}
                  renderInput={(params) => (
                    <TextField
                      {...params}
                      label="Customers:"
                      variant="outlined"
                      InputProps={{
                        ...params.InputProps,
                        endAdornment: (
                          <>
                            {isLoadingChangeCustomers ? (
                              <CircularProgress color="inherit" size={20} />
                            ) : null}
                            {params.InputProps.endAdornment}
                          </>
                        ),
                      }}
                    />
                  )}
                />
              )}

On input change I fetch new results like so:

  const handleCustomerSearchTermChange = (e, newValue) => {
    setCustomerSearchTerm(newValue); // I know I should throttle this later, but this is for testing (and throtthlig doesn't make a difference as I tested)
  };

useEffect(() => {
    mutateChangeCustomers();
  }, [customerSearchTerm]);

  const {
    data: changeCustomers,
    error: errorChangeCustomers,
    isLoading: isLoadingChangeCustomers,
    mutate: mutateChangeCustomers,
  } = useSWR(`/admin/getChangeCustomers/${customerSearchTerm}`, getAll);

  useEffect(() => {
    if (changeCustomers) setCustomers(changeCustomers);

    console.log(changeCustomers);
  }, [changeCustomers]);

So I kinda found what the problem was:
When opening the Autocomplete it will always start off with 14 items, expected behaviour, but when typing in it, it will not remove those items instead it will just add the new items to the existing ones, which is very weird since the customers state is only holding new values.
The only way I found to make the old ones dissapear is by typing in a wrong searchTerm so that nothing gets shown in the dropdown and then backspacing to a correct searchTerm and then it only shows the filtered ones and not the old ones

@RoseannePeeters RoseannePeeters added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Mar 24, 2024
@danilo-leal danilo-leal changed the title Autocomplete Async server filtering not working. [material-ui][Autocomplete] Async server filtering not working Mar 25, 2024
@danilo-leal danilo-leal added package: material-ui Specific to @mui/material component: autocomplete This is the name of the generic UI component, not the React module! labels Mar 25, 2024
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 status: waiting for maintainer These issues haven't been looked at yet by a maintainer
Projects
None yet
Development

No branches or pull requests

3 participants