UNPKG

3.57 kBTypeScriptView Raw
1import { IObjectWithKey, ISelection, SelectionMode } from './Selection.types';
2/**
3 * {@docCategory Selection}
4 */
5export interface ISelectionOptions<TItem = IObjectWithKey> {
6 onSelectionChanged?: () => void;
7 onItemsChanged?: () => void;
8 /** Custom logic to generate item keys. Required if `TItem` does not have a `key` property. */
9 getKey?: (item: TItem, index?: number) => string | number;
10 canSelectItem?: (item: TItem, index?: number) => boolean;
11 selectionMode?: SelectionMode;
12 items?: TItem[];
13}
14/**
15 * Selection options with required `getKey` property.
16 * {@docCategory Selection}
17 */
18export declare type ISelectionOptionsWithRequiredGetKey<TItem> = ISelectionOptions<TItem> & Required<Pick<ISelectionOptions<TItem>, 'getKey'>>;
19/**
20 * {@docCategory Selection}
21 */
22export declare class Selection<TItem = IObjectWithKey> implements ISelection<TItem> {
23 /** Number of items selected. Do not modify. */
24 count: number;
25 readonly mode: SelectionMode;
26 private _getKey;
27 private _canSelectItem;
28 private _changeEventSuppressionCount;
29 private _items;
30 private _selectedItems;
31 private _selectedIndices;
32 private _isAllSelected;
33 private _exemptedIndices;
34 private _exemptedCount;
35 private _keyToIndexMap;
36 private _anchoredIndex;
37 private _onSelectionChanged;
38 private _onItemsChanged;
39 private _hasChanged;
40 private _unselectableIndices;
41 private _unselectableCount;
42 private _isModal;
43 /**
44 * Create a new Selection. If `TItem` does not have a `key` property, you must provide an options
45 * object with a `getKey` implementation. Providing options is optional otherwise.
46 * (At most one `options` object is accepted.)
47 */
48 constructor(...options: TItem extends IObjectWithKey ? [] | [ISelectionOptions<TItem>] : [ISelectionOptionsWithRequiredGetKey<TItem>]);
49 canSelectItem(item: TItem, index?: number): boolean;
50 getKey(item: TItem, index?: number): string;
51 setChangeEvents(isEnabled: boolean, suppressChange?: boolean): void;
52 isModal(): boolean;
53 setModal(isModal: boolean): void;
54 /**
55 * Selection needs the items, call this method to set them. If the set
56 * of items is the same, this will re-evaluate selection and index maps.
57 * Otherwise, shouldClear should be set to true, so that selection is
58 * cleared.
59 */
60 setItems(items: TItem[], shouldClear?: boolean): void;
61 getItems(): TItem[];
62 getSelection(): TItem[];
63 getSelectedCount(): number;
64 getSelectedIndices(): number[];
65 getItemIndex(key: string): number;
66 isRangeSelected(fromIndex: number, count: number): boolean;
67 isAllSelected(): boolean;
68 isKeySelected(key: string): boolean;
69 isIndexSelected(index: number): boolean;
70 setAllSelected(isAllSelected: boolean): void;
71 setKeySelected(key: string, isSelected: boolean, shouldAnchor: boolean): void;
72 setIndexSelected(index: number, isSelected: boolean, shouldAnchor: boolean): void;
73 setRangeSelected(fromIndex: number, count: number, isSelected: boolean, shouldAnchor: boolean): void;
74 selectToKey(key: string, clearSelection?: boolean): void;
75 selectToRange(fromIndex: number, count: number, clearSelection?: boolean): void;
76 selectToIndex(index: number, clearSelection?: boolean): void;
77 toggleAllSelected(): void;
78 toggleKeySelected(key: string): void;
79 toggleIndexSelected(index: number): void;
80 toggleRangeSelected(fromIndex: number, count: number): void;
81 private _updateCount;
82 private _setAllSelected;
83 private _change;
84}