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

defaultValue prop does not work on Autocomplete #244

Open
ImLuze opened this issue Jul 30, 2021 · 3 comments
Open

defaultValue prop does not work on Autocomplete #244

ImLuze opened this issue Jul 30, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@ImLuze
Copy link

ImLuze commented Jul 30, 2021

Setting the defaultValue prop on an <Autocomplete /> component does nothing. At first glance it looks like that prop is not being used inside the Autocomplete.jsx file at the moment, except for assigning it to the state.

To Reproduce

Steps to reproduce the behavior:

  1. Render an Autocomplete component with the defaultValue prop
  2. Notice how the component still displays the placeholder by default.

Expected behaviour

Expected the Autocomplete component to display the defaultValue prop by default

OS (please complete the following information):

  • Browser [safari, chrome]
  • Version [14.1, 92.0]
ImLuze added a commit to ImLuze/antwerp-ui_react that referenced this issue Aug 3, 2021
The defaultValue prop was not being used, which resulted in it not
working.

Closes digipolisantwerp#244
@jsebrech
Copy link
Contributor

jsebrech commented Sep 3, 2021

The defaultValue property is picked up by AutoComplete already. You can see it in action in the live examples: https://digipolisantwerp.github.io/antwerp-ui_react/#autocomplete

Can you give more details of exactly how you're using AutoComplete so that defaultValue is not picked up?

By the way, the exact code that assigns defaultValue can be found in these files:

The logic that sends it to the input field depends on the selection mode:

@ImLuze
Copy link
Author

ImLuze commented Nov 27, 2021

Hi,

Thanks for the respons and sorry for my late answer. I dug through our code a bit more and tried to isolate the issue:
I created a CodeSandbox reproduction: CodeSandbox.

Issues I've found

I managed to bring it down to 2 core issues:

1. Async defaultValue

We currently need to do an async request to get the data which we use as defaultValue for the Autocomplete component. This seems to be the main issue. I can work around this by updating the components key prop when the defaultValue loads in. This causes the component to force-remount. Although it's not ideal as it does reset the components internal state.

2. defaultValue not matching any item value.

One of our use cases allows the user to filter a list based on the name of a person. We use an Autocomplete component with a list of all people they can filter by. Being able to autocomplete this helps with spelling difficult names.

But if someone only types in a first name or a last name, instead of the full name. Then the query doesn't match any of the values of the provided items and the default value does not show up.


Are these issues that can be fixed on our end?

@jsebrech
Copy link
Contributor

Indeed, this looks like a bug. If defaultValue is undefined and later defined when the user has not picked a value yet, the AutoComplete should update itself, but it does not.

You can work around it by reinitializing the component when defaultValue changes:

  useEffect(() => {
    cmp.searchMode.initialize();
  }, defaultValue);

@jsebrech jsebrech added the bug Something isn't working label Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants