UNPKG

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