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

Reset items after submission #217

Closed
JedBh opened this issue Sep 21, 2023 · 5 comments · Fixed by #218
Closed

Reset items after submission #217

JedBh opened this issue Sep 21, 2023 · 5 comments · Fixed by #218
Assignees
Labels
bug Something isn't working

Comments

@JedBh
Copy link

JedBh commented Sep 21, 2023

Recently I worked with NextJS, Autocomplete, Chakra UI and Formik.
Everything works perfectly, but after submission the items don't reset. So, let's say it's a country input and I chose the USA, after I submit I click again on the country input and the only option is the USA. Of course, you can start typing again and it will be normal. I found a solution, I added a key to the Autocomplete and after each submission the key changes hence resetting the Autocomplete - this is not an efficient solution.

So if there is a fix for this please let me know!

@Kysluss
Copy link
Collaborator

Kysluss commented Sep 22, 2023

Hi @JedBh,

Would you be able to post an example to reproduce the issue (either codesandbox, or just an example component)? I'm going to be out of town for a few days, but I'd like to take a look at this when I get back. I'm using Chakra, Autocomplete, and Formik in a couple of my projects and don't think I've run into this yet, but I wasn't looking for it either so it may have been overlooked.

@JedBh
Copy link
Author

JedBh commented Sep 27, 2023

Hi @Kysluss,

First thank you for returning to me, I also had some time out of town, sorry for the delay.

Look at this first image, here I search for Germany.

Now I select it, then I submit it.

After submission and reset, Germany is the only option to choose from. Of course you can start typing and it will give you other countries but it's a little annoying.

If you want you can also view this in this repo.

@Kysluss
Copy link
Collaborator

Kysluss commented Sep 30, 2023

Hi @JedBh

Sorry for the delay on this. I took a look at the screenshots and the repo. It looks pretty similar to what I've done with other formik projects I've used so I'm going to mark this as a bug. I should be able to work on it in a day or so. I'm really sorry for the delay, but thank you for reporting it.

@Kysluss Kysluss self-assigned this Sep 30, 2023
@Kysluss Kysluss added the bug Something isn't working label Sep 30, 2023
@Kysluss
Copy link
Collaborator

Kysluss commented Oct 1, 2023

Hi @JedBh ,

I have a potential workaround for the issue you're seeing. If you pass a ref to the AutoComplete component, you can remove the selected item in your handleSubmit function. Below is some stubbed out code I used in testing.

I'm working on a more sustainable solution in my test environment, but I want to do more testing to make sure I'm not inadvertently breaking something else.

const autoCompleteRef = useRef();

const handleSubmit = useCallback(() => {
	setSubmitting(true);
	setTimeout(() => {
		alert(JSON.stringify({country}, null, 2));
		autoCompleteRef.current.removeItem(country);
		setCountry('');
		setSubmitting(false);
	}, 1000);
}, [country, setSubmitting]);

For your autocomplete component, make sure to pass the ref to it

<AutoComplete
	value={country}
	ref={autoCompleteRef}
       ..........
>

@Kysluss
Copy link
Collaborator

Kysluss commented Oct 5, 2023

I had some time to finish my testing tonight. This should be fixed in v5.2.8. Thank you for the bug report!

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