Skip to content

Commit c551868

Browse files
authored
feat: add generic type for useSelection (#578)
1 parent 12cdf95 commit c551868

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

packages/module/src/Hooks/selection.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { useState } from "react";
33

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

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

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

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

27-
const setSelectedItems = (items: any[]) => {
27+
const setSelectedItems = (items: T[]) => {
2828
setSelected(items);
2929
};
3030

0 commit comments

Comments
 (0)