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

Fix issues with controlled Select #1471

Open
wants to merge 9 commits into
base: development
Choose a base branch
from

Conversation

NikoHelle
Copy link
Contributor

Description

Fixed following issues:

  • multiselect memoization issue: when selecting an option calls the onChange which then refreshes props and re-renders the component, the selected option keeps has old data in the memoized "trigger" function. When an option with old "trigger" reference is unselected, the whole data set is reverted to old state and selections made after it are cleared.

  • crash if keyboard is used after onChange re-renders the component. Memoization issue in the useKeyboard. onKeyDown did not have correct dependencies.

  • "open" state was not kept consistently when onChange caused re-render. When component props change, the open can be reverted to closed-state even if the component has internally opened it. This causes the multiselect close on each selection. The reset of the internal state is now handled by comparing old state with new props after a re-render.

  • Multiselect groups with all options selected are not marked selected after re-render and clicking them had no effect.

Copy link
Contributor

@timwessman timwessman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work, Niko! This helps us a lot. Thanks for your contribution. Now we have a state that doesn't mix up. Yep, the main problem was onChange in the state.

I rebased this on the latest development, fixed the conflict and updated reference images (as the clearable property is now false as a default)

NikoHelle and others added 9 commits February 7, 2025 12:47
If onChange is in state, it keeps ref to the initial "optionGroups".

Easily tested by selecting an option, changing language and selecting again. Lang is always the initial (fi)
Does not fix anything, but is a good practice.
onKeyDown is memoized, but the dependency array is empty, so the ref to the function does not update
The trigger function was not compared, so clicking an item second time, used old data in the memoized trigger.
When parent of the DataProvider is re-rendered and new data is passed, the old is cleared.

onReset can restore data from the state the storages were before reset.
If props change, the "open" is reset to the value it was before re-render. Usually it was "false", so the menu will close once props change.

This is not good behaviour when multiselect items are selected and list keeps closing.

The "open" value is restored on each render.
… options are externally updated

If groups are reset externally, the group label is not updated accordingly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants