Skip to content

Commit

Permalink
Bugfix/select all crashes (equinor#3428)
Browse files Browse the repository at this point in the history
* Move toggleAllSelected so it is clear which variables it is using

* Remove deeply suspicious useEffect

This resolves a crash that occurs when clicking select all 'too' quickly
which otherwise can trigger an infinite loop inside this useEffect.
  • Loading branch information
mhwaage committed Jun 11, 2024
1 parent aeb41ec commit be3b8bb
Showing 1 changed file with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,6 @@ function AutocompleteInner<T>(
return _availableItems
}, [_availableItems, showSelectAll])

const toggleAllSelected = () => {
if (selectedItems.length === inputOptions.length) {
setSelectedItems([])
} else {
setSelectedItems(inputOptions)
}
}

useEffect(() => {
const availableHash = JSON.stringify(inputOptions)
const optionsHash = JSON.stringify(options)
Expand Down Expand Up @@ -446,6 +438,14 @@ function AutocompleteInner<T>(
return 'NONE'
}, [inputOptions, selectedItems])

const toggleAllSelected = () => {
if (selectedItems.length === inputOptions.length) {
setSelectedItems([])
} else {
setSelectedItems(inputOptions)
}
}

const getLabel = useCallback(
(item: T) => {
//note: non strict check for null or undefined to allow 0
Expand Down Expand Up @@ -726,12 +726,6 @@ function AutocompleteInner<T>(
reset: resetCombobox,
} = useCombobox(comboBoxProps)

useEffect(() => {
if (isControlled) {
setSelectedItems(selectedOptions)
}
}, [isControlled, selectedOptions, setSelectedItems])

const { x, y, refs, update, strategy } = useFloating<HTMLInputElement>({
placement: 'bottom-start',
middleware: [
Expand Down

0 comments on commit be3b8bb

Please sign in to comment.