-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathtypes.ts
More file actions
383 lines (326 loc) · 12.4 KB
/
types.ts
File metadata and controls
383 lines (326 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import type { Key, ReactElement, ReactNode } from 'react';
import type { DataGridProps } from './DataGrid';
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export type Maybe<T> = T | undefined | null;
export type StateSetter<S> = React.Dispatch<React.SetStateAction<S>>;
export interface Column<TRow, TSummaryRow = unknown> {
/** The name of the column. Displayed in the header cell by default */
readonly name: string | ReactElement;
/** A unique key to distinguish each column */
readonly key: string;
/**
* Column width. If not specified, it will be determined automatically based on grid width and specified widths of other columns
* @default 'auto'
*/
readonly width?: Maybe<number | string>;
/**
* Minimum column width in pixels
* @default 50
*/
readonly minWidth?: Maybe<number>;
/** Maximum column width in pixels */
readonly maxWidth?: Maybe<number>;
/** Class name(s) for the cell */
readonly cellClass?: Maybe<string | ((row: TRow) => Maybe<string>)>;
/** Class name(s) for the header cell */
readonly headerCellClass?: Maybe<string>;
/** Class name(s) for the summary cell */
readonly summaryCellClass?: Maybe<string | ((row: TSummaryRow) => Maybe<string>)>;
/** Render function to render the content of cells */
readonly renderCell?: Maybe<(props: RenderCellProps<TRow, TSummaryRow>) => ReactNode>;
/** Render function to render the content of the header cell */
readonly renderHeaderCell?: Maybe<(props: RenderHeaderCellProps<TRow, TSummaryRow>) => ReactNode>;
/** Render function to render the content of summary cells */
readonly renderSummaryCell?: Maybe<
(props: RenderSummaryCellProps<TSummaryRow, TRow>) => ReactNode
>;
/** Render function to render the content of group cells */
readonly renderGroupCell?: Maybe<(props: RenderGroupCellProps<TRow, TSummaryRow>) => ReactNode>;
/** Render function to render the content of edit cells. When set, the column is automatically set to be editable */
readonly renderEditCell?: Maybe<(props: RenderEditCellProps<TRow, TSummaryRow>) => ReactNode>;
/** Enables cell editing. If set and no editor property specified, then a textinput will be used as the cell editor */
readonly editable?: Maybe<boolean | ((row: TRow) => boolean)>;
readonly colSpan?: Maybe<(args: ColSpanArgs<TRow, TSummaryRow>) => Maybe<number>>;
/** Determines whether column is frozen */
readonly frozen?: Maybe<boolean>;
/** Enable resizing of the column */
readonly resizable?: Maybe<boolean>;
/** Enable sorting of the column */
readonly sortable?: Maybe<boolean>;
/** Enable dragging of the column */
readonly draggable?: Maybe<boolean>;
/** Sets the column sort order to be descending instead of ascending the first time the column is sorted */
readonly sortDescendingFirst?: Maybe<boolean>;
/** Options for cell editing */
readonly editorOptions?: Maybe<{
/**
* Render the cell content in addition to the edit cell.
* Enable this option when the editor is rendered outside the grid, like a modal for example.
* By default, the cell content is not rendered when the edit cell is open.
* @default false
*/
readonly displayCellContent?: Maybe<boolean>;
/**
* Commit changes when clicking outside the cell
* @default true
*/
readonly commitOnOutsideClick?: Maybe<boolean>;
/**
* Close the editor when the row changes externally
* @default true
*/
readonly closeOnExternalRowChange?: Maybe<boolean>;
}>;
}
export interface CalculatedColumn<TRow, TSummaryRow = unknown> extends Column<TRow, TSummaryRow> {
readonly parent: CalculatedColumnParent<TRow, TSummaryRow> | undefined;
readonly idx: number;
readonly level: number;
readonly width: number | string;
readonly minWidth: number;
readonly maxWidth: number | undefined;
readonly resizable: boolean;
readonly sortable: boolean;
readonly draggable: boolean;
readonly frozen: boolean;
readonly renderCell: (props: RenderCellProps<TRow, TSummaryRow>) => ReactNode;
readonly renderHeaderCell: (props: RenderHeaderCellProps<TRow, TSummaryRow>) => ReactNode;
}
export interface ColumnGroup<R, SR = unknown> {
/** The name of the column group, it will be displayed in the header cell */
readonly name: string | ReactElement;
readonly headerCellClass?: Maybe<string>;
readonly children: readonly ColumnOrColumnGroup<R, SR>[];
}
export interface CalculatedColumnParent<R, SR> {
readonly name: string | ReactElement;
readonly parent: CalculatedColumnParent<R, SR> | undefined;
readonly idx: number;
readonly colSpan: number;
readonly level: number;
readonly headerCellClass?: Maybe<string>;
}
export type ColumnOrColumnGroup<R, SR = unknown> = Column<R, SR> | ColumnGroup<R, SR>;
export type CalculatedColumnOrColumnGroup<R, SR> =
| CalculatedColumnParent<R, SR>
| CalculatedColumn<R, SR>;
export interface Position {
readonly idx: number;
readonly rowIdx: number;
}
export interface RenderCellProps<TRow, TSummaryRow = unknown> {
column: CalculatedColumn<TRow, TSummaryRow>;
row: TRow;
rowIdx: number;
isCellEditable: boolean;
tabIndex: number;
onRowChange: (row: TRow) => void;
}
export interface RenderSummaryCellProps<TSummaryRow, TRow = unknown> {
column: CalculatedColumn<TRow, TSummaryRow>;
row: TSummaryRow;
tabIndex: number;
}
export interface RenderGroupCellProps<TRow, TSummaryRow = unknown> {
groupKey: unknown;
column: CalculatedColumn<TRow, TSummaryRow>;
row: GroupRow<TRow>;
childRows: readonly TRow[];
isExpanded: boolean;
tabIndex: number;
toggleGroup: () => void;
}
export interface RenderEditCellProps<TRow, TSummaryRow = unknown> {
column: CalculatedColumn<TRow, TSummaryRow>;
row: TRow;
rowIdx: number;
onRowChange: (row: TRow, commitChanges?: boolean) => void;
onClose: (commitChanges?: boolean, shouldFocusCell?: boolean) => void;
}
export interface RenderHeaderCellProps<TRow, TSummaryRow = unknown> {
column: CalculatedColumn<TRow, TSummaryRow>;
sortDirection: SortDirection | undefined;
priority: number | undefined;
tabIndex: number;
}
interface BaseCellRendererProps<TRow, TSummaryRow = unknown>
extends Omit<React.ComponentProps<'div'>, 'children'>,
Pick<
DataGridProps<TRow, TSummaryRow>,
'onCellMouseDown' | 'onCellClick' | 'onCellDoubleClick' | 'onCellContextMenu'
> {
rowIdx: number;
selectCell: (position: Position, options?: SelectCellOptions) => void;
}
export interface CellRendererProps<TRow, TSummaryRow>
extends BaseCellRendererProps<TRow, TSummaryRow> {
column: CalculatedColumn<TRow, TSummaryRow>;
row: TRow;
colSpan: number | undefined;
isDraggedOver: boolean;
isCellSelected: boolean;
onRowChange: (column: CalculatedColumn<TRow, TSummaryRow>, newRow: TRow) => void;
}
export type CellEvent<E extends React.SyntheticEvent<HTMLDivElement>> = E & {
preventGridDefault: () => void;
isGridDefaultPrevented: () => boolean;
};
export type CellMouseEvent = CellEvent<React.MouseEvent<HTMLDivElement>>;
export type CellKeyboardEvent = CellEvent<React.KeyboardEvent<HTMLDivElement>>;
export type CellClipboardEvent = React.ClipboardEvent<HTMLDivElement>;
export interface CellMouseArgs<TRow, TSummaryRow = unknown> {
column: CalculatedColumn<TRow, TSummaryRow>;
row: TRow;
rowIdx: number;
selectCell: (enableEditor?: boolean) => void;
}
interface SelectCellKeyDownArgs<TRow, TSummaryRow = unknown> {
mode: 'SELECT';
column: CalculatedColumn<TRow, TSummaryRow>;
row: TRow;
rowIdx: number;
selectCell: (position: Position, options?: SelectCellOptions) => void;
}
export interface EditCellKeyDownArgs<TRow, TSummaryRow = unknown> {
mode: 'EDIT';
column: CalculatedColumn<TRow, TSummaryRow>;
row: TRow;
rowIdx: number;
navigate: () => void;
onClose: (commitChanges?: boolean, shouldFocusCell?: boolean) => void;
}
export type CellKeyDownArgs<TRow, TSummaryRow = unknown> =
| SelectCellKeyDownArgs<TRow, TSummaryRow>
| EditCellKeyDownArgs<TRow, TSummaryRow>;
export interface CellSelectArgs<TRow, TSummaryRow = unknown> {
rowIdx: number;
row: TRow | undefined;
column: CalculatedColumn<TRow, TSummaryRow>;
}
export type CellMouseEventHandler<R, SR> = Maybe<
(args: CellMouseArgs<NoInfer<R>, NoInfer<SR>>, event: CellMouseEvent) => void
>;
export interface BaseRenderRowProps<TRow, TSummaryRow = unknown>
extends BaseCellRendererProps<TRow, TSummaryRow> {
viewportColumns: readonly CalculatedColumn<TRow, TSummaryRow>[];
rowIdx: number;
selectedCellIdx: number | undefined;
isRowSelectionDisabled: boolean;
isRowSelected: boolean;
gridRowStart: number;
}
export interface RenderRowProps<TRow, TSummaryRow = unknown>
extends BaseRenderRowProps<TRow, TSummaryRow> {
row: TRow;
lastFrozenColumnIndex: number;
draggedOverCellIdx: number | undefined;
selectedCellEditor: ReactElement<RenderEditCellProps<TRow>> | undefined;
onRowChange: (column: CalculatedColumn<TRow, TSummaryRow>, rowIdx: number, newRow: TRow) => void;
rowClass: Maybe<(row: TRow, rowIdx: number) => Maybe<string>>;
}
export interface RowsChangeData<R, SR = unknown> {
indexes: number[];
column: CalculatedColumn<R, SR>;
}
export interface SelectRowEvent<TRow> {
row: TRow;
checked: boolean;
isShiftClick: boolean;
}
export interface SelectHeaderRowEvent {
checked: boolean;
}
export interface FillEvent<TRow> {
columnKey: string;
sourceRow: TRow;
targetRow: TRow;
}
interface CellCopyPasteArgs<TRow, TSummaryRow = unknown> {
column: CalculatedColumn<TRow, TSummaryRow>;
row: TRow;
}
export type CellCopyArgs<TRow, TSummaryRow = unknown> = CellCopyPasteArgs<TRow, TSummaryRow>;
export type CellPasteArgs<TRow, TSummaryRow = unknown> = CellCopyPasteArgs<TRow, TSummaryRow>;
export interface GroupRow<TRow> {
readonly childRows: readonly TRow[];
readonly id: string;
readonly parentId: unknown;
readonly groupKey: unknown;
readonly isExpanded: boolean;
readonly level: number;
readonly posInSet: number;
readonly setSize: number;
readonly startRowIndex: number;
}
export interface SortColumn {
readonly columnKey: string;
readonly direction: SortDirection;
}
export type CellNavigationMode = 'NONE' | 'CHANGE_ROW';
export type SortDirection = 'ASC' | 'DESC';
export type ColSpanArgs<TRow, TSummaryRow> =
| { type: 'HEADER' }
| { type: 'ROW'; row: TRow }
| { type: 'SUMMARY'; row: TSummaryRow };
export type RowHeightArgs<TRow> =
| { type: 'ROW'; row: TRow }
| { type: 'GROUP'; row: GroupRow<TRow> };
export interface RenderSortIconProps {
sortDirection: SortDirection | undefined;
}
export interface RenderSortPriorityProps {
priority: number | undefined;
}
export interface RenderSortStatusProps extends RenderSortIconProps, RenderSortPriorityProps {}
export interface RenderCheckboxProps
extends Pick<
React.ComponentProps<'input'>,
'aria-label' | 'aria-labelledby' | 'checked' | 'tabIndex' | 'disabled'
> {
indeterminate?: boolean | undefined;
onChange: (checked: boolean, shift: boolean) => void;
}
export interface Renderers<TRow, TSummaryRow> {
renderCell?: Maybe<(key: Key, props: CellRendererProps<TRow, TSummaryRow>) => ReactNode>;
renderCheckbox?: Maybe<(props: RenderCheckboxProps) => ReactNode>;
renderRow?: Maybe<(key: Key, props: RenderRowProps<TRow, TSummaryRow>) => ReactNode>;
renderSortStatus?: Maybe<(props: RenderSortStatusProps) => ReactNode>;
noRowsFallback?: Maybe<ReactNode>;
}
export interface SelectCellOptions {
enableEditor?: Maybe<boolean>;
shouldFocusCell?: Maybe<boolean>;
}
export interface ColumnWidth {
readonly type: 'resized' | 'measured';
readonly width: number;
}
export type ColumnWidths = ReadonlyMap<string, ColumnWidth>;
export type Direction = 'ltr' | 'rtl';
export type ResizedWidth = number | 'max-content';
export interface VirtualizationOptions {
/** Enable row virtualization */
/** @default 4 */
rows?: boolean | {overscanThreshold: number};
columns?: boolean;
}
export function isVirtualizationOptions(obj: unknown): obj is VirtualizationOptions {
if (typeof obj !== 'object' || obj === null) return false;
const o = obj as VirtualizationOptions;
if ('rows' in o && typeof o.rows !== 'undefined') {
if (typeof o.rows !== 'boolean') {
if (
typeof o.rows !== 'object' ||
o.rows === null ||
!('overscanThreshold' in o.rows) ||
typeof (o.rows as any).overscanThreshold !== 'number'
) {
return false;
}
}
}
if ('columns' in o && typeof o.columns !== 'undefined' && typeof o.columns !== 'boolean') {
return false;
}
return true;
}