-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(ComboBox): avoid extra onSelectionChange calls #9729
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -369,14 +369,21 @@ export function useComboBoxState<T extends object, M extends SelectionMode = 'si | |
| closeMenu(); | ||
| }; | ||
|
|
||
| let commitSelection = () => { | ||
| // If multiple things are controlled, call onSelectionChange | ||
| let commitSelection = (shouldForceSelectionChange = false) => { | ||
| // If multiple things are controlled, call onSelectionChange only when selecting the focused item, | ||
| // or when inputValue needs to be synced back to the selected item on commit/blur. | ||
|
Comment on lines
+373
to
+374
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decided to post this for others walking this logic for this change:
of those cases, we only really need to fire the |
||
| if (value !== undefined && props.inputValue !== undefined) { | ||
| props.onSelectionChange?.(selectedKey); | ||
| props.onChange?.(displayValue); | ||
| let itemText = selectedKey != null ? collection.getItem(selectedKey)?.textValue ?? '' : ''; | ||
| if ( | ||
| shouldForceSelectionChange || | ||
| selectionMode === 'multiple' || | ||
| inputValue !== itemText | ||
| ) { | ||
| props.onSelectionChange?.(selectedKey); | ||
| props.onChange?.(displayValue); | ||
| } | ||
|
|
||
| // Stop menu from reopening from useEffect | ||
| let itemText = selectedKey != null ? collection.getItem(selectedKey)?.textValue ?? '' : ''; | ||
| setLastValue(itemText); | ||
| closeMenu(); | ||
| } else { | ||
|
|
@@ -401,7 +408,7 @@ export function useComboBoxState<T extends object, M extends SelectionMode = 'si | |
| // Reset inputValue and close menu here if the selected key is already the focused key. Otherwise | ||
| // fire onSelectionChange to allow the application to control the closing. | ||
| if (selectionManager.isSelected(selectionManager.focusedKey) && selectionMode === 'single') { | ||
| commitSelection(); | ||
| commitSelection(true); | ||
| } else { | ||
| selectionManager.select(selectionManager.focusedKey); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this still true if it allows a custom value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think useComboBoxState will still clear the selected key on field blur via
react-spectrum/packages/@react-stately/combobox/src/useComboBoxState.ts
Line 422 in a6e720d