react-stately 3.48.0 → 3.49.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.
- package/dist/exports/index.cjs +6 -0
- package/dist/exports/index.cjs.map +1 -1
- package/dist/exports/index.js +5 -1
- package/dist/exports/index.js.map +1 -1
- package/dist/exports/index.mjs +5 -1
- package/dist/exports/index.mjs.map +1 -1
- package/dist/exports/useTokenFieldState.cjs +25 -0
- package/dist/exports/useTokenFieldState.cjs.map +1 -0
- package/dist/exports/useTokenFieldState.js +19 -0
- package/dist/exports/useTokenFieldState.js.map +1 -0
- package/dist/exports/useTokenFieldState.mjs +19 -0
- package/dist/exports/useTokenFieldState.mjs.map +1 -0
- package/dist/private/combobox/useComboBoxState.cjs +2 -1
- package/dist/private/combobox/useComboBoxState.cjs.map +1 -1
- package/dist/private/combobox/useComboBoxState.js +2 -1
- package/dist/private/combobox/useComboBoxState.js.map +1 -1
- package/dist/private/combobox/useComboBoxState.mjs +2 -1
- package/dist/private/combobox/useComboBoxState.mjs.map +1 -1
- package/dist/private/data/useTreeData.cjs +1 -0
- package/dist/private/data/useTreeData.cjs.map +1 -1
- package/dist/private/data/useTreeData.js +1 -0
- package/dist/private/data/useTreeData.js.map +1 -1
- package/dist/private/data/useTreeData.mjs +1 -0
- package/dist/private/data/useTreeData.mjs.map +1 -1
- package/dist/private/datepicker/useDateRangePickerState.cjs +1 -0
- package/dist/private/datepicker/useDateRangePickerState.cjs.map +1 -1
- package/dist/private/datepicker/useDateRangePickerState.js +1 -0
- package/dist/private/datepicker/useDateRangePickerState.js.map +1 -1
- package/dist/private/datepicker/useDateRangePickerState.mjs +1 -0
- package/dist/private/datepicker/useDateRangePickerState.mjs.map +1 -1
- package/dist/private/form/useFormValidationState.cjs +1 -1
- package/dist/private/form/useFormValidationState.cjs.map +1 -1
- package/dist/private/form/useFormValidationState.js +1 -1
- package/dist/private/form/useFormValidationState.js.map +1 -1
- package/dist/private/form/useFormValidationState.mjs +1 -1
- package/dist/private/form/useFormValidationState.mjs.map +1 -1
- package/dist/private/grid/useGridState.cjs +12 -10
- package/dist/private/grid/useGridState.cjs.map +1 -1
- package/dist/private/grid/useGridState.js +12 -10
- package/dist/private/grid/useGridState.js.map +1 -1
- package/dist/private/grid/useGridState.mjs +12 -10
- package/dist/private/grid/useGridState.mjs.map +1 -1
- package/dist/private/layout/ListLayout.cjs +0 -3
- package/dist/private/layout/ListLayout.cjs.map +1 -1
- package/dist/private/layout/ListLayout.js +0 -3
- package/dist/private/layout/ListLayout.js.map +1 -1
- package/dist/private/layout/ListLayout.mjs +0 -3
- package/dist/private/layout/ListLayout.mjs.map +1 -1
- package/dist/private/menu/useMenuTriggerState.cjs.map +1 -1
- package/dist/private/menu/useMenuTriggerState.js.map +1 -1
- package/dist/private/menu/useMenuTriggerState.mjs.map +1 -1
- package/dist/private/menu/useSubmenuTriggerState.cjs +3 -1
- package/dist/private/menu/useSubmenuTriggerState.cjs.map +1 -1
- package/dist/private/menu/useSubmenuTriggerState.js +3 -1
- package/dist/private/menu/useSubmenuTriggerState.js.map +1 -1
- package/dist/private/menu/useSubmenuTriggerState.mjs +3 -1
- package/dist/private/menu/useSubmenuTriggerState.mjs.map +1 -1
- package/dist/private/overlays/useOverlayTriggerState.cjs +4 -1
- package/dist/private/overlays/useOverlayTriggerState.cjs.map +1 -1
- package/dist/private/overlays/useOverlayTriggerState.js +5 -2
- package/dist/private/overlays/useOverlayTriggerState.js.map +1 -1
- package/dist/private/overlays/useOverlayTriggerState.mjs +5 -2
- package/dist/private/overlays/useOverlayTriggerState.mjs.map +1 -1
- package/dist/private/select/useSelectState.cjs +1 -0
- package/dist/private/select/useSelectState.cjs.map +1 -1
- package/dist/private/select/useSelectState.js +1 -0
- package/dist/private/select/useSelectState.js.map +1 -1
- package/dist/private/select/useSelectState.mjs +1 -0
- package/dist/private/select/useSelectState.mjs.map +1 -1
- package/dist/private/slider/useSliderState.cjs.map +1 -1
- package/dist/private/slider/useSliderState.js.map +1 -1
- package/dist/private/slider/useSliderState.mjs.map +1 -1
- package/dist/private/table/TableUtils.cjs +13 -1
- package/dist/private/table/TableUtils.cjs.map +1 -1
- package/dist/private/table/TableUtils.js +13 -1
- package/dist/private/table/TableUtils.js.map +1 -1
- package/dist/private/table/TableUtils.mjs +13 -1
- package/dist/private/table/TableUtils.mjs.map +1 -1
- package/dist/private/table/useTableState.cjs +1 -0
- package/dist/private/table/useTableState.cjs.map +1 -1
- package/dist/private/table/useTableState.js +1 -0
- package/dist/private/table/useTableState.js.map +1 -1
- package/dist/private/table/useTableState.mjs +1 -0
- package/dist/private/table/useTableState.mjs.map +1 -1
- package/dist/private/tokenfield/TokenFieldValue.cjs +287 -0
- package/dist/private/tokenfield/TokenFieldValue.cjs.map +1 -0
- package/dist/private/tokenfield/TokenFieldValue.js +285 -0
- package/dist/private/tokenfield/TokenFieldValue.js.map +1 -0
- package/dist/private/tokenfield/TokenFieldValue.mjs +282 -0
- package/dist/private/tokenfield/TokenFieldValue.mjs.map +1 -0
- package/dist/private/tokenfield/useTokenFieldState.cjs +37 -0
- package/dist/private/tokenfield/useTokenFieldState.cjs.map +1 -0
- package/dist/private/tokenfield/useTokenFieldState.js +32 -0
- package/dist/private/tokenfield/useTokenFieldState.js.map +1 -0
- package/dist/private/tokenfield/useTokenFieldState.mjs +32 -0
- package/dist/private/tokenfield/useTokenFieldState.mjs.map +1 -0
- package/dist/private/tooltip/useTooltipTriggerState.cjs +18 -6
- package/dist/private/tooltip/useTooltipTriggerState.cjs.map +1 -1
- package/dist/private/tooltip/useTooltipTriggerState.js +19 -7
- package/dist/private/tooltip/useTooltipTriggerState.js.map +1 -1
- package/dist/private/tooltip/useTooltipTriggerState.mjs +19 -7
- package/dist/private/tooltip/useTooltipTriggerState.mjs.map +1 -1
- package/dist/private/virtualizer/ScrollAnchor.cjs +136 -0
- package/dist/private/virtualizer/ScrollAnchor.cjs.map +1 -0
- package/dist/private/virtualizer/ScrollAnchor.js +132 -0
- package/dist/private/virtualizer/ScrollAnchor.js.map +1 -0
- package/dist/private/virtualizer/ScrollAnchor.mjs +131 -0
- package/dist/private/virtualizer/ScrollAnchor.mjs.map +1 -0
- package/dist/private/virtualizer/Virtualizer.cjs +50 -1
- package/dist/private/virtualizer/Virtualizer.cjs.map +1 -1
- package/dist/private/virtualizer/Virtualizer.js +54 -1
- package/dist/private/virtualizer/Virtualizer.js.map +1 -1
- package/dist/private/virtualizer/Virtualizer.mjs +50 -1
- package/dist/private/virtualizer/Virtualizer.mjs.map +1 -1
- package/dist/private/virtualizer/useVirtualizerState.cjs +2 -1
- package/dist/private/virtualizer/useVirtualizerState.cjs.map +1 -1
- package/dist/private/virtualizer/useVirtualizerState.js +2 -1
- package/dist/private/virtualizer/useVirtualizerState.js.map +1 -1
- package/dist/private/virtualizer/useVirtualizerState.mjs +2 -1
- package/dist/private/virtualizer/useVirtualizerState.mjs.map +1 -1
- package/dist/types/exports/index.d.ts +4 -0
- package/dist/types/exports/useTokenFieldState.d.ts +4 -0
- package/dist/types/src/combobox/useComboBoxState.d.ts +1 -1
- package/dist/types/src/menu/useMenuTriggerState.d.ts +1 -1
- package/dist/types/src/overlays/useOverlayTriggerState.d.ts +9 -0
- package/dist/types/src/table/useTableState.d.ts +7 -0
- package/dist/types/src/tokenfield/TokenFieldValue.d.ts +71 -0
- package/dist/types/src/tokenfield/useTokenFieldState.d.ts +19 -0
- package/dist/types/src/tooltip/useTooltipTriggerState.d.ts +5 -0
- package/dist/types/src/virtualizer/ScrollAnchor.d.ts +81 -0
- package/dist/types/src/virtualizer/Virtualizer.d.ts +1 -0
- package/dist/types/src/virtualizer/types.d.ts +2 -0
- package/package.json +5 -5
- package/useTokenFieldState/package.json +5 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;AAAA;;;;;;;;;;CAUC;AA6IM,SAAS,0CAA8B,OAAuB;IACnE,IAAI,gBACF,eAAe,EAAE,uBACjB,mBAAmB,UACnB,SAAS,CAAC;YAAc;eAAA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AAK,CAAC,GAAG;mBAAa;mBAAiB,SAAS,KAAK,CAAC;aAAS;YACnF,SAAS;QACX;IACF;IAEA,uDAAuD;IACvD,OAAO,WACL,UACA,SAAS,GAAG,EACZ,CAAA,aAAe,CAAA;YACb,KAAK,WAAW,GAAG;YACnB,WAAW,WAAW,SAAS;YAC/B,OAAO,WAAW,KAAK;YACvB,UAAU;mBACL,WAAW,QAAQ,CAAE,KAAK,CAAC,GAAG;mBAC9B;mBACA,WAAW,QAAQ,CAAE,KAAK,CAAC;aAC/B;QACH,CAAA,GACA;AAEJ","sources":["packages/react-stately/src/data/useTreeData.ts"],"sourcesContent":["/*\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\nimport {Key} from '@react-types/shared';\nimport {useState} from 'react';\n\nexport interface TreeOptions<T extends object> {\n /** Initial root items in the tree. */\n initialItems?: T[];\n /** The keys for the initially selected items. */\n initialSelectedKeys?: Iterable<Key>;\n /** A function that returns a unique key for an item object. */\n getKey?: (item: T) => Key;\n /** A function that returns the children for an item object. */\n getChildren?: (item: T) => T[];\n}\n\ninterface TreeNode<T extends object> {\n /** A unique key for the tree node. */\n key: Key;\n /** The key of the parent node. */\n parentKey?: Key | null;\n /** The value object for the tree node. */\n value: T;\n /** Children of the tree node. */\n children: TreeNode<T>[] | null;\n}\n\nexport interface TreeData<T extends object> {\n /** The root nodes in the tree. */\n items: TreeNode<T>[];\n\n /** The keys of the currently selected items in the tree. */\n selectedKeys: Set<Key>;\n\n /** Sets the selected keys. */\n setSelectedKeys(keys: Set<Key>): void;\n\n /**\n * Gets a node from the tree by key.\n *\n * @param key - The key of the item to retrieve.\n */\n getItem(key: Key): TreeNode<T> | undefined;\n\n /**\n * Inserts an item into a parent node as a child.\n *\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param index - The index within the parent to insert into.\n * @param value - The value to insert.\n */\n insert(parentKey: Key | null, index: number, ...values: T[]): void;\n\n /**\n * Inserts items into the list before the item at the given key.\n *\n * @param key - The key of the item to insert before.\n * @param values - The values to insert.\n */\n insertBefore(key: Key, ...values: T[]): void;\n\n /**\n * Inserts items into the list after the item at the given key.\n *\n * @param key - The key of the item to insert after.\n * @param values - The values to insert.\n */\n insertAfter(key: Key, ...values: T[]): void;\n\n /**\n * Appends an item into a parent node as a child.\n *\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n append(parentKey: Key | null, ...values: T[]): void;\n\n /**\n * Prepends an item into a parent node as a child.\n *\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n prepend(parentKey: Key | null, ...value: T[]): void;\n\n /**\n * Removes an item from the tree by its key.\n *\n * @param key - The key of the item to remove.\n */\n remove(...keys: Key[]): void;\n\n /**\n * Removes all items from the tree that are currently\n * in the set of selected items.\n */\n removeSelectedItems(): void;\n\n /**\n * Moves an item within the tree.\n *\n * @param key - The key of the item to move.\n * @param toParentKey - The key of the new parent to insert into. `null` for the root.\n * @param index - The index within the new parent to insert at.\n */\n move(key: Key, toParentKey: Key | null, index: number): void;\n\n /**\n * Moves one or more items before a given key.\n *\n * @param key - The key of the item to move the items before.\n * @param keys - The keys of the items to move.\n */\n moveBefore(key: Key, keys: Iterable<Key>): void;\n\n /**\n * Moves one or more items after a given key.\n *\n * @param key - The key of the item to move the items after.\n * @param keys - The keys of the items to move.\n */\n moveAfter(key: Key, keys: Iterable<Key>): void;\n\n /**\n * Updates an item in the tree.\n *\n * @param key - The key of the item to update.\n * @param newValue - The new value for the item.\n */\n update(key: Key, newValue: T): void;\n}\n\ninterface TreeDataState<T extends object> {\n items: TreeNode<T>[];\n nodeMap: Map<Key, TreeNode<T>>;\n}\n\n/**\n * Manages state for an immutable tree data structure, and provides convenience methods to\n * update the data over time.\n */\nexport function useTreeData<T extends object>(options: TreeOptions<T>): TreeData<T> {\n let {\n initialItems = [],\n initialSelectedKeys,\n getKey = (item: any) => item.id ?? item.key,\n getChildren = (item: any) => item.children\n } = options;\n\n // We only want to compute this on initial render.\n let [tree, setItems] = useState<TreeDataState<T>>(() => buildTree(initialItems, new Map()));\n let {items, nodeMap} = tree;\n\n let [selectedKeys, setSelectedKeys] = useState(new Set<Key>(initialSelectedKeys || []));\n\n function buildTree(\n initialItems: T[] | null = [],\n map: Map<Key, TreeNode<T>>,\n parentKey?: Key | null\n ) {\n if (initialItems == null) {\n initialItems = [];\n }\n return {\n items: initialItems.map(item => {\n let node: TreeNode<T> = {\n key: getKey(item),\n parentKey: parentKey ?? null,\n value: item,\n children: null\n };\n\n node.children = buildTree(getChildren(item), map, node.key).items;\n map.set(node.key, node);\n return node;\n }),\n nodeMap: map\n };\n }\n\n function updateTree(\n items: TreeNode<T>[],\n key: Key | null,\n update: (node: TreeNode<T>) => TreeNode<T> | null,\n originalMap: Map<Key, TreeNode<T>>\n ) {\n let node = key == null ? null : originalMap.get(key);\n if (node == null) {\n return {items, nodeMap: originalMap};\n }\n let map = new Map<Key, TreeNode<T>>(originalMap);\n\n // Create a new node. If null, then delete the node, otherwise replace.\n let newNode = update(node);\n if (newNode == null) {\n deleteNode(node, map);\n } else {\n addNode(newNode, map);\n }\n\n // Walk up the tree and update each parent to refer to the new children.\n while (node && node.parentKey) {\n let nextParent = map.get(node.parentKey)!;\n let copy: TreeNode<T> = {\n key: nextParent.key,\n parentKey: nextParent.parentKey,\n value: nextParent.value,\n children: null\n };\n\n let children = nextParent.children;\n if (newNode == null && children) {\n children = children.filter(c => c !== node);\n }\n\n copy.children =\n children?.map(child => {\n if (child === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return child;\n }) ?? null;\n\n map.set(copy.key, copy);\n\n newNode = copy;\n node = nextParent;\n }\n\n if (newNode == null) {\n items = items.filter(c => c !== node);\n }\n\n return {\n items: items.map(item => {\n if (item === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return item;\n }),\n nodeMap: map\n };\n }\n\n function addNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.set(node.key, node);\n if (node.children) {\n for (let child of node.children) {\n addNode(child, map);\n }\n }\n }\n\n function deleteNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.delete(node.key);\n if (node.children) {\n for (let child of node.children) {\n deleteNode(child, map);\n }\n }\n }\n return {\n items,\n selectedKeys,\n setSelectedKeys,\n getItem(key: Key) {\n return nodeMap.get(key);\n },\n insert(parentKey: Key | null, index: number, ...values: T[]) {\n setItems(({items, nodeMap: originalMap}) => {\n let {items: newNodes, nodeMap: newMap} = buildTree(values, new Map(originalMap), parentKey);\n\n // If parentKey is null, insert into the root.\n if (parentKey == null) {\n return {\n items: [...items.slice(0, index), ...newNodes, ...items.slice(index)],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(\n items,\n parentKey,\n parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n ...newNodes,\n ...parentNode.children!.slice(index)\n ]\n }),\n newMap\n );\n });\n },\n insertBefore(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index, ...values);\n },\n insertAfter(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index + 1, ...values);\n },\n prepend(parentKey: Key | null, ...values: T[]) {\n this.insert(parentKey, 0, ...values);\n },\n append(parentKey: Key | null, ...values: T[]) {\n if (parentKey == null) {\n this.insert(null, items.length, ...values);\n } else {\n let parentNode = nodeMap.get(parentKey);\n if (!parentNode) {\n return;\n }\n\n this.insert(parentKey, parentNode.children!.length, ...values);\n }\n },\n remove(...keys: Key[]) {\n if (keys.length === 0) {\n return;\n }\n\n let newItems = items;\n let prevMap = nodeMap;\n let newTree;\n for (let key of keys) {\n newTree = updateTree(newItems, key, () => null, prevMap);\n prevMap = newTree.nodeMap;\n newItems = newTree.items;\n }\n\n setItems(newTree);\n\n let selection = new Set(selectedKeys);\n for (let key of selectedKeys) {\n if (!newTree.nodeMap.has(key)) {\n selection.delete(key);\n }\n }\n\n setSelectedKeys(selection);\n },\n removeSelectedItems() {\n this.remove(...selectedKeys);\n },\n move(key: Key, toParentKey: Key | null, index: number) {\n setItems(({items, nodeMap: originalMap}) => {\n let node = originalMap.get(key);\n if (!node) {\n return {items, nodeMap: originalMap};\n }\n\n let {items: newItems, nodeMap: newMap} = updateTree(items, key, () => null, originalMap);\n\n const movedNode = {\n ...node,\n parentKey: toParentKey\n };\n\n // If parentKey is null, insert into the root.\n if (toParentKey == null) {\n addNode(movedNode, newMap);\n return {\n items: [...newItems.slice(0, index), movedNode, ...newItems.slice(index)],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(\n newItems,\n toParentKey,\n parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n movedNode,\n ...parentNode.children!.slice(index)\n ]\n }),\n newMap\n );\n });\n },\n moveBefore(key: Key, keys: Iterable<Key>) {\n setItems(prevState => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n moveAfter(key: Key, keys: Iterable<Key>) {\n setItems(prevState => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n toIndex++;\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n update(oldKey: Key, newValue: T) {\n setItems(({items, nodeMap: originalMap}) =>\n updateTree(\n items,\n oldKey,\n oldNode => {\n let node: TreeNode<T> = {\n key: oldNode.key,\n parentKey: oldNode.parentKey,\n value: newValue,\n children: null\n };\n\n let tree = buildTree(getChildren(newValue), originalMap, node.key);\n node.children = tree.items;\n return node;\n },\n originalMap\n )\n );\n }\n };\n}\n\nfunction moveItems<T extends object>(\n state: TreeDataState<T>,\n keys: Iterable<Key>,\n toParent: TreeNode<T> | null,\n toIndex: number,\n updateTree: (\n items: TreeNode<T>[],\n key: Key | null,\n update: (node: TreeNode<T>) => TreeNode<T> | null,\n originalMap: Map<Key, TreeNode<T>>\n ) => TreeDataState<T>,\n addNode: (node: TreeNode<T>, map: Map<Key, TreeNode<T>>) => void\n): TreeDataState<T> {\n let {items, nodeMap} = state;\n\n let parent = toParent;\n let removeKeys = new Set(keys);\n while (parent?.parentKey != null) {\n if (removeKeys.has(parent.key)) {\n throw new Error('Cannot move an item to be a child of itself.');\n }\n parent = nodeMap.get(parent.parentKey!) ?? null;\n }\n\n let originalToIndex = toIndex;\n\n let keyArray = Array.isArray(keys) ? keys : [...keys];\n // depth first search to put keys in order\n let inOrderKeys: Map<Key, number> = new Map();\n let removedItems: Array<TreeNode<T>> = [];\n let newItems = items;\n let newMap = nodeMap;\n let i = 0;\n\n function traversal(node, {inorder, postorder}) {\n inorder?.(node);\n if (node != null) {\n for (let child of node.children ?? []) {\n traversal(child, {inorder, postorder});\n postorder?.(child);\n }\n }\n }\n\n function inorder(child) {\n // in-order so we add items as we encounter them in the tree, then we can insert them in expected order later\n if (keyArray.includes(child.key)) {\n inOrderKeys.set(child.key, i++);\n }\n }\n\n function postorder(child) {\n // remove items and update the tree from the leaves and work upwards toward the root, this way\n // we don't copy child node references from parents inadvertently\n if (keyArray.includes(child.key)) {\n removedItems.push({...newMap.get(child.key)!, parentKey: toParent?.key ?? null});\n let {items: nextItems, nodeMap: nextMap} = updateTree(\n newItems,\n child.key,\n () => null,\n newMap\n );\n newItems = nextItems;\n newMap = nextMap;\n }\n // decrement the index if the child being removed is in the target parent and before the target index\n // the root node is special, it is null, and will not have a key, however, a parentKey can still point to it\n if (\n (child.parentKey === toParent || child.parentKey === toParent?.key) &&\n keyArray.includes(child.key) &&\n (toParent?.children ? toParent.children.indexOf(child) : items.indexOf(child)) <\n originalToIndex\n ) {\n toIndex--;\n }\n }\n\n traversal({children: items}, {inorder, postorder});\n\n let inOrderItems = removedItems.sort((a, b) =>\n inOrderKeys.get(a.key)! > inOrderKeys.get(b.key)! ? 1 : -1\n );\n // If parentKey is null, insert into the root.\n if (!toParent || toParent.key == null) {\n inOrderItems.forEach(movedNode => {\n addNode(movedNode, newMap);\n });\n return {\n items: [...newItems.slice(0, toIndex), ...inOrderItems, ...newItems.slice(toIndex)],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(\n newItems,\n toParent.key,\n parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, toIndex),\n ...inOrderItems,\n ...parentNode.children!.slice(toIndex)\n ]\n }),\n newMap\n );\n}\n"],"names":[],"version":3,"file":"useTreeData.js.map"}
|
|
1
|
+
{"mappings":";;AAAA;;;;;;;;;;CAUC;AA6I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AS,GAAG,IAAI,MAAM;QACrC,aAAa,OAAO,CAAC,CAAA;YACnB,QAAQ,WAAW;QACrB;QACA,OAAO;YACL,OAAO;mBAAI,SAAS,KAAK,CAAC,GAAG;mBAAa;mBAAiB,SAAS,KAAK,CAAC;aAAS;YACnF,SAAS;QACX;IACF;IAEA,uDAAuD;IACvD,OAAO,WACL,UACA,SAAS,GAAG,EACZ,CAAA,aAAe,CAAA;YACb,KAAK,WAAW,GAAG;YACnB,WAAW,WAAW,SAAS;YAC/B,OAAO,WAAW,KAAK;YACvB,UAAU;mBACL,WAAW,QAAQ,CAAE,KAAK,CAAC,GAAG;mBAC9B;mBACA,WAAW,QAAQ,CAAE,KAAK,CAAC;aAC/B;QACH,CAAA,GACA;AAEJ","sources":["packages/react-stately/src/data/useTreeData.ts"],"sourcesContent":["/*\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\nimport {Key} from '@react-types/shared';\nimport {useState} from 'react';\n\nexport interface TreeOptions<T extends object> {\n /** Initial root items in the tree. */\n initialItems?: T[];\n /** The keys for the initially selected items. */\n initialSelectedKeys?: Iterable<Key>;\n /** A function that returns a unique key for an item object. */\n getKey?: (item: T) => Key;\n /** A function that returns the children for an item object. */\n getChildren?: (item: T) => T[];\n}\n\ninterface TreeNode<T extends object> {\n /** A unique key for the tree node. */\n key: Key;\n /** The key of the parent node. */\n parentKey?: Key | null;\n /** The value object for the tree node. */\n value: T;\n /** Children of the tree node. */\n children: TreeNode<T>[] | null;\n}\n\nexport interface TreeData<T extends object> {\n /** The root nodes in the tree. */\n items: TreeNode<T>[];\n\n /** The keys of the currently selected items in the tree. */\n selectedKeys: Set<Key>;\n\n /** Sets the selected keys. */\n setSelectedKeys(keys: Set<Key>): void;\n\n /**\n * Gets a node from the tree by key.\n *\n * @param key - The key of the item to retrieve.\n */\n getItem(key: Key): TreeNode<T> | undefined;\n\n /**\n * Inserts an item into a parent node as a child.\n *\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param index - The index within the parent to insert into.\n * @param value - The value to insert.\n */\n insert(parentKey: Key | null, index: number, ...values: T[]): void;\n\n /**\n * Inserts items into the list before the item at the given key.\n *\n * @param key - The key of the item to insert before.\n * @param values - The values to insert.\n */\n insertBefore(key: Key, ...values: T[]): void;\n\n /**\n * Inserts items into the list after the item at the given key.\n *\n * @param key - The key of the item to insert after.\n * @param values - The values to insert.\n */\n insertAfter(key: Key, ...values: T[]): void;\n\n /**\n * Appends an item into a parent node as a child.\n *\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n append(parentKey: Key | null, ...values: T[]): void;\n\n /**\n * Prepends an item into a parent node as a child.\n *\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n prepend(parentKey: Key | null, ...value: T[]): void;\n\n /**\n * Removes an item from the tree by its key.\n *\n * @param key - The key of the item to remove.\n */\n remove(...keys: Key[]): void;\n\n /**\n * Removes all items from the tree that are currently\n * in the set of selected items.\n */\n removeSelectedItems(): void;\n\n /**\n * Moves an item within the tree.\n *\n * @param key - The key of the item to move.\n * @param toParentKey - The key of the new parent to insert into. `null` for the root.\n * @param index - The index within the new parent to insert at.\n */\n move(key: Key, toParentKey: Key | null, index: number): void;\n\n /**\n * Moves one or more items before a given key.\n *\n * @param key - The key of the item to move the items before.\n * @param keys - The keys of the items to move.\n */\n moveBefore(key: Key, keys: Iterable<Key>): void;\n\n /**\n * Moves one or more items after a given key.\n *\n * @param key - The key of the item to move the items after.\n * @param keys - The keys of the items to move.\n */\n moveAfter(key: Key, keys: Iterable<Key>): void;\n\n /**\n * Updates an item in the tree.\n *\n * @param key - The key of the item to update.\n * @param newValue - The new value for the item.\n */\n update(key: Key, newValue: T): void;\n}\n\ninterface TreeDataState<T extends object> {\n items: TreeNode<T>[];\n nodeMap: Map<Key, TreeNode<T>>;\n}\n\n/**\n * Manages state for an immutable tree data structure, and provides convenience methods to\n * update the data over time.\n */\nexport function useTreeData<T extends object>(options: TreeOptions<T>): TreeData<T> {\n let {\n initialItems = [],\n initialSelectedKeys,\n getKey = (item: any) => item.id ?? item.key,\n getChildren = (item: any) => item.children\n } = options;\n\n // We only want to compute this on initial render.\n let [tree, setItems] = useState<TreeDataState<T>>(() => buildTree(initialItems, new Map()));\n let {items, nodeMap} = tree;\n\n let [selectedKeys, setSelectedKeys] = useState(new Set<Key>(initialSelectedKeys || []));\n\n // oxlint-disable-next-line react/react-compiler\n function buildTree(\n initialItems: T[] | null = [],\n map: Map<Key, TreeNode<T>>,\n parentKey?: Key | null\n ) {\n if (initialItems == null) {\n initialItems = [];\n }\n return {\n items: initialItems.map(item => {\n let node: TreeNode<T> = {\n key: getKey(item),\n parentKey: parentKey ?? null,\n value: item,\n children: null\n };\n\n node.children = buildTree(getChildren(item), map, node.key).items;\n map.set(node.key, node);\n return node;\n }),\n nodeMap: map\n };\n }\n\n function updateTree(\n items: TreeNode<T>[],\n key: Key | null,\n update: (node: TreeNode<T>) => TreeNode<T> | null,\n originalMap: Map<Key, TreeNode<T>>\n ) {\n let node = key == null ? null : originalMap.get(key);\n if (node == null) {\n return {items, nodeMap: originalMap};\n }\n let map = new Map<Key, TreeNode<T>>(originalMap);\n\n // Create a new node. If null, then delete the node, otherwise replace.\n let newNode = update(node);\n if (newNode == null) {\n deleteNode(node, map);\n } else {\n addNode(newNode, map);\n }\n\n // Walk up the tree and update each parent to refer to the new children.\n while (node && node.parentKey) {\n let nextParent = map.get(node.parentKey)!;\n let copy: TreeNode<T> = {\n key: nextParent.key,\n parentKey: nextParent.parentKey,\n value: nextParent.value,\n children: null\n };\n\n let children = nextParent.children;\n if (newNode == null && children) {\n children = children.filter(c => c !== node);\n }\n\n copy.children =\n children?.map(child => {\n if (child === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return child;\n }) ?? null;\n\n map.set(copy.key, copy);\n\n newNode = copy;\n node = nextParent;\n }\n\n if (newNode == null) {\n items = items.filter(c => c !== node);\n }\n\n return {\n items: items.map(item => {\n if (item === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return item;\n }),\n nodeMap: map\n };\n }\n\n function addNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.set(node.key, node);\n if (node.children) {\n for (let child of node.children) {\n addNode(child, map);\n }\n }\n }\n\n function deleteNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.delete(node.key);\n if (node.children) {\n for (let child of node.children) {\n deleteNode(child, map);\n }\n }\n }\n return {\n items,\n selectedKeys,\n setSelectedKeys,\n getItem(key: Key) {\n return nodeMap.get(key);\n },\n insert(parentKey: Key | null, index: number, ...values: T[]) {\n setItems(({items, nodeMap: originalMap}) => {\n let {items: newNodes, nodeMap: newMap} = buildTree(values, new Map(originalMap), parentKey);\n\n // If parentKey is null, insert into the root.\n if (parentKey == null) {\n return {\n items: [...items.slice(0, index), ...newNodes, ...items.slice(index)],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(\n items,\n parentKey,\n parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n ...newNodes,\n ...parentNode.children!.slice(index)\n ]\n }),\n newMap\n );\n });\n },\n insertBefore(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index, ...values);\n },\n insertAfter(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index + 1, ...values);\n },\n prepend(parentKey: Key | null, ...values: T[]) {\n this.insert(parentKey, 0, ...values);\n },\n append(parentKey: Key | null, ...values: T[]) {\n if (parentKey == null) {\n this.insert(null, items.length, ...values);\n } else {\n let parentNode = nodeMap.get(parentKey);\n if (!parentNode) {\n return;\n }\n\n this.insert(parentKey, parentNode.children!.length, ...values);\n }\n },\n remove(...keys: Key[]) {\n if (keys.length === 0) {\n return;\n }\n\n let newItems = items;\n let prevMap = nodeMap;\n let newTree;\n for (let key of keys) {\n newTree = updateTree(newItems, key, () => null, prevMap);\n prevMap = newTree.nodeMap;\n newItems = newTree.items;\n }\n\n setItems(newTree);\n\n let selection = new Set(selectedKeys);\n for (let key of selectedKeys) {\n if (!newTree.nodeMap.has(key)) {\n selection.delete(key);\n }\n }\n\n setSelectedKeys(selection);\n },\n removeSelectedItems() {\n this.remove(...selectedKeys);\n },\n move(key: Key, toParentKey: Key | null, index: number) {\n setItems(({items, nodeMap: originalMap}) => {\n let node = originalMap.get(key);\n if (!node) {\n return {items, nodeMap: originalMap};\n }\n\n let {items: newItems, nodeMap: newMap} = updateTree(items, key, () => null, originalMap);\n\n const movedNode = {\n ...node,\n parentKey: toParentKey\n };\n\n // If parentKey is null, insert into the root.\n if (toParentKey == null) {\n addNode(movedNode, newMap);\n return {\n items: [...newItems.slice(0, index), movedNode, ...newItems.slice(index)],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(\n newItems,\n toParentKey,\n parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n movedNode,\n ...parentNode.children!.slice(index)\n ]\n }),\n newMap\n );\n });\n },\n moveBefore(key: Key, keys: Iterable<Key>) {\n setItems(prevState => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n moveAfter(key: Key, keys: Iterable<Key>) {\n setItems(prevState => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n toIndex++;\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n update(oldKey: Key, newValue: T) {\n setItems(({items, nodeMap: originalMap}) =>\n updateTree(\n items,\n oldKey,\n oldNode => {\n let node: TreeNode<T> = {\n key: oldNode.key,\n parentKey: oldNode.parentKey,\n value: newValue,\n children: null\n };\n\n let tree = buildTree(getChildren(newValue), originalMap, node.key);\n node.children = tree.items;\n return node;\n },\n originalMap\n )\n );\n }\n };\n}\n\nfunction moveItems<T extends object>(\n state: TreeDataState<T>,\n keys: Iterable<Key>,\n toParent: TreeNode<T> | null,\n toIndex: number,\n updateTree: (\n items: TreeNode<T>[],\n key: Key | null,\n update: (node: TreeNode<T>) => TreeNode<T> | null,\n originalMap: Map<Key, TreeNode<T>>\n ) => TreeDataState<T>,\n addNode: (node: TreeNode<T>, map: Map<Key, TreeNode<T>>) => void\n): TreeDataState<T> {\n let {items, nodeMap} = state;\n\n let parent = toParent;\n let removeKeys = new Set(keys);\n while (parent?.parentKey != null) {\n if (removeKeys.has(parent.key)) {\n throw new Error('Cannot move an item to be a child of itself.');\n }\n parent = nodeMap.get(parent.parentKey!) ?? null;\n }\n\n let originalToIndex = toIndex;\n\n let keyArray = Array.isArray(keys) ? keys : [...keys];\n // depth first search to put keys in order\n let inOrderKeys: Map<Key, number> = new Map();\n let removedItems: Array<TreeNode<T>> = [];\n let newItems = items;\n let newMap = nodeMap;\n let i = 0;\n\n function traversal(node, {inorder, postorder}) {\n inorder?.(node);\n if (node != null) {\n for (let child of node.children ?? []) {\n traversal(child, {inorder, postorder});\n postorder?.(child);\n }\n }\n }\n\n function inorder(child) {\n // in-order so we add items as we encounter them in the tree, then we can insert them in expected order later\n if (keyArray.includes(child.key)) {\n inOrderKeys.set(child.key, i++);\n }\n }\n\n function postorder(child) {\n // remove items and update the tree from the leaves and work upwards toward the root, this way\n // we don't copy child node references from parents inadvertently\n if (keyArray.includes(child.key)) {\n removedItems.push({...newMap.get(child.key)!, parentKey: toParent?.key ?? null});\n let {items: nextItems, nodeMap: nextMap} = updateTree(\n newItems,\n child.key,\n () => null,\n newMap\n );\n newItems = nextItems;\n newMap = nextMap;\n }\n // decrement the index if the child being removed is in the target parent and before the target index\n // the root node is special, it is null, and will not have a key, however, a parentKey can still point to it\n if (\n (child.parentKey === toParent || child.parentKey === toParent?.key) &&\n keyArray.includes(child.key) &&\n (toParent?.children ? toParent.children.indexOf(child) : items.indexOf(child)) <\n originalToIndex\n ) {\n toIndex--;\n }\n }\n\n traversal({children: items}, {inorder, postorder});\n\n let inOrderItems = removedItems.sort((a, b) =>\n inOrderKeys.get(a.key)! > inOrderKeys.get(b.key)! ? 1 : -1\n );\n // If parentKey is null, insert into the root.\n if (!toParent || toParent.key == null) {\n inOrderItems.forEach(movedNode => {\n addNode(movedNode, newMap);\n });\n return {\n items: [...newItems.slice(0, toIndex), ...inOrderItems, ...newItems.slice(toIndex)],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(\n newItems,\n toParent.key,\n parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, toIndex),\n ...inOrderItems,\n ...parentNode.children!.slice(toIndex)\n ]\n }),\n newMap\n );\n}\n"],"names":[],"version":3,"file":"useTreeData.js.map"}
|
|
@@ -17,6 +17,7 @@ function $48fd4b7af0dd022e$export$d14e1352e21f4a16(options) {
|
|
|
17
17
|
let [tree, setItems] = (0, $d8tpy$useState)(()=>buildTree(initialItems, new Map()));
|
|
18
18
|
let { items: items, nodeMap: nodeMap } = tree;
|
|
19
19
|
let [selectedKeys, setSelectedKeys] = (0, $d8tpy$useState)(new Set(initialSelectedKeys || []));
|
|
20
|
+
// oxlint-disable-next-line react/react-compiler
|
|
20
21
|
function buildTree(initialItems = [], map, parentKey) {
|
|
21
22
|
if (initialItems == null) initialItems = [];
|
|
22
23
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;AAAA;;;;;;;;;;CAUC;AA6IM,SAAS,0CAA8B,OAAuB;IACnE,IAAI,gBACF,eAAe,EAAE,uBACjB,mBAAmB,UACnB,SAAS,CAAC,OAAc,KAAK,EAAE,IAAI,KAAK,GAAG,eAC3C,cAAc,CAAC,OAAc,KAAK,QAAQ,EAC3C,GAAG;IAEJ,kDAAkD;IAClD,IAAI,CAAC,MAAM,SAAS,GAAG,CAAA,GAAA,eAAO,EAAoB,IAAM,UAAU,cAAc,IAAI;IACpF,IAAI,SAAC,KAAK,WAAE,OAAO,EAAC,GAAG;IAEvB,IAAI,CAAC,cAAc,gBAAgB,GAAG,CAAA,GAAA,eAAO,EAAE,IAAI,IAAS,uBAAuB,EAAE;IAErF,SAAS,UACP,eAA2B,EAAE,EAC7B,GAA0B,EAC1B,SAAsB;QAEtB,IAAI,gBAAgB,MAClB,eAAe,EAAE;QAEnB,OAAO;YACL,OAAO,aAAa,GAAG,CAAC,CAAA;gBACtB,IAAI,OAAoB;oBACtB,KAAK,OAAO;oBACZ,WAAW,aAAa;oBACxB,OAAO;oBACP,UAAU;gBACZ;gBAEA,KAAK,QAAQ,GAAG,UAAU,YAAY,OAAO,KAAK,KAA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;mBAAa;mBAAiB,SAAS,KAAK,CAAC;aAAS;YACnF,SAAS;QACX;IACF;IAEA,uDAAuD;IACvD,OAAO,WACL,UACA,SAAS,GAAG,EACZ,CAAA,aAAe,CAAA;YACb,KAAK,WAAW,GAAG;YACnB,WAAW,WAAW,SAAS;YAC/B,OAAO,WAAW,KAAK;YACvB,UAAU;mBACL,WAAW,QAAQ,CAAE,KAAK,CAAC,GAAG;mBAC9B;mBACA,WAAW,QAAQ,CAAE,KAAK,CAAC;aAC/B;QACH,CAAA,GACA;AAEJ","sources":["packages/react-stately/src/data/useTreeData.ts"],"sourcesContent":["/*\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\nimport {Key} from '@react-types/shared';\nimport {useState} from 'react';\n\nexport interface TreeOptions<T extends object> {\n /** Initial root items in the tree. */\n initialItems?: T[];\n /** The keys for the initially selected items. */\n initialSelectedKeys?: Iterable<Key>;\n /** A function that returns a unique key for an item object. */\n getKey?: (item: T) => Key;\n /** A function that returns the children for an item object. */\n getChildren?: (item: T) => T[];\n}\n\ninterface TreeNode<T extends object> {\n /** A unique key for the tree node. */\n key: Key;\n /** The key of the parent node. */\n parentKey?: Key | null;\n /** The value object for the tree node. */\n value: T;\n /** Children of the tree node. */\n children: TreeNode<T>[] | null;\n}\n\nexport interface TreeData<T extends object> {\n /** The root nodes in the tree. */\n items: TreeNode<T>[];\n\n /** The keys of the currently selected items in the tree. */\n selectedKeys: Set<Key>;\n\n /** Sets the selected keys. */\n setSelectedKeys(keys: Set<Key>): void;\n\n /**\n * Gets a node from the tree by key.\n *\n * @param key - The key of the item to retrieve.\n */\n getItem(key: Key): TreeNode<T> | undefined;\n\n /**\n * Inserts an item into a parent node as a child.\n *\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param index - The index within the parent to insert into.\n * @param value - The value to insert.\n */\n insert(parentKey: Key | null, index: number, ...values: T[]): void;\n\n /**\n * Inserts items into the list before the item at the given key.\n *\n * @param key - The key of the item to insert before.\n * @param values - The values to insert.\n */\n insertBefore(key: Key, ...values: T[]): void;\n\n /**\n * Inserts items into the list after the item at the given key.\n *\n * @param key - The key of the item to insert after.\n * @param values - The values to insert.\n */\n insertAfter(key: Key, ...values: T[]): void;\n\n /**\n * Appends an item into a parent node as a child.\n *\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n append(parentKey: Key | null, ...values: T[]): void;\n\n /**\n * Prepends an item into a parent node as a child.\n *\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n prepend(parentKey: Key | null, ...value: T[]): void;\n\n /**\n * Removes an item from the tree by its key.\n *\n * @param key - The key of the item to remove.\n */\n remove(...keys: Key[]): void;\n\n /**\n * Removes all items from the tree that are currently\n * in the set of selected items.\n */\n removeSelectedItems(): void;\n\n /**\n * Moves an item within the tree.\n *\n * @param key - The key of the item to move.\n * @param toParentKey - The key of the new parent to insert into. `null` for the root.\n * @param index - The index within the new parent to insert at.\n */\n move(key: Key, toParentKey: Key | null, index: number): void;\n\n /**\n * Moves one or more items before a given key.\n *\n * @param key - The key of the item to move the items before.\n * @param keys - The keys of the items to move.\n */\n moveBefore(key: Key, keys: Iterable<Key>): void;\n\n /**\n * Moves one or more items after a given key.\n *\n * @param key - The key of the item to move the items after.\n * @param keys - The keys of the items to move.\n */\n moveAfter(key: Key, keys: Iterable<Key>): void;\n\n /**\n * Updates an item in the tree.\n *\n * @param key - The key of the item to update.\n * @param newValue - The new value for the item.\n */\n update(key: Key, newValue: T): void;\n}\n\ninterface TreeDataState<T extends object> {\n items: TreeNode<T>[];\n nodeMap: Map<Key, TreeNode<T>>;\n}\n\n/**\n * Manages state for an immutable tree data structure, and provides convenience methods to\n * update the data over time.\n */\nexport function useTreeData<T extends object>(options: TreeOptions<T>): TreeData<T> {\n let {\n initialItems = [],\n initialSelectedKeys,\n getKey = (item: any) => item.id ?? item.key,\n getChildren = (item: any) => item.children\n } = options;\n\n // We only want to compute this on initial render.\n let [tree, setItems] = useState<TreeDataState<T>>(() => buildTree(initialItems, new Map()));\n let {items, nodeMap} = tree;\n\n let [selectedKeys, setSelectedKeys] = useState(new Set<Key>(initialSelectedKeys || []));\n\n function buildTree(\n initialItems: T[] | null = [],\n map: Map<Key, TreeNode<T>>,\n parentKey?: Key | null\n ) {\n if (initialItems == null) {\n initialItems = [];\n }\n return {\n items: initialItems.map(item => {\n let node: TreeNode<T> = {\n key: getKey(item),\n parentKey: parentKey ?? null,\n value: item,\n children: null\n };\n\n node.children = buildTree(getChildren(item), map, node.key).items;\n map.set(node.key, node);\n return node;\n }),\n nodeMap: map\n };\n }\n\n function updateTree(\n items: TreeNode<T>[],\n key: Key | null,\n update: (node: TreeNode<T>) => TreeNode<T> | null,\n originalMap: Map<Key, TreeNode<T>>\n ) {\n let node = key == null ? null : originalMap.get(key);\n if (node == null) {\n return {items, nodeMap: originalMap};\n }\n let map = new Map<Key, TreeNode<T>>(originalMap);\n\n // Create a new node. If null, then delete the node, otherwise replace.\n let newNode = update(node);\n if (newNode == null) {\n deleteNode(node, map);\n } else {\n addNode(newNode, map);\n }\n\n // Walk up the tree and update each parent to refer to the new children.\n while (node && node.parentKey) {\n let nextParent = map.get(node.parentKey)!;\n let copy: TreeNode<T> = {\n key: nextParent.key,\n parentKey: nextParent.parentKey,\n value: nextParent.value,\n children: null\n };\n\n let children = nextParent.children;\n if (newNode == null && children) {\n children = children.filter(c => c !== node);\n }\n\n copy.children =\n children?.map(child => {\n if (child === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return child;\n }) ?? null;\n\n map.set(copy.key, copy);\n\n newNode = copy;\n node = nextParent;\n }\n\n if (newNode == null) {\n items = items.filter(c => c !== node);\n }\n\n return {\n items: items.map(item => {\n if (item === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return item;\n }),\n nodeMap: map\n };\n }\n\n function addNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.set(node.key, node);\n if (node.children) {\n for (let child of node.children) {\n addNode(child, map);\n }\n }\n }\n\n function deleteNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.delete(node.key);\n if (node.children) {\n for (let child of node.children) {\n deleteNode(child, map);\n }\n }\n }\n return {\n items,\n selectedKeys,\n setSelectedKeys,\n getItem(key: Key) {\n return nodeMap.get(key);\n },\n insert(parentKey: Key | null, index: number, ...values: T[]) {\n setItems(({items, nodeMap: originalMap}) => {\n let {items: newNodes, nodeMap: newMap} = buildTree(values, new Map(originalMap), parentKey);\n\n // If parentKey is null, insert into the root.\n if (parentKey == null) {\n return {\n items: [...items.slice(0, index), ...newNodes, ...items.slice(index)],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(\n items,\n parentKey,\n parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n ...newNodes,\n ...parentNode.children!.slice(index)\n ]\n }),\n newMap\n );\n });\n },\n insertBefore(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index, ...values);\n },\n insertAfter(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index + 1, ...values);\n },\n prepend(parentKey: Key | null, ...values: T[]) {\n this.insert(parentKey, 0, ...values);\n },\n append(parentKey: Key | null, ...values: T[]) {\n if (parentKey == null) {\n this.insert(null, items.length, ...values);\n } else {\n let parentNode = nodeMap.get(parentKey);\n if (!parentNode) {\n return;\n }\n\n this.insert(parentKey, parentNode.children!.length, ...values);\n }\n },\n remove(...keys: Key[]) {\n if (keys.length === 0) {\n return;\n }\n\n let newItems = items;\n let prevMap = nodeMap;\n let newTree;\n for (let key of keys) {\n newTree = updateTree(newItems, key, () => null, prevMap);\n prevMap = newTree.nodeMap;\n newItems = newTree.items;\n }\n\n setItems(newTree);\n\n let selection = new Set(selectedKeys);\n for (let key of selectedKeys) {\n if (!newTree.nodeMap.has(key)) {\n selection.delete(key);\n }\n }\n\n setSelectedKeys(selection);\n },\n removeSelectedItems() {\n this.remove(...selectedKeys);\n },\n move(key: Key, toParentKey: Key | null, index: number) {\n setItems(({items, nodeMap: originalMap}) => {\n let node = originalMap.get(key);\n if (!node) {\n return {items, nodeMap: originalMap};\n }\n\n let {items: newItems, nodeMap: newMap} = updateTree(items, key, () => null, originalMap);\n\n const movedNode = {\n ...node,\n parentKey: toParentKey\n };\n\n // If parentKey is null, insert into the root.\n if (toParentKey == null) {\n addNode(movedNode, newMap);\n return {\n items: [...newItems.slice(0, index), movedNode, ...newItems.slice(index)],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(\n newItems,\n toParentKey,\n parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n movedNode,\n ...parentNode.children!.slice(index)\n ]\n }),\n newMap\n );\n });\n },\n moveBefore(key: Key, keys: Iterable<Key>) {\n setItems(prevState => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n moveAfter(key: Key, keys: Iterable<Key>) {\n setItems(prevState => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n toIndex++;\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n update(oldKey: Key, newValue: T) {\n setItems(({items, nodeMap: originalMap}) =>\n updateTree(\n items,\n oldKey,\n oldNode => {\n let node: TreeNode<T> = {\n key: oldNode.key,\n parentKey: oldNode.parentKey,\n value: newValue,\n children: null\n };\n\n let tree = buildTree(getChildren(newValue), originalMap, node.key);\n node.children = tree.items;\n return node;\n },\n originalMap\n )\n );\n }\n };\n}\n\nfunction moveItems<T extends object>(\n state: TreeDataState<T>,\n keys: Iterable<Key>,\n toParent: TreeNode<T> | null,\n toIndex: number,\n updateTree: (\n items: TreeNode<T>[],\n key: Key | null,\n update: (node: TreeNode<T>) => TreeNode<T> | null,\n originalMap: Map<Key, TreeNode<T>>\n ) => TreeDataState<T>,\n addNode: (node: TreeNode<T>, map: Map<Key, TreeNode<T>>) => void\n): TreeDataState<T> {\n let {items, nodeMap} = state;\n\n let parent = toParent;\n let removeKeys = new Set(keys);\n while (parent?.parentKey != null) {\n if (removeKeys.has(parent.key)) {\n throw new Error('Cannot move an item to be a child of itself.');\n }\n parent = nodeMap.get(parent.parentKey!) ?? null;\n }\n\n let originalToIndex = toIndex;\n\n let keyArray = Array.isArray(keys) ? keys : [...keys];\n // depth first search to put keys in order\n let inOrderKeys: Map<Key, number> = new Map();\n let removedItems: Array<TreeNode<T>> = [];\n let newItems = items;\n let newMap = nodeMap;\n let i = 0;\n\n function traversal(node, {inorder, postorder}) {\n inorder?.(node);\n if (node != null) {\n for (let child of node.children ?? []) {\n traversal(child, {inorder, postorder});\n postorder?.(child);\n }\n }\n }\n\n function inorder(child) {\n // in-order so we add items as we encounter them in the tree, then we can insert them in expected order later\n if (keyArray.includes(child.key)) {\n inOrderKeys.set(child.key, i++);\n }\n }\n\n function postorder(child) {\n // remove items and update the tree from the leaves and work upwards toward the root, this way\n // we don't copy child node references from parents inadvertently\n if (keyArray.includes(child.key)) {\n removedItems.push({...newMap.get(child.key)!, parentKey: toParent?.key ?? null});\n let {items: nextItems, nodeMap: nextMap} = updateTree(\n newItems,\n child.key,\n () => null,\n newMap\n );\n newItems = nextItems;\n newMap = nextMap;\n }\n // decrement the index if the child being removed is in the target parent and before the target index\n // the root node is special, it is null, and will not have a key, however, a parentKey can still point to it\n if (\n (child.parentKey === toParent || child.parentKey === toParent?.key) &&\n keyArray.includes(child.key) &&\n (toParent?.children ? toParent.children.indexOf(child) : items.indexOf(child)) <\n originalToIndex\n ) {\n toIndex--;\n }\n }\n\n traversal({children: items}, {inorder, postorder});\n\n let inOrderItems = removedItems.sort((a, b) =>\n inOrderKeys.get(a.key)! > inOrderKeys.get(b.key)! ? 1 : -1\n );\n // If parentKey is null, insert into the root.\n if (!toParent || toParent.key == null) {\n inOrderItems.forEach(movedNode => {\n addNode(movedNode, newMap);\n });\n return {\n items: [...newItems.slice(0, toIndex), ...inOrderItems, ...newItems.slice(toIndex)],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(\n newItems,\n toParent.key,\n parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, toIndex),\n ...inOrderItems,\n ...parentNode.children!.slice(toIndex)\n ]\n }),\n newMap\n );\n}\n"],"names":[],"version":3,"file":"useTreeData.mjs.map"}
|
|
1
|
+
{"mappings":";;AAAA;;;;;;;;;;CAUC;AA6IM,SAAS,0CAA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AAS,OAAO,EAAC,GAAG,WACzC,UACA,MAAM,GAAG,EACT,IAAM,MACN;YAEF,WAAW;YACX,SAAS;QACX;QACA,qGAAqG;QACrG,4GAA4G;QAC5G,IACE,AAAC,CAAA,MAAM,SAAS,KAAK,YAAY,MAAM,SAAS,KAAK,UAAU,GAAE,KACjE,SAAS,QAAQ,CAAC,MAAM,GAAG,KAC3B,AAAC,CAAA,UAAU,WAAW,SAAS,QAAQ,CAAC,OAAO,CAAC,SAAS,MAAM,OAAO,CAAC,MAAK,IAC1E,iBAEF;IAEJ;IAEA,UAAU;QAAC,UAAU;IAAK,GAAG;iBAAC;mBAAS;IAAS;IAEhD,IAAI,eAAe,aAAa,IAAI,CAAC,CAAC,GAAG,IACvC,YAAY,GAAG,CAAC,EAAE,GAAG,IAAK,YAAY,GAAG,CAAC,EAAE,GAAG,IAAK,IAAI;IAE1D,8CAA8C;IAC9C,IAAI,CAAC,YAAY,SAAS,GAAG,IAAI,MAAM;QACrC,aAAa,OAAO,CAAC,CAAA;YACnB,QAAQ,WAAW;QACrB;QACA,OAAO;YACL,OAAO;mBAAI,SAAS,KAAK,CAAC,GAAG;mBAAa;mBAAiB,SAAS,KAAK,CAAC;aAAS;YACnF,SAAS;QACX;IACF;IAEA,uDAAuD;IACvD,OAAO,WACL,UACA,SAAS,GAAG,EACZ,CAAA,aAAe,CAAA;YACb,KAAK,WAAW,GAAG;YACnB,WAAW,WAAW,SAAS;YAC/B,OAAO,WAAW,KAAK;YACvB,UAAU;mBACL,WAAW,QAAQ,CAAE,KAAK,CAAC,GAAG;mBAC9B;mBACA,WAAW,QAAQ,CAAE,KAAK,CAAC;aAC/B;QACH,CAAA,GACA;AAEJ","sources":["packages/react-stately/src/data/useTreeData.ts"],"sourcesContent":["/*\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\nimport {Key} from '@react-types/shared';\nimport {useState} from 'react';\n\nexport interface TreeOptions<T extends object> {\n /** Initial root items in the tree. */\n initialItems?: T[];\n /** The keys for the initially selected items. */\n initialSelectedKeys?: Iterable<Key>;\n /** A function that returns a unique key for an item object. */\n getKey?: (item: T) => Key;\n /** A function that returns the children for an item object. */\n getChildren?: (item: T) => T[];\n}\n\ninterface TreeNode<T extends object> {\n /** A unique key for the tree node. */\n key: Key;\n /** The key of the parent node. */\n parentKey?: Key | null;\n /** The value object for the tree node. */\n value: T;\n /** Children of the tree node. */\n children: TreeNode<T>[] | null;\n}\n\nexport interface TreeData<T extends object> {\n /** The root nodes in the tree. */\n items: TreeNode<T>[];\n\n /** The keys of the currently selected items in the tree. */\n selectedKeys: Set<Key>;\n\n /** Sets the selected keys. */\n setSelectedKeys(keys: Set<Key>): void;\n\n /**\n * Gets a node from the tree by key.\n *\n * @param key - The key of the item to retrieve.\n */\n getItem(key: Key): TreeNode<T> | undefined;\n\n /**\n * Inserts an item into a parent node as a child.\n *\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param index - The index within the parent to insert into.\n * @param value - The value to insert.\n */\n insert(parentKey: Key | null, index: number, ...values: T[]): void;\n\n /**\n * Inserts items into the list before the item at the given key.\n *\n * @param key - The key of the item to insert before.\n * @param values - The values to insert.\n */\n insertBefore(key: Key, ...values: T[]): void;\n\n /**\n * Inserts items into the list after the item at the given key.\n *\n * @param key - The key of the item to insert after.\n * @param values - The values to insert.\n */\n insertAfter(key: Key, ...values: T[]): void;\n\n /**\n * Appends an item into a parent node as a child.\n *\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n append(parentKey: Key | null, ...values: T[]): void;\n\n /**\n * Prepends an item into a parent node as a child.\n *\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n prepend(parentKey: Key | null, ...value: T[]): void;\n\n /**\n * Removes an item from the tree by its key.\n *\n * @param key - The key of the item to remove.\n */\n remove(...keys: Key[]): void;\n\n /**\n * Removes all items from the tree that are currently\n * in the set of selected items.\n */\n removeSelectedItems(): void;\n\n /**\n * Moves an item within the tree.\n *\n * @param key - The key of the item to move.\n * @param toParentKey - The key of the new parent to insert into. `null` for the root.\n * @param index - The index within the new parent to insert at.\n */\n move(key: Key, toParentKey: Key | null, index: number): void;\n\n /**\n * Moves one or more items before a given key.\n *\n * @param key - The key of the item to move the items before.\n * @param keys - The keys of the items to move.\n */\n moveBefore(key: Key, keys: Iterable<Key>): void;\n\n /**\n * Moves one or more items after a given key.\n *\n * @param key - The key of the item to move the items after.\n * @param keys - The keys of the items to move.\n */\n moveAfter(key: Key, keys: Iterable<Key>): void;\n\n /**\n * Updates an item in the tree.\n *\n * @param key - The key of the item to update.\n * @param newValue - The new value for the item.\n */\n update(key: Key, newValue: T): void;\n}\n\ninterface TreeDataState<T extends object> {\n items: TreeNode<T>[];\n nodeMap: Map<Key, TreeNode<T>>;\n}\n\n/**\n * Manages state for an immutable tree data structure, and provides convenience methods to\n * update the data over time.\n */\nexport function useTreeData<T extends object>(options: TreeOptions<T>): TreeData<T> {\n let {\n initialItems = [],\n initialSelectedKeys,\n getKey = (item: any) => item.id ?? item.key,\n getChildren = (item: any) => item.children\n } = options;\n\n // We only want to compute this on initial render.\n let [tree, setItems] = useState<TreeDataState<T>>(() => buildTree(initialItems, new Map()));\n let {items, nodeMap} = tree;\n\n let [selectedKeys, setSelectedKeys] = useState(new Set<Key>(initialSelectedKeys || []));\n\n // oxlint-disable-next-line react/react-compiler\n function buildTree(\n initialItems: T[] | null = [],\n map: Map<Key, TreeNode<T>>,\n parentKey?: Key | null\n ) {\n if (initialItems == null) {\n initialItems = [];\n }\n return {\n items: initialItems.map(item => {\n let node: TreeNode<T> = {\n key: getKey(item),\n parentKey: parentKey ?? null,\n value: item,\n children: null\n };\n\n node.children = buildTree(getChildren(item), map, node.key).items;\n map.set(node.key, node);\n return node;\n }),\n nodeMap: map\n };\n }\n\n function updateTree(\n items: TreeNode<T>[],\n key: Key | null,\n update: (node: TreeNode<T>) => TreeNode<T> | null,\n originalMap: Map<Key, TreeNode<T>>\n ) {\n let node = key == null ? null : originalMap.get(key);\n if (node == null) {\n return {items, nodeMap: originalMap};\n }\n let map = new Map<Key, TreeNode<T>>(originalMap);\n\n // Create a new node. If null, then delete the node, otherwise replace.\n let newNode = update(node);\n if (newNode == null) {\n deleteNode(node, map);\n } else {\n addNode(newNode, map);\n }\n\n // Walk up the tree and update each parent to refer to the new children.\n while (node && node.parentKey) {\n let nextParent = map.get(node.parentKey)!;\n let copy: TreeNode<T> = {\n key: nextParent.key,\n parentKey: nextParent.parentKey,\n value: nextParent.value,\n children: null\n };\n\n let children = nextParent.children;\n if (newNode == null && children) {\n children = children.filter(c => c !== node);\n }\n\n copy.children =\n children?.map(child => {\n if (child === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return child;\n }) ?? null;\n\n map.set(copy.key, copy);\n\n newNode = copy;\n node = nextParent;\n }\n\n if (newNode == null) {\n items = items.filter(c => c !== node);\n }\n\n return {\n items: items.map(item => {\n if (item === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return item;\n }),\n nodeMap: map\n };\n }\n\n function addNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.set(node.key, node);\n if (node.children) {\n for (let child of node.children) {\n addNode(child, map);\n }\n }\n }\n\n function deleteNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.delete(node.key);\n if (node.children) {\n for (let child of node.children) {\n deleteNode(child, map);\n }\n }\n }\n return {\n items,\n selectedKeys,\n setSelectedKeys,\n getItem(key: Key) {\n return nodeMap.get(key);\n },\n insert(parentKey: Key | null, index: number, ...values: T[]) {\n setItems(({items, nodeMap: originalMap}) => {\n let {items: newNodes, nodeMap: newMap} = buildTree(values, new Map(originalMap), parentKey);\n\n // If parentKey is null, insert into the root.\n if (parentKey == null) {\n return {\n items: [...items.slice(0, index), ...newNodes, ...items.slice(index)],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(\n items,\n parentKey,\n parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n ...newNodes,\n ...parentNode.children!.slice(index)\n ]\n }),\n newMap\n );\n });\n },\n insertBefore(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index, ...values);\n },\n insertAfter(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index + 1, ...values);\n },\n prepend(parentKey: Key | null, ...values: T[]) {\n this.insert(parentKey, 0, ...values);\n },\n append(parentKey: Key | null, ...values: T[]) {\n if (parentKey == null) {\n this.insert(null, items.length, ...values);\n } else {\n let parentNode = nodeMap.get(parentKey);\n if (!parentNode) {\n return;\n }\n\n this.insert(parentKey, parentNode.children!.length, ...values);\n }\n },\n remove(...keys: Key[]) {\n if (keys.length === 0) {\n return;\n }\n\n let newItems = items;\n let prevMap = nodeMap;\n let newTree;\n for (let key of keys) {\n newTree = updateTree(newItems, key, () => null, prevMap);\n prevMap = newTree.nodeMap;\n newItems = newTree.items;\n }\n\n setItems(newTree);\n\n let selection = new Set(selectedKeys);\n for (let key of selectedKeys) {\n if (!newTree.nodeMap.has(key)) {\n selection.delete(key);\n }\n }\n\n setSelectedKeys(selection);\n },\n removeSelectedItems() {\n this.remove(...selectedKeys);\n },\n move(key: Key, toParentKey: Key | null, index: number) {\n setItems(({items, nodeMap: originalMap}) => {\n let node = originalMap.get(key);\n if (!node) {\n return {items, nodeMap: originalMap};\n }\n\n let {items: newItems, nodeMap: newMap} = updateTree(items, key, () => null, originalMap);\n\n const movedNode = {\n ...node,\n parentKey: toParentKey\n };\n\n // If parentKey is null, insert into the root.\n if (toParentKey == null) {\n addNode(movedNode, newMap);\n return {\n items: [...newItems.slice(0, index), movedNode, ...newItems.slice(index)],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(\n newItems,\n toParentKey,\n parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n movedNode,\n ...parentNode.children!.slice(index)\n ]\n }),\n newMap\n );\n });\n },\n moveBefore(key: Key, keys: Iterable<Key>) {\n setItems(prevState => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n moveAfter(key: Key, keys: Iterable<Key>) {\n setItems(prevState => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n toIndex++;\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n update(oldKey: Key, newValue: T) {\n setItems(({items, nodeMap: originalMap}) =>\n updateTree(\n items,\n oldKey,\n oldNode => {\n let node: TreeNode<T> = {\n key: oldNode.key,\n parentKey: oldNode.parentKey,\n value: newValue,\n children: null\n };\n\n let tree = buildTree(getChildren(newValue), originalMap, node.key);\n node.children = tree.items;\n return node;\n },\n originalMap\n )\n );\n }\n };\n}\n\nfunction moveItems<T extends object>(\n state: TreeDataState<T>,\n keys: Iterable<Key>,\n toParent: TreeNode<T> | null,\n toIndex: number,\n updateTree: (\n items: TreeNode<T>[],\n key: Key | null,\n update: (node: TreeNode<T>) => TreeNode<T> | null,\n originalMap: Map<Key, TreeNode<T>>\n ) => TreeDataState<T>,\n addNode: (node: TreeNode<T>, map: Map<Key, TreeNode<T>>) => void\n): TreeDataState<T> {\n let {items, nodeMap} = state;\n\n let parent = toParent;\n let removeKeys = new Set(keys);\n while (parent?.parentKey != null) {\n if (removeKeys.has(parent.key)) {\n throw new Error('Cannot move an item to be a child of itself.');\n }\n parent = nodeMap.get(parent.parentKey!) ?? null;\n }\n\n let originalToIndex = toIndex;\n\n let keyArray = Array.isArray(keys) ? keys : [...keys];\n // depth first search to put keys in order\n let inOrderKeys: Map<Key, number> = new Map();\n let removedItems: Array<TreeNode<T>> = [];\n let newItems = items;\n let newMap = nodeMap;\n let i = 0;\n\n function traversal(node, {inorder, postorder}) {\n inorder?.(node);\n if (node != null) {\n for (let child of node.children ?? []) {\n traversal(child, {inorder, postorder});\n postorder?.(child);\n }\n }\n }\n\n function inorder(child) {\n // in-order so we add items as we encounter them in the tree, then we can insert them in expected order later\n if (keyArray.includes(child.key)) {\n inOrderKeys.set(child.key, i++);\n }\n }\n\n function postorder(child) {\n // remove items and update the tree from the leaves and work upwards toward the root, this way\n // we don't copy child node references from parents inadvertently\n if (keyArray.includes(child.key)) {\n removedItems.push({...newMap.get(child.key)!, parentKey: toParent?.key ?? null});\n let {items: nextItems, nodeMap: nextMap} = updateTree(\n newItems,\n child.key,\n () => null,\n newMap\n );\n newItems = nextItems;\n newMap = nextMap;\n }\n // decrement the index if the child being removed is in the target parent and before the target index\n // the root node is special, it is null, and will not have a key, however, a parentKey can still point to it\n if (\n (child.parentKey === toParent || child.parentKey === toParent?.key) &&\n keyArray.includes(child.key) &&\n (toParent?.children ? toParent.children.indexOf(child) : items.indexOf(child)) <\n originalToIndex\n ) {\n toIndex--;\n }\n }\n\n traversal({children: items}, {inorder, postorder});\n\n let inOrderItems = removedItems.sort((a, b) =>\n inOrderKeys.get(a.key)! > inOrderKeys.get(b.key)! ? 1 : -1\n );\n // If parentKey is null, insert into the root.\n if (!toParent || toParent.key == null) {\n inOrderItems.forEach(movedNode => {\n addNode(movedNode, newMap);\n });\n return {\n items: [...newItems.slice(0, toIndex), ...inOrderItems, ...newItems.slice(toIndex)],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(\n newItems,\n toParent.key,\n parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, toIndex),\n ...inOrderItems,\n ...parentNode.children!.slice(toIndex)\n ]\n }),\n newMap\n );\n}\n"],"names":[],"version":3,"file":"useTreeData.mjs.map"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;;;AAkGM,SAAS,yCACd,KAAqC;IAErC,IAAI,eAAe,CAAA,GAAA,gDAAqB,EAAE;IAC1C,IAAI,CAAC,iBAAiB,mBAAmB,GAAG,CAAA,GAAA,4CAAiB,EAG3D,MAAM,KAAK,EAAE,MAAM,YAAY,IAAI,MAAM,MAAM,QAAQ;IACzD,IAAI,CAAC,aAAa,GAAG,CAAA,GAAA,qBAAO,EAAE;IAC9B,IAAI,CAAC,kBAAkB,oBAAoB,GAAG,CAAA,GAAA,qBAAO,EACnD,IAAM,mBAAmB;YAAC,OAAO;YAAM,KAAK;QAAI;IAGlD,0DAA0D;IAC1D,IAAI,mBAAmB,QAAQ,iBAAiB,KAAK,IAAI,iBAAiB,GAAG,EAAE;QAC7E,mBAAmB;YAAC,OAAO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AAM,MAAM,EAAE,IAAK;wBACrC,IAAI,OAAO,KAAK,CAAC,EAAE;wBACnB,IAAI,KAAK,MAAM,KAAK,YAAY,KAAK,IAAI,KAAK,WAC5C,iBAAiB;6BACZ,IAAI,KAAK,MAAM,KAAK,YACzB;oBAEJ;oBAEA,2DAA2D;oBAC3D,IAAI,QAAQ;oBACZ,IAAI,MAAM;oBACV,IAAK,IAAI,IAAI,GAAG,IAAI,MAAM,MAAM,EAAE,IAAK;wBACrC,IAAI,IAAI,gBACN,SAAS,KAAK,CAAC,EAAE,CAAC,KAAK;6BAClB,IAAI,IAAI,gBACb,OAAO,KAAK,CAAC,EAAE,CAAC,KAAK;oBAEzB;oBAEA,OAAO;+BAAC;6BAAO;oBAAG;gBACpB,EAAE,OAAM;gBACN,SAAS;gBACX;gBAEA,eAAe;YACjB,OAAO;gBACL,IAAI,aAAa,CAAA,GAAA,0CAAe,EAAE,cAAc;oBAC9C,aAAa;oBACb,UAAU;oBACV,cAAc,MAAM,YAAY;oBAChC,WAAW,MAAM,SAAS;gBAC5B;gBAEA,eAAe,IAAI,CAAA,GAAA,0CAAY,EAAE,QAAQ;YAC3C;YAEA,OAAO;gBACL,OAAO,eAAe,MAAM,CAAC;gBAC7B,KAAK,aAAa,MAAM,CAAC;YAC3B;QACF;QACA,kBAAiB,MAAM,EAAE,aAA+B;YACtD,IAAI,aAAa;gBAAC,GAAG,UAAU;gBAAE,GAAG,aAAa;YAAA;YACjD,IAAI,mBAAmB,CAAA,GAAA,0CAAe,EAAE,CAAC,GAAG;YAC5C,OAAO,IAAI,CAAA,GAAA,0CAAY,EAAE,QAAQ;QACnC;IACF;AACF;AAEA,SAAS,sCAAmB,KAAkC;IAC5D,OAAO,OAAO,SAAS,QAAQ,MAAM,GAAG,IAAI;AAC9C","sources":["packages/react-stately/src/datepicker/useDateRangePickerState.ts"],"sourcesContent":["/*\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\nimport {DateFormatter, toCalendarDate, toCalendarDateTime} from '@internationalized/date';\nimport {\n DateRange,\n DateRangePickerProps,\n DateValue,\n Granularity,\n MappedDateValue,\n TimeValue\n} from './types';\nimport {\n FieldOptions,\n FormatterOptions,\n getFormatOptions,\n getPlaceholderTime,\n getRangeValidationResult,\n useDefaultProps\n} from './utils';\nimport {FormValidationState, useFormValidationState} from '../form/useFormValidationState';\nimport {OverlayTriggerState, useOverlayTriggerState} from '../overlays/useOverlayTriggerState';\nimport {RangeValue, ValidationState} from '@react-types/shared';\nimport {useControlledState} from '../utils/useControlledState';\nimport {useMemo, useState} from 'react';\n\nexport interface DateRangePickerStateOptions<\n T extends DateValue = DateValue\n> extends DateRangePickerProps<T> {\n /**\n * Determines whether the date picker popover should close automatically when a date is selected.\n *\n * @default true\n */\n shouldCloseOnSelect?: boolean | (() => boolean);\n}\n\ntype TimeRange = RangeValue<TimeValue>;\nexport interface DateRangePickerState extends OverlayTriggerState, FormValidationState {\n /** The currently selected date range. */\n value: RangeValue<DateValue | null>;\n /** The default selected date range. */\n defaultValue: DateRange | null;\n /** Sets the selected date range. */\n setValue(value: DateRange | null): void;\n /**\n * The date portion of the selected range. This may be set prior to `value` if the user has\n * selected a date range but has not yet selected a time range.\n */\n dateRange: RangeValue<DateValue | null> | null;\n /** Sets the date portion of the selected range. */\n setDateRange(value: DateRange): void;\n /**\n * The time portion of the selected range. This may be set prior to `value` if the user has\n * selected a time range but has not yet selected a date range.\n */\n timeRange: RangeValue<TimeValue | null> | null;\n /** Sets the time portion of the selected range. */\n setTimeRange(value: TimeRange): void;\n /** Sets the date portion of either the start or end of the selected range. */\n setDate(part: 'start' | 'end', value: DateValue | null): void;\n /** Sets the time portion of either the start or end of the selected range. */\n setTime(part: 'start' | 'end', value: TimeValue | null): void;\n /** Sets the date and time of either the start or end of the selected range. */\n setDateTime(part: 'start' | 'end', value: DateValue | null): void;\n /** The granularity for the field, based on the `granularity` prop and current value. */\n granularity: Granularity;\n /**\n * Whether the date range picker supports selecting times, according to the `granularity` prop and\n * current value.\n */\n hasTime: boolean;\n /** Whether the calendar popover is currently open. */\n isOpen: boolean;\n /** Sets whether the calendar popover is open. */\n setOpen(isOpen: boolean): void;\n /**\n * The current validation state of the date range picker, based on the `validationState`,\n * `minValue`, and `maxValue` props.\n *\n * @deprecated Use `isInvalid` instead.\n */\n validationState: ValidationState | null;\n /**\n * Whether the date range picker is invalid, based on the `isInvalid`, `minValue`, and `maxValue`\n * props.\n */\n isInvalid: boolean;\n /** Formats the selected range using the given options. */\n formatValue(locale: string, fieldOptions: FieldOptions): {start: string; end: string} | null;\n /** Gets a formatter based on state's props. */\n getDateFormatter(locale: string, formatOptions: FormatterOptions): DateFormatter;\n}\n\n/**\n * Provides state management for a date range picker component.\n * A date range picker combines two DateFields and a RangeCalendar popover to allow\n * users to enter or select a date and time range.\n */\nexport function useDateRangePickerState<T extends DateValue = DateValue>(\n props: DateRangePickerStateOptions<T>\n): DateRangePickerState {\n let overlayState = useOverlayTriggerState(props);\n let [controlledValue, setControlledValue] = useControlledState<\n DateRange | null,\n RangeValue<MappedDateValue<T>> | null\n >(props.value, props.defaultValue || null, props.onChange);\n let [initialValue] = useState(controlledValue);\n let [placeholderValue, setPlaceholderValue] = useState<RangeValue<DateValue | null>>(\n () => controlledValue || {start: null, end: null}\n );\n\n // Reset the placeholder if the value prop is set to null.\n if (controlledValue == null && placeholderValue.start && placeholderValue.end) {\n placeholderValue = {start: null, end: null};\n setPlaceholderValue(placeholderValue);\n }\n\n let value = controlledValue || placeholderValue;\n\n let setValue = (newValue: RangeValue<DateValue | null> | null) => {\n value = newValue || {start: null, end: null};\n setPlaceholderValue(value);\n if (isCompleteRange(value)) {\n setControlledValue(value);\n } else {\n setControlledValue(null);\n }\n };\n\n let v = value?.start || value?.end || props.placeholderValue || null;\n let [granularity, defaultTimeZone] = useDefaultProps(v, props.granularity);\n let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second';\n let shouldCloseOnSelect = props.shouldCloseOnSelect ?? true;\n\n let [dateRange, setSelectedDateRange] = useState<RangeValue<DateValue | null> | null>(null);\n let [timeRange, setSelectedTimeRange] = useState<RangeValue<TimeValue | null> | null>(null);\n\n if (value && isCompleteRange(value)) {\n dateRange = value;\n if ('hour' in value.start) {\n timeRange = value as TimeRange;\n }\n }\n\n let commitValue = (dateRange: DateRange, timeRange: TimeRange) => {\n setValue({\n start:\n 'timeZone' in timeRange.start\n ? timeRange.start.set(toCalendarDate(dateRange.start))\n : toCalendarDateTime(dateRange.start, timeRange.start),\n end:\n 'timeZone' in timeRange.end\n ? timeRange.end.set(toCalendarDate(dateRange.end))\n : toCalendarDateTime(dateRange.end, timeRange.end)\n });\n setSelectedDateRange(null);\n setSelectedTimeRange(null);\n validation.commitValidation();\n };\n\n // Intercept setValue to make sure the Time section is not changed by date selection in Calendar\n let setDateRange = (range: RangeValue<DateValue | null>) => {\n let shouldClose =\n typeof shouldCloseOnSelect === 'function' ? shouldCloseOnSelect() : shouldCloseOnSelect;\n if (hasTime) {\n // Set a placeholder time if the popover is closing so we don't leave the field in an incomplete state.\n if (isCompleteRange(range) && (shouldClose || (timeRange?.start && timeRange?.end))) {\n commitValue(range, {\n start: timeRange?.start || getPlaceholderTime(props.placeholderValue),\n end: timeRange?.end || getPlaceholderTime(props.placeholderValue)\n });\n } else {\n setSelectedDateRange(range);\n }\n } else if (isCompleteRange(range)) {\n setValue(range);\n validation.commitValidation();\n } else {\n setSelectedDateRange(range);\n }\n\n if (shouldClose) {\n overlayState.setOpen(false);\n }\n };\n\n let setTimeRange = (range: RangeValue<TimeValue | null>) => {\n if (isCompleteRange(dateRange) && isCompleteRange(range)) {\n commitValue(dateRange, range);\n } else {\n setSelectedTimeRange(range);\n }\n };\n\n let showEra =\n (value?.start?.calendar.identifier === 'gregory' && value.start.era === 'BC') ||\n (value?.end?.calendar.identifier === 'gregory' && value.end.era === 'BC');\n let formatOpts = useMemo(\n () => ({\n granularity,\n timeZone: defaultTimeZone,\n hideTimeZone: props.hideTimeZone,\n hourCycle: props.hourCycle,\n shouldForceLeadingZeros: props.shouldForceLeadingZeros,\n showEra\n }),\n [\n granularity,\n props.hourCycle,\n props.shouldForceLeadingZeros,\n defaultTimeZone,\n props.hideTimeZone,\n showEra\n ]\n );\n\n let {minValue, maxValue, isDateUnavailable} = props;\n let builtinValidation = useMemo(\n () =>\n getRangeValidationResult(\n value,\n minValue,\n maxValue,\n isDateUnavailable ? date => isDateUnavailable(date, null) : undefined,\n formatOpts\n ),\n [value, minValue, maxValue, isDateUnavailable, formatOpts]\n );\n\n let validation = useFormValidationState({\n ...props,\n value: controlledValue as RangeValue<MappedDateValue<T>> | null,\n name: useMemo(\n () => [props.startName, props.endName].filter(n => n != null),\n [props.startName, props.endName]\n ),\n builtinValidation\n });\n\n let isValueInvalid = validation.displayValidation.isInvalid;\n let validationState: ValidationState | null =\n props.validationState || (isValueInvalid ? 'invalid' : null);\n\n return {\n ...validation,\n value,\n defaultValue: props.defaultValue ?? initialValue,\n setValue,\n dateRange,\n timeRange,\n granularity,\n hasTime,\n setDate(part, date) {\n if (part === 'start') {\n setDateRange({start: date, end: dateRange?.end ?? null});\n } else {\n setDateRange({start: dateRange?.start ?? null, end: date});\n }\n },\n setTime(part, time) {\n if (part === 'start') {\n setTimeRange({start: time, end: timeRange?.end ?? null});\n } else {\n setTimeRange({start: timeRange?.start ?? null, end: time});\n }\n },\n setDateTime(part, dateTime) {\n if (part === 'start') {\n setValue({start: dateTime, end: value?.end ?? null});\n } else {\n setValue({start: value?.start ?? null, end: dateTime});\n }\n },\n setDateRange,\n setTimeRange,\n ...overlayState,\n setOpen(isOpen) {\n // Commit the selected date range when the calendar is closed. Use a placeholder time if one wasn't set.\n // If only the time range was set and not the date range, don't commit. The state will be preserved until\n // the user opens the popover again.\n if (!isOpen && !(value?.start && value?.end) && isCompleteRange(dateRange) && hasTime) {\n commitValue(dateRange, {\n start: timeRange?.start || getPlaceholderTime(props.placeholderValue),\n end: timeRange?.end || getPlaceholderTime(props.placeholderValue)\n });\n }\n\n overlayState.setOpen(isOpen);\n },\n validationState,\n isInvalid: isValueInvalid,\n formatValue(locale, fieldOptions) {\n if (!value || !value.start || !value.end) {\n return null;\n }\n\n let startTimeZone = 'timeZone' in value.start ? value.start.timeZone : undefined;\n let startGranularity =\n props.granularity || (value.start && 'minute' in value.start ? 'minute' : 'day');\n let endTimeZone = 'timeZone' in value.end ? value.end.timeZone : undefined;\n let endGranularity =\n props.granularity || (value.end && 'minute' in value.end ? 'minute' : 'day');\n\n let startOptions = getFormatOptions(fieldOptions, {\n granularity: startGranularity,\n timeZone: startTimeZone,\n hideTimeZone: props.hideTimeZone,\n hourCycle: props.hourCycle,\n showEra:\n (value.start.calendar.identifier === 'gregory' && value.start.era === 'BC') ||\n (value.end.calendar.identifier === 'gregory' && value.end.era === 'BC')\n });\n\n let startDate = value.start.toDate(startTimeZone || 'UTC');\n let endDate = value.end.toDate(endTimeZone || 'UTC');\n\n let startFormatter = new DateFormatter(locale, startOptions);\n let endFormatter: Intl.DateTimeFormat;\n if (\n startTimeZone === endTimeZone &&\n startGranularity === endGranularity &&\n value.start.compare(value.end) !== 0\n ) {\n // Use formatRange, as it results in shorter output when some of the fields\n // are shared between the start and end dates (e.g. the same month).\n // Formatting will fail if the end date is before the start date. Fall back below when that happens.\n try {\n let parts = startFormatter.formatRangeToParts(startDate, endDate);\n\n // Find the separator between the start and end date. This is determined\n // by finding the last shared literal before the end range.\n let separatorIndex = -1;\n for (let i = 0; i < parts.length; i++) {\n let part = parts[i];\n if (part.source === 'shared' && part.type === 'literal') {\n separatorIndex = i;\n } else if (part.source === 'endRange') {\n break;\n }\n }\n\n // Now we can combine the parts into start and end strings.\n let start = '';\n let end = '';\n for (let i = 0; i < parts.length; i++) {\n if (i < separatorIndex) {\n start += parts[i].value;\n } else if (i > separatorIndex) {\n end += parts[i].value;\n }\n }\n\n return {start, end};\n } catch {\n // ignore\n }\n\n endFormatter = startFormatter;\n } else {\n let endOptions = getFormatOptions(fieldOptions, {\n granularity: endGranularity,\n timeZone: endTimeZone,\n hideTimeZone: props.hideTimeZone,\n hourCycle: props.hourCycle\n });\n\n endFormatter = new DateFormatter(locale, endOptions);\n }\n\n return {\n start: startFormatter.format(startDate),\n end: endFormatter.format(endDate)\n };\n },\n getDateFormatter(locale, formatOptions: FormatterOptions) {\n let newOptions = {...formatOpts, ...formatOptions};\n let newFormatOptions = getFormatOptions({}, newOptions);\n return new DateFormatter(locale, newFormatOptions);\n }\n };\n}\n\nfunction isCompleteRange<T>(value: RangeValue<T | null> | null): value is RangeValue<T> {\n return value?.start != null && value.end != null;\n}\n"],"names":[],"version":3,"file":"useDateRangePickerState.cjs.map"}
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;;;AAkGM,SAAS,yCACd,KAAqC;IAErC,IAAI,eAAe,CAAA,GAAA,gDAAqB,EAAE;IAC1C,IAAI,CAAC,iBAAiB,mBAAmB,GAAG,CAAA,GAAA,4CAAiB,EAG3D,MAAM,KAAK,EAAE,MAAM,YAAY,IAAI,MAAM,MAAM,QAAQ;IACzD,IAAI,CAAC,aAAa,GAAG,CAAA,GAAA,qBAAO,EAAE;IAC9B,IAAI,CAAC,kBAAkB,oBAAoB,GAAG,CAAA,GAAA,qBAAO,EACnD,IAAM,mBAAmB;YAAC,OAAO;YAAM,KAAK;QAAI;IAGlD,0DAA0D;IAC1D,IAAI,mBAAmB,QAAQ,iBAAiB,KAAK,IAAI,iBAAiB,GAAG,EAAE;QAC7E,mBAAmB;YAAC,OAAO;YAAM,KAAK;QAAI;QAC1C,oBAAoB;IACtB;IAEA,IAA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OAAO,KAAK,CAAC,EAAE;wBACnB,IAAI,KAAK,MAAM,KAAK,YAAY,KAAK,IAAI,KAAK,WAC5C,iBAAiB;6BACZ,IAAI,KAAK,MAAM,KAAK,YACzB;oBAEJ;oBAEA,2DAA2D;oBAC3D,IAAI,QAAQ;oBACZ,IAAI,MAAM;oBACV,IAAK,IAAI,IAAI,GAAG,IAAI,MAAM,MAAM,EAAE,IAAK;wBACrC,IAAI,IAAI,gBACN,SAAS,KAAK,CAAC,EAAE,CAAC,KAAK;6BAClB,IAAI,IAAI,gBACb,OAAO,KAAK,CAAC,EAAE,CAAC,KAAK;oBAEzB;oBAEA,OAAO;+BAAC;6BAAO;oBAAG;gBACpB,EAAE,OAAM;gBACN,SAAS;gBACX;gBAEA,eAAe;YACjB,OAAO;gBACL,IAAI,aAAa,CAAA,GAAA,0CAAe,EAAE,cAAc;oBAC9C,aAAa;oBACb,UAAU;oBACV,cAAc,MAAM,YAAY;oBAChC,WAAW,MAAM,SAAS;gBAC5B;gBAEA,eAAe,IAAI,CAAA,GAAA,0CAAY,EAAE,QAAQ;YAC3C;YAEA,OAAO;gBACL,OAAO,eAAe,MAAM,CAAC;gBAC7B,KAAK,aAAa,MAAM,CAAC;YAC3B;QACF;QACA,kBAAiB,MAAM,EAAE,aAA+B;YACtD,IAAI,aAAa;gBAAC,GAAG,UAAU;gBAAE,GAAG,aAAa;YAAA;YACjD,IAAI,mBAAmB,CAAA,GAAA,0CAAe,EAAE,CAAC,GAAG;YAC5C,OAAO,IAAI,CAAA,GAAA,0CAAY,EAAE,QAAQ;QACnC;IACF;AACF;AAEA,SAAS,sCAAmB,KAAkC;IAC5D,OAAO,OAAO,SAAS,QAAQ,MAAM,GAAG,IAAI;AAC9C","sources":["packages/react-stately/src/datepicker/useDateRangePickerState.ts"],"sourcesContent":["/*\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\nimport {DateFormatter, toCalendarDate, toCalendarDateTime} from '@internationalized/date';\nimport {\n DateRange,\n DateRangePickerProps,\n DateValue,\n Granularity,\n MappedDateValue,\n TimeValue\n} from './types';\nimport {\n FieldOptions,\n FormatterOptions,\n getFormatOptions,\n getPlaceholderTime,\n getRangeValidationResult,\n useDefaultProps\n} from './utils';\nimport {FormValidationState, useFormValidationState} from '../form/useFormValidationState';\nimport {OverlayTriggerState, useOverlayTriggerState} from '../overlays/useOverlayTriggerState';\nimport {RangeValue, ValidationState} from '@react-types/shared';\nimport {useControlledState} from '../utils/useControlledState';\nimport {useMemo, useState} from 'react';\n\nexport interface DateRangePickerStateOptions<\n T extends DateValue = DateValue\n> extends DateRangePickerProps<T> {\n /**\n * Determines whether the date picker popover should close automatically when a date is selected.\n *\n * @default true\n */\n shouldCloseOnSelect?: boolean | (() => boolean);\n}\n\ntype TimeRange = RangeValue<TimeValue>;\nexport interface DateRangePickerState extends OverlayTriggerState, FormValidationState {\n /** The currently selected date range. */\n value: RangeValue<DateValue | null>;\n /** The default selected date range. */\n defaultValue: DateRange | null;\n /** Sets the selected date range. */\n setValue(value: DateRange | null): void;\n /**\n * The date portion of the selected range. This may be set prior to `value` if the user has\n * selected a date range but has not yet selected a time range.\n */\n dateRange: RangeValue<DateValue | null> | null;\n /** Sets the date portion of the selected range. */\n setDateRange(value: DateRange): void;\n /**\n * The time portion of the selected range. This may be set prior to `value` if the user has\n * selected a time range but has not yet selected a date range.\n */\n timeRange: RangeValue<TimeValue | null> | null;\n /** Sets the time portion of the selected range. */\n setTimeRange(value: TimeRange): void;\n /** Sets the date portion of either the start or end of the selected range. */\n setDate(part: 'start' | 'end', value: DateValue | null): void;\n /** Sets the time portion of either the start or end of the selected range. */\n setTime(part: 'start' | 'end', value: TimeValue | null): void;\n /** Sets the date and time of either the start or end of the selected range. */\n setDateTime(part: 'start' | 'end', value: DateValue | null): void;\n /** The granularity for the field, based on the `granularity` prop and current value. */\n granularity: Granularity;\n /**\n * Whether the date range picker supports selecting times, according to the `granularity` prop and\n * current value.\n */\n hasTime: boolean;\n /** Whether the calendar popover is currently open. */\n isOpen: boolean;\n /** Sets whether the calendar popover is open. */\n setOpen(isOpen: boolean): void;\n /**\n * The current validation state of the date range picker, based on the `validationState`,\n * `minValue`, and `maxValue` props.\n *\n * @deprecated Use `isInvalid` instead.\n */\n validationState: ValidationState | null;\n /**\n * Whether the date range picker is invalid, based on the `isInvalid`, `minValue`, and `maxValue`\n * props.\n */\n isInvalid: boolean;\n /** Formats the selected range using the given options. */\n formatValue(locale: string, fieldOptions: FieldOptions): {start: string; end: string} | null;\n /** Gets a formatter based on state's props. */\n getDateFormatter(locale: string, formatOptions: FormatterOptions): DateFormatter;\n}\n\n/**\n * Provides state management for a date range picker component.\n * A date range picker combines two DateFields and a RangeCalendar popover to allow\n * users to enter or select a date and time range.\n */\nexport function useDateRangePickerState<T extends DateValue = DateValue>(\n props: DateRangePickerStateOptions<T>\n): DateRangePickerState {\n let overlayState = useOverlayTriggerState(props);\n let [controlledValue, setControlledValue] = useControlledState<\n DateRange | null,\n RangeValue<MappedDateValue<T>> | null\n >(props.value, props.defaultValue || null, props.onChange);\n let [initialValue] = useState(controlledValue);\n let [placeholderValue, setPlaceholderValue] = useState<RangeValue<DateValue | null>>(\n () => controlledValue || {start: null, end: null}\n );\n\n // Reset the placeholder if the value prop is set to null.\n if (controlledValue == null && placeholderValue.start && placeholderValue.end) {\n placeholderValue = {start: null, end: null};\n setPlaceholderValue(placeholderValue);\n }\n\n let value = controlledValue || placeholderValue;\n\n let setValue = (newValue: RangeValue<DateValue | null> | null) => {\n // oxlint-disable-next-line react/react-compiler\n value = newValue || {start: null, end: null};\n setPlaceholderValue(value);\n if (isCompleteRange(value)) {\n setControlledValue(value);\n } else {\n setControlledValue(null);\n }\n };\n\n let v = value?.start || value?.end || props.placeholderValue || null;\n let [granularity, defaultTimeZone] = useDefaultProps(v, props.granularity);\n let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second';\n let shouldCloseOnSelect = props.shouldCloseOnSelect ?? true;\n\n let [dateRange, setSelectedDateRange] = useState<RangeValue<DateValue | null> | null>(null);\n let [timeRange, setSelectedTimeRange] = useState<RangeValue<TimeValue | null> | null>(null);\n\n if (value && isCompleteRange(value)) {\n dateRange = value;\n if ('hour' in value.start) {\n timeRange = value as TimeRange;\n }\n }\n\n let commitValue = (dateRange: DateRange, timeRange: TimeRange) => {\n setValue({\n start:\n 'timeZone' in timeRange.start\n ? timeRange.start.set(toCalendarDate(dateRange.start))\n : toCalendarDateTime(dateRange.start, timeRange.start),\n end:\n 'timeZone' in timeRange.end\n ? timeRange.end.set(toCalendarDate(dateRange.end))\n : toCalendarDateTime(dateRange.end, timeRange.end)\n });\n setSelectedDateRange(null);\n setSelectedTimeRange(null);\n validation.commitValidation();\n };\n\n // Intercept setValue to make sure the Time section is not changed by date selection in Calendar\n let setDateRange = (range: RangeValue<DateValue | null>) => {\n let shouldClose =\n typeof shouldCloseOnSelect === 'function' ? shouldCloseOnSelect() : shouldCloseOnSelect;\n if (hasTime) {\n // Set a placeholder time if the popover is closing so we don't leave the field in an incomplete state.\n if (isCompleteRange(range) && (shouldClose || (timeRange?.start && timeRange?.end))) {\n commitValue(range, {\n start: timeRange?.start || getPlaceholderTime(props.placeholderValue),\n end: timeRange?.end || getPlaceholderTime(props.placeholderValue)\n });\n } else {\n setSelectedDateRange(range);\n }\n } else if (isCompleteRange(range)) {\n setValue(range);\n validation.commitValidation();\n } else {\n setSelectedDateRange(range);\n }\n\n if (shouldClose) {\n overlayState.setOpen(false);\n }\n };\n\n let setTimeRange = (range: RangeValue<TimeValue | null>) => {\n if (isCompleteRange(dateRange) && isCompleteRange(range)) {\n commitValue(dateRange, range);\n } else {\n setSelectedTimeRange(range);\n }\n };\n\n let showEra =\n (value?.start?.calendar.identifier === 'gregory' && value.start.era === 'BC') ||\n (value?.end?.calendar.identifier === 'gregory' && value.end.era === 'BC');\n let formatOpts = useMemo(\n () => ({\n granularity,\n timeZone: defaultTimeZone,\n hideTimeZone: props.hideTimeZone,\n hourCycle: props.hourCycle,\n shouldForceLeadingZeros: props.shouldForceLeadingZeros,\n showEra\n }),\n [\n granularity,\n props.hourCycle,\n props.shouldForceLeadingZeros,\n defaultTimeZone,\n props.hideTimeZone,\n showEra\n ]\n );\n\n let {minValue, maxValue, isDateUnavailable} = props;\n let builtinValidation = useMemo(\n () =>\n getRangeValidationResult(\n value,\n minValue,\n maxValue,\n isDateUnavailable ? date => isDateUnavailable(date, null) : undefined,\n formatOpts\n ),\n [value, minValue, maxValue, isDateUnavailable, formatOpts]\n );\n\n let validation = useFormValidationState({\n ...props,\n value: controlledValue as RangeValue<MappedDateValue<T>> | null,\n name: useMemo(\n () => [props.startName, props.endName].filter(n => n != null),\n [props.startName, props.endName]\n ),\n builtinValidation\n });\n\n let isValueInvalid = validation.displayValidation.isInvalid;\n let validationState: ValidationState | null =\n props.validationState || (isValueInvalid ? 'invalid' : null);\n\n return {\n ...validation,\n value,\n defaultValue: props.defaultValue ?? initialValue,\n setValue,\n dateRange,\n timeRange,\n granularity,\n hasTime,\n setDate(part, date) {\n if (part === 'start') {\n setDateRange({start: date, end: dateRange?.end ?? null});\n } else {\n setDateRange({start: dateRange?.start ?? null, end: date});\n }\n },\n setTime(part, time) {\n if (part === 'start') {\n setTimeRange({start: time, end: timeRange?.end ?? null});\n } else {\n setTimeRange({start: timeRange?.start ?? null, end: time});\n }\n },\n setDateTime(part, dateTime) {\n if (part === 'start') {\n setValue({start: dateTime, end: value?.end ?? null});\n } else {\n setValue({start: value?.start ?? null, end: dateTime});\n }\n },\n setDateRange,\n setTimeRange,\n ...overlayState,\n setOpen(isOpen) {\n // Commit the selected date range when the calendar is closed. Use a placeholder time if one wasn't set.\n // If only the time range was set and not the date range, don't commit. The state will be preserved until\n // the user opens the popover again.\n if (!isOpen && !(value?.start && value?.end) && isCompleteRange(dateRange) && hasTime) {\n commitValue(dateRange, {\n start: timeRange?.start || getPlaceholderTime(props.placeholderValue),\n end: timeRange?.end || getPlaceholderTime(props.placeholderValue)\n });\n }\n\n overlayState.setOpen(isOpen);\n },\n validationState,\n isInvalid: isValueInvalid,\n formatValue(locale, fieldOptions) {\n if (!value || !value.start || !value.end) {\n return null;\n }\n\n let startTimeZone = 'timeZone' in value.start ? value.start.timeZone : undefined;\n let startGranularity =\n props.granularity || (value.start && 'minute' in value.start ? 'minute' : 'day');\n let endTimeZone = 'timeZone' in value.end ? value.end.timeZone : undefined;\n let endGranularity =\n props.granularity || (value.end && 'minute' in value.end ? 'minute' : 'day');\n\n let startOptions = getFormatOptions(fieldOptions, {\n granularity: startGranularity,\n timeZone: startTimeZone,\n hideTimeZone: props.hideTimeZone,\n hourCycle: props.hourCycle,\n showEra:\n (value.start.calendar.identifier === 'gregory' && value.start.era === 'BC') ||\n (value.end.calendar.identifier === 'gregory' && value.end.era === 'BC')\n });\n\n let startDate = value.start.toDate(startTimeZone || 'UTC');\n let endDate = value.end.toDate(endTimeZone || 'UTC');\n\n let startFormatter = new DateFormatter(locale, startOptions);\n let endFormatter: Intl.DateTimeFormat;\n if (\n startTimeZone === endTimeZone &&\n startGranularity === endGranularity &&\n value.start.compare(value.end) !== 0\n ) {\n // Use formatRange, as it results in shorter output when some of the fields\n // are shared between the start and end dates (e.g. the same month).\n // Formatting will fail if the end date is before the start date. Fall back below when that happens.\n try {\n let parts = startFormatter.formatRangeToParts(startDate, endDate);\n\n // Find the separator between the start and end date. This is determined\n // by finding the last shared literal before the end range.\n let separatorIndex = -1;\n for (let i = 0; i < parts.length; i++) {\n let part = parts[i];\n if (part.source === 'shared' && part.type === 'literal') {\n separatorIndex = i;\n } else if (part.source === 'endRange') {\n break;\n }\n }\n\n // Now we can combine the parts into start and end strings.\n let start = '';\n let end = '';\n for (let i = 0; i < parts.length; i++) {\n if (i < separatorIndex) {\n start += parts[i].value;\n } else if (i > separatorIndex) {\n end += parts[i].value;\n }\n }\n\n return {start, end};\n } catch {\n // ignore\n }\n\n endFormatter = startFormatter;\n } else {\n let endOptions = getFormatOptions(fieldOptions, {\n granularity: endGranularity,\n timeZone: endTimeZone,\n hideTimeZone: props.hideTimeZone,\n hourCycle: props.hourCycle\n });\n\n endFormatter = new DateFormatter(locale, endOptions);\n }\n\n return {\n start: startFormatter.format(startDate),\n end: endFormatter.format(endDate)\n };\n },\n getDateFormatter(locale, formatOptions: FormatterOptions) {\n let newOptions = {...formatOpts, ...formatOptions};\n let newFormatOptions = getFormatOptions({}, newOptions);\n return new DateFormatter(locale, newFormatOptions);\n }\n };\n}\n\nfunction isCompleteRange<T>(value: RangeValue<T | null> | null): value is RangeValue<T> {\n return value?.start != null && value.end != null;\n}\n"],"names":[],"version":3,"file":"useDateRangePickerState.cjs.map"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;AAAA;;;;;;;;;;CAUC;;;;;;AAkGM,SAAS,yCACd,KAAqC;QAgGlC,cACA;IA/FH,IAAI,eAAe,CAAA,GAAA,yCAAqB,EAAE;IAC1C,IAAI,CAAC,iBAAiB,mBAAmB,GAAG,CAAA,GAAA,yCAAiB,EAG3D,MAAM,KAAK,EAAE,MAAM,YAAY,IAAI,MAAM,MAAM,QAAQ;IACzD,IAAI,CAAC,aAAa,GAAG,CAAA,GAAA,eAAO,EAAE;IAC9B,IAAI,CAAC,kBAAkB,oBAAoB,GAAG,CAAA,GAAA,eAAO,EACnD,IAAM,mBAAmB;YAAC,OAAO;YAAM,KAAK;QAAI;IAGlD,0DAA0D;IAC1D,IAAI,mBAAmB,QAAQ,iBAAiB,KAAK,IAAI,iBAAiB,GAAG,EAAE;QAC7E,mBAAmB;YAAC,OAAO;YAAM,KAAK;QAAI;QAC1C,oBAAoB;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;gBAEA,eAAe;YACjB,OAAO;gBACL,IAAI,aAAa,CAAA,GAAA,yCAAe,EAAE,cAAc;oBAC9C,aAAa;oBACb,UAAU;oBACV,cAAc,MAAM,YAAY;oBAChC,WAAW,MAAM,SAAS;gBAC5B;gBAEA,eAAe,IAAI,CAAA,GAAA,oBAAY,EAAE,QAAQ;YAC3C;YAEA,OAAO;gBACL,OAAO,eAAe,MAAM,CAAC;gBAC7B,KAAK,aAAa,MAAM,CAAC;YAC3B;QACF;QACA,kBAAiB,MAAM,EAAE,aAA+B;YACtD,IAAI,aAAa;gBAAC,GAAG,UAAU;gBAAE,GAAG,aAAa;YAAA;YACjD,IAAI,mBAAmB,CAAA,GAAA,yCAAe,EAAE,CAAC,GAAG;YAC5C,OAAO,IAAI,CAAA,GAAA,oBAAY,EAAE,QAAQ;QACnC;IACF;AACF;AAEA,SAAS,sCAAmB,KAAkC;IAC5D,OAAO,CAAA,kBAAA,4BAAA,MAAO,KAAK,KAAI,QAAQ,MAAM,GAAG,IAAI;AAC9C","sources":["packages/react-stately/src/datepicker/useDateRangePickerState.ts"],"sourcesContent":["/*\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\nimport {DateFormatter, toCalendarDate, toCalendarDateTime} from '@internationalized/date';\nimport {\n DateRange,\n DateRangePickerProps,\n DateValue,\n Granularity,\n MappedDateValue,\n TimeValue\n} from './types';\nimport {\n FieldOptions,\n FormatterOptions,\n getFormatOptions,\n getPlaceholderTime,\n getRangeValidationResult,\n useDefaultProps\n} from './utils';\nimport {FormValidationState, useFormValidationState} from '../form/useFormValidationState';\nimport {OverlayTriggerState, useOverlayTriggerState} from '../overlays/useOverlayTriggerState';\nimport {RangeValue, ValidationState} from '@react-types/shared';\nimport {useControlledState} from '../utils/useControlledState';\nimport {useMemo, useState} from 'react';\n\nexport interface DateRangePickerStateOptions<\n T extends DateValue = DateValue\n> extends DateRangePickerProps<T> {\n /**\n * Determines whether the date picker popover should close automatically when a date is selected.\n *\n * @default true\n */\n shouldCloseOnSelect?: boolean | (() => boolean);\n}\n\ntype TimeRange = RangeValue<TimeValue>;\nexport interface DateRangePickerState extends OverlayTriggerState, FormValidationState {\n /** The currently selected date range. */\n value: RangeValue<DateValue | null>;\n /** The default selected date range. */\n defaultValue: DateRange | null;\n /** Sets the selected date range. */\n setValue(value: DateRange | null): void;\n /**\n * The date portion of the selected range. This may be set prior to `value` if the user has\n * selected a date range but has not yet selected a time range.\n */\n dateRange: RangeValue<DateValue | null> | null;\n /** Sets the date portion of the selected range. */\n setDateRange(value: DateRange): void;\n /**\n * The time portion of the selected range. This may be set prior to `value` if the user has\n * selected a time range but has not yet selected a date range.\n */\n timeRange: RangeValue<TimeValue | null> | null;\n /** Sets the time portion of the selected range. */\n setTimeRange(value: TimeRange): void;\n /** Sets the date portion of either the start or end of the selected range. */\n setDate(part: 'start' | 'end', value: DateValue | null): void;\n /** Sets the time portion of either the start or end of the selected range. */\n setTime(part: 'start' | 'end', value: TimeValue | null): void;\n /** Sets the date and time of either the start or end of the selected range. */\n setDateTime(part: 'start' | 'end', value: DateValue | null): void;\n /** The granularity for the field, based on the `granularity` prop and current value. */\n granularity: Granularity;\n /**\n * Whether the date range picker supports selecting times, according to the `granularity` prop and\n * current value.\n */\n hasTime: boolean;\n /** Whether the calendar popover is currently open. */\n isOpen: boolean;\n /** Sets whether the calendar popover is open. */\n setOpen(isOpen: boolean): void;\n /**\n * The current validation state of the date range picker, based on the `validationState`,\n * `minValue`, and `maxValue` props.\n *\n * @deprecated Use `isInvalid` instead.\n */\n validationState: ValidationState | null;\n /**\n * Whether the date range picker is invalid, based on the `isInvalid`, `minValue`, and `maxValue`\n * props.\n */\n isInvalid: boolean;\n /** Formats the selected range using the given options. */\n formatValue(locale: string, fieldOptions: FieldOptions): {start: string; end: string} | null;\n /** Gets a formatter based on state's props. */\n getDateFormatter(locale: string, formatOptions: FormatterOptions): DateFormatter;\n}\n\n/**\n * Provides state management for a date range picker component.\n * A date range picker combines two DateFields and a RangeCalendar popover to allow\n * users to enter or select a date and time range.\n */\nexport function useDateRangePickerState<T extends DateValue = DateValue>(\n props: DateRangePickerStateOptions<T>\n): DateRangePickerState {\n let overlayState = useOverlayTriggerState(props);\n let [controlledValue, setControlledValue] = useControlledState<\n DateRange | null,\n RangeValue<MappedDateValue<T>> | null\n >(props.value, props.defaultValue || null, props.onChange);\n let [initialValue] = useState(controlledValue);\n let [placeholderValue, setPlaceholderValue] = useState<RangeValue<DateValue | null>>(\n () => controlledValue || {start: null, end: null}\n );\n\n // Reset the placeholder if the value prop is set to null.\n if (controlledValue == null && placeholderValue.start && placeholderValue.end) {\n placeholderValue = {start: null, end: null};\n setPlaceholderValue(placeholderValue);\n }\n\n let value = controlledValue || placeholderValue;\n\n let setValue = (newValue: RangeValue<DateValue | null> | null) => {\n value = newValue || {start: null, end: null};\n setPlaceholderValue(value);\n if (isCompleteRange(value)) {\n setControlledValue(value);\n } else {\n setControlledValue(null);\n }\n };\n\n let v = value?.start || value?.end || props.placeholderValue || null;\n let [granularity, defaultTimeZone] = useDefaultProps(v, props.granularity);\n let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second';\n let shouldCloseOnSelect = props.shouldCloseOnSelect ?? true;\n\n let [dateRange, setSelectedDateRange] = useState<RangeValue<DateValue | null> | null>(null);\n let [timeRange, setSelectedTimeRange] = useState<RangeValue<TimeValue | null> | null>(null);\n\n if (value && isCompleteRange(value)) {\n dateRange = value;\n if ('hour' in value.start) {\n timeRange = value as TimeRange;\n }\n }\n\n let commitValue = (dateRange: DateRange, timeRange: TimeRange) => {\n setValue({\n start:\n 'timeZone' in timeRange.start\n ? timeRange.start.set(toCalendarDate(dateRange.start))\n : toCalendarDateTime(dateRange.start, timeRange.start),\n end:\n 'timeZone' in timeRange.end\n ? timeRange.end.set(toCalendarDate(dateRange.end))\n : toCalendarDateTime(dateRange.end, timeRange.end)\n });\n setSelectedDateRange(null);\n setSelectedTimeRange(null);\n validation.commitValidation();\n };\n\n // Intercept setValue to make sure the Time section is not changed by date selection in Calendar\n let setDateRange = (range: RangeValue<DateValue | null>) => {\n let shouldClose =\n typeof shouldCloseOnSelect === 'function' ? shouldCloseOnSelect() : shouldCloseOnSelect;\n if (hasTime) {\n // Set a placeholder time if the popover is closing so we don't leave the field in an incomplete state.\n if (isCompleteRange(range) && (shouldClose || (timeRange?.start && timeRange?.end))) {\n commitValue(range, {\n start: timeRange?.start || getPlaceholderTime(props.placeholderValue),\n end: timeRange?.end || getPlaceholderTime(props.placeholderValue)\n });\n } else {\n setSelectedDateRange(range);\n }\n } else if (isCompleteRange(range)) {\n setValue(range);\n validation.commitValidation();\n } else {\n setSelectedDateRange(range);\n }\n\n if (shouldClose) {\n overlayState.setOpen(false);\n }\n };\n\n let setTimeRange = (range: RangeValue<TimeValue | null>) => {\n if (isCompleteRange(dateRange) && isCompleteRange(range)) {\n commitValue(dateRange, range);\n } else {\n setSelectedTimeRange(range);\n }\n };\n\n let showEra =\n (value?.start?.calendar.identifier === 'gregory' && value.start.era === 'BC') ||\n (value?.end?.calendar.identifier === 'gregory' && value.end.era === 'BC');\n let formatOpts = useMemo(\n () => ({\n granularity,\n timeZone: defaultTimeZone,\n hideTimeZone: props.hideTimeZone,\n hourCycle: props.hourCycle,\n shouldForceLeadingZeros: props.shouldForceLeadingZeros,\n showEra\n }),\n [\n granularity,\n props.hourCycle,\n props.shouldForceLeadingZeros,\n defaultTimeZone,\n props.hideTimeZone,\n showEra\n ]\n );\n\n let {minValue, maxValue, isDateUnavailable} = props;\n let builtinValidation = useMemo(\n () =>\n getRangeValidationResult(\n value,\n minValue,\n maxValue,\n isDateUnavailable ? date => isDateUnavailable(date, null) : undefined,\n formatOpts\n ),\n [value, minValue, maxValue, isDateUnavailable, formatOpts]\n );\n\n let validation = useFormValidationState({\n ...props,\n value: controlledValue as RangeValue<MappedDateValue<T>> | null,\n name: useMemo(\n () => [props.startName, props.endName].filter(n => n != null),\n [props.startName, props.endName]\n ),\n builtinValidation\n });\n\n let isValueInvalid = validation.displayValidation.isInvalid;\n let validationState: ValidationState | null =\n props.validationState || (isValueInvalid ? 'invalid' : null);\n\n return {\n ...validation,\n value,\n defaultValue: props.defaultValue ?? initialValue,\n setValue,\n dateRange,\n timeRange,\n granularity,\n hasTime,\n setDate(part, date) {\n if (part === 'start') {\n setDateRange({start: date, end: dateRange?.end ?? null});\n } else {\n setDateRange({start: dateRange?.start ?? null, end: date});\n }\n },\n setTime(part, time) {\n if (part === 'start') {\n setTimeRange({start: time, end: timeRange?.end ?? null});\n } else {\n setTimeRange({start: timeRange?.start ?? null, end: time});\n }\n },\n setDateTime(part, dateTime) {\n if (part === 'start') {\n setValue({start: dateTime, end: value?.end ?? null});\n } else {\n setValue({start: value?.start ?? null, end: dateTime});\n }\n },\n setDateRange,\n setTimeRange,\n ...overlayState,\n setOpen(isOpen) {\n // Commit the selected date range when the calendar is closed. Use a placeholder time if one wasn't set.\n // If only the time range was set and not the date range, don't commit. The state will be preserved until\n // the user opens the popover again.\n if (!isOpen && !(value?.start && value?.end) && isCompleteRange(dateRange) && hasTime) {\n commitValue(dateRange, {\n start: timeRange?.start || getPlaceholderTime(props.placeholderValue),\n end: timeRange?.end || getPlaceholderTime(props.placeholderValue)\n });\n }\n\n overlayState.setOpen(isOpen);\n },\n validationState,\n isInvalid: isValueInvalid,\n formatValue(locale, fieldOptions) {\n if (!value || !value.start || !value.end) {\n return null;\n }\n\n let startTimeZone = 'timeZone' in value.start ? value.start.timeZone : undefined;\n let startGranularity =\n props.granularity || (value.start && 'minute' in value.start ? 'minute' : 'day');\n let endTimeZone = 'timeZone' in value.end ? value.end.timeZone : undefined;\n let endGranularity =\n props.granularity || (value.end && 'minute' in value.end ? 'minute' : 'day');\n\n let startOptions = getFormatOptions(fieldOptions, {\n granularity: startGranularity,\n timeZone: startTimeZone,\n hideTimeZone: props.hideTimeZone,\n hourCycle: props.hourCycle,\n showEra:\n (value.start.calendar.identifier === 'gregory' && value.start.era === 'BC') ||\n (value.end.calendar.identifier === 'gregory' && value.end.era === 'BC')\n });\n\n let startDate = value.start.toDate(startTimeZone || 'UTC');\n let endDate = value.end.toDate(endTimeZone || 'UTC');\n\n let startFormatter = new DateFormatter(locale, startOptions);\n let endFormatter: Intl.DateTimeFormat;\n if (\n startTimeZone === endTimeZone &&\n startGranularity === endGranularity &&\n value.start.compare(value.end) !== 0\n ) {\n // Use formatRange, as it results in shorter output when some of the fields\n // are shared between the start and end dates (e.g. the same month).\n // Formatting will fail if the end date is before the start date. Fall back below when that happens.\n try {\n let parts = startFormatter.formatRangeToParts(startDate, endDate);\n\n // Find the separator between the start and end date. This is determined\n // by finding the last shared literal before the end range.\n let separatorIndex = -1;\n for (let i = 0; i < parts.length; i++) {\n let part = parts[i];\n if (part.source === 'shared' && part.type === 'literal') {\n separatorIndex = i;\n } else if (part.source === 'endRange') {\n break;\n }\n }\n\n // Now we can combine the parts into start and end strings.\n let start = '';\n let end = '';\n for (let i = 0; i < parts.length; i++) {\n if (i < separatorIndex) {\n start += parts[i].value;\n } else if (i > separatorIndex) {\n end += parts[i].value;\n }\n }\n\n return {start, end};\n } catch {\n // ignore\n }\n\n endFormatter = startFormatter;\n } else {\n let endOptions = getFormatOptions(fieldOptions, {\n granularity: endGranularity,\n timeZone: endTimeZone,\n hideTimeZone: props.hideTimeZone,\n hourCycle: props.hourCycle\n });\n\n endFormatter = new DateFormatter(locale, endOptions);\n }\n\n return {\n start: startFormatter.format(startDate),\n end: endFormatter.format(endDate)\n };\n },\n getDateFormatter(locale, formatOptions: FormatterOptions) {\n let newOptions = {...formatOpts, ...formatOptions};\n let newFormatOptions = getFormatOptions({}, newOptions);\n return new DateFormatter(locale, newFormatOptions);\n }\n };\n}\n\nfunction isCompleteRange<T>(value: RangeValue<T | null> | null): value is RangeValue<T> {\n return value?.start != null && value.end != null;\n}\n"],"names":[],"version":3,"file":"useDateRangePickerState.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;AAAA;;;;;;;;;;CAUC;;;;;;AAkGM,SAAS,yCACd,KAAqC;QAiGlC,cACA;IAhGH,IAAI,eAAe,CAAA,GAAA,yCAAqB,EAAE;IAC1C,IAAI,CAAC,iBAAiB,mBAAmB,GAAG,CAAA,GAAA,yCAAiB,EAG3D,MAAM,KAAK,EAAE,MAAM,YAAY,IAAI,MAAM,MAAM,QAAQ;IACzD,IAAI,CAAC,aAAa,GAAG,CAAA,GAAA,eAAO,EAAE;IAC9B,IAAI,CAAC,kBAAkB,oBAAoB,GAAG,CAAA,GAAA,eAAO,EACnD,IAAM,mBAAmB;YAAC,OAAO;YAAM,KAAK;QAAI;IAGlD,0DAA0D;IAC1D,IAAI,mBAAmB,QAAQ,iBAAiB,KAAK,IAAI,iBAAiB,GAAG,EAAE;QAC7E,mBAAmB;YAAC,OAAO;YAAM,KAAK;QAAI;QAC1C,oBAAoB;IACtB;IAEA,IAAI,QAAQ,mBAAmB;IAE/B,IAAI,WAAW,CAAC;QACd,gDAAgD;QAChD,QAAQ,YAAY;YAAC,OAAO;YAAM,KAAK;QAAI;QAC3C,oBAAoB;QACpB,IAAI,sCAAgB,QAClB,mBAAmB;aAEnB,mBAAmB;IAEvB;IAEA,IAAI,IAAI,CAAA,kBAAA,4BAAA,MAAO,KAAK,MAAI,kBAAA,4BAAA,MAAO,GAAG,KAAI,MAAM,gBAAgB,IAAI;IAChE,IAAI,CAAC,aAAa,gBAAgB,GAAG,CAAA,GAAA,yCAAc,EAAE,GAAG,MAAM,WAAW;IACzE,IAAI,UAAU,gBAAgB,UAAU,gBAAgB,YAAY,gBAAgB;QAC1D;IAA1B,IAAI,sBAAsB,CAAA,6BAAA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AO,IAAI,CAAA,GAAA,oBAAY,EAAE,QAAQ;QACnC;IACF;AACF;AAEA,SAAS,sCAAmB,KAAkC;IAC5D,OAAO,CAAA,kBAAA,4BAAA,MAAO,KAAK,KAAI,QAAQ,MAAM,GAAG,IAAI;AAC9C","sources":["packages/react-stately/src/datepicker/useDateRangePickerState.ts"],"sourcesContent":["/*\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\nimport {DateFormatter, toCalendarDate, toCalendarDateTime} from '@internationalized/date';\nimport {\n DateRange,\n DateRangePickerProps,\n DateValue,\n Granularity,\n MappedDateValue,\n TimeValue\n} from './types';\nimport {\n FieldOptions,\n FormatterOptions,\n getFormatOptions,\n getPlaceholderTime,\n getRangeValidationResult,\n useDefaultProps\n} from './utils';\nimport {FormValidationState, useFormValidationState} from '../form/useFormValidationState';\nimport {OverlayTriggerState, useOverlayTriggerState} from '../overlays/useOverlayTriggerState';\nimport {RangeValue, ValidationState} from '@react-types/shared';\nimport {useControlledState} from '../utils/useControlledState';\nimport {useMemo, useState} from 'react';\n\nexport interface DateRangePickerStateOptions<\n T extends DateValue = DateValue\n> extends DateRangePickerProps<T> {\n /**\n * Determines whether the date picker popover should close automatically when a date is selected.\n *\n * @default true\n */\n shouldCloseOnSelect?: boolean | (() => boolean);\n}\n\ntype TimeRange = RangeValue<TimeValue>;\nexport interface DateRangePickerState extends OverlayTriggerState, FormValidationState {\n /** The currently selected date range. */\n value: RangeValue<DateValue | null>;\n /** The default selected date range. */\n defaultValue: DateRange | null;\n /** Sets the selected date range. */\n setValue(value: DateRange | null): void;\n /**\n * The date portion of the selected range. This may be set prior to `value` if the user has\n * selected a date range but has not yet selected a time range.\n */\n dateRange: RangeValue<DateValue | null> | null;\n /** Sets the date portion of the selected range. */\n setDateRange(value: DateRange): void;\n /**\n * The time portion of the selected range. This may be set prior to `value` if the user has\n * selected a time range but has not yet selected a date range.\n */\n timeRange: RangeValue<TimeValue | null> | null;\n /** Sets the time portion of the selected range. */\n setTimeRange(value: TimeRange): void;\n /** Sets the date portion of either the start or end of the selected range. */\n setDate(part: 'start' | 'end', value: DateValue | null): void;\n /** Sets the time portion of either the start or end of the selected range. */\n setTime(part: 'start' | 'end', value: TimeValue | null): void;\n /** Sets the date and time of either the start or end of the selected range. */\n setDateTime(part: 'start' | 'end', value: DateValue | null): void;\n /** The granularity for the field, based on the `granularity` prop and current value. */\n granularity: Granularity;\n /**\n * Whether the date range picker supports selecting times, according to the `granularity` prop and\n * current value.\n */\n hasTime: boolean;\n /** Whether the calendar popover is currently open. */\n isOpen: boolean;\n /** Sets whether the calendar popover is open. */\n setOpen(isOpen: boolean): void;\n /**\n * The current validation state of the date range picker, based on the `validationState`,\n * `minValue`, and `maxValue` props.\n *\n * @deprecated Use `isInvalid` instead.\n */\n validationState: ValidationState | null;\n /**\n * Whether the date range picker is invalid, based on the `isInvalid`, `minValue`, and `maxValue`\n * props.\n */\n isInvalid: boolean;\n /** Formats the selected range using the given options. */\n formatValue(locale: string, fieldOptions: FieldOptions): {start: string; end: string} | null;\n /** Gets a formatter based on state's props. */\n getDateFormatter(locale: string, formatOptions: FormatterOptions): DateFormatter;\n}\n\n/**\n * Provides state management for a date range picker component.\n * A date range picker combines two DateFields and a RangeCalendar popover to allow\n * users to enter or select a date and time range.\n */\nexport function useDateRangePickerState<T extends DateValue = DateValue>(\n props: DateRangePickerStateOptions<T>\n): DateRangePickerState {\n let overlayState = useOverlayTriggerState(props);\n let [controlledValue, setControlledValue] = useControlledState<\n DateRange | null,\n RangeValue<MappedDateValue<T>> | null\n >(props.value, props.defaultValue || null, props.onChange);\n let [initialValue] = useState(controlledValue);\n let [placeholderValue, setPlaceholderValue] = useState<RangeValue<DateValue | null>>(\n () => controlledValue || {start: null, end: null}\n );\n\n // Reset the placeholder if the value prop is set to null.\n if (controlledValue == null && placeholderValue.start && placeholderValue.end) {\n placeholderValue = {start: null, end: null};\n setPlaceholderValue(placeholderValue);\n }\n\n let value = controlledValue || placeholderValue;\n\n let setValue = (newValue: RangeValue<DateValue | null> | null) => {\n // oxlint-disable-next-line react/react-compiler\n value = newValue || {start: null, end: null};\n setPlaceholderValue(value);\n if (isCompleteRange(value)) {\n setControlledValue(value);\n } else {\n setControlledValue(null);\n }\n };\n\n let v = value?.start || value?.end || props.placeholderValue || null;\n let [granularity, defaultTimeZone] = useDefaultProps(v, props.granularity);\n let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second';\n let shouldCloseOnSelect = props.shouldCloseOnSelect ?? true;\n\n let [dateRange, setSelectedDateRange] = useState<RangeValue<DateValue | null> | null>(null);\n let [timeRange, setSelectedTimeRange] = useState<RangeValue<TimeValue | null> | null>(null);\n\n if (value && isCompleteRange(value)) {\n dateRange = value;\n if ('hour' in value.start) {\n timeRange = value as TimeRange;\n }\n }\n\n let commitValue = (dateRange: DateRange, timeRange: TimeRange) => {\n setValue({\n start:\n 'timeZone' in timeRange.start\n ? timeRange.start.set(toCalendarDate(dateRange.start))\n : toCalendarDateTime(dateRange.start, timeRange.start),\n end:\n 'timeZone' in timeRange.end\n ? timeRange.end.set(toCalendarDate(dateRange.end))\n : toCalendarDateTime(dateRange.end, timeRange.end)\n });\n setSelectedDateRange(null);\n setSelectedTimeRange(null);\n validation.commitValidation();\n };\n\n // Intercept setValue to make sure the Time section is not changed by date selection in Calendar\n let setDateRange = (range: RangeValue<DateValue | null>) => {\n let shouldClose =\n typeof shouldCloseOnSelect === 'function' ? shouldCloseOnSelect() : shouldCloseOnSelect;\n if (hasTime) {\n // Set a placeholder time if the popover is closing so we don't leave the field in an incomplete state.\n if (isCompleteRange(range) && (shouldClose || (timeRange?.start && timeRange?.end))) {\n commitValue(range, {\n start: timeRange?.start || getPlaceholderTime(props.placeholderValue),\n end: timeRange?.end || getPlaceholderTime(props.placeholderValue)\n });\n } else {\n setSelectedDateRange(range);\n }\n } else if (isCompleteRange(range)) {\n setValue(range);\n validation.commitValidation();\n } else {\n setSelectedDateRange(range);\n }\n\n if (shouldClose) {\n overlayState.setOpen(false);\n }\n };\n\n let setTimeRange = (range: RangeValue<TimeValue | null>) => {\n if (isCompleteRange(dateRange) && isCompleteRange(range)) {\n commitValue(dateRange, range);\n } else {\n setSelectedTimeRange(range);\n }\n };\n\n let showEra =\n (value?.start?.calendar.identifier === 'gregory' && value.start.era === 'BC') ||\n (value?.end?.calendar.identifier === 'gregory' && value.end.era === 'BC');\n let formatOpts = useMemo(\n () => ({\n granularity,\n timeZone: defaultTimeZone,\n hideTimeZone: props.hideTimeZone,\n hourCycle: props.hourCycle,\n shouldForceLeadingZeros: props.shouldForceLeadingZeros,\n showEra\n }),\n [\n granularity,\n props.hourCycle,\n props.shouldForceLeadingZeros,\n defaultTimeZone,\n props.hideTimeZone,\n showEra\n ]\n );\n\n let {minValue, maxValue, isDateUnavailable} = props;\n let builtinValidation = useMemo(\n () =>\n getRangeValidationResult(\n value,\n minValue,\n maxValue,\n isDateUnavailable ? date => isDateUnavailable(date, null) : undefined,\n formatOpts\n ),\n [value, minValue, maxValue, isDateUnavailable, formatOpts]\n );\n\n let validation = useFormValidationState({\n ...props,\n value: controlledValue as RangeValue<MappedDateValue<T>> | null,\n name: useMemo(\n () => [props.startName, props.endName].filter(n => n != null),\n [props.startName, props.endName]\n ),\n builtinValidation\n });\n\n let isValueInvalid = validation.displayValidation.isInvalid;\n let validationState: ValidationState | null =\n props.validationState || (isValueInvalid ? 'invalid' : null);\n\n return {\n ...validation,\n value,\n defaultValue: props.defaultValue ?? initialValue,\n setValue,\n dateRange,\n timeRange,\n granularity,\n hasTime,\n setDate(part, date) {\n if (part === 'start') {\n setDateRange({start: date, end: dateRange?.end ?? null});\n } else {\n setDateRange({start: dateRange?.start ?? null, end: date});\n }\n },\n setTime(part, time) {\n if (part === 'start') {\n setTimeRange({start: time, end: timeRange?.end ?? null});\n } else {\n setTimeRange({start: timeRange?.start ?? null, end: time});\n }\n },\n setDateTime(part, dateTime) {\n if (part === 'start') {\n setValue({start: dateTime, end: value?.end ?? null});\n } else {\n setValue({start: value?.start ?? null, end: dateTime});\n }\n },\n setDateRange,\n setTimeRange,\n ...overlayState,\n setOpen(isOpen) {\n // Commit the selected date range when the calendar is closed. Use a placeholder time if one wasn't set.\n // If only the time range was set and not the date range, don't commit. The state will be preserved until\n // the user opens the popover again.\n if (!isOpen && !(value?.start && value?.end) && isCompleteRange(dateRange) && hasTime) {\n commitValue(dateRange, {\n start: timeRange?.start || getPlaceholderTime(props.placeholderValue),\n end: timeRange?.end || getPlaceholderTime(props.placeholderValue)\n });\n }\n\n overlayState.setOpen(isOpen);\n },\n validationState,\n isInvalid: isValueInvalid,\n formatValue(locale, fieldOptions) {\n if (!value || !value.start || !value.end) {\n return null;\n }\n\n let startTimeZone = 'timeZone' in value.start ? value.start.timeZone : undefined;\n let startGranularity =\n props.granularity || (value.start && 'minute' in value.start ? 'minute' : 'day');\n let endTimeZone = 'timeZone' in value.end ? value.end.timeZone : undefined;\n let endGranularity =\n props.granularity || (value.end && 'minute' in value.end ? 'minute' : 'day');\n\n let startOptions = getFormatOptions(fieldOptions, {\n granularity: startGranularity,\n timeZone: startTimeZone,\n hideTimeZone: props.hideTimeZone,\n hourCycle: props.hourCycle,\n showEra:\n (value.start.calendar.identifier === 'gregory' && value.start.era === 'BC') ||\n (value.end.calendar.identifier === 'gregory' && value.end.era === 'BC')\n });\n\n let startDate = value.start.toDate(startTimeZone || 'UTC');\n let endDate = value.end.toDate(endTimeZone || 'UTC');\n\n let startFormatter = new DateFormatter(locale, startOptions);\n let endFormatter: Intl.DateTimeFormat;\n if (\n startTimeZone === endTimeZone &&\n startGranularity === endGranularity &&\n value.start.compare(value.end) !== 0\n ) {\n // Use formatRange, as it results in shorter output when some of the fields\n // are shared between the start and end dates (e.g. the same month).\n // Formatting will fail if the end date is before the start date. Fall back below when that happens.\n try {\n let parts = startFormatter.formatRangeToParts(startDate, endDate);\n\n // Find the separator between the start and end date. This is determined\n // by finding the last shared literal before the end range.\n let separatorIndex = -1;\n for (let i = 0; i < parts.length; i++) {\n let part = parts[i];\n if (part.source === 'shared' && part.type === 'literal') {\n separatorIndex = i;\n } else if (part.source === 'endRange') {\n break;\n }\n }\n\n // Now we can combine the parts into start and end strings.\n let start = '';\n let end = '';\n for (let i = 0; i < parts.length; i++) {\n if (i < separatorIndex) {\n start += parts[i].value;\n } else if (i > separatorIndex) {\n end += parts[i].value;\n }\n }\n\n return {start, end};\n } catch {\n // ignore\n }\n\n endFormatter = startFormatter;\n } else {\n let endOptions = getFormatOptions(fieldOptions, {\n granularity: endGranularity,\n timeZone: endTimeZone,\n hideTimeZone: props.hideTimeZone,\n hourCycle: props.hourCycle\n });\n\n endFormatter = new DateFormatter(locale, endOptions);\n }\n\n return {\n start: startFormatter.format(startDate),\n end: endFormatter.format(endDate)\n };\n },\n getDateFormatter(locale, formatOptions: FormatterOptions) {\n let newOptions = {...formatOpts, ...formatOptions};\n let newFormatOptions = getFormatOptions({}, newOptions);\n return new DateFormatter(locale, newFormatOptions);\n }\n };\n}\n\nfunction isCompleteRange<T>(value: RangeValue<T | null> | null): value is RangeValue<T> {\n return value?.start != null && value.end != null;\n}\n"],"names":[],"version":3,"file":"useDateRangePickerState.js.map"}
|