Skip to content

Commit

Permalink
Multiselection - avoid calling onChange if the value if the changes a…
Browse files Browse the repository at this point in the history
…re synced with the value prop (#2324)
  • Loading branch information
JohnC-80 authored Jul 18, 2024
1 parent 32ab3da commit 95188e9
Show file tree
Hide file tree
Showing 3 changed files with 505 additions and 442 deletions.
9 changes: 7 additions & 2 deletions lib/MultiSelection/MultiSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ const MultiSelection = ({
boil each object down to a string, so that the strict-equals will
have a better time succeeding. */
onSelectedItemsChange(changes) {
onChange(changes.selectedItems);
// only trigger onChange if selectedItems is different from from the incoming value prop.
// this avoids instances when the value can appear *not to change for the user but it's really the cause of
// values just changing between re-renders and react just catching whichever onChange was triggered last.
if (!isEqual(value, changes.selectedItems)) {
onChange(changes.selectedItems);
}
awaitingChange.current = false;
},
onStateChange({ selectedItems: newSelectedItems, type }) {
Expand Down Expand Up @@ -422,7 +427,7 @@ const MultiSelection = ({
getInputProps={getInputProps}
getDropdownProps={getDropdownProps}
isOpen={isOpen}
placeholder={selectedItems.length === 0 && placeholder}
placeholder={selectedItems.length === 0 ? placeholder : ''}
menuId={menuId}
setFilterValue={setFilterValue}
setFilterFocus={setFilterFocused}
Expand Down
Loading

0 comments on commit 95188e9

Please sign in to comment.