Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/module/src/Hooks/selection.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useState } from "react";

export interface UseDataViewSelectionProps {
export interface UseDataViewSelectionProps<T = any> {
/** Function to compare items when checking if item is selected */
matchOption: (item: any, another: any) => boolean;
matchOption: (item: T, another: T) => boolean;
/** Array of initially selected entries */
initialSelected?: (any)[];
initialSelected?: (T)[];
}

export const useDataViewSelection = (props?: UseDataViewSelectionProps) => {
const [ selected, setSelected ] = useState<any[]>(props?.initialSelected ?? []);
export const useDataViewSelection = <T = any>(props?: UseDataViewSelectionProps<T>) => {
const [ selected, setSelected ] = useState<T[]>(props?.initialSelected ?? []);
const matchOption = props?.matchOption ? props.matchOption : (option, another) => (option === another);

const onSelect = (isSelecting: boolean, items?: any[] | any) => {
const onSelect = (isSelecting: boolean, items?: T[] | T) => {
isSelecting && items ?
setSelected(prev => {
const newSelectedItems = [ ...prev ];
Expand All @@ -22,9 +22,9 @@ export const useDataViewSelection = (props?: UseDataViewSelectionProps) => {
: setSelected(items ? prev => prev.filter(prevSelected => !(Array.isArray(items) ? items : [ items ]).some(item => matchOption(item, prevSelected))) : []);
};

const isSelected = (item: any): boolean => Boolean(selected.find(selected => matchOption(selected, item)));
const isSelected = (item: T): boolean => Boolean(selected.find(selected => matchOption(selected, item)));

const setSelectedItems = (items: any[]) => {
const setSelectedItems = (items: T[]) => {
setSelected(items);
};

Expand Down