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

no-issue: Set the initial selection of a multi-value autocomplete input #5326

Merged
merged 1 commit into from
Jan 12, 2024

Conversation

rohan-bes
Copy link
Collaborator

Issue https://beyondessential.slack.com/archives/C011G5L1J12/p1704949776764939:

This makes it easier for users to add/remove from the existing selection rather than needing to remember it

- This makes it easier for users to add/remove from the existing selection rather than needing to remember it
@@ -37,12 +39,27 @@ const ReduxAutocompleteComponent = React.memo(
placeholder,
helperText,
}) => {
const [hasSetInitialSelection, setHasSetInitialSelection] = useState(false);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There might be a better way to ensure this only is done once, but this is what first came to mind...

Copy link
Contributor

@alexd-bes alexd-bes left a comment

Choose a reason for hiding this comment

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

This is soooo nice to have fixed, thank you!

@rohan-bes rohan-bes merged commit b97a54b into dev Jan 12, 2024
42 checks passed
@rohan-bes rohan-bes deleted the autocomplete-initial-selection branch January 12, 2024 03:54
Comment on lines 44 to 48
React.useEffect(() => {
return () => {
onClearState();
};
}, []);
Copy link
Contributor

Choose a reason for hiding this comment

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

I presume this is intentional, but what makes this different from:

React.useEffect(onClearState, []);

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If you return a function from useEffect() that function will get run whenever the component is removed from the DOM (it's called the cleanup function). So in this case it will clear the autocomplete when it's removed from the DOM. Whereas useEffect(onClearState, []) would clear the autocomplete when it is first rendered. Possibly that might result with the same behaviour in practice, but clearing in the cleanup function feels a bit more elegant.

We should probably have a comment explaining this though, I didn't know what it was doing when I first saw it either.

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.

3 participants