@react-stately/grid 3.11.9 → 3.12.1

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/types.d.ts DELETED
@@ -1,46 +0,0 @@
1
- import { GridCollection as _GridCollection1, GridNode, GridRow } from "@react-types/grid";
2
- import { Key } from "@react-types/shared";
3
- import { MultipleSelectionState, MultipleSelectionStateProps, SelectionManager } from "@react-stately/selection";
4
- export interface GridState<T, C extends _GridCollection1<T>> {
5
- collection: C;
6
- /** A set of keys for rows that are disabled. */
7
- disabledKeys: Set<Key>;
8
- /** A selection manager to read and update row selection state. */
9
- selectionManager: SelectionManager;
10
- /** Whether keyboard navigation is disabled, such as when the arrow keys should be handled by a component within a cell. */
11
- isKeyboardNavigationDisabled: boolean;
12
- }
13
- export interface GridStateOptions<T, C extends _GridCollection1<T>> extends MultipleSelectionStateProps {
14
- collection: C;
15
- disabledKeys?: Iterable<Key>;
16
- focusMode?: 'row' | 'cell';
17
- /** @private - do not use unless you know what you're doing. */
18
- UNSAFE_selectionState?: MultipleSelectionState;
19
- }
20
- /**
21
- * Provides state management for a grid component. Handles row selection and focusing a grid cell's focusable child if applicable.
22
- */
23
- export function useGridState<T extends object, C extends _GridCollection1<T>>(props: GridStateOptions<T, C>): GridState<T, C>;
24
- interface GridCollectionOptions<T> {
25
- columnCount: number;
26
- items: GridRow<T>[];
27
- visitNode?: (cell: GridNode<T>) => GridNode<T>;
28
- }
29
- export class GridCollection<T> implements _GridCollection1<T> {
30
- keyMap: Map<Key, GridNode<T>>;
31
- columnCount: number;
32
- rows: GridNode<T>[];
33
- constructor(opts: GridCollectionOptions<T>);
34
- [Symbol.iterator](): IterableIterator<GridNode<T>>;
35
- get size(): number;
36
- getKeys(): IterableIterator<Key>;
37
- getKeyBefore(key: Key): Key | null;
38
- getKeyAfter(key: Key): Key | null;
39
- getFirstKey(): Key | null;
40
- getLastKey(): Key | null;
41
- getItem(key: Key): GridNode<T> | null;
42
- at(id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BAER,IAAI,EAAE,sBAAsB,CAAC,CAAC;IA6GzC,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAgB,CAAC,SAAS,CAAC,CAAC,CAAC;IAInD,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,OAAO,IAAI,gBAAgB,CAAC,GAAG,CAAC;IAIhC,YAAY,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,GAAG,IAAI;IAKlC,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,GAAG,IAAI;IAKjC,WAAW,IAAI,GAAG,GAAG,IAAI;IAIzB,UAAU,IAAI,GAAG,GAAG,IAAI;IAKxB,OAAO,CAAC,GAAG,EAAE,GAAG,GAAG,SAAS,CAAC,CAAC,GAAG,IAAI;IAIrC,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,GAAG,IAAI;IAKnC,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC;CAI7C","sources":["packages/@react-stately/grid/src/packages/@react-stately/grid/src/useGridState.ts","packages/@react-stately/grid/src/packages/@react-stately/grid/src/GridCollection.ts","packages/@react-stately/grid/src/packages/@react-stately/grid/src/index.ts","packages/@react-stately/grid/src/index.ts"],"sourcesContent":[null,null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useGridState} from './useGridState';\nexport {GridCollection} from './GridCollection';\n\nexport type {GridStateOptions, GridState} from './useGridState';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
@@ -1,91 +0,0 @@
1
- var $5Cus8$reactstatelycollections = require("@react-stately/collections");
2
- var $5Cus8$reactstatelyselection = require("@react-stately/selection");
3
- var $5Cus8$react = require("react");
4
-
5
-
6
- function $parcel$export(e, n, v, s) {
7
- Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
8
- }
9
-
10
- $parcel$export(module.exports, "useGridState", () => $38009b28e45912ea$export$4007ac09ff9c68ed);
11
-
12
-
13
-
14
- function $38009b28e45912ea$export$4007ac09ff9c68ed(props) {
15
- let { collection: collection, focusMode: focusMode } = props;
16
- // eslint-disable-next-line react-hooks/rules-of-hooks
17
- let selectionState = props.UNSAFE_selectionState || (0, $5Cus8$reactstatelyselection.useMultipleSelectionState)(props);
18
- let disabledKeys = (0, $5Cus8$react.useMemo)(()=>props.disabledKeys ? new Set(props.disabledKeys) : new Set(), [
19
- props.disabledKeys
20
- ]);
21
- let setFocusedKey = selectionState.setFocusedKey;
22
- selectionState.setFocusedKey = (key, child)=>{
23
- // If focusMode is cell and an item is focused, focus a child cell instead.
24
- if (focusMode === 'cell' && key != null) {
25
- let item = collection.getItem(key);
26
- if ((item === null || item === void 0 ? void 0 : item.type) === 'item') {
27
- var _getLastItem, _getFirstItem;
28
- let children = (0, $5Cus8$reactstatelycollections.getChildNodes)(item, collection);
29
- var _getLastItem_key, _getFirstItem_key;
30
- if (child === 'last') key = (_getLastItem_key = (_getLastItem = (0, $5Cus8$reactstatelycollections.getLastItem)(children)) === null || _getLastItem === void 0 ? void 0 : _getLastItem.key) !== null && _getLastItem_key !== void 0 ? _getLastItem_key : null;
31
- else key = (_getFirstItem_key = (_getFirstItem = (0, $5Cus8$reactstatelycollections.getFirstItem)(children)) === null || _getFirstItem === void 0 ? void 0 : _getFirstItem.key) !== null && _getFirstItem_key !== void 0 ? _getFirstItem_key : null;
32
- }
33
- }
34
- setFocusedKey(key, child);
35
- };
36
- let selectionManager = (0, $5Cus8$react.useMemo)(()=>new (0, $5Cus8$reactstatelyselection.SelectionManager)(collection, selectionState), [
37
- collection,
38
- selectionState
39
- ]);
40
- // Reset focused key if that item is deleted from the collection.
41
- const cachedCollection = (0, $5Cus8$react.useRef)(null);
42
- (0, $5Cus8$react.useEffect)(()=>{
43
- if (selectionState.focusedKey != null && cachedCollection.current && !collection.getItem(selectionState.focusedKey)) {
44
- const node = cachedCollection.current.getItem(selectionState.focusedKey);
45
- const parentNode = (node === null || node === void 0 ? void 0 : node.parentKey) != null && (node.type === 'cell' || node.type === 'rowheader' || node.type === 'column') ? cachedCollection.current.getItem(node.parentKey) : node;
46
- if (!parentNode) {
47
- selectionState.setFocusedKey(null);
48
- return;
49
- }
50
- const cachedRows = cachedCollection.current.rows;
51
- const rows = collection.rows;
52
- const diff = cachedRows.length - rows.length;
53
- let index = Math.min(diff > 1 ? Math.max(parentNode.index - diff + 1, 0) : parentNode.index, rows.length - 1);
54
- let newRow = null;
55
- while(index >= 0){
56
- if (!selectionManager.isDisabled(rows[index].key) && rows[index].type !== 'headerrow') {
57
- newRow = rows[index];
58
- break;
59
- }
60
- // Find next, not disabled row.
61
- if (index < rows.length - 1) index++;
62
- else {
63
- if (index > parentNode.index) index = parentNode.index;
64
- index--;
65
- }
66
- }
67
- if (newRow) {
68
- const childNodes = newRow.hasChildNodes ? [
69
- ...(0, $5Cus8$reactstatelycollections.getChildNodes)(newRow, collection)
70
- ] : [];
71
- const keyToFocus = newRow.hasChildNodes && parentNode !== node && node && node.index < childNodes.length ? childNodes[node.index].key : newRow.key;
72
- selectionState.setFocusedKey(keyToFocus);
73
- } else selectionState.setFocusedKey(null);
74
- }
75
- cachedCollection.current = collection;
76
- }, [
77
- collection,
78
- selectionManager,
79
- selectionState,
80
- selectionState.focusedKey
81
- ]);
82
- return {
83
- collection: collection,
84
- disabledKeys: disabledKeys,
85
- isKeyboardNavigationDisabled: false,
86
- selectionManager: selectionManager
87
- };
88
- }
89
-
90
-
91
- //# sourceMappingURL=useGridState.main.js.map
@@ -1 +0,0 @@
1
- {"mappings":";;;;;;;;;;;;;AA2BO,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AAG;QAAC;QAAY;QAAkB;QAAgB,eAAe,UAAU;KAAC;IAE5E,OAAO;oBACL;sBACA;QACA,8BAA8B;0BAC9B;IACF;AACF","sources":["packages/@react-stately/grid/src/useGridState.ts"],"sourcesContent":["import {getChildNodes, getFirstItem, getLastItem} from '@react-stately/collections';\nimport {GridCollection, GridNode} from '@react-types/grid';\nimport {Key} from '@react-types/shared';\nimport {MultipleSelectionState, MultipleSelectionStateProps, SelectionManager, useMultipleSelectionState} from '@react-stately/selection';\nimport {useEffect, useMemo, useRef} from 'react';\n\nexport interface GridState<T, C extends GridCollection<T>> {\n collection: C,\n /** A set of keys for rows that are disabled. */\n disabledKeys: Set<Key>,\n /** A selection manager to read and update row selection state. */\n selectionManager: SelectionManager,\n /** Whether keyboard navigation is disabled, such as when the arrow keys should be handled by a component within a cell. */\n isKeyboardNavigationDisabled: boolean\n}\n\nexport interface GridStateOptions<T, C extends GridCollection<T>> extends MultipleSelectionStateProps {\n collection: C,\n disabledKeys?: Iterable<Key>,\n focusMode?: 'row' | 'cell',\n /** @private - do not use unless you know what you're doing. */\n UNSAFE_selectionState?: MultipleSelectionState\n}\n\n/**\n * Provides state management for a grid component. Handles row selection and focusing a grid cell's focusable child if applicable.\n */\nexport function useGridState<T extends object, C extends GridCollection<T>>(props: GridStateOptions<T, C>): GridState<T, C> {\n let {collection, focusMode} = props;\n // eslint-disable-next-line react-hooks/rules-of-hooks\n let selectionState = props.UNSAFE_selectionState || useMultipleSelectionState(props);\n let disabledKeys = useMemo(() =>\n props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()\n , [props.disabledKeys]);\n\n let setFocusedKey = selectionState.setFocusedKey;\n selectionState.setFocusedKey = (key, child) => {\n // If focusMode is cell and an item is focused, focus a child cell instead.\n if (focusMode === 'cell' && key != null) {\n let item = collection.getItem(key);\n if (item?.type === 'item') {\n let children = getChildNodes(item, collection);\n if (child === 'last') {\n key = getLastItem(children)?.key ?? null;\n } else {\n key = getFirstItem(children)?.key ?? null;\n }\n }\n }\n\n setFocusedKey(key, child);\n };\n\n let selectionManager = useMemo(() =>\n new SelectionManager(collection, selectionState)\n , [collection, selectionState]\n );\n\n // Reset focused key if that item is deleted from the collection.\n const cachedCollection = useRef<C | null>(null);\n useEffect(() => {\n if (selectionState.focusedKey != null && cachedCollection.current && !collection.getItem(selectionState.focusedKey)) {\n const node = cachedCollection.current.getItem(selectionState.focusedKey);\n const parentNode =\n node?.parentKey != null && (node.type === 'cell' || node.type === 'rowheader' || node.type === 'column') ?\n cachedCollection.current.getItem(node.parentKey) :\n node;\n if (!parentNode) {\n selectionState.setFocusedKey(null);\n return;\n }\n const cachedRows = cachedCollection.current.rows;\n const rows = collection.rows;\n const diff = cachedRows.length - rows.length;\n let index = Math.min(\n (\n diff > 1 ?\n Math.max(parentNode.index - diff + 1, 0) :\n parentNode.index\n ),\n rows.length - 1);\n let newRow: GridNode<T> | null = null;\n while (index >= 0) {\n if (!selectionManager.isDisabled(rows[index].key) && rows[index].type !== 'headerrow') {\n newRow = rows[index];\n break;\n }\n // Find next, not disabled row.\n if (index < rows.length - 1) {\n index++;\n // Otherwise, find previous, not disabled row.\n } else {\n if (index > parentNode.index) {\n index = parentNode.index;\n }\n index--;\n }\n }\n if (newRow) {\n const childNodes = newRow.hasChildNodes ? [...getChildNodes(newRow, collection)] : [];\n const keyToFocus =\n newRow.hasChildNodes &&\n parentNode !== node &&\n node &&\n node.index < childNodes.length ?\n childNodes[node.index].key :\n newRow.key;\n selectionState.setFocusedKey(keyToFocus);\n } else {\n selectionState.setFocusedKey(null);\n }\n }\n cachedCollection.current = collection;\n }, [collection, selectionManager, selectionState, selectionState.focusedKey]);\n\n return {\n collection,\n disabledKeys,\n isKeyboardNavigationDisabled: false,\n selectionManager\n };\n}\n"],"names":[],"version":3,"file":"useGridState.main.js.map"}
@@ -1,86 +0,0 @@
1
- import {getChildNodes as $7nPCv$getChildNodes, getLastItem as $7nPCv$getLastItem, getFirstItem as $7nPCv$getFirstItem} from "@react-stately/collections";
2
- import {useMultipleSelectionState as $7nPCv$useMultipleSelectionState, SelectionManager as $7nPCv$SelectionManager} from "@react-stately/selection";
3
- import {useMemo as $7nPCv$useMemo, useRef as $7nPCv$useRef, useEffect as $7nPCv$useEffect} from "react";
4
-
5
-
6
-
7
-
8
- function $62967d126f3aa823$export$4007ac09ff9c68ed(props) {
9
- let { collection: collection, focusMode: focusMode } = props;
10
- // eslint-disable-next-line react-hooks/rules-of-hooks
11
- let selectionState = props.UNSAFE_selectionState || (0, $7nPCv$useMultipleSelectionState)(props);
12
- let disabledKeys = (0, $7nPCv$useMemo)(()=>props.disabledKeys ? new Set(props.disabledKeys) : new Set(), [
13
- props.disabledKeys
14
- ]);
15
- let setFocusedKey = selectionState.setFocusedKey;
16
- selectionState.setFocusedKey = (key, child)=>{
17
- // If focusMode is cell and an item is focused, focus a child cell instead.
18
- if (focusMode === 'cell' && key != null) {
19
- let item = collection.getItem(key);
20
- if ((item === null || item === void 0 ? void 0 : item.type) === 'item') {
21
- var _getLastItem, _getFirstItem;
22
- let children = (0, $7nPCv$getChildNodes)(item, collection);
23
- var _getLastItem_key, _getFirstItem_key;
24
- if (child === 'last') key = (_getLastItem_key = (_getLastItem = (0, $7nPCv$getLastItem)(children)) === null || _getLastItem === void 0 ? void 0 : _getLastItem.key) !== null && _getLastItem_key !== void 0 ? _getLastItem_key : null;
25
- else key = (_getFirstItem_key = (_getFirstItem = (0, $7nPCv$getFirstItem)(children)) === null || _getFirstItem === void 0 ? void 0 : _getFirstItem.key) !== null && _getFirstItem_key !== void 0 ? _getFirstItem_key : null;
26
- }
27
- }
28
- setFocusedKey(key, child);
29
- };
30
- let selectionManager = (0, $7nPCv$useMemo)(()=>new (0, $7nPCv$SelectionManager)(collection, selectionState), [
31
- collection,
32
- selectionState
33
- ]);
34
- // Reset focused key if that item is deleted from the collection.
35
- const cachedCollection = (0, $7nPCv$useRef)(null);
36
- (0, $7nPCv$useEffect)(()=>{
37
- if (selectionState.focusedKey != null && cachedCollection.current && !collection.getItem(selectionState.focusedKey)) {
38
- const node = cachedCollection.current.getItem(selectionState.focusedKey);
39
- const parentNode = (node === null || node === void 0 ? void 0 : node.parentKey) != null && (node.type === 'cell' || node.type === 'rowheader' || node.type === 'column') ? cachedCollection.current.getItem(node.parentKey) : node;
40
- if (!parentNode) {
41
- selectionState.setFocusedKey(null);
42
- return;
43
- }
44
- const cachedRows = cachedCollection.current.rows;
45
- const rows = collection.rows;
46
- const diff = cachedRows.length - rows.length;
47
- let index = Math.min(diff > 1 ? Math.max(parentNode.index - diff + 1, 0) : parentNode.index, rows.length - 1);
48
- let newRow = null;
49
- while(index >= 0){
50
- if (!selectionManager.isDisabled(rows[index].key) && rows[index].type !== 'headerrow') {
51
- newRow = rows[index];
52
- break;
53
- }
54
- // Find next, not disabled row.
55
- if (index < rows.length - 1) index++;
56
- else {
57
- if (index > parentNode.index) index = parentNode.index;
58
- index--;
59
- }
60
- }
61
- if (newRow) {
62
- const childNodes = newRow.hasChildNodes ? [
63
- ...(0, $7nPCv$getChildNodes)(newRow, collection)
64
- ] : [];
65
- const keyToFocus = newRow.hasChildNodes && parentNode !== node && node && node.index < childNodes.length ? childNodes[node.index].key : newRow.key;
66
- selectionState.setFocusedKey(keyToFocus);
67
- } else selectionState.setFocusedKey(null);
68
- }
69
- cachedCollection.current = collection;
70
- }, [
71
- collection,
72
- selectionManager,
73
- selectionState,
74
- selectionState.focusedKey
75
- ]);
76
- return {
77
- collection: collection,
78
- disabledKeys: disabledKeys,
79
- isKeyboardNavigationDisabled: false,
80
- selectionManager: selectionManager
81
- };
82
- }
83
-
84
-
85
- export {$62967d126f3aa823$export$4007ac09ff9c68ed as useGridState};
86
- //# sourceMappingURL=useGridState.module.js.map
@@ -1,86 +0,0 @@
1
- import {getChildNodes as $7nPCv$getChildNodes, getLastItem as $7nPCv$getLastItem, getFirstItem as $7nPCv$getFirstItem} from "@react-stately/collections";
2
- import {useMultipleSelectionState as $7nPCv$useMultipleSelectionState, SelectionManager as $7nPCv$SelectionManager} from "@react-stately/selection";
3
- import {useMemo as $7nPCv$useMemo, useRef as $7nPCv$useRef, useEffect as $7nPCv$useEffect} from "react";
4
-
5
-
6
-
7
-
8
- function $62967d126f3aa823$export$4007ac09ff9c68ed(props) {
9
- let { collection: collection, focusMode: focusMode } = props;
10
- // eslint-disable-next-line react-hooks/rules-of-hooks
11
- let selectionState = props.UNSAFE_selectionState || (0, $7nPCv$useMultipleSelectionState)(props);
12
- let disabledKeys = (0, $7nPCv$useMemo)(()=>props.disabledKeys ? new Set(props.disabledKeys) : new Set(), [
13
- props.disabledKeys
14
- ]);
15
- let setFocusedKey = selectionState.setFocusedKey;
16
- selectionState.setFocusedKey = (key, child)=>{
17
- // If focusMode is cell and an item is focused, focus a child cell instead.
18
- if (focusMode === 'cell' && key != null) {
19
- let item = collection.getItem(key);
20
- if ((item === null || item === void 0 ? void 0 : item.type) === 'item') {
21
- var _getLastItem, _getFirstItem;
22
- let children = (0, $7nPCv$getChildNodes)(item, collection);
23
- var _getLastItem_key, _getFirstItem_key;
24
- if (child === 'last') key = (_getLastItem_key = (_getLastItem = (0, $7nPCv$getLastItem)(children)) === null || _getLastItem === void 0 ? void 0 : _getLastItem.key) !== null && _getLastItem_key !== void 0 ? _getLastItem_key : null;
25
- else key = (_getFirstItem_key = (_getFirstItem = (0, $7nPCv$getFirstItem)(children)) === null || _getFirstItem === void 0 ? void 0 : _getFirstItem.key) !== null && _getFirstItem_key !== void 0 ? _getFirstItem_key : null;
26
- }
27
- }
28
- setFocusedKey(key, child);
29
- };
30
- let selectionManager = (0, $7nPCv$useMemo)(()=>new (0, $7nPCv$SelectionManager)(collection, selectionState), [
31
- collection,
32
- selectionState
33
- ]);
34
- // Reset focused key if that item is deleted from the collection.
35
- const cachedCollection = (0, $7nPCv$useRef)(null);
36
- (0, $7nPCv$useEffect)(()=>{
37
- if (selectionState.focusedKey != null && cachedCollection.current && !collection.getItem(selectionState.focusedKey)) {
38
- const node = cachedCollection.current.getItem(selectionState.focusedKey);
39
- const parentNode = (node === null || node === void 0 ? void 0 : node.parentKey) != null && (node.type === 'cell' || node.type === 'rowheader' || node.type === 'column') ? cachedCollection.current.getItem(node.parentKey) : node;
40
- if (!parentNode) {
41
- selectionState.setFocusedKey(null);
42
- return;
43
- }
44
- const cachedRows = cachedCollection.current.rows;
45
- const rows = collection.rows;
46
- const diff = cachedRows.length - rows.length;
47
- let index = Math.min(diff > 1 ? Math.max(parentNode.index - diff + 1, 0) : parentNode.index, rows.length - 1);
48
- let newRow = null;
49
- while(index >= 0){
50
- if (!selectionManager.isDisabled(rows[index].key) && rows[index].type !== 'headerrow') {
51
- newRow = rows[index];
52
- break;
53
- }
54
- // Find next, not disabled row.
55
- if (index < rows.length - 1) index++;
56
- else {
57
- if (index > parentNode.index) index = parentNode.index;
58
- index--;
59
- }
60
- }
61
- if (newRow) {
62
- const childNodes = newRow.hasChildNodes ? [
63
- ...(0, $7nPCv$getChildNodes)(newRow, collection)
64
- ] : [];
65
- const keyToFocus = newRow.hasChildNodes && parentNode !== node && node && node.index < childNodes.length ? childNodes[node.index].key : newRow.key;
66
- selectionState.setFocusedKey(keyToFocus);
67
- } else selectionState.setFocusedKey(null);
68
- }
69
- cachedCollection.current = collection;
70
- }, [
71
- collection,
72
- selectionManager,
73
- selectionState,
74
- selectionState.focusedKey
75
- ]);
76
- return {
77
- collection: collection,
78
- disabledKeys: disabledKeys,
79
- isKeyboardNavigationDisabled: false,
80
- selectionManager: selectionManager
81
- };
82
- }
83
-
84
-
85
- export {$62967d126f3aa823$export$4007ac09ff9c68ed as useGridState};
86
- //# sourceMappingURL=useGridState.module.js.map
@@ -1 +0,0 @@
1
- {"mappings":";;;;;;;AA2BO,SAAS,0CAA4D,KAA6B;IACvG,IAAI,cAAC,UAAU,aAAE,SAAS,EAAC,GAAG;IAC9B,sDAAsD;IACtD,IAAI,iBAAiB,MAAM,qBAAqB,IAAI,CAAA,GAAA,gCAAwB,EAAE;IAC9E,IAAI,eAAe,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from '@react-stately/collections';\nimport {GridCollection, GridNode} from '@react-types/grid';\nimport {Key} from '@react-types/shared';\nimport {MultipleSelectionState, MultipleSelectionStateProps, SelectionManager, useMultipleSelectionState} from '@react-stately/selection';\nimport {useEffect, useMemo, useRef} from 'react';\n\nexport interface GridState<T, C extends GridCollection<T>> {\n collection: C,\n /** A set of keys for rows that are disabled. */\n disabledKeys: Set<Key>,\n /** A selection manager to read and update row selection state. */\n selectionManager: SelectionManager,\n /** Whether keyboard navigation is disabled, such as when the arrow keys should be handled by a component within a cell. */\n isKeyboardNavigationDisabled: boolean\n}\n\nexport interface GridStateOptions<T, C extends GridCollection<T>> extends MultipleSelectionStateProps {\n collection: C,\n disabledKeys?: Iterable<Key>,\n focusMode?: 'row' | 'cell',\n /** @private - do not use unless you know what you're doing. */\n UNSAFE_selectionState?: MultipleSelectionState\n}\n\n/**\n * Provides state management for a grid component. Handles row selection and focusing a grid cell's focusable child if applicable.\n */\nexport function useGridState<T extends object, C extends GridCollection<T>>(props: GridStateOptions<T, C>): GridState<T, C> {\n let {collection, focusMode} = props;\n // eslint-disable-next-line react-hooks/rules-of-hooks\n let selectionState = props.UNSAFE_selectionState || useMultipleSelectionState(props);\n let disabledKeys = useMemo(() =>\n props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()\n , [props.disabledKeys]);\n\n let setFocusedKey = selectionState.setFocusedKey;\n selectionState.setFocusedKey = (key, child) => {\n // If focusMode is cell and an item is focused, focus a child cell instead.\n if (focusMode === 'cell' && key != null) {\n let item = collection.getItem(key);\n if (item?.type === 'item') {\n let children = getChildNodes(item, collection);\n if (child === 'last') {\n key = getLastItem(children)?.key ?? null;\n } else {\n key = getFirstItem(children)?.key ?? null;\n }\n }\n }\n\n setFocusedKey(key, child);\n };\n\n let selectionManager = useMemo(() =>\n new SelectionManager(collection, selectionState)\n , [collection, selectionState]\n );\n\n // Reset focused key if that item is deleted from the collection.\n const cachedCollection = useRef<C | null>(null);\n useEffect(() => {\n if (selectionState.focusedKey != null && cachedCollection.current && !collection.getItem(selectionState.focusedKey)) {\n const node = cachedCollection.current.getItem(selectionState.focusedKey);\n const parentNode =\n node?.parentKey != null && (node.type === 'cell' || node.type === 'rowheader' || node.type === 'column') ?\n cachedCollection.current.getItem(node.parentKey) :\n node;\n if (!parentNode) {\n selectionState.setFocusedKey(null);\n return;\n }\n const cachedRows = cachedCollection.current.rows;\n const rows = collection.rows;\n const diff = cachedRows.length - rows.length;\n let index = Math.min(\n (\n diff > 1 ?\n Math.max(parentNode.index - diff + 1, 0) :\n parentNode.index\n ),\n rows.length - 1);\n let newRow: GridNode<T> | null = null;\n while (index >= 0) {\n if (!selectionManager.isDisabled(rows[index].key) && rows[index].type !== 'headerrow') {\n newRow = rows[index];\n break;\n }\n // Find next, not disabled row.\n if (index < rows.length - 1) {\n index++;\n // Otherwise, find previous, not disabled row.\n } else {\n if (index > parentNode.index) {\n index = parentNode.index;\n }\n index--;\n }\n }\n if (newRow) {\n const childNodes = newRow.hasChildNodes ? [...getChildNodes(newRow, collection)] : [];\n const keyToFocus =\n newRow.hasChildNodes &&\n parentNode !== node &&\n node &&\n node.index < childNodes.length ?\n childNodes[node.index].key :\n newRow.key;\n selectionState.setFocusedKey(keyToFocus);\n } else {\n selectionState.setFocusedKey(null);\n }\n }\n cachedCollection.current = collection;\n }, [collection, selectionManager, selectionState, selectionState.focusedKey]);\n\n return {\n collection,\n disabledKeys,\n isKeyboardNavigationDisabled: false,\n selectionManager\n };\n}\n"],"names":[],"version":3,"file":"useGridState.module.js.map"}
@@ -1,180 +0,0 @@
1
- /*
2
- * Copyright 2020 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 {GridNode, GridRow, GridCollection as IGridCollection} from '@react-types/grid';
14
- import {Key} from '@react-types/shared';
15
-
16
- interface GridCollectionOptions<T> {
17
- columnCount: number,
18
- items: GridRow<T>[],
19
- visitNode?: (cell: GridNode<T>) => GridNode<T>
20
- }
21
-
22
- export class GridCollection<T> implements IGridCollection<T> {
23
- keyMap: Map<Key, GridNode<T>> = new Map();
24
- columnCount: number;
25
- rows: GridNode<T>[];
26
-
27
- constructor(opts: GridCollectionOptions<T>) {
28
- this.keyMap = new Map();
29
- this.columnCount = opts?.columnCount;
30
- this.rows = [];
31
-
32
- let visit = (node: GridNode<T>) => {
33
- // If the node is the same object as the previous node for the same key,
34
- // we can skip this node and its children. We always visit columns though,
35
- // because we depend on order to build the columns array.
36
- let prevNode = this.keyMap.get(node.key);
37
- if (opts.visitNode) {
38
- node = opts.visitNode(node);
39
- }
40
-
41
- this.keyMap.set(node.key, node);
42
-
43
- let childKeys = new Set();
44
- let last: GridNode<T> | null = null;
45
- let rowHasCellWithColSpan = false;
46
-
47
- if (node.type === 'item') {
48
- for (let child of node.childNodes) {
49
- if (child.props?.colSpan !== undefined) {
50
- rowHasCellWithColSpan = true;
51
- break;
52
- }
53
- }
54
- }
55
-
56
- for (let child of node.childNodes as Iterable<GridNode<T>>) {
57
- if (child.type === 'cell' && rowHasCellWithColSpan) {
58
- child.colspan = child.props?.colSpan;
59
- child.colSpan = child.props?.colSpan;
60
- child.colIndex = !last ? child.index : (last.colIndex ?? last.index) + (last.colSpan ?? 1);
61
- }
62
-
63
- if (child.type === 'cell' && child.parentKey == null) {
64
- // if child is a cell parent key isn't already established by the collection, match child node to parent row
65
- child.parentKey = node.key;
66
- }
67
- childKeys.add(child.key);
68
-
69
- if (last) {
70
- last.nextKey = child.key;
71
- child.prevKey = last.key;
72
- } else {
73
- child.prevKey = null;
74
- }
75
-
76
- visit(child);
77
- last = child;
78
- }
79
-
80
- if (last) {
81
- last.nextKey = null;
82
- }
83
-
84
- // Remove deleted nodes and their children from the key map
85
- if (prevNode) {
86
- for (let child of prevNode.childNodes) {
87
- if (!childKeys.has(child.key)) {
88
- remove(child);
89
- }
90
- }
91
- }
92
- };
93
-
94
- let remove = (node: GridNode<T>) => {
95
- this.keyMap.delete(node.key);
96
- for (let child of node.childNodes) {
97
- if (this.keyMap.get(child.key) === child) {
98
- remove(child);
99
- }
100
- }
101
- };
102
-
103
- let last: GridNode<T> | null = null;
104
- for (let [i, node] of opts.items.entries()) {
105
- let rowNode: GridNode<T> = {
106
- ...node,
107
- level: node.level ?? 0,
108
- key: node.key ?? 'row-' + i,
109
- type: node.type ?? 'row',
110
- value: node.value ?? null,
111
- hasChildNodes: true,
112
- childNodes: [...node.childNodes],
113
- rendered: node.rendered,
114
- textValue: node.textValue ?? '',
115
- index: node.index ?? i
116
- };
117
-
118
- if (last) {
119
- last.nextKey = rowNode.key;
120
- rowNode.prevKey = last.key;
121
- } else {
122
- rowNode.prevKey = null;
123
- }
124
-
125
- this.rows.push(rowNode);
126
- visit(rowNode);
127
-
128
- last = rowNode;
129
- }
130
-
131
- if (last) {
132
- last.nextKey = null;
133
- }
134
- }
135
-
136
- *[Symbol.iterator](): IterableIterator<GridNode<T>> {
137
- yield* [...this.rows];
138
- }
139
-
140
- get size(): number {
141
- return [...this.rows].length;
142
- }
143
-
144
- getKeys(): IterableIterator<Key> {
145
- return this.keyMap.keys();
146
- }
147
-
148
- getKeyBefore(key: Key): Key | null {
149
- let node = this.keyMap.get(key);
150
- return node ? node.prevKey ?? null : null;
151
- }
152
-
153
- getKeyAfter(key: Key): Key | null {
154
- let node = this.keyMap.get(key);
155
- return node ? node.nextKey ?? null : null;
156
- }
157
-
158
- getFirstKey(): Key | null {
159
- return [...this.rows][0]?.key;
160
- }
161
-
162
- getLastKey(): Key | null {
163
- let rows = [...this.rows];
164
- return rows[rows.length - 1]?.key;
165
- }
166
-
167
- getItem(key: Key): GridNode<T> | null {
168
- return this.keyMap.get(key) ?? null;
169
- }
170
-
171
- at(idx: number): GridNode<T> | null {
172
- const keys = [...this.getKeys()];
173
- return this.getItem(keys[idx]);
174
- }
175
-
176
- getChildren(key: Key): Iterable<GridNode<T>> {
177
- let node = this.keyMap.get(key);
178
- return node?.childNodes || [];
179
- }
180
- }
@@ -1,122 +0,0 @@
1
- import {getChildNodes, getFirstItem, getLastItem} from '@react-stately/collections';
2
- import {GridCollection, GridNode} from '@react-types/grid';
3
- import {Key} from '@react-types/shared';
4
- import {MultipleSelectionState, MultipleSelectionStateProps, SelectionManager, useMultipleSelectionState} from '@react-stately/selection';
5
- import {useEffect, useMemo, useRef} from 'react';
6
-
7
- export interface GridState<T, C extends GridCollection<T>> {
8
- collection: C,
9
- /** A set of keys for rows that are disabled. */
10
- disabledKeys: Set<Key>,
11
- /** A selection manager to read and update row selection state. */
12
- selectionManager: SelectionManager,
13
- /** Whether keyboard navigation is disabled, such as when the arrow keys should be handled by a component within a cell. */
14
- isKeyboardNavigationDisabled: boolean
15
- }
16
-
17
- export interface GridStateOptions<T, C extends GridCollection<T>> extends MultipleSelectionStateProps {
18
- collection: C,
19
- disabledKeys?: Iterable<Key>,
20
- focusMode?: 'row' | 'cell',
21
- /** @private - do not use unless you know what you're doing. */
22
- UNSAFE_selectionState?: MultipleSelectionState
23
- }
24
-
25
- /**
26
- * Provides state management for a grid component. Handles row selection and focusing a grid cell's focusable child if applicable.
27
- */
28
- export function useGridState<T extends object, C extends GridCollection<T>>(props: GridStateOptions<T, C>): GridState<T, C> {
29
- let {collection, focusMode} = props;
30
- // eslint-disable-next-line react-hooks/rules-of-hooks
31
- let selectionState = props.UNSAFE_selectionState || useMultipleSelectionState(props);
32
- let disabledKeys = useMemo(() =>
33
- props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()
34
- , [props.disabledKeys]);
35
-
36
- let setFocusedKey = selectionState.setFocusedKey;
37
- selectionState.setFocusedKey = (key, child) => {
38
- // If focusMode is cell and an item is focused, focus a child cell instead.
39
- if (focusMode === 'cell' && key != null) {
40
- let item = collection.getItem(key);
41
- if (item?.type === 'item') {
42
- let children = getChildNodes(item, collection);
43
- if (child === 'last') {
44
- key = getLastItem(children)?.key ?? null;
45
- } else {
46
- key = getFirstItem(children)?.key ?? null;
47
- }
48
- }
49
- }
50
-
51
- setFocusedKey(key, child);
52
- };
53
-
54
- let selectionManager = useMemo(() =>
55
- new SelectionManager(collection, selectionState)
56
- , [collection, selectionState]
57
- );
58
-
59
- // Reset focused key if that item is deleted from the collection.
60
- const cachedCollection = useRef<C | null>(null);
61
- useEffect(() => {
62
- if (selectionState.focusedKey != null && cachedCollection.current && !collection.getItem(selectionState.focusedKey)) {
63
- const node = cachedCollection.current.getItem(selectionState.focusedKey);
64
- const parentNode =
65
- node?.parentKey != null && (node.type === 'cell' || node.type === 'rowheader' || node.type === 'column') ?
66
- cachedCollection.current.getItem(node.parentKey) :
67
- node;
68
- if (!parentNode) {
69
- selectionState.setFocusedKey(null);
70
- return;
71
- }
72
- const cachedRows = cachedCollection.current.rows;
73
- const rows = collection.rows;
74
- const diff = cachedRows.length - rows.length;
75
- let index = Math.min(
76
- (
77
- diff > 1 ?
78
- Math.max(parentNode.index - diff + 1, 0) :
79
- parentNode.index
80
- ),
81
- rows.length - 1);
82
- let newRow: GridNode<T> | null = null;
83
- while (index >= 0) {
84
- if (!selectionManager.isDisabled(rows[index].key) && rows[index].type !== 'headerrow') {
85
- newRow = rows[index];
86
- break;
87
- }
88
- // Find next, not disabled row.
89
- if (index < rows.length - 1) {
90
- index++;
91
- // Otherwise, find previous, not disabled row.
92
- } else {
93
- if (index > parentNode.index) {
94
- index = parentNode.index;
95
- }
96
- index--;
97
- }
98
- }
99
- if (newRow) {
100
- const childNodes = newRow.hasChildNodes ? [...getChildNodes(newRow, collection)] : [];
101
- const keyToFocus =
102
- newRow.hasChildNodes &&
103
- parentNode !== node &&
104
- node &&
105
- node.index < childNodes.length ?
106
- childNodes[node.index].key :
107
- newRow.key;
108
- selectionState.setFocusedKey(keyToFocus);
109
- } else {
110
- selectionState.setFocusedKey(null);
111
- }
112
- }
113
- cachedCollection.current = collection;
114
- }, [collection, selectionManager, selectionState, selectionState.focusedKey]);
115
-
116
- return {
117
- collection,
118
- disabledKeys,
119
- isKeyboardNavigationDisabled: false,
120
- selectionManager
121
- };
122
- }