@react-stately/table 3.0.0-beta.0 → 3.0.0-nightly-641446f65-240905
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.
- package/dist/Cell.main.js +38 -0
- package/dist/Cell.main.js.map +1 -0
- package/dist/Cell.mjs +33 -0
- package/dist/Cell.module.js +33 -0
- package/dist/Cell.module.js.map +1 -0
- package/dist/Column.main.js +75 -0
- package/dist/Column.main.js.map +1 -0
- package/dist/Column.mjs +66 -0
- package/dist/Column.module.js +66 -0
- package/dist/Column.module.js.map +1 -0
- package/dist/Row.main.js +97 -0
- package/dist/Row.main.js.map +1 -0
- package/dist/Row.mjs +88 -0
- package/dist/Row.module.js +88 -0
- package/dist/Row.module.js.map +1 -0
- package/dist/TableBody.main.js +61 -0
- package/dist/TableBody.main.js.map +1 -0
- package/dist/TableBody.mjs +52 -0
- package/dist/TableBody.module.js +52 -0
- package/dist/TableBody.module.js.map +1 -0
- package/dist/TableCollection.main.js +288 -0
- package/dist/TableCollection.main.js.map +1 -0
- package/dist/TableCollection.mjs +282 -0
- package/dist/TableCollection.module.js +282 -0
- package/dist/TableCollection.module.js.map +1 -0
- package/dist/TableColumnLayout.main.js +113 -0
- package/dist/TableColumnLayout.main.js.map +1 -0
- package/dist/TableColumnLayout.mjs +108 -0
- package/dist/TableColumnLayout.module.js +108 -0
- package/dist/TableColumnLayout.module.js.map +1 -0
- package/dist/TableHeader.main.js +56 -0
- package/dist/TableHeader.main.js.map +1 -0
- package/dist/TableHeader.mjs +47 -0
- package/dist/TableHeader.module.js +47 -0
- package/dist/TableHeader.module.js.map +1 -0
- package/dist/TableUtils.main.js +182 -0
- package/dist/TableUtils.main.js.map +1 -0
- package/dist/TableUtils.mjs +175 -0
- package/dist/TableUtils.module.js +175 -0
- package/dist/TableUtils.module.js.map +1 -0
- package/dist/import.mjs +37 -0
- package/dist/main.js +39 -593
- package/dist/main.js.map +1 -1
- package/dist/module.js +22 -564
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +163 -9
- package/dist/types.d.ts.map +1 -1
- package/dist/useTableColumnResizeState.main.js +109 -0
- package/dist/useTableColumnResizeState.main.js.map +1 -0
- package/dist/useTableColumnResizeState.mjs +104 -0
- package/dist/useTableColumnResizeState.module.js +104 -0
- package/dist/useTableColumnResizeState.module.js.map +1 -0
- package/dist/useTableState.main.js +71 -0
- package/dist/useTableState.main.js.map +1 -0
- package/dist/useTableState.mjs +66 -0
- package/dist/useTableState.module.js +66 -0
- package/dist/useTableState.module.js.map +1 -0
- package/dist/useTreeGridState.main.js +207 -0
- package/dist/useTreeGridState.main.js.map +1 -0
- package/dist/useTreeGridState.mjs +202 -0
- package/dist/useTreeGridState.module.js +202 -0
- package/dist/useTreeGridState.module.js.map +1 -0
- package/package.json +18 -10
- package/src/Cell.ts +4 -1
- package/src/Column.ts +12 -2
- package/src/Row.ts +50 -10
- package/src/TableBody.ts +6 -2
- package/src/TableCollection.ts +99 -15
- package/src/TableColumnLayout.ts +127 -0
- package/src/TableHeader.ts +11 -2
- package/src/TableUtils.ts +253 -0
- package/src/index.ts +15 -6
- package/src/useTableColumnResizeState.ts +147 -0
- package/src/useTableState.ts +43 -16
- package/src/useTreeGridState.ts +277 -0
- package/LICENSE +0 -201
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2022 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {ColumnSize} from '@react-types/table';
|
|
14
|
+
import {GridNode} from '@react-types/grid';
|
|
15
|
+
import {Key} from '@react-types/shared';
|
|
16
|
+
import {TableColumnLayout} from './TableColumnLayout';
|
|
17
|
+
import {TableState} from './useTableState';
|
|
18
|
+
import {useCallback, useMemo, useState} from 'react';
|
|
19
|
+
|
|
20
|
+
export interface TableColumnResizeStateProps<T> {
|
|
21
|
+
/**
|
|
22
|
+
* Current width of the table or table viewport that the columns
|
|
23
|
+
* should be calculated against.
|
|
24
|
+
*/
|
|
25
|
+
tableWidth: number,
|
|
26
|
+
/** A function that is called to find the default width for a given column. */
|
|
27
|
+
getDefaultWidth?: (node: GridNode<T>) => ColumnSize | null | undefined,
|
|
28
|
+
/** A function that is called to find the default minWidth for a given column. */
|
|
29
|
+
getDefaultMinWidth?: (node: GridNode<T>) => ColumnSize | null | undefined
|
|
30
|
+
}
|
|
31
|
+
export interface TableColumnResizeState<T> {
|
|
32
|
+
/**
|
|
33
|
+
* Called to update the state that a resize event has occurred.
|
|
34
|
+
* Returns the new widths for all columns based on the resized column.
|
|
35
|
+
*/
|
|
36
|
+
updateResizedColumns: (key: Key, width: number) => Map<Key, ColumnSize>,
|
|
37
|
+
/** Callback for when onColumnResize has started. */
|
|
38
|
+
startResize: (key: Key) => void,
|
|
39
|
+
/** Callback for when onColumnResize has ended. */
|
|
40
|
+
endResize: () => void,
|
|
41
|
+
/** Gets the current width for the specified column. */
|
|
42
|
+
getColumnWidth: (key: Key) => number,
|
|
43
|
+
/** Gets the current minWidth for the specified column. */
|
|
44
|
+
getColumnMinWidth: (key: Key) => number,
|
|
45
|
+
/** Gets the current maxWidth for the specified column. */
|
|
46
|
+
getColumnMaxWidth: (key: Key) => number,
|
|
47
|
+
/** Key of the currently resizing column. */
|
|
48
|
+
resizingColumn: Key | null,
|
|
49
|
+
/** A reference to the table state. */
|
|
50
|
+
tableState: TableState<T>,
|
|
51
|
+
/** A map of the current column widths. */
|
|
52
|
+
columnWidths: Map<Key, number>
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Provides column width state management for a table component with column resizing support. Handles building
|
|
57
|
+
* a map of column widths calculated from the table's width and any provided column width information from the collection.
|
|
58
|
+
* In addition, it tracks the currently resizing column and provides callbacks for updating the widths upon resize operations.
|
|
59
|
+
* @param props - Props for the table.
|
|
60
|
+
* @param state - State for the table, as returned by `useTableState`.
|
|
61
|
+
*/
|
|
62
|
+
export function useTableColumnResizeState<T>(props: TableColumnResizeStateProps<T>, state: TableState<T>): TableColumnResizeState<T> {
|
|
63
|
+
let {
|
|
64
|
+
getDefaultWidth,
|
|
65
|
+
getDefaultMinWidth,
|
|
66
|
+
tableWidth = 0
|
|
67
|
+
} = props;
|
|
68
|
+
|
|
69
|
+
let [resizingColumn, setResizingColumn] = useState<Key | null>(null);
|
|
70
|
+
let columnLayout = useMemo(
|
|
71
|
+
() => new TableColumnLayout({
|
|
72
|
+
getDefaultWidth,
|
|
73
|
+
getDefaultMinWidth
|
|
74
|
+
}),
|
|
75
|
+
[getDefaultWidth, getDefaultMinWidth]
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
let [controlledColumns, uncontrolledColumns] = useMemo(() =>
|
|
79
|
+
columnLayout.splitColumnsIntoControlledAndUncontrolled(state.collection.columns)
|
|
80
|
+
, [state.collection.columns, columnLayout]);
|
|
81
|
+
|
|
82
|
+
// uncontrolled column widths
|
|
83
|
+
let [uncontrolledWidths, setUncontrolledWidths] = useState(() =>
|
|
84
|
+
columnLayout.getInitialUncontrolledWidths(uncontrolledColumns)
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
// Update uncontrolled widths if the columns changed.
|
|
88
|
+
let [lastColumns, setLastColumns] = useState(state.collection.columns);
|
|
89
|
+
if (state.collection.columns !== lastColumns) {
|
|
90
|
+
if (
|
|
91
|
+
state.collection.columns.length !== lastColumns.length ||
|
|
92
|
+
state.collection.columns.some((c, i) => c.key !== lastColumns[i].key)
|
|
93
|
+
) {
|
|
94
|
+
let newUncontrolledWidths = columnLayout.getInitialUncontrolledWidths(uncontrolledColumns);
|
|
95
|
+
setUncontrolledWidths(newUncontrolledWidths);
|
|
96
|
+
}
|
|
97
|
+
setLastColumns(state.collection.columns);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// combine columns back into one map that maintains same order as the columns
|
|
101
|
+
let colWidths = useMemo(() =>
|
|
102
|
+
columnLayout.recombineColumns(state.collection.columns, uncontrolledWidths, uncontrolledColumns, controlledColumns)
|
|
103
|
+
, [state.collection.columns, uncontrolledWidths, uncontrolledColumns, controlledColumns, columnLayout]);
|
|
104
|
+
|
|
105
|
+
let startResize = useCallback((key: Key) => {
|
|
106
|
+
setResizingColumn(key);
|
|
107
|
+
}, [setResizingColumn]);
|
|
108
|
+
|
|
109
|
+
let updateResizedColumns = useCallback((key: Key, width: number): Map<Key, ColumnSize> => {
|
|
110
|
+
let newSizes = columnLayout.resizeColumnWidth(state.collection, uncontrolledWidths, key, width);
|
|
111
|
+
let map = new Map(Array.from(uncontrolledColumns).map(([key]) => [key, newSizes.get(key)]));
|
|
112
|
+
map.set(key, width);
|
|
113
|
+
setUncontrolledWidths(map);
|
|
114
|
+
return newSizes;
|
|
115
|
+
}, [uncontrolledColumns, setUncontrolledWidths, columnLayout, state.collection, uncontrolledWidths]);
|
|
116
|
+
|
|
117
|
+
let endResize = useCallback(() => {
|
|
118
|
+
setResizingColumn(null);
|
|
119
|
+
}, [setResizingColumn]);
|
|
120
|
+
|
|
121
|
+
let columnWidths = useMemo(() =>
|
|
122
|
+
columnLayout.buildColumnWidths(tableWidth, state.collection, colWidths)
|
|
123
|
+
, [tableWidth, state.collection, colWidths, columnLayout]);
|
|
124
|
+
|
|
125
|
+
return useMemo(() => ({
|
|
126
|
+
resizingColumn,
|
|
127
|
+
updateResizedColumns,
|
|
128
|
+
startResize,
|
|
129
|
+
endResize,
|
|
130
|
+
getColumnWidth: (key: Key) =>
|
|
131
|
+
columnLayout.getColumnWidth(key),
|
|
132
|
+
getColumnMinWidth: (key: Key) =>
|
|
133
|
+
columnLayout.getColumnMinWidth(key),
|
|
134
|
+
getColumnMaxWidth: (key: Key) =>
|
|
135
|
+
columnLayout.getColumnMaxWidth(key),
|
|
136
|
+
tableState: state,
|
|
137
|
+
columnWidths
|
|
138
|
+
}), [
|
|
139
|
+
columnLayout,
|
|
140
|
+
columnWidths,
|
|
141
|
+
resizingColumn,
|
|
142
|
+
updateResizedColumns,
|
|
143
|
+
startResize,
|
|
144
|
+
endResize,
|
|
145
|
+
state
|
|
146
|
+
]);
|
|
147
|
+
}
|
package/src/useTableState.ts
CHANGED
|
@@ -10,10 +10,11 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import {CollectionBase, MultipleSelection, Node, SelectionMode, Sortable, SortDescriptor, SortDirection} from '@react-types/shared';
|
|
14
13
|
import {GridState, useGridState} from '@react-stately/grid';
|
|
15
|
-
import {TableCollection as ITableCollection} from '@react-types/table';
|
|
16
|
-
import {Key,
|
|
14
|
+
import {TableCollection as ITableCollection, TableBodyProps, TableHeaderProps} from '@react-types/table';
|
|
15
|
+
import {Key, Node, SelectionMode, Sortable, SortDescriptor, SortDirection} from '@react-types/shared';
|
|
16
|
+
import {MultipleSelectionState, MultipleSelectionStateProps} from '@react-stately/selection';
|
|
17
|
+
import {ReactElement, useCallback, useMemo, useState} from 'react';
|
|
17
18
|
import {TableCollection} from './TableCollection';
|
|
18
19
|
import {useCollection} from '@react-stately/collections';
|
|
19
20
|
|
|
@@ -25,18 +26,35 @@ export interface TableState<T> extends GridState<T, ITableCollection<T>> {
|
|
|
25
26
|
/** The current sorted column and direction. */
|
|
26
27
|
sortDescriptor: SortDescriptor,
|
|
27
28
|
/** Calls the provided onSortChange handler with the provided column key and sort direction. */
|
|
28
|
-
sort(columnKey: Key): void
|
|
29
|
+
sort(columnKey: Key, direction?: 'ascending' | 'descending'): void,
|
|
30
|
+
/** Whether keyboard navigation is disabled, such as when the arrow keys should be handled by a component within a cell. */
|
|
31
|
+
isKeyboardNavigationDisabled: boolean,
|
|
32
|
+
/** Set whether keyboard navigation is disabled, such as when the arrow keys should be handled by a component within a cell. */
|
|
33
|
+
setKeyboardNavigationDisabled: (val: boolean) => void
|
|
29
34
|
}
|
|
30
35
|
|
|
31
36
|
export interface CollectionBuilderContext<T> {
|
|
32
37
|
showSelectionCheckboxes: boolean,
|
|
38
|
+
showDragButtons: boolean,
|
|
33
39
|
selectionMode: SelectionMode,
|
|
34
40
|
columns: Node<T>[]
|
|
35
41
|
}
|
|
36
42
|
|
|
37
|
-
export interface TableStateProps<T> extends
|
|
43
|
+
export interface TableStateProps<T> extends MultipleSelectionStateProps, Sortable {
|
|
44
|
+
/** The elements that make up the table. Includes the TableHeader, TableBody, Columns, and Rows. */
|
|
45
|
+
children?: [ReactElement<TableHeaderProps<T>>, ReactElement<TableBodyProps<T>>],
|
|
46
|
+
/** A list of row keys to disable. */
|
|
47
|
+
disabledKeys?: Iterable<Key>,
|
|
48
|
+
/** A pre-constructed collection to use instead of building one from items and children. */
|
|
49
|
+
collection?: ITableCollection<T>,
|
|
38
50
|
/** Whether the row selection checkboxes should be displayed. */
|
|
39
|
-
showSelectionCheckboxes?: boolean
|
|
51
|
+
showSelectionCheckboxes?: boolean,
|
|
52
|
+
/** Whether the row drag button should be displayed.
|
|
53
|
+
* @private
|
|
54
|
+
*/
|
|
55
|
+
showDragButtons?: boolean,
|
|
56
|
+
/** @private - do not use unless you know what you're doing. */
|
|
57
|
+
UNSAFE_selectionState?: MultipleSelectionState
|
|
40
58
|
}
|
|
41
59
|
|
|
42
60
|
const OPPOSITE_SORT_DIRECTION = {
|
|
@@ -48,21 +66,28 @@ const OPPOSITE_SORT_DIRECTION = {
|
|
|
48
66
|
* Provides state management for a table component. Handles building a collection
|
|
49
67
|
* of columns and rows from props. In addition, it tracks row selection and manages sort order changes.
|
|
50
68
|
*/
|
|
51
|
-
export function useTableState<T extends object>(props: TableStateProps<T>): TableState<T>
|
|
52
|
-
let
|
|
69
|
+
export function useTableState<T extends object>(props: TableStateProps<T>): TableState<T> {
|
|
70
|
+
let [isKeyboardNavigationDisabled, setKeyboardNavigationDisabled] = useState(false);
|
|
71
|
+
let {selectionMode = 'none', showSelectionCheckboxes, showDragButtons} = props;
|
|
53
72
|
|
|
54
73
|
let context = useMemo(() => ({
|
|
55
|
-
showSelectionCheckboxes:
|
|
74
|
+
showSelectionCheckboxes: showSelectionCheckboxes && selectionMode !== 'none',
|
|
75
|
+
showDragButtons: showDragButtons,
|
|
56
76
|
selectionMode,
|
|
57
77
|
columns: []
|
|
58
|
-
|
|
78
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
79
|
+
}), [props.children, showSelectionCheckboxes, selectionMode, showDragButtons]);
|
|
59
80
|
|
|
60
|
-
let collection = useCollection<T,
|
|
81
|
+
let collection = useCollection<T, ITableCollection<T>>(
|
|
61
82
|
props,
|
|
62
|
-
(nodes
|
|
83
|
+
useCallback((nodes) => new TableCollection(nodes, null, context), [context]),
|
|
63
84
|
context
|
|
64
85
|
);
|
|
65
|
-
let {disabledKeys, selectionManager} = useGridState({
|
|
86
|
+
let {disabledKeys, selectionManager} = useGridState({
|
|
87
|
+
...props,
|
|
88
|
+
collection,
|
|
89
|
+
disabledBehavior: props.disabledBehavior || 'selection'
|
|
90
|
+
});
|
|
66
91
|
|
|
67
92
|
return {
|
|
68
93
|
collection,
|
|
@@ -70,12 +95,14 @@ export function useTableState<T extends object>(props: TableStateProps<T>): Tabl
|
|
|
70
95
|
selectionManager,
|
|
71
96
|
showSelectionCheckboxes: props.showSelectionCheckboxes || false,
|
|
72
97
|
sortDescriptor: props.sortDescriptor,
|
|
73
|
-
|
|
98
|
+
isKeyboardNavigationDisabled: collection.size === 0 || isKeyboardNavigationDisabled,
|
|
99
|
+
setKeyboardNavigationDisabled,
|
|
100
|
+
sort(columnKey: Key, direction?: 'ascending' | 'descending') {
|
|
74
101
|
props.onSortChange({
|
|
75
102
|
column: columnKey,
|
|
76
|
-
direction: props.sortDescriptor?.column === columnKey
|
|
103
|
+
direction: direction ?? (props.sortDescriptor?.column === columnKey
|
|
77
104
|
? OPPOSITE_SORT_DIRECTION[props.sortDescriptor.direction]
|
|
78
|
-
: 'ascending'
|
|
105
|
+
: 'ascending')
|
|
79
106
|
});
|
|
80
107
|
}
|
|
81
108
|
};
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {CollectionBuilder} from '@react-stately/collections';
|
|
14
|
+
import {GridNode} from '@react-types/grid';
|
|
15
|
+
import {Key} from '@react-types/shared';
|
|
16
|
+
import {ReactElement, useMemo} from 'react';
|
|
17
|
+
import {TableCollection} from './TableCollection';
|
|
18
|
+
import {tableNestedRows} from '@react-stately/flags';
|
|
19
|
+
import {TableState, TableStateProps, useTableState} from './useTableState';
|
|
20
|
+
import {useControlledState} from '@react-stately/utils';
|
|
21
|
+
|
|
22
|
+
export interface TreeGridState<T> extends TableState<T> {
|
|
23
|
+
/** A set of keys for items that are expanded. */
|
|
24
|
+
expandedKeys: 'all' | Set<Key>,
|
|
25
|
+
/** Toggles the expanded state for a row by its key. */
|
|
26
|
+
toggleKey(key: Key): void,
|
|
27
|
+
/** The key map containing nodes representing the collection's tree grid structure. */
|
|
28
|
+
keyMap: Map<Key, GridNode<T>>,
|
|
29
|
+
/** The number of leaf columns provided by the user. */
|
|
30
|
+
userColumnCount: number
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface TreeGridStateProps<T> extends Omit<TableStateProps<T>, 'collection'> {
|
|
34
|
+
/** The currently expanded keys in the collection (controlled). */
|
|
35
|
+
UNSTABLE_expandedKeys?: 'all' | Iterable<Key>,
|
|
36
|
+
/** The initial expanded keys in the collection (uncontrolled). */
|
|
37
|
+
UNSTABLE_defaultExpandedKeys?: 'all' | Iterable<Key>,
|
|
38
|
+
/** Handler that is called when items are expanded or collapsed. */
|
|
39
|
+
UNSTABLE_onExpandedChange?: (keys: Set<Key>) => any
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Provides state management for a tree grid component. Handles building a collection
|
|
44
|
+
* of columns and rows from props. In addition, it tracks and manages expanded rows, row selection, and sort order changes.
|
|
45
|
+
*/
|
|
46
|
+
export function UNSTABLE_useTreeGridState<T extends object>(props: TreeGridStateProps<T>): TreeGridState<T> {
|
|
47
|
+
let {
|
|
48
|
+
selectionMode = 'none',
|
|
49
|
+
showSelectionCheckboxes,
|
|
50
|
+
showDragButtons,
|
|
51
|
+
UNSTABLE_expandedKeys: propExpandedKeys,
|
|
52
|
+
UNSTABLE_defaultExpandedKeys: propDefaultExpandedKeys,
|
|
53
|
+
UNSTABLE_onExpandedChange,
|
|
54
|
+
children
|
|
55
|
+
} = props;
|
|
56
|
+
|
|
57
|
+
if (!tableNestedRows()) {
|
|
58
|
+
throw new Error('Feature flag for table nested rows must be enabled to use useTreeGridState.');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let [expandedKeys, setExpandedKeys] = useControlledState(
|
|
62
|
+
propExpandedKeys ? convertExpanded(propExpandedKeys) : undefined,
|
|
63
|
+
propDefaultExpandedKeys ? convertExpanded(propDefaultExpandedKeys) : new Set(),
|
|
64
|
+
UNSTABLE_onExpandedChange
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
let context = useMemo(() => ({
|
|
68
|
+
showSelectionCheckboxes: showSelectionCheckboxes && selectionMode !== 'none',
|
|
69
|
+
showDragButtons: showDragButtons,
|
|
70
|
+
selectionMode,
|
|
71
|
+
columns: []
|
|
72
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
73
|
+
}), [children, showSelectionCheckboxes, selectionMode, showDragButtons]);
|
|
74
|
+
|
|
75
|
+
let builder = useMemo(() => new CollectionBuilder<T>(), []);
|
|
76
|
+
let nodes = useMemo(() => builder.build({children: children as ReactElement<any>[]}, context), [builder, children, context]);
|
|
77
|
+
let treeGridCollection = useMemo(() => {
|
|
78
|
+
return generateTreeGridCollection<T>(nodes, {showSelectionCheckboxes, showDragButtons, expandedKeys});
|
|
79
|
+
}, [nodes, showSelectionCheckboxes, showDragButtons, expandedKeys]);
|
|
80
|
+
|
|
81
|
+
let onToggle = (key: Key) => {
|
|
82
|
+
setExpandedKeys(toggleKey(expandedKeys, key, treeGridCollection));
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
let collection = useMemo(() => {
|
|
86
|
+
return new TableCollection(treeGridCollection.tableNodes, null, context);
|
|
87
|
+
}, [context, treeGridCollection.tableNodes]);
|
|
88
|
+
|
|
89
|
+
let tableState = useTableState({...props, collection});
|
|
90
|
+
return {
|
|
91
|
+
...tableState,
|
|
92
|
+
keyMap: treeGridCollection.keyMap,
|
|
93
|
+
userColumnCount: treeGridCollection.userColumnCount,
|
|
94
|
+
expandedKeys,
|
|
95
|
+
toggleKey: onToggle
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function toggleKey<T>(currentExpandedKeys: 'all' | Set<Key>, key: Key, collection: TreeGridCollection<T>): Set<Key> {
|
|
100
|
+
let updatedExpandedKeys: Set<Key>;
|
|
101
|
+
if (currentExpandedKeys === 'all') {
|
|
102
|
+
updatedExpandedKeys = new Set(collection.flattenedRows.filter(row => row.props.UNSTABLE_childItems || row.props.children.length > collection.userColumnCount).map(row => row.key));
|
|
103
|
+
updatedExpandedKeys.delete(key);
|
|
104
|
+
} else {
|
|
105
|
+
updatedExpandedKeys = new Set(currentExpandedKeys);
|
|
106
|
+
if (updatedExpandedKeys.has(key)) {
|
|
107
|
+
updatedExpandedKeys.delete(key);
|
|
108
|
+
} else {
|
|
109
|
+
updatedExpandedKeys.add(key);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return updatedExpandedKeys;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function convertExpanded(expanded: 'all' | Iterable<Key>): 'all' | Set<Key> {
|
|
117
|
+
if (!expanded) {
|
|
118
|
+
return new Set<Key>();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return expanded === 'all'
|
|
122
|
+
? 'all'
|
|
123
|
+
: new Set(expanded);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
interface TreeGridCollectionOptions {
|
|
127
|
+
showSelectionCheckboxes?: boolean,
|
|
128
|
+
showDragButtons?: boolean,
|
|
129
|
+
expandedKeys: 'all' | Set<Key>
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
interface TreeGridCollection<T> {
|
|
133
|
+
keyMap: Map<Key, GridNode<T>>,
|
|
134
|
+
tableNodes: GridNode<T>[],
|
|
135
|
+
flattenedRows: GridNode<T>[],
|
|
136
|
+
userColumnCount: number
|
|
137
|
+
}
|
|
138
|
+
function generateTreeGridCollection<T>(nodes, opts: TreeGridCollectionOptions): TreeGridCollection<T> {
|
|
139
|
+
let {
|
|
140
|
+
expandedKeys = new Set()
|
|
141
|
+
} = opts;
|
|
142
|
+
|
|
143
|
+
let body: GridNode<T>;
|
|
144
|
+
let flattenedRows = [];
|
|
145
|
+
let columnCount = 0;
|
|
146
|
+
let userColumnCount = 0;
|
|
147
|
+
let originalColumns = [];
|
|
148
|
+
let keyMap = new Map();
|
|
149
|
+
|
|
150
|
+
if (opts?.showSelectionCheckboxes) {
|
|
151
|
+
columnCount++;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (opts?.showDragButtons) {
|
|
155
|
+
columnCount++;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
let topLevelRows = [];
|
|
159
|
+
let visit = (node: GridNode<T>) => {
|
|
160
|
+
switch (node.type) {
|
|
161
|
+
case 'body':
|
|
162
|
+
body = node;
|
|
163
|
+
keyMap.set(body.key, body);
|
|
164
|
+
break;
|
|
165
|
+
case 'column':
|
|
166
|
+
if (!node.hasChildNodes) {
|
|
167
|
+
userColumnCount++;
|
|
168
|
+
}
|
|
169
|
+
break;
|
|
170
|
+
case 'item':
|
|
171
|
+
topLevelRows.push(node);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
for (let child of node.childNodes) {
|
|
176
|
+
visit(child);
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
for (let node of nodes) {
|
|
181
|
+
if (node.type === 'column') {
|
|
182
|
+
originalColumns.push(node);
|
|
183
|
+
}
|
|
184
|
+
visit(node);
|
|
185
|
+
}
|
|
186
|
+
columnCount += userColumnCount;
|
|
187
|
+
|
|
188
|
+
// Update each grid node in the treegrid table with values specific to a treegrid structure. Also store a set of flattened row nodes for TableCollection to consume
|
|
189
|
+
let globalRowCount = 0;
|
|
190
|
+
let visitNode = (node: GridNode<T>, i?: number) => {
|
|
191
|
+
// Clone row node and its children so modifications to the node for treegrid specific values aren't applied on the nodes provided
|
|
192
|
+
// to TableCollection. Index, level, and parent keys are all changed to reflect a flattened row structure rather than the treegrid structure
|
|
193
|
+
// values automatically calculated via CollectionBuilder
|
|
194
|
+
if (node.type === 'item') {
|
|
195
|
+
let childNodes = [];
|
|
196
|
+
for (let child of node.childNodes) {
|
|
197
|
+
if (child.type === 'cell') {
|
|
198
|
+
let cellClone = {...child};
|
|
199
|
+
if (cellClone.index + 1 === columnCount) {
|
|
200
|
+
cellClone.nextKey = null;
|
|
201
|
+
}
|
|
202
|
+
childNodes.push({...cellClone});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
let clone = {...node, childNodes: childNodes, parentKey: body.key, level: 1, index: globalRowCount++};
|
|
206
|
+
flattenedRows.push(clone);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
let newProps = {};
|
|
210
|
+
|
|
211
|
+
// Assign indexOfType to cells and rows for aria-posinset
|
|
212
|
+
if (node.type !== 'placeholder' && node.type !== 'column') {
|
|
213
|
+
newProps['indexOfType'] = i;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Use Object.assign instead of spread to preserve object reference for keyMap. Also ensures retrieving nodes
|
|
217
|
+
// via .childNodes returns the same object as the one found via keyMap look up
|
|
218
|
+
Object.assign(node, newProps);
|
|
219
|
+
keyMap.set(node.key, node);
|
|
220
|
+
|
|
221
|
+
let lastNode: GridNode<T>;
|
|
222
|
+
let rowIndex = 0;
|
|
223
|
+
for (let child of node.childNodes) {
|
|
224
|
+
if (!(child.type === 'item' && expandedKeys !== 'all' && !expandedKeys.has(node.key))) {
|
|
225
|
+
if (child.parentKey == null) {
|
|
226
|
+
// if child is a cell/expanded row/column and the parent key isn't already established by the collection, match child node to parent row
|
|
227
|
+
child.parentKey = node.key;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (lastNode) {
|
|
231
|
+
lastNode.nextKey = child.key;
|
|
232
|
+
child.prevKey = lastNode.key;
|
|
233
|
+
} else {
|
|
234
|
+
child.prevKey = null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (child.type === 'item') {
|
|
238
|
+
visitNode(child, rowIndex++);
|
|
239
|
+
} else {
|
|
240
|
+
// We enforce that the cells come before rows so can just reuse cell index
|
|
241
|
+
visitNode(child, child.index);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
lastNode = child;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (lastNode) {
|
|
249
|
+
lastNode.nextKey = null;
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
let last: GridNode<T>;
|
|
254
|
+
topLevelRows.forEach((node: GridNode<T>, i) => {
|
|
255
|
+
visitNode(node as GridNode<T>, i);
|
|
256
|
+
|
|
257
|
+
if (last) {
|
|
258
|
+
last.nextKey = node.key;
|
|
259
|
+
node.prevKey = last.key;
|
|
260
|
+
} else {
|
|
261
|
+
node.prevKey = null;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
last = node;
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
if (last) {
|
|
268
|
+
last.nextKey = null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
keyMap,
|
|
273
|
+
userColumnCount,
|
|
274
|
+
flattenedRows,
|
|
275
|
+
tableNodes: [...originalColumns, {...body, childNodes: flattenedRows}]
|
|
276
|
+
};
|
|
277
|
+
}
|