@apia/table2-controller 4.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,330 @@
1
+ import * as react from 'react';
2
+ import { HTMLAttributes, ReactNode, FC, ThHTMLAttributes, FocusEvent } from 'react';
3
+ import { EventEmitter } from '@apia/util';
4
+
5
+ type RowsGroupBaseState = ElementBaseState & {
6
+ rows: Row[];
7
+ domProperties?: HTMLAttributes<HTMLTableSectionElement>;
8
+ properties?: Record<string, unknown>;
9
+ };
10
+ declare class RowsGroup<U extends RowsGroupBaseState> extends Element<U & RowsGroupBaseState> {
11
+ table: TableController;
12
+ constructor(table: TableController, state: U);
13
+ get rows(): Row[];
14
+ addRow(row: Row): this;
15
+ addRows(rows: Row[]): this;
16
+ clear(): Row[];
17
+ getRowById(id: string | number): Row | undefined;
18
+ getRowByIndex(index: number): Row;
19
+ getRowIndex(row: Row): number;
20
+ insertAfter(anchor: Row, newElement: Row): void;
21
+ insertBefore(anchor: Row, newElement: Row): void;
22
+ removeRow(row: Row): void;
23
+ swapRows(a: Row, b: Row): void;
24
+ private runAddRowPlugin;
25
+ }
26
+
27
+ type RowProperties = {
28
+ id: string;
29
+ cells: Cell[];
30
+ isExpandible?: boolean;
31
+ isExpanded?: boolean;
32
+ isHidden?: boolean;
33
+ isSelectable?: boolean;
34
+ isSelected?: boolean;
35
+ domProperties?: HTMLAttributes<HTMLTableRowElement>;
36
+ properties?: Record<string, unknown>;
37
+ };
38
+ type TBuildRow = Omit<RowProperties, 'cells'> & {
39
+ cells: CellState[];
40
+ };
41
+ type RowState = Omit<RowProperties, 'isSelected'>;
42
+ declare class Row extends Element<RowState> {
43
+ private properties?;
44
+ protected container: null | RowsGroup<RowsGroupBaseState>;
45
+ addCell(cell: Cell): this;
46
+ addCellAfter(anchor: Cell, newElement: Cell): void;
47
+ addCellBefore(anchor: Cell, newElement: Cell): void;
48
+ build(data: TBuildRow): void;
49
+ constructor(id: string, properties?: Omit<RowProperties, "id"> | undefined);
50
+ get cells(): Cell<CellState<any>>[];
51
+ get isSelected(): boolean;
52
+ getCellByColumnName(colName: string): Cell<CellState<any>> | undefined;
53
+ getCellByIndex(index: number): Cell<CellState<any>>;
54
+ getCellIndex(cell: Cell): number;
55
+ getContainer(): RowsGroup<RowsGroupBaseState> | null;
56
+ getFollowingRow(): Row;
57
+ get index(): number;
58
+ getPreviousRow(): Row;
59
+ insertAfter(row: Row): void;
60
+ insertBefore(row: Row): void;
61
+ remove(): void;
62
+ scrollIntoView(): void;
63
+ setContainer(container: null | RowsGroup<RowsGroupBaseState>): this;
64
+ setTable(table: null | TableController): this;
65
+ swap(withRow: Row): void;
66
+ toggleSelection(forceState?: boolean): void;
67
+ }
68
+
69
+ type TableBodyState = ElementBaseState & RowsGroupBaseState & {};
70
+ type TBuildBody = {
71
+ rows: TBuildRow[];
72
+ };
73
+ declare class TableBody extends RowsGroup<TableBodyState> {
74
+ constructor(table: TableController);
75
+ build(data: TBuildBody): void;
76
+ }
77
+
78
+ type TableHeadState = ElementBaseState & RowsGroupBaseState & {};
79
+ type TBuildHead = {
80
+ columns: CellState[];
81
+ filters: any[];
82
+ };
83
+ declare class TableHead extends RowsGroup<TableHeadState> {
84
+ constructor(table: TableController);
85
+ build(data: TBuildHead): void;
86
+ }
87
+
88
+ /**
89
+ * This class is the default way to customize the table behavior.
90
+ */
91
+ interface TablePlugin {
92
+ getRenderers?: () => {
93
+ cell?: React.FC<{
94
+ cell: Cell;
95
+ }>;
96
+ };
97
+ onAddRow?: (controller: TableController, target: RowsGroup<any>, newRow: Row) => void;
98
+ onResize?: (controller: TableController, colName: string, newSize: number) => void;
99
+ /**
100
+ * This hook is called when the table is renderized.
101
+ * The elements returned by this hook will be added to the dom of the element.
102
+ */
103
+ useElementDomProps?: (controller: TableController, _element: TableController | Element<any>, _context: any) => HTMLAttributes<HTMLElement>;
104
+ }
105
+ declare const defaultPlugins: () => (WaiAriaPlugin | FocusControllerPlugin)[];
106
+
107
+ type TableState = {
108
+ allowEdition: boolean;
109
+ allowResize: boolean;
110
+ allowSelection: boolean;
111
+ editionMode: boolean;
112
+ focusedCell: Cell | null;
113
+ hiddenColumns: Set<string>;
114
+ isSelectionMultiple: boolean;
115
+ plugins: TablePlugin[];
116
+ selection: Set<Row>;
117
+ };
118
+ type TBuildTable = TBuildHead & TBuildBody;
119
+ declare class TableController extends EventEmitter<{
120
+ cellFocus: {
121
+ cell: Cell;
122
+ doFocus: boolean;
123
+ };
124
+ columnClick: Cell;
125
+ editionMode: boolean;
126
+ keyEnter: null;
127
+ rowDelete: Row;
128
+ scrollIntoRow: Row;
129
+ selectionChange: Row[];
130
+ }> {
131
+ private state;
132
+ constructor(props?: Partial<Omit<TableState, 'selection'>>);
133
+ setHiddenColumns(cols: Set<string>): void;
134
+ body: TableBody;
135
+ head: TableHead;
136
+ /**
137
+ * Clears all data in the table, except for the events
138
+ * listeners and recreates the structure based on the passed data.
139
+ */
140
+ build(data: TBuildTable): void;
141
+ getRowById(id: string | number): Row | undefined;
142
+ /**
143
+ * EDITION MODE
144
+ */
145
+ get isEditionMode(): boolean;
146
+ toggleEditionMode(force?: boolean): void;
147
+ /**
148
+ * SELECTION
149
+ */
150
+ clearSelection(): void;
151
+ /**
152
+ * Returns the selected rows.
153
+ */
154
+ getSelectedRows(): Row[];
155
+ isSelected(row: Row): boolean;
156
+ /**
157
+ * Toggles the selection state of a given row.
158
+ *
159
+ * If the `isSelected` parameter is provided, it explicitly sets the selection
160
+ * state of the row to the given value (`true` for selected, `false` for deselected).
161
+ * If `isSelected` is not provided, the method will toggle the current selection state
162
+ * of the row (i.e., select it if it's currently not selected, or deselect it if it is).
163
+ *
164
+ * @param row - The row to toggle selection for.
165
+ * @param isSelected - Optional. A boolean indicating whether the row should be selected (`true`)
166
+ * or deselected (`false`). If not provided, the selection state will be toggled.
167
+ */
168
+ toggleRowSelection(row: Row, isSelected?: boolean): void;
169
+ protected restoreFocus(): void;
170
+ removeRow(row: Row): void;
171
+ /**
172
+ * FOCUS
173
+ */
174
+ /**
175
+ * @returns The currently focused cell in the table.
176
+ */
177
+ getFocusedCell(): Cell<CellState<any>> | null;
178
+ setFocusedCell(cell: Cell, doFocus?: boolean): void;
179
+ setSelection(indices: number[]): void;
180
+ scrollRowIntoView(row: Row): void;
181
+ /**
182
+ * STATE
183
+ */
184
+ /**
185
+ * Returns the current state of the `TableController` instance.
186
+ *
187
+ * @param prop - The property of the state to retrieve.
188
+ */
189
+ getState<K extends keyof TableState>(prop: K): TableState[K];
190
+ /**
191
+ * Updates the state of the `TableController` instance.
192
+ *
193
+ * This method provides two overloads:
194
+ * 1. A callback function that takes the current state and returns a new state.
195
+ * 2. A key-value pair where the key is a property of the state, and the value is the new value for that property.
196
+ *
197
+ * @param prop - Either a callback function to update the state or the key of the state property to update.
198
+ * @param value - (Optional) The new value for the specified state property. This is required if `prop` is a key.
199
+ *
200
+ * @returns The `TableController` instance for method chaining.
201
+ *
202
+ * ### Usage:
203
+ *
204
+ * **Using a callback function:**
205
+ * ```tsx
206
+ * tableController.setState((state) => ({
207
+ * ...state,
208
+ * allowEdition: false,
209
+ * }));
210
+ * ```
211
+ *
212
+ * **Using a key-value pair:**
213
+ * ```tsx
214
+ * tableController.setState('allowEdition', false);
215
+ * ```
216
+ */
217
+ setState(cb: (s: TableState) => TableState): this;
218
+ setState<K extends keyof TableState>(prop: K, value: TableState[K]): this;
219
+ toggleColumnVisibility(columnName: string, isVisible?: boolean): void;
220
+ isColumnVisible(columnName: string): boolean;
221
+ /**
222
+ * PLUGINS
223
+ */
224
+ getPluginOfType<T extends TablePlugin>(clazz: new (...args: any[]) => T): T | undefined;
225
+ /**
226
+ * Sets the plugins for the table.
227
+ */
228
+ setPlugins(cb: (currentPlugins: TablePlugin[]) => TablePlugin[]): void;
229
+ getApplicablePlugins<K extends keyof TablePlugin>(method: K): ((...args: Parameters<NonNullable<TablePlugin[K]>> extends [
230
+ any,
231
+ ...infer Rest
232
+ ] ? Rest : void[]) => ReturnType<NonNullable<TablePlugin[K]>>)[];
233
+ }
234
+
235
+ type ElementBaseState = {
236
+ properties?: Record<string, unknown>;
237
+ };
238
+ declare abstract class Element<T extends ElementBaseState> {
239
+ protected state: T;
240
+ protected table: TableController;
241
+ constructor(state: T);
242
+ getTable(): TableController;
243
+ setTable(table: null | TableController): this;
244
+ getState<K extends keyof T>(prop: K): T[K];
245
+ setState(cb: (s: T) => T): this;
246
+ setState<K extends keyof T>(prop: K, value: T[K]): this;
247
+ }
248
+
249
+ type CellState<T extends CellState<any> = CellState<any>> = {
250
+ colName: string;
251
+ content?: ReactNode;
252
+ className?: string;
253
+ isFocused?: boolean;
254
+ isHidden?: boolean;
255
+ Renderer?: FC<{
256
+ cell: Cell<T>;
257
+ }>;
258
+ domProperties?: ThHTMLAttributes<HTMLTableCellElement>;
259
+ properties?: Record<string, unknown>;
260
+ };
261
+ declare class Cell<State extends CellState<any> = CellState<any>> extends Element<State> {
262
+ protected parentRow: Row;
263
+ getIndex(): number;
264
+ getRow(): Row;
265
+ setRow(row: Row): this;
266
+ /**
267
+ *
268
+ * @param doFocus - If true, the cell will be focused after being set as the focused cell.
269
+ * If false, the cell will contain the tabIndex = 0 but will not be focused.
270
+ */
271
+ focus(doFocus?: boolean): void;
272
+ updateState(state: Partial<State>): void;
273
+ }
274
+
275
+ declare class FocusControllerPlugin implements TablePlugin {
276
+ rootElement: HTMLElement | null;
277
+ scrollIntoView(rowId: string): void;
278
+ useElementDomProps(controller: TableController, element: TableController | Element<any>, _context: any): {
279
+ className: string;
280
+ onClick: (ev: react.MouseEvent) => void;
281
+ onKeyDown: (ev: react.KeyboardEvent) => void;
282
+ onBlur: (ev: FocusEvent) => void;
283
+ ref: (el: HTMLElement | null) => void;
284
+ } | {
285
+ className?: undefined;
286
+ onClick?: undefined;
287
+ onKeyDown?: undefined;
288
+ onBlur?: undefined;
289
+ ref?: undefined;
290
+ };
291
+ }
292
+
293
+ declare class WaiAriaPlugin implements TablePlugin {
294
+ useElementDomProps(controller: TableController, element: TableController | Element<any>, context: any): {
295
+ 'aria-selected': boolean;
296
+ 'aria-rowindex': any;
297
+ 'aria-colindex'?: undefined;
298
+ role?: undefined;
299
+ 'aria-multiselectable'?: undefined;
300
+ 'aria-colcount'?: undefined;
301
+ 'aria-rowcount'?: undefined;
302
+ } | {
303
+ 'aria-colindex': any;
304
+ 'aria-selected'?: undefined;
305
+ 'aria-rowindex'?: undefined;
306
+ role?: undefined;
307
+ 'aria-multiselectable'?: undefined;
308
+ 'aria-colcount'?: undefined;
309
+ 'aria-rowcount'?: undefined;
310
+ } | {
311
+ role: string;
312
+ 'aria-multiselectable': boolean | undefined;
313
+ 'aria-colcount': number;
314
+ 'aria-rowcount': number;
315
+ 'aria-selected'?: undefined;
316
+ 'aria-rowindex'?: undefined;
317
+ 'aria-colindex'?: undefined;
318
+ } | {
319
+ 'aria-selected'?: undefined;
320
+ 'aria-rowindex'?: undefined;
321
+ 'aria-colindex'?: undefined;
322
+ role?: undefined;
323
+ 'aria-multiselectable'?: undefined;
324
+ 'aria-colcount'?: undefined;
325
+ 'aria-rowcount'?: undefined;
326
+ };
327
+ }
328
+
329
+ export { Cell, type CellState, Element, type ElementBaseState, FocusControllerPlugin, Row, type RowProperties, type RowState, RowsGroup, type RowsGroupBaseState, type TBuildBody, type TBuildHead, type TBuildRow, type TBuildTable, TableBody, type TableBodyState, TableController, TableHead, type TableHeadState, type TablePlugin, type TableState, WaiAriaPlugin, defaultPlugins };
330
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sources":[],"sourcesContent":[],"names":[],"mappings":""}