schummar-table 0.12.0 → 0.12.3

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.
Files changed (64) hide show
  1. package/dist/cjs/index.js +3427 -0
  2. package/dist/cjs/index.js.map +1 -0
  3. package/dist/cjs/package.json +1 -0
  4. package/dist/es/index.js +3427 -0
  5. package/dist/es/index.js.map +1 -0
  6. package/dist/types/components/autoFocusTextField.d.ts +5 -0
  7. package/dist/types/components/cell.d.ts +6 -0
  8. package/dist/types/components/clearFiltersButton.d.ts +1 -0
  9. package/dist/types/components/columnFooter.d.ts +1 -0
  10. package/dist/types/components/columnHeader.d.ts +6 -0
  11. package/dist/types/components/columnSelection.d.ts +2 -0
  12. package/dist/types/components/dateFilter.d.ts +11 -0
  13. package/dist/types/components/dateInput.d.ts +5 -0
  14. package/dist/types/components/datePicker.d.ts +25 -0
  15. package/dist/types/components/expandControl.d.ts +6 -0
  16. package/dist/types/components/export.d.ts +2 -0
  17. package/dist/types/components/filterControl.d.ts +3 -0
  18. package/dist/types/components/formControlLabel.d.ts +6 -0
  19. package/dist/types/components/resizeHandle.d.ts +5 -0
  20. package/dist/types/components/row.d.ts +7 -0
  21. package/dist/types/components/selectComponent.d.ts +5 -0
  22. package/dist/types/components/selectFilter.d.ts +12 -0
  23. package/dist/types/components/sortComponent.d.ts +4 -0
  24. package/dist/types/components/spinnner.d.ts +3 -0
  25. package/dist/types/components/table.d.ts +9 -0
  26. package/dist/types/components/textFilter.d.ts +8 -0
  27. package/dist/types/components/virtualized.d.ts +7 -0
  28. package/dist/types/hooks/useFilter.d.ts +7 -0
  29. package/dist/types/hooks/useMemoMap.d.ts +2 -0
  30. package/dist/types/hooks/useTableMemo.d.ts +6 -0
  31. package/dist/types/hooks/useTheme.d.ts +2 -0
  32. package/dist/types/index.d.ts +13 -0
  33. package/dist/types/internalState/calcItems.d.ts +3 -0
  34. package/dist/types/internalState/calcProps.d.ts +2 -0
  35. package/dist/types/internalState/cleanupState.d.ts +3 -0
  36. package/dist/types/internalState/filterColumns.d.ts +3 -0
  37. package/dist/types/internalState/normalizeExpanded.d.ts +3 -0
  38. package/dist/types/internalState/syncSelections.d.ts +3 -0
  39. package/dist/types/internalState/tableStateStorage.d.ts +13 -0
  40. package/dist/types/internalState/useTableState.d.ts +3 -0
  41. package/dist/types/misc/csvExport.d.ts +7 -0
  42. package/dist/types/misc/debounce.d.ts +4 -0
  43. package/dist/types/misc/helpers.d.ts +16 -0
  44. package/dist/types/misc/queue.d.ts +7 -0
  45. package/dist/types/misc/textMatch.d.ts +2 -0
  46. package/dist/types/misc/throttle.d.ts +5 -0
  47. package/dist/types/theme/defaultTheme/badge.d.ts +2 -0
  48. package/dist/types/theme/defaultTheme/button.d.ts +2 -0
  49. package/dist/types/theme/defaultTheme/checkbox.d.ts +2 -0
  50. package/dist/types/theme/defaultTheme/defaultClasses.d.ts +19 -0
  51. package/dist/types/theme/defaultTheme/defaultColors.d.ts +2 -0
  52. package/dist/types/theme/defaultTheme/defaultComponents.d.ts +2 -0
  53. package/dist/types/theme/defaultTheme/defaultIcons.d.ts +2 -0
  54. package/dist/types/theme/defaultTheme/defaultTexts.d.ts +2 -0
  55. package/dist/types/theme/defaultTheme/iconButton.d.ts +2 -0
  56. package/dist/types/theme/defaultTheme/index.d.ts +2 -0
  57. package/dist/types/theme/defaultTheme/popover.d.ts +7 -0
  58. package/dist/types/theme/defaultTheme/textField.d.ts +2 -0
  59. package/dist/types/theme/mui4Theme/index.d.ts +7 -0
  60. package/dist/types/theme/mui5Theme/index.d.ts +7 -0
  61. package/dist/types/theme/tableTheme.d.ts +10 -0
  62. package/dist/types/theme/useCssVariables.d.ts +9 -0
  63. package/dist/types/types.d.ts +336 -0
  64. package/package.json +3 -2
@@ -0,0 +1,336 @@
1
+ import { CSSInterpolation } from '@emotion/serialize';
2
+ import React, { ComponentType, DependencyList, ReactNode, Ref } from 'react';
3
+ import { TableStateStorage } from './internalState/tableStateStorage';
4
+ import { CsvExportOptions } from './misc/csvExport';
5
+ export declare type Sort = {
6
+ columnId: string | number;
7
+ direction: SortDirection;
8
+ };
9
+ export declare type SortDirection = 'asc' | 'desc';
10
+ export declare type Id = string | number;
11
+ export declare type KeyOfType<T, S> = {
12
+ [K in keyof T]: T[K] extends S ? K : never;
13
+ }[keyof T];
14
+ export declare type FunctionWithDeps<F extends (...args: any[]) => any> = F | [function: F, ...deps: DependencyList];
15
+ export declare type MemoizedFunctions<T> = {
16
+ [K in keyof T]: Exclude<T[K], [function: (...args: any[]) => any, ...deps: DependencyList[]]>;
17
+ };
18
+ export interface TableTheme<T = unknown> {
19
+ /** Define display texts. */
20
+ text: {
21
+ selectColumns: ReactNode;
22
+ noResults: ReactNode;
23
+ exportTitle: ReactNode;
24
+ exportCopy: ReactNode;
25
+ exportDownload: ReactNode;
26
+ today: ReactNode;
27
+ thisWeek: ReactNode;
28
+ reset: ReactNode;
29
+ loading: ReactNode;
30
+ clearFilters: ReactNode;
31
+ deselectAll: ReactNode;
32
+ resetAll: ReactNode;
33
+ };
34
+ /** Define styles. */
35
+ classes?: {
36
+ table?: string;
37
+ headerCell?: string;
38
+ footerCell?: string;
39
+ cell?: string | FunctionWithDeps<(item: T, index: number) => string | undefined>;
40
+ evenCell?: string;
41
+ oddCell?: string;
42
+ };
43
+ /** Define components to be used in the table. */
44
+ components: {
45
+ IconButton: ComponentType<{
46
+ children: ReactNode;
47
+ onClick?: (e: React.MouseEvent<Element>) => void;
48
+ className?: string;
49
+ }>;
50
+ Button: ComponentType<{
51
+ children: ReactNode;
52
+ onClick?: (e: React.MouseEvent<Element>) => void;
53
+ startIcon?: ReactNode;
54
+ variant?: 'text' | 'outlined' | 'contained';
55
+ disabled?: boolean;
56
+ className?: string;
57
+ }>;
58
+ Checkbox: ComponentType<{
59
+ checked: boolean;
60
+ onChange: (e: React.ChangeEvent) => void;
61
+ disabled?: boolean;
62
+ className?: string;
63
+ }>;
64
+ Popover: ComponentType<{
65
+ anchorEl: Element | null;
66
+ open: boolean;
67
+ hidden?: boolean;
68
+ onClose: () => void;
69
+ children: ReactNode;
70
+ className?: string;
71
+ align?: 'center' | 'left';
72
+ }>;
73
+ Badge: ComponentType<{
74
+ children: ReactNode;
75
+ badgeContent: ReactNode;
76
+ }>;
77
+ TextField: ComponentType<{
78
+ value: string | null;
79
+ onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
80
+ endIcon?: ReactNode;
81
+ className?: string;
82
+ inputRef?: Ref<HTMLInputElement>;
83
+ }>;
84
+ Spinner: (props: {
85
+ className?: string;
86
+ }) => JSX.Element;
87
+ };
88
+ /** Define icons for the table. */
89
+ icons: {
90
+ [K in 'Settings' | 'Export' | 'Clipboard' | 'ChevronRight' | 'Search' | 'Clear' | 'ArrowDropDown' | 'FilterList' | 'ArrowUpward']: ComponentType<{
91
+ className?: string;
92
+ }>;
93
+ };
94
+ /** Define colors */
95
+ colors: {
96
+ primary: {
97
+ main: string;
98
+ light: string;
99
+ contrastText: string;
100
+ };
101
+ secondary: {
102
+ main: string;
103
+ light: string;
104
+ contrastText: string;
105
+ };
106
+ };
107
+ /** Spacing. */
108
+ spacing: string | number;
109
+ }
110
+ export declare type PartialTableTheme<T = unknown> = {
111
+ [K in keyof TableTheme<T>]?: TableTheme<T>[K] extends Record<string, any> ? Partial<TableTheme<T>[K]> : TableTheme<T>[K];
112
+ };
113
+ export declare type MemoizedTableTheme<T> = Omit<TableTheme, 'classes' | 'text'> & {
114
+ classes: MemoizedFunctions<TableTheme<T>['classes']>;
115
+ text: MemoizedFunctions<TableTheme<T>['text']>;
116
+ };
117
+ export interface TableProps<T> extends PartialTableTheme<T> {
118
+ /** The data to be rendered. One item per row. */
119
+ items?: T[];
120
+ /** Unique id for each item/row. */
121
+ id: FunctionWithDeps<(item: T) => Id> | KeyOfType<T, Id>;
122
+ /** Create a nested structure by assigning parents to items. Child items are hidden until the parent is expanded. */
123
+ parentId?: FunctionWithDeps<(item: T) => Id | undefined> | KeyOfType<T, Id | undefined>;
124
+ /** If true for an item, it means that children will be loaded asynchronously as soon as item is expanded. */
125
+ hasDeferredChildren?: (item: T) => boolean;
126
+ /** Column definitions. */
127
+ columns: Column<T, any>[] | ((col: <V>(value: (item: T) => V, column: Omit<Column<T, V>, 'value'>) => Column<T, V>) => Column<T, any>[]);
128
+ /** Default props for all column. Will take effect if not overriden in column definition. */
129
+ defaultColumnProps?: Omit<Column<T, unknown>, 'id' | 'value'>;
130
+ /** Wrap each cell */
131
+ wrapCell?: FunctionWithDeps<(content: ReactNode, value: unknown, item: T, index: number) => ReactNode>;
132
+ /** Display a cell at the start of each row. Useful for "go to details" button for example. */
133
+ rowAction?: ReactNode | FunctionWithDeps<(item: T, index: number) => ReactNode>;
134
+ /** Default sort order. */
135
+ defaultSort?: Sort[];
136
+ /** If given, controls the sort order. */
137
+ sort?: Sort[];
138
+ /** Called when sort order changes. */
139
+ onSortChange?: (sort: Sort[]) => void;
140
+ /** Handle sorting externally, e.g. server side */
141
+ externalSort?: boolean;
142
+ /** Default selection. */
143
+ defaultSelection?: Set<Id>;
144
+ /** If given, controls the selection. */
145
+ selection?: Set<Id>;
146
+ /** Called when selection changes. */
147
+ onSelectionChange?: (selection: Set<Id>) => void;
148
+ /** Whether to show checkboxes at the start of each row.
149
+ * @default true
150
+ */
151
+ enableSelection?: boolean;
152
+ /** Select and deselect children if a parent is selected or deselected.
153
+ * @default true
154
+ */
155
+ selectSyncChildren?: boolean;
156
+ /** Expand parents whose children fit a newly selected filter
157
+ * @default false
158
+ */
159
+ revealFiltered?: boolean;
160
+ /** Default expanded rows. */
161
+ defaultExpanded?: Set<Id>;
162
+ /** If given, controls expanded rows. */
163
+ expanded?: Set<Id>;
164
+ /** Called when expanded rows change. */
165
+ onExpandedChange?: (expanded: Set<Id>) => void;
166
+ /** If enabled and one row is expanded, other rows will be closed.
167
+ * @default false
168
+ */
169
+ expandOnlyOne?: boolean;
170
+ /** Default hidden columns. */
171
+ defaultHiddenColumns?: Set<Id>;
172
+ /** If given, controls hidden columns. */
173
+ hiddenColumns?: Set<Id>;
174
+ /** Called when hidden columns change. */
175
+ onHiddenColumnsChange?: (hiddenColumns: Set<Id>) => void;
176
+ /** Whether to stretch the table component over the available space. If value is "left" or "right", align accordingly. */
177
+ fullWidth?: boolean | 'left' | 'right';
178
+ /** Whether the table header should be sticky.
179
+ * @default true
180
+ */
181
+ stickyHeader?: boolean | {
182
+ top: number;
183
+ };
184
+ /** Whether the table footer should be sticky.
185
+ * @default true
186
+ */
187
+ stickyFooter?: boolean | {
188
+ bottom: number;
189
+ };
190
+ /** Whether the table cells should only be rendered when in viewport.
191
+ * @default true
192
+ */
193
+ virtual?: boolean | {
194
+ rowHeight?: number;
195
+ initalRowHeight?: number;
196
+ throttleScroll?: number;
197
+ overscan?: number;
198
+ overscanBottom?: number;
199
+ overscanTop?: number;
200
+ };
201
+ /** Enable menu to select which columns are visible.
202
+ * @default true
203
+ */
204
+ enableColumnSelection?: boolean;
205
+ /** Enable exporting to csv.
206
+ * @default false
207
+ */
208
+ enableExport?: boolean | {
209
+ copy?: boolean | CsvExportOptions;
210
+ download?: boolean | CsvExportOptions;
211
+ };
212
+ /** Shows a button to clear all filters while any are active
213
+ * @default false
214
+ */
215
+ enableClearFiltersButton?: boolean;
216
+ /** Allow to drag and drop column separators to resize the column left of it.
217
+ * @default true
218
+ */
219
+ enableColumnResize?: boolean;
220
+ /** Allow to drag and drop column header to reorder columns.
221
+ * @default true
222
+ */
223
+ enableColumnReorder?: boolean;
224
+ /** If enabled, automatically store table state in localStorage, localForage or another compatible storage. */
225
+ persist?: {
226
+ storage: TableStateStorage;
227
+ id: string;
228
+ include?: ('sort' | 'selection' | 'expanded' | 'hiddenColumns' | 'filterValues' | 'columnWidths' | 'columnOrder')[];
229
+ exclude?: ('sort' | 'selection' | 'expanded' | 'hiddenColumns' | 'filterValues' | 'columnWidths' | 'columnOrder')[];
230
+ };
231
+ debug?: (...output: any) => void;
232
+ debugRender?: (...output: any) => void;
233
+ onReset?: () => void;
234
+ }
235
+ export declare type InternalTableProps<T> = MemoizedFunctions<Omit<TableProps<T>, 'id' | 'parentId' | 'columns' | 'defaultColumnProps' | 'enableExport'> & {
236
+ id: (item: T) => Id;
237
+ parentId?: (item: T) => Id | undefined;
238
+ columns: InternalColumn<T, unknown>[];
239
+ enableExport: {
240
+ copy?: CsvExportOptions;
241
+ download?: CsvExportOptions;
242
+ };
243
+ }>;
244
+ export declare type TableItem<T = unknown> = {
245
+ id: Id;
246
+ parentId?: Id;
247
+ children: TableItem<T>[];
248
+ value: T;
249
+ };
250
+ export declare type Column<T, V> = {
251
+ /** Column id. If not provided, the index in the column array will be used.
252
+ * An explicit id is better however for controlling column related states, persitance etc.
253
+ */
254
+ id?: string;
255
+ /** Render table header for this column. */
256
+ header?: ReactNode | FunctionWithDeps<(values: V[], items: T[]) => ReactNode>;
257
+ /** Render table header for this column. */
258
+ footer?: ReactNode | FunctionWithDeps<(values: V[], items: T[]) => ReactNode>;
259
+ /** Extract value for this column */
260
+ value: FunctionWithDeps<(item: T) => V>;
261
+ /** Render table cell. If not provided, a string representation of the value will be rendered. */
262
+ renderCell?: FunctionWithDeps<(value: V, item: T) => ReactNode>;
263
+ /** Serialize column value for exports. If not provided, a string representation of the value will be used. */
264
+ exportCell?: (value: V, item: T) => string | number;
265
+ /** Customize sort criteria. By default it will be the value itself in case it's a number or Date, or a string representation of the value otherwise. */
266
+ sortBy?: FunctionWithDeps<(value: V, item: T) => unknown>[];
267
+ /** Set filter component that will be displayed in the column header */
268
+ filter?: ReactNode;
269
+ /** Prevent hiding the column. */
270
+ cannotHide?: boolean;
271
+ /** Specify a css width.
272
+ * @default 'max-content'
273
+ */
274
+ width?: string;
275
+ /** Provide css class names to override columns styles. */
276
+ classes?: TableTheme<T>['classes'];
277
+ };
278
+ export declare type InternalColumn<T, V> = MemoizedFunctions<Required<Omit<Column<T, V>, 'id' | 'sortBy'>, 'header' | 'renderCell' | 'exportCell' | 'sortBy'> & {
279
+ id: Id;
280
+ sortBy: ((value: V, item: T) => unknown)[];
281
+ }>;
282
+ declare type Required<T, S> = T & {
283
+ [P in keyof T as P extends S ? P : never]-?: T[P];
284
+ };
285
+ export declare type InternalTableState<T> = {
286
+ props: InternalTableProps<T>;
287
+ key: any;
288
+ sort: Sort[];
289
+ selection: Set<Id>;
290
+ expanded: Set<Id>;
291
+ rowHeights: Map<Id, number>;
292
+ filters: Map<Id, MemoizedFunctions<FilterImplementation<T, any, any, any>>>;
293
+ filterValues: Map<Id, any>;
294
+ hiddenColumns: Set<Id>;
295
+ columnWidths: Map<Id, string>;
296
+ columnOrder: Id[];
297
+ columnStyleOverride: Map<Id, CSSInterpolation>;
298
+ activeColumns: InternalColumn<T, unknown>[];
299
+ items: TableItem<T>[];
300
+ itemsById: Map<Id, TableItem<T>>;
301
+ activeItems: TableItem<T>[];
302
+ activeItemsById: Map<Id, TableItem<T>>;
303
+ lastSelectedId?: Id;
304
+ };
305
+ export declare type CommonFilterProps<T, V, F, S extends SerializableValue> = {
306
+ /** Filter by? By default the column value will be used. If filterBy returns an array, an items will be active if at least one entry matches the active filter. */
307
+ filterBy?: FunctionWithDeps<(value: V, item: T) => F | F[]>;
308
+ /** Preselected filter value. */
309
+ defaultValue?: S;
310
+ /** Controlled filter value. */
311
+ value?: S;
312
+ /** Notifies on filter change. */
313
+ onChange?: (value?: S) => void;
314
+ /** Table should not filter using this filter. It will be done externally, e.g. server side. */
315
+ external?: boolean;
316
+ /** Whether to persist filter value (given that filter persitance is enabled for the table).
317
+ * @default true
318
+ */
319
+ persist?: boolean;
320
+ };
321
+ export declare type FilterImplementation<T, V, F, S extends SerializableValue> = CommonFilterProps<T, V, F, S> & {
322
+ /** Unique filter id. Used to persist filter values. */
323
+ id: string;
324
+ /** Whether the filter is active currently. */
325
+ isActive: (filterValue: S) => boolean;
326
+ /** When the filter is active, this function is used to filter the items to be displayed. */
327
+ test: (filterValue: S, value: F) => boolean;
328
+ };
329
+ export declare type SerializableValue = string | number | boolean | null | Date | SerializableArray | SerializableObject | SerializableSet | SerializableMap;
330
+ export declare type SerializableObject = {
331
+ [key: string]: SerializableValue;
332
+ };
333
+ export declare type SerializableArray = SerializableValue[];
334
+ export declare type SerializableSet = Set<SerializableValue>;
335
+ export declare type SerializableMap = Map<SerializableValue, SerializableValue>;
336
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "schummar-table",
3
3
  "repository": "schummar/schummar-table",
4
- "version": "0.12.0",
4
+ "version": "0.12.3",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "sideEffects": false,
@@ -108,6 +108,7 @@
108
108
  "docs"
109
109
  ],
110
110
  "volta": {
111
- "node": "16.17.0"
111
+ "node": "16.17.0",
112
+ "npm": "8.19.1"
112
113
  }
113
114
  }