fix(ComboBox): avoid extra onSelectionChange calls#9729
Conversation
|
Build successful! 🎉 |
| expect(preventDefault).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should only call commit on Tab when the menu is open', function () { |
There was a problem hiding this comment.
is this still true if it allows a custom value?
There was a problem hiding this comment.
I think useComboBoxState will still clear the selected key on field blur via
still
LFDanLu
left a comment
There was a problem hiding this comment.
I think these changes are fine after walking through it, but will need to do some careful testing.
| // 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. |
There was a problem hiding this comment.
Decided to post this for others walking this logic for this change:
commitSelection is only called in these cases:
- when
revertis called aka pressingEsc, which should revert theinputValueand make sure theselectedKeydoesn't change - when
inputValue === itemTextin theallowCustomValuecase ofcommitValueof which results in a no-op except for multiple selection mode which we don't really support anyways - conversely, used to reset the
inputValueand close the menu incommitValueifallowCustomValueis false. This is only actually called as.closeor if the menu is already closed and we are committing the value - as a part of
commitif we are selecting an already selected key
of those cases, we only really need to fire the onSelectionChange and onChange for the last two cases which seems to check out with new conditions here. The only one I'm not entirely sure is the selectionMode === 'multiple' part of the check, do we really need that? It might not hurt to have it I guess since it would simply call it for an extra case where inputValue === itemText but that is only when inputValue = '' since selectedKey is always null for multiple selection
|
Build successful! 🎉 |
Closes #9727
Before:
onSelectionChangeonce (expected)After:
onSelectionChangecalls occur when values are already in sync✅ Pull Request Checklist:
📝 Test Instructions:
Unit tests should cover, but smoke test in storybook.
🧢 Your Project: