@ostack.tech/ui 0.14.2 → 0.15.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,11 +1,14 @@
1
1
  import { ComponentPropsWithoutRef, Key, ReactNode, Ref } from 'react';
2
2
  import { SortDirection } from '../../utils/sorting.ts';
3
+ import { CheckboxChecked } from '../Checkbox';
3
4
  import { DataTableApi } from './DataTableApi.ts';
4
5
  import { DataTableCellProps } from './DataTableCell.tsx';
5
6
  import { DataTableColumns } from './DataTableColumn.tsx';
6
7
  import { DataTableRowProps } from './DataTableRow.tsx';
7
8
  /** Display mode of the data table. */
8
9
  export type DataTableDisplayMode = "paged" | "scrolled";
10
+ /** Selection mode of the data table. */
11
+ export type DataTableSelectionMode = "additive" | "subtractive";
9
12
  /**
10
13
  * Options passed to the `getRows` function to get the rows meant to be
11
14
  * rendered.
@@ -57,6 +60,25 @@ export interface DataTableProps<T = any> extends ComponentPropsWithoutRef<"div">
57
60
  * Either this property or `getRows` should be provided.
58
61
  */
59
62
  rows?: T[];
63
+ /**
64
+ * Function called whenever the rows visible on screen change. This function
65
+ * is only called when a `rows` property is provided.
66
+ *
67
+ * This function is called once during startup of the data table.
68
+ *
69
+ * @param visibleRows Array of currently visible rows.
70
+ */
71
+ onVisibleRowsChange?: (visibleRows: T[]) => void;
72
+ /**
73
+ * Function called whenever the processed rows (the rows after being filtered
74
+ * and/or sorted) change. This function is only called when a `rows` property
75
+ * is provided.
76
+ *
77
+ * This function is called once during startup of the data table.
78
+ *
79
+ * @param processedRows Array of rows after being filtered and/or sorted.
80
+ */
81
+ onProcessedRowsChange?: (processedRows: T[]) => void;
60
82
  /**
61
83
  * Function used to dynamically get the rows of the data table.
62
84
  *
@@ -168,10 +190,25 @@ export interface DataTableProps<T = any> extends ComponentPropsWithoutRef<"div">
168
190
  * @default false
169
191
  */
170
192
  showSelectionColumn?: boolean;
171
- /** Default keys of the selected rows. */
193
+ /**
194
+ * The strategy used to interpret the `selectedRows` property:
195
+ *
196
+ * - "additive": No rows are selected by default, `selectedRows` lists the keys
197
+ * of the rows to include.
198
+ * - "subtractive": All rows are selected by default, `selectedRows` lists the
199
+ * keys of the rows to exclude.
200
+ *
201
+ * @default "additive"
202
+ */
203
+ selectionMode?: DataTableSelectionMode;
204
+ /**
205
+ * Default keys of the selected rows. See also the `selectionMode` property
206
+ * for how this array will be interpreted.
207
+ */
172
208
  defaultSelectedRows?: Key[];
173
209
  /**
174
- * Controlled keys of the selected rows.
210
+ * Controlled keys of the selected rows. See also the `selectionMode` property
211
+ * for how this array will be interpreted.
175
212
  *
176
213
  * Use together with `onSelectedRowsChange` to change the selected rows.
177
214
  */
@@ -179,9 +216,18 @@ export interface DataTableProps<T = any> extends ComponentPropsWithoutRef<"div">
179
216
  /**
180
217
  * Function called whenever the selected rows of the data table change.
181
218
  *
182
- * @param selectedRows Array containing the keys of the newly selected rows.
219
+ * @param selectedRows Array containing the keys of the selected rows.
220
+ * @param selectionMode Strategy used to interpret the selected rows.
221
+ */
222
+ onSelectedRowsChange?: (selectedRows: Key[], selectionMode: DataTableSelectionMode) => void;
223
+ /** Controlled checked state of the "select all rows" checkbox. */
224
+ selectAllChecked?: CheckboxChecked;
225
+ /**
226
+ * Function called when a user toggles the "select all rows" checkbox.
227
+ *
228
+ * @param checked Whether the checkbox is being checked or unchecked.
183
229
  */
184
- onSelectedRowsChange?: (selectedRows: Key[]) => void;
230
+ onSelectAll?: (checked: boolean) => void;
185
231
  /**
186
232
  * Keys of the rows whose selections should be disabled (rows with the
187
233
  * identifiers provided will have their selection checkboxes disabled).
@@ -1,26 +1,16 @@
1
- import { Key, ReactNode, Ref } from 'react';
1
+ import { Key, ReactNode } from 'react';
2
2
  import { SortDirection } from '../../utils/sorting.ts';
3
- import { DataTableDisplayMode, DataTableGetRowsOptions, DataTableGetRowsResult } from './DataTable.tsx';
4
- import { DataTableApi } from './DataTableApi.ts';
5
- import { DataTableCellProps } from './DataTableCell.tsx';
3
+ import { DataTableDisplayMode, DataTableGetRowsOptions, DataTableGetRowsResult, DataTableProps, DataTableSelectionMode } from './DataTable.tsx';
6
4
  import { DataTableColumns } from './DataTableColumn.tsx';
7
- import { DataTableRowProps } from './DataTableRow.tsx';
8
5
  /** Value of the data table context. */
9
- export interface DataTableContextValue<T = any> {
6
+ export interface DataTableContextValue<T = any> extends Pick<DataTableProps<T>, "required" | "renderRow" | "renderCell" | "selectAllChecked" | "onSelectAll" | "selectAllRowsLabel" | "selectRowLabel" | "showAllRowsWhilePrinting" | "apiRef"> {
10
7
  displayMode: DataTableDisplayMode;
11
8
  rowKey: string | number | symbol | ((row: T) => Key);
12
- required?: boolean;
13
9
  showSelectionColumn: boolean;
14
- renderRow?: (props: DataTableRowProps) => ReactNode;
15
- renderCell?: (props: DataTableCellProps) => ReactNode;
16
10
  dynamicRowHeight: boolean;
17
11
  estimatedRowHeight: number | ((index: number) => number);
18
12
  overscan: number;
19
- selectAllRowsLabel?: ReactNode;
20
- selectRowLabel?: ReactNode;
21
13
  generatedId: string;
22
- showAllRowsWhilePrinting?: boolean;
23
- apiRef?: Ref<DataTableApi<T> | undefined>;
24
14
  store: DataTableStore<T>;
25
15
  }
26
16
  /** State of the data table. */
@@ -31,7 +21,7 @@ export interface DataTableState<T = any> {
31
21
  headCount: number;
32
22
  _rows?: T[];
33
23
  _filteredRows: () => T[] | undefined;
34
- transformedRows: () => T[] | undefined;
24
+ processedRows: () => T[] | undefined;
35
25
  getRows?: (options: DataTableGetRowsOptions) => DataTableGetRowsResult<T> | PromiseLike<DataTableGetRowsResult<T>>;
36
26
  loading: boolean;
37
27
  loadingCount: number;
@@ -42,7 +32,7 @@ export interface DataTableState<T = any> {
42
32
  };
43
33
  abortController?: AbortController;
44
34
  cachedRows?: Record<number, T | undefined>;
45
- rowsWindow: (offset: number, limit: number) => (T | undefined)[];
35
+ visibleRows: () => (T | undefined)[];
46
36
  _totalCount?: number;
47
37
  totalCount: () => number | undefined;
48
38
  bodyCount: () => number;
@@ -58,6 +48,7 @@ export interface DataTableState<T = any> {
58
48
  filter: string;
59
49
  sortBy: string | null;
60
50
  sortDirection: SortDirection;
51
+ selectionMode: DataTableSelectionMode;
61
52
  selectedRows: [Set<Key>];
62
53
  disabledSelections?: Set<Key>;
63
54
  scrolledRangeStart: number;
@@ -68,6 +59,7 @@ export interface DataTableState<T = any> {
68
59
  }
69
60
  /** Actions of the data table. */
70
61
  export interface DataTableActions<T> {
62
+ getRowsWindow: (offset: number, limit: number) => (T | undefined)[];
71
63
  setGetRows: (getRows: DataTableState["getRows"]) => void;
72
64
  setOffset: (offset: number) => void;
73
65
  setLimit: (limit: number) => void;
@@ -84,52 +76,39 @@ export type DataTableStore<T> = ReturnType<typeof useCreateDataTableContext<T>>[
84
76
  /** Data table context. */
85
77
  export declare const DataTableContext: import('react').Context<DataTableContextValue<any> | null>;
86
78
  /** Options used to create the data table context value. */
87
- export interface UseCreateDataTableContextOptions<T> extends Omit<DataTableContextValue, "generatedId" | "store"> {
88
- columns: DataTableColumns<T>;
79
+ export interface UseCreateDataTableContextOptions<T> extends Pick<DataTableProps<T>, "columns" | "rows" | "onVisibleRowsChange" | "onProcessedRowsChange" | "getRows" | "onOffsetChange" | "onLimitChange" | "filter" | "onFilterChange" | "sortBy" | "onSort" | "defaultSelectedRows" | "selectedRows" | "onSelectedRowsChange" | "disabledSelections" | "selectAllRowsLabel" | "selectRowLabel">, Omit<DataTableContextValue<T>, "generatedId" | "store"> {
89
80
  leafColumns: DataTableColumns<T>;
90
81
  headCount: number;
91
- rows?: T[];
92
- getRows?: (options: DataTableGetRowsOptions) => DataTableGetRowsResult<T> | PromiseLike<DataTableGetRowsResult<T>>;
93
82
  loading: boolean;
94
83
  loadingCount: number;
95
84
  defaultOffset: number;
96
85
  offset?: number;
97
- onOffsetChange?: (offset: number) => void;
98
86
  defaultLimit: number;
99
87
  limit?: number;
100
- onLimitChange?: (limit: number) => void;
101
88
  defaultFilter: string;
102
- filter?: string;
103
- onFilterChange?: (filter: string) => void;
104
89
  defaultSortBy: string | null;
105
- sortBy?: string | null;
106
90
  defaultSortDirection: SortDirection | undefined;
107
91
  sortDirection?: SortDirection;
108
- onSort?: (sortBy: string | null, sortDirection: SortDirection) => void;
109
- showSelectionColumn: boolean;
110
- defaultSelectedRows?: Key[];
111
- selectedRows?: Key[];
112
- onSelectedRowsChange?: (selectedRows: Key[]) => void;
113
- disabledSelections?: Key[];
114
- selectAllRowsLabel?: ReactNode;
115
- selectRowLabel?: ReactNode;
92
+ selectionMode: DataTableSelectionMode;
116
93
  }
117
94
  /** Hook which creates the data table context value. */
118
- export declare function useCreateDataTableContext<T>({ displayMode, columns, rows, getRows, rowKey, loadingCount, required, loading, renderRow, renderCell, dynamicRowHeight, estimatedRowHeight, overscan, showAllRowsWhilePrinting, apiRef, leafColumns, headCount, defaultOffset, offset, onOffsetChange, defaultLimit, limit, onLimitChange, defaultFilter, filter, onFilterChange, defaultSortBy, sortBy, defaultSortDirection, sortDirection, onSort, showSelectionColumn, defaultSelectedRows, selectedRows, onSelectedRowsChange, disabledSelections, selectAllRowsLabel, selectRowLabel, }: UseCreateDataTableContextOptions<T>): {
95
+ export declare function useCreateDataTableContext<T>({ displayMode, columns, rows, onVisibleRowsChange, onProcessedRowsChange, getRows, rowKey, loadingCount, required, loading, renderRow, renderCell, dynamicRowHeight, estimatedRowHeight, overscan, showAllRowsWhilePrinting, apiRef, leafColumns, headCount, defaultOffset, offset, onOffsetChange, defaultLimit, limit, onLimitChange, defaultFilter, filter, onFilterChange, defaultSortBy, sortBy, defaultSortDirection, sortDirection, onSort, showSelectionColumn, selectionMode, defaultSelectedRows, selectedRows, onSelectedRowsChange, selectAllChecked, onSelectAll, disabledSelections, selectAllRowsLabel, selectRowLabel, }: UseCreateDataTableContextOptions<T>): {
119
96
  displayMode: DataTableDisplayMode;
120
- rowKey: string | number | symbol | ((row: any) => Key);
97
+ rowKey: string | number | symbol | ((row: T) => Key);
121
98
  required: boolean | undefined;
122
99
  showSelectionColumn: boolean;
123
- renderRow: ((props: DataTableRowProps) => ReactNode) | undefined;
124
- renderCell: ((props: DataTableCellProps) => ReactNode) | undefined;
100
+ renderRow: ((props: import('./DataTableRow.tsx').DataTableRowProps) => ReactNode) | undefined;
101
+ renderCell: ((props: import('./DataTableCell.tsx').DataTableCellProps) => ReactNode) | undefined;
125
102
  dynamicRowHeight: boolean;
126
103
  estimatedRowHeight: number | ((index: number) => number);
127
104
  overscan: number;
105
+ selectAllChecked: import('@radix-ui/react-checkbox').CheckedState | undefined;
106
+ onSelectAll: ((checked: boolean) => void) | undefined;
128
107
  selectAllRowsLabel: ReactNode;
129
108
  selectRowLabel: ReactNode;
130
109
  generatedId: string;
131
110
  showAllRowsWhilePrinting: boolean | undefined;
132
- apiRef: Ref<DataTableApi<any> | undefined> | undefined;
111
+ apiRef: import('react').Ref<import('./DataTableApi.ts').DataTableApi<T> | undefined> | undefined;
133
112
  store: Omit<import('zustand').StoreApi<DataTableState<T>>, "subscribe"> & {
134
113
  subscribe: {
135
114
  (listener: (selectedState: DataTableState<T>, previousSelectedState: DataTableState<T>) => void): () => void;
@@ -1,5 +1,6 @@
1
1
  import { ComponentPropsWithoutRef, Key, MouseEvent, ReactNode } from 'react';
2
2
  import { Button } from '../Button';
3
+ import { DataTableSelectionMode } from './DataTable.tsx';
3
4
  /** Properties of the data table selection trigger component. */
4
5
  export interface DataTableSelectionTriggerProps extends Omit<ComponentPropsWithoutRef<typeof Button<"button">>, "onClick"> {
5
6
  /**
@@ -19,9 +20,10 @@ export interface DataTableSelectionTriggerProps extends Omit<ComponentPropsWitho
19
20
  * Action to run when triggering the action.
20
21
  *
21
22
  * @param selectedRows Rows currently selected.
23
+ * @param selectionMode Selection mode currently active.
22
24
  * @param event Original click event.
23
25
  */
24
- onClick: (selectedRows: Key[], event: MouseEvent) => void;
26
+ onClick: (selectedRows: Key[], selectionMode: DataTableSelectionMode, event: MouseEvent) => void;
25
27
  children: ReactNode;
26
28
  }
27
29
  /** Trigger used to run an action over selected rows. */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ostack.tech/ui",
3
3
  "description": "ostack/UI component library.",
4
- "version": "0.14.2",
4
+ "version": "0.15.0",
5
5
  "homepage": "https://ui.ostack.tech/",
6
6
  "author": {
7
7
  "name": "Opensoft",