You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
portainer/app/react/hooks/useListSelection.ts

21 lines
576 B

import { useState } from 'react';
export function useListSelection<T>(
initialValue: Array<T> = [],
compareFn: (a: T, b: T) => boolean = (a, b) => a === b
) {
const [selectedItems, setSelectedItems] = useState<Array<T>>(initialValue);
function handleChangeSelect(currentItem: T, selected: boolean) {
if (selected) {
setSelectedItems((items) => [...items, currentItem]);
} else {
setSelectedItems((items) =>
items.filter((item) => !compareFn(item, currentItem))
);
}
}
return [selectedItems, handleChangeSelect] as const;
}