schummar-table 0.6.0 → 0.7.0

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.
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
- import { Id, InternalColumn } from '../types';
3
- export declare function calcClassNames<T>(classes: InternalColumn<any, any>['classes'] | undefined, item: T, index: number): (string | false | undefined)[];
2
+ import { Id, MemoizedTableTheme } from '../types';
3
+ export declare function calcClassNames<T>(classes: MemoizedTableTheme<any>['classes'] | undefined, item: T, index: number): (string | false | undefined)[];
4
4
  export declare const Row: React.MemoExoticComponent<(<T>({ itemId, rowIndex }: {
5
5
  itemId: Id;
6
6
  rowIndex: number;
@@ -3,4 +3,5 @@ export declare function useFilter<T, V, F, S extends SerializableValue>(impl: Fi
3
3
  value: S | undefined;
4
4
  onChange: (value?: S | undefined) => void;
5
5
  close: () => void;
6
+ filterBy: (value: V, item: T) => F | F[];
6
7
  };
@@ -0,0 +1,2 @@
1
+ import { FunctionWithDeps } from '../types';
2
+ export declare function useMemoMap(): <Fn extends (...args: any[]) => any>(key: string, fn: FunctionWithDeps<Fn>) => Fn;
@@ -0,0 +1,6 @@
1
+ import { ReactNode } from 'react';
2
+ import { FunctionWithDeps } from '../types';
3
+ export declare function TableMemoContextProvider({ children }: {
4
+ children: ReactNode;
5
+ }): import("@emotion/react/jsx-runtime").JSX.Element;
6
+ export declare function useTableMemo(): <Fn extends (...args: any[]) => any>(key: string, fn: FunctionWithDeps<Fn>) => Fn;
@@ -1,2 +1,2 @@
1
- import { TableTheme } from '../types';
2
- export declare function useTheme<T>(): TableTheme<T>;
1
+ import { MemoizedTableTheme } from '../types';
2
+ export declare function useTheme<T, S>(selector: (theme: MemoizedTableTheme<T>) => S): S;
@@ -1,4 +1,5 @@
1
1
  export declare function throttle<Args extends any[]>(fn: (...args: Args) => void, ms: number): {
2
2
  (...args: Args): void;
3
3
  flush(): void;
4
+ cancel(): void;
4
5
  };
@@ -1,5 +1,5 @@
1
1
  import { CSSInterpolation } from '@emotion/serialize';
2
- import React, { ComponentType, ReactNode, Ref } from 'react';
2
+ import React, { ComponentType, DependencyList, ReactNode, Ref } from 'react';
3
3
  import { TableStateStorage } from './internalState/tableStateStorage';
4
4
  import { CsvExportOptions } from './misc/csvExport';
5
5
  export declare type Sort = {
@@ -11,6 +11,10 @@ export declare type Id = string | number;
11
11
  export declare type KeyOfType<T, S> = {
12
12
  [K in keyof T]: T[K] extends S ? K : never;
13
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
+ };
14
18
  export interface TableTheme<T = unknown> {
15
19
  /** Define display texts. */
16
20
  text: {
@@ -23,13 +27,13 @@ export interface TableTheme<T = unknown> {
23
27
  thisWeek: ReactNode;
24
28
  reset: ReactNode;
25
29
  loading: ReactNode;
26
- selected: (count: number) => ReactNode;
30
+ selected: FunctionWithDeps<(count: number) => ReactNode>;
27
31
  };
28
32
  /** Define styles. */
29
33
  classes?: {
30
34
  table?: string;
31
35
  headerCell?: string;
32
- cell?: string | ((item: T, index: number) => string | undefined);
36
+ cell?: string | FunctionWithDeps<(item: T, index: number) => string | undefined>;
33
37
  evenCell?: string;
34
38
  oddCell?: string;
35
39
  };
@@ -102,19 +106,27 @@ export interface TableTheme<T = unknown> {
102
106
  export declare type PartialTableTheme<T = unknown> = {
103
107
  [K in keyof TableTheme<T>]?: TableTheme<T>[K] extends Record<string, any> ? Partial<TableTheme<T>[K]> : TableTheme<T>[K];
104
108
  };
109
+ export declare type MemoizedTableTheme<T> = Omit<TableTheme, 'classes' | 'text'> & {
110
+ classes: MemoizedFunctions<TableTheme<T>['classes']>;
111
+ text: MemoizedFunctions<TableTheme<T>['text']>;
112
+ };
105
113
  export interface TableProps<T> extends PartialTableTheme<T> {
106
114
  /** The data to be rendered. One item per row. */
107
115
  items?: T[];
108
116
  /** Unique id for each item/row. */
109
- id: ((item: T) => Id) | KeyOfType<T, Id>;
117
+ id: FunctionWithDeps<(item: T) => Id> | KeyOfType<T, Id>;
110
118
  /** Create a nested structure by assigning parents to items. Child items are hidden until the parent is expanded. */
111
- parentId?: ((item: T) => Id | undefined) | KeyOfType<T, Id | undefined>;
119
+ parentId?: FunctionWithDeps<(item: T) => Id | undefined> | KeyOfType<T, Id | undefined>;
112
120
  /** If true for an item, it means that children will be loaded asynchronously as soon as item is expanded. */
113
121
  hasDeferredChildren?: (item: T) => boolean;
114
122
  /** Column definitions. */
115
123
  columns: Column<T, any>[] | ((col: <V>(value: (item: T) => V, column: Omit<Column<T, V>, 'value'>) => Column<T, V>) => Column<T, any>[]);
124
+ /** Default props for all column. Will take effect if not overriden in column definition. */
125
+ defaultColumnProps?: Omit<Column<T, unknown>, 'id' | 'value'>;
126
+ /** Wrap each cell */
127
+ wrapCell?: FunctionWithDeps<(content: ReactNode, value: unknown, item: T, index: number) => ReactNode>;
116
128
  /** Display a cell at the start of each row. Useful for "go to details" button for example. */
117
- rowAction?: ReactNode | ((item: T, index: number) => ReactNode);
129
+ rowAction?: ReactNode | FunctionWithDeps<(item: T, index: number) => ReactNode>;
118
130
  /** Default sort order. */
119
131
  defaultSort?: Sort[];
120
132
  /** If given, controls the sort order. */
@@ -155,8 +167,6 @@ export interface TableProps<T> extends PartialTableTheme<T> {
155
167
  hiddenColumns?: Set<Id>;
156
168
  /** Called when hidden columns change. */
157
169
  onHiddenColumnsChange?: (hiddenColumns: Set<Id>) => void;
158
- /** Default width for columns. */
159
- defaultWidth?: string;
160
170
  /** Whether to stretch the table component over the available space. If value is "left" or "right", align accordingly. */
161
171
  fullWidth?: boolean | 'left' | 'right';
162
172
  /** Whether the table header should be sticky.
@@ -205,7 +215,7 @@ export interface TableProps<T> extends PartialTableTheme<T> {
205
215
  debug?: (...output: any) => void;
206
216
  debugRender?: (...output: any) => void;
207
217
  }
208
- export declare type InternalTableProps<T> = Omit<TableProps<T>, 'id' | 'parentId' | 'columns' | 'enableExport'> & {
218
+ export declare type InternalTableProps<T> = MemoizedFunctions<Omit<TableProps<T>, 'id' | 'parentId' | 'columns' | 'defaultColumnProps' | 'enableExport'> & {
209
219
  id: (item: T) => Id;
210
220
  parentId?: (item: T) => Id | undefined;
211
221
  columns: InternalColumn<T, unknown>[];
@@ -213,7 +223,7 @@ export declare type InternalTableProps<T> = Omit<TableProps<T>, 'id' | 'parentId
213
223
  copy?: CsvExportOptions;
214
224
  download?: CsvExportOptions;
215
225
  };
216
- };
226
+ }>;
217
227
  export declare type TableItem<T = unknown> = T & {
218
228
  id: Id;
219
229
  parentId?: Id;
@@ -227,13 +237,13 @@ export declare type Column<T, V> = {
227
237
  /** Render table header for this column. */
228
238
  header?: ReactNode;
229
239
  /** Extract value for this column */
230
- value: (item: T) => V;
240
+ value: FunctionWithDeps<(item: T) => V>;
231
241
  /** Render table cell. If not provided, a string representation of the value will be rendered. */
232
- renderCell?: (value: V, item: T) => ReactNode;
242
+ renderCell?: FunctionWithDeps<(value: V, item: T) => ReactNode>;
233
243
  /** Serialize column value for exports. If not provided, a string representation of the value will be used. */
234
244
  exportCell?: (value: V, item: T) => string | number;
235
245
  /** 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. */
236
- sortBy?: ((value: V, item: T) => unknown) | ((value: V) => unknown)[];
246
+ sortBy?: FunctionWithDeps<(value: V, item: T) => unknown>[];
237
247
  /** Set filter component that will be displayed in the column header */
238
248
  filter?: ReactNode;
239
249
  /** Prevent hiding the column. */
@@ -244,12 +254,11 @@ export declare type Column<T, V> = {
244
254
  width?: string;
245
255
  /** Provide css class names to override columns styles. */
246
256
  classes?: TableTheme<T>['classes'];
247
- /** If the column definition changes, supply parameters that it depends on. If not set, the column will not update */
248
- dependencies?: any[];
249
257
  };
250
- export declare type InternalColumn<T, V> = Required<Omit<Column<T, V>, 'id'>, 'header' | 'renderCell' | 'exportCell' | 'sortBy'> & {
258
+ export declare type InternalColumn<T, V> = MemoizedFunctions<Required<Omit<Column<T, V>, 'id' | 'sortBy'>, 'header' | 'renderCell' | 'exportCell' | 'sortBy'> & {
251
259
  id: Id;
252
- };
260
+ sortBy: ((value: V, item: T) => unknown)[];
261
+ }>;
253
262
  declare type Required<T, S> = T & {
254
263
  [P in keyof T as P extends S ? P : never]-?: T[P];
255
264
  };
@@ -259,7 +268,7 @@ export declare type InternalTableState<T> = {
259
268
  selection: Set<Id>;
260
269
  expanded: Set<Id>;
261
270
  rowHeights: Map<Id, number>;
262
- filters: Map<Id, FilterImplementation<T, any, any, any>>;
271
+ filters: Map<Id, MemoizedFunctions<FilterImplementation<T, any, any, any>>>;
263
272
  filterValues: Map<Id, any>;
264
273
  hiddenColumns: Set<Id>;
265
274
  columnWidths: Map<Id, string>;
@@ -274,7 +283,7 @@ export declare type InternalTableState<T> = {
274
283
  };
275
284
  export declare type CommonFilterProps<T, V, F, S extends SerializableValue> = {
276
285
  /** 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. */
277
- filterBy?: (value: V, item: T) => F | F[];
286
+ filterBy?: FunctionWithDeps<(value: V, item: T) => F | F[]>;
278
287
  /** Preselected filter value. */
279
288
  defaultValue?: S;
280
289
  /** Controlled filter value. */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "schummar-table",
3
3
  "repository": "schummar/schummar-table",
4
- "version": "0.6.0",
4
+ "version": "0.7.0",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "sideEffects": false,