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;;;;;AA+IM,SAAS,0CACd,KAA+B;IAE/B,IAAI,iBAAC,gBAAgB,+BAAe,sBAAsB,kBAAkB,UAAS,G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AAI;YAC/C,IAAI,UAAU,UAAU,CAAC,IAAI,KAAK,KAAK,MAAM,qBAAqB,EAAE;gBAClE,iBAAiB;gBACjB,aAAa,MAAM;YACrB;QACF;mBACA;oBACA;IACF;AACF;AAEA,SAAS,mCAAa,KAAqC;IACzD,IAAI,UAAU,WACZ,OAAO;IAET,IAAI,UAAU,MACZ,OAAO,EAAE;IAEX,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ;QAAC;KAAM;AAC/C","sources":["packages/react-stately/src/select/useSelectState.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 {\n CollectionBase,\n CollectionStateBase,\n FocusableProps,\n FocusStrategy,\n HelpTextProps,\n InputBase,\n Key,\n LabelableProps,\n Node,\n Selection,\n TextInputBase,\n Validation,\n ValueBase\n} from '@react-types/shared';\nimport {FormValidationState, useFormValidationState} from '../form/useFormValidationState';\nimport {ListState, useListState} from '../list/useListState';\nimport {OverlayTriggerState, useOverlayTriggerState} from '../overlays/useOverlayTriggerState';\nimport {useControlledState} from '../utils/useControlledState';\nimport {useMemo, useState} from 'react';\n\nexport type SelectionMode = 'single' | 'multiple';\nexport type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : readonly Key[];\nexport type ChangeValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];\ntype ValidationType<M extends SelectionMode> = M extends 'single' ? Key : Key[];\n\nexport interface SelectProps<T, M extends SelectionMode = 'single'>\n extends\n CollectionBase<T>,\n Omit<InputBase, 'isReadOnly'>,\n ValueBase<ValueType<M>, ChangeValueType<M>>,\n Validation<ValidationType<M>>,\n HelpTextProps,\n LabelableProps,\n TextInputBase,\n FocusableProps {\n /**\n * Whether single or multiple selection is enabled.\n *\n * @default 'single'\n */\n selectionMode?: M;\n /**\n * The currently selected key in the collection (controlled).\n *\n * @deprecated\n */\n selectedKey?: Key | null;\n /**\n * The initial selected key in the collection (uncontrolled).\n *\n * @deprecated\n */\n defaultSelectedKey?: Key | null;\n /**\n * Handler that is called when the selection changes.\n *\n * @deprecated\n */\n onSelectionChange?: (key: Key | null) => void;\n /** Sets the open state of the menu. */\n isOpen?: boolean;\n /** Sets the default open state of the menu. */\n defaultOpen?: boolean;\n /** Method that is called when the open state of the menu changes. */\n onOpenChange?: (isOpen: boolean) => void;\n /**\n * Whether the Select should close when an item is selected. Defaults to true if selectionMode is\n * single, false otherwise.\n */\n shouldCloseOnSelect?: boolean;\n /** Whether the select should be allowed to be open when the collection is empty. */\n allowsEmptyCollection?: boolean;\n}\n\nexport interface SelectStateOptions<T, M extends SelectionMode = 'single'>\n extends Omit<SelectProps<T, M>, 'children'>, CollectionStateBase<T> {}\n\nexport interface SelectState<T, M extends SelectionMode = 'single'>\n extends ListState<T>, OverlayTriggerState, FormValidationState {\n /**\n * The key for the first selected item.\n *\n * @deprecated\n */\n readonly selectedKey: Key | null;\n\n /**\n * The default selected key.\n *\n * @deprecated\n */\n readonly defaultSelectedKey: Key | null;\n\n /**\n * Sets the selected key.\n *\n * @deprecated\n */\n setSelectedKey(key: Key | null): void;\n\n /** The current select value. */\n readonly value: ValueType<M>;\n\n /** The default select value. */\n readonly defaultValue: ValueType<M>;\n\n /** Sets the select value. */\n setValue(value: Key | readonly Key[] | null): void;\n\n /**\n * The value of the first selected item.\n *\n * @deprecated\n */\n readonly selectedItem: Node<T> | null;\n\n /** The value of the selected items. */\n readonly selectedItems: Node<T>[];\n\n /** Whether the select is currently focused. */\n readonly isFocused: boolean;\n\n /** Sets whether the select is focused. */\n setFocused(isFocused: boolean): void;\n\n /** Controls which item will be auto focused when the menu opens. */\n readonly focusStrategy: FocusStrategy | null;\n\n /** Opens the menu. */\n open(focusStrategy?: FocusStrategy | null): void;\n\n /** Toggles the menu. */\n toggle(focusStrategy?: FocusStrategy | null): void;\n}\n\n/**\n * Provides state management for a select component. Handles building a collection\n * of items from props, handles the open state for the popup menu, and manages\n * multiple selection state.\n */\nexport function useSelectState<T, M extends SelectionMode = 'single'>(\n props: SelectStateOptions<T, M>\n): SelectState<T, M> {\n let {selectionMode = 'single' as M, shouldCloseOnSelect = selectionMode === 'single'} = props;\n let triggerState = useOverlayTriggerState(props);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy | null>(null);\n let defaultValue = useMemo(() => {\n return props.defaultValue !== undefined\n ? props.defaultValue\n : ((selectionMode === 'single' ? (props.defaultSelectedKey ?? null) : []) as ValueType<M>);\n }, [props.defaultValue, props.defaultSelectedKey, selectionMode]);\n let value = useMemo(() => {\n return props.value !== undefined\n ? props.value\n : ((selectionMode === 'single' ? props.selectedKey : undefined) as ValueType<M>);\n }, [props.value, props.selectedKey, selectionMode]);\n let [controlledValue, setControlledValue] = useControlledState<Key | readonly Key[] | null>(\n value,\n defaultValue,\n props.onChange as any\n );\n // Only display the first selected item if in single selection mode but the value is an array.\n let displayValue =\n selectionMode === 'single' && Array.isArray(controlledValue)\n ? controlledValue[0]\n : controlledValue;\n let setValue = (value: Key | Key[] | null) => {\n if (selectionMode === 'single') {\n let key = Array.isArray(value) ? (value[0] ?? null) : value;\n setControlledValue(key);\n if (key !== displayValue) {\n props.onSelectionChange?.(key);\n }\n } else {\n let keys: Key[] = [];\n if (Array.isArray(value)) {\n keys = value;\n } else if (value != null) {\n keys = [value];\n }\n\n setControlledValue(keys);\n }\n };\n\n let listState = useListState({\n ...props,\n selectionMode,\n disallowEmptySelection: selectionMode === 'single',\n allowDuplicateSelectionEvents: true,\n selectedKeys: useMemo(() => convertValue(displayValue), [displayValue]),\n onSelectionChange: (keys: Selection) => {\n // impossible, but TS doesn't know that\n if (keys === 'all') {\n return;\n }\n\n if (selectionMode === 'single') {\n let key = keys.values().next().value ?? null;\n setValue(key);\n } else {\n setValue([...keys]);\n }\n if (shouldCloseOnSelect) {\n triggerState.close();\n }\n\n validationState.commitValidation();\n }\n });\n\n let selectedKey = listState.selectionManager.firstSelectedKey;\n let selectedItems = useMemo(() => {\n return [...listState.selectionManager.selectedKeys]\n .map(key => listState.collection.getItem(key))\n .filter(item => item != null);\n }, [listState.selectionManager.selectedKeys, listState.collection]);\n\n let validationState = useFormValidationState({\n ...props,\n value: Array.isArray(displayValue) && displayValue.length === 0 ? null : (displayValue as any)\n });\n\n let [isFocused, setFocused] = useState(false);\n let [initialValue] = useState(displayValue);\n\n return {\n ...validationState,\n ...listState,\n ...triggerState,\n value: displayValue as ValueType<M>,\n defaultValue: defaultValue ?? (initialValue as ValueType<M>),\n setValue,\n selectedKey,\n setSelectedKey: setValue,\n selectedItem: selectedItems[0] ?? null,\n selectedItems,\n defaultSelectedKey:\n props.defaultSelectedKey ?? (props.selectionMode === 'single' ? (initialValue as Key) : null),\n focusStrategy,\n open(focusStrategy: FocusStrategy | null = null) {\n // Don't open if the collection is empty.\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.open();\n }\n },\n toggle(focusStrategy: FocusStrategy | null = null) {\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.toggle();\n }\n },\n isFocused,\n setFocused\n };\n}\n\nfunction convertValue(value: Key | Key[] | null | undefined) {\n if (value === undefined) {\n return undefined;\n }\n if (value === null) {\n return [];\n }\n return Array.isArray(value) ? value : [value];\n}\n"],"names":[],"version":3,"file":"useSelectState.cjs.map"}
|
|
1
|
+
{"mappings":";;;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;;AA+IM,SAAS,0CACd,KAA+B;IAE/B,IAAI,iBAAC,gBAAgB,+BAAe,sBAAsB,kBAAkB,UAAS,GAAG;IACxF,IAAI,eAAe,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/C,IAAI,UAAU,UAAU,CAAC,IAAI,KAAK,KAAK,MAAM,qBAAqB,EAAE;gBAClE,iBAAiB;gBACjB,aAAa,MAAM;YACrB;QACF;mBACA;oBACA;IACF;AACF;AAEA,SAAS,mCAAa,KAAqC;IACzD,IAAI,UAAU,WACZ,OAAO;IAET,IAAI,UAAU,MACZ,OAAO,EAAE;IAEX,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ;QAAC;KAAM;AAC/C","sources":["packages/react-stately/src/select/useSelectState.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 {\n CollectionBase,\n CollectionStateBase,\n FocusableProps,\n FocusStrategy,\n HelpTextProps,\n InputBase,\n Key,\n LabelableProps,\n Node,\n Selection,\n TextInputBase,\n Validation,\n ValueBase\n} from '@react-types/shared';\nimport {FormValidationState, useFormValidationState} from '../form/useFormValidationState';\nimport {ListState, useListState} from '../list/useListState';\nimport {OverlayTriggerState, useOverlayTriggerState} from '../overlays/useOverlayTriggerState';\nimport {useControlledState} from '../utils/useControlledState';\nimport {useMemo, useState} from 'react';\n\nexport type SelectionMode = 'single' | 'multiple';\nexport type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : readonly Key[];\nexport type ChangeValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];\ntype ValidationType<M extends SelectionMode> = M extends 'single' ? Key : Key[];\n\nexport interface SelectProps<T, M extends SelectionMode = 'single'>\n extends\n CollectionBase<T>,\n Omit<InputBase, 'isReadOnly'>,\n ValueBase<ValueType<M>, ChangeValueType<M>>,\n Validation<ValidationType<M>>,\n HelpTextProps,\n LabelableProps,\n TextInputBase,\n FocusableProps {\n /**\n * Whether single or multiple selection is enabled.\n *\n * @default 'single'\n */\n selectionMode?: M;\n /**\n * The currently selected key in the collection (controlled).\n *\n * @deprecated\n */\n selectedKey?: Key | null;\n /**\n * The initial selected key in the collection (uncontrolled).\n *\n * @deprecated\n */\n defaultSelectedKey?: Key | null;\n /**\n * Handler that is called when the selection changes.\n *\n * @deprecated\n */\n onSelectionChange?: (key: Key | null) => void;\n /** Sets the open state of the menu. */\n isOpen?: boolean;\n /** Sets the default open state of the menu. */\n defaultOpen?: boolean;\n /** Method that is called when the open state of the menu changes. */\n onOpenChange?: (isOpen: boolean) => void;\n /**\n * Whether the Select should close when an item is selected. Defaults to true if selectionMode is\n * single, false otherwise.\n */\n shouldCloseOnSelect?: boolean;\n /** Whether the select should be allowed to be open when the collection is empty. */\n allowsEmptyCollection?: boolean;\n}\n\nexport interface SelectStateOptions<T, M extends SelectionMode = 'single'>\n extends Omit<SelectProps<T, M>, 'children'>, CollectionStateBase<T> {}\n\nexport interface SelectState<T, M extends SelectionMode = 'single'>\n extends ListState<T>, OverlayTriggerState, FormValidationState {\n /**\n * The key for the first selected item.\n *\n * @deprecated\n */\n readonly selectedKey: Key | null;\n\n /**\n * The default selected key.\n *\n * @deprecated\n */\n readonly defaultSelectedKey: Key | null;\n\n /**\n * Sets the selected key.\n *\n * @deprecated\n */\n setSelectedKey(key: Key | null): void;\n\n /** The current select value. */\n readonly value: ValueType<M>;\n\n /** The default select value. */\n readonly defaultValue: ValueType<M>;\n\n /** Sets the select value. */\n setValue(value: Key | readonly Key[] | null): void;\n\n /**\n * The value of the first selected item.\n *\n * @deprecated\n */\n readonly selectedItem: Node<T> | null;\n\n /** The value of the selected items. */\n readonly selectedItems: Node<T>[];\n\n /** Whether the select is currently focused. */\n readonly isFocused: boolean;\n\n /** Sets whether the select is focused. */\n setFocused(isFocused: boolean): void;\n\n /** Controls which item will be auto focused when the menu opens. */\n readonly focusStrategy: FocusStrategy | null;\n\n /** Opens the menu. */\n open(focusStrategy?: FocusStrategy | null): void;\n\n /** Toggles the menu. */\n toggle(focusStrategy?: FocusStrategy | null): void;\n}\n\n/**\n * Provides state management for a select component. Handles building a collection\n * of items from props, handles the open state for the popup menu, and manages\n * multiple selection state.\n */\nexport function useSelectState<T, M extends SelectionMode = 'single'>(\n props: SelectStateOptions<T, M>\n): SelectState<T, M> {\n let {selectionMode = 'single' as M, shouldCloseOnSelect = selectionMode === 'single'} = props;\n let triggerState = useOverlayTriggerState(props);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy | null>(null);\n let defaultValue = useMemo(() => {\n return props.defaultValue !== undefined\n ? props.defaultValue\n : ((selectionMode === 'single' ? (props.defaultSelectedKey ?? null) : []) as ValueType<M>);\n }, [props.defaultValue, props.defaultSelectedKey, selectionMode]);\n let value = useMemo(() => {\n return props.value !== undefined\n ? props.value\n : ((selectionMode === 'single' ? props.selectedKey : undefined) as ValueType<M>);\n }, [props.value, props.selectedKey, selectionMode]);\n let [controlledValue, setControlledValue] = useControlledState<Key | readonly Key[] | null>(\n value,\n defaultValue,\n props.onChange as any\n );\n // Only display the first selected item if in single selection mode but the value is an array.\n let displayValue =\n selectionMode === 'single' && Array.isArray(controlledValue)\n ? controlledValue[0]\n : controlledValue;\n let setValue = (value: Key | Key[] | null) => {\n if (selectionMode === 'single') {\n let key = Array.isArray(value) ? (value[0] ?? null) : value;\n setControlledValue(key);\n if (key !== displayValue) {\n props.onSelectionChange?.(key);\n }\n } else {\n let keys: Key[] = [];\n if (Array.isArray(value)) {\n keys = value;\n } else if (value != null) {\n keys = [value];\n }\n\n setControlledValue(keys);\n }\n };\n\n // oxlint-disable-next-line react/react-compiler\n let listState = useListState({\n ...props,\n selectionMode,\n disallowEmptySelection: selectionMode === 'single',\n allowDuplicateSelectionEvents: true,\n selectedKeys: useMemo(() => convertValue(displayValue), [displayValue]),\n onSelectionChange: (keys: Selection) => {\n // impossible, but TS doesn't know that\n if (keys === 'all') {\n return;\n }\n\n if (selectionMode === 'single') {\n let key = keys.values().next().value ?? null;\n setValue(key);\n } else {\n setValue([...keys]);\n }\n if (shouldCloseOnSelect) {\n triggerState.close();\n }\n\n validationState.commitValidation();\n }\n });\n\n let selectedKey = listState.selectionManager.firstSelectedKey;\n let selectedItems = useMemo(() => {\n return [...listState.selectionManager.selectedKeys]\n .map(key => listState.collection.getItem(key))\n .filter(item => item != null);\n }, [listState.selectionManager.selectedKeys, listState.collection]);\n\n let validationState = useFormValidationState({\n ...props,\n value: Array.isArray(displayValue) && displayValue.length === 0 ? null : (displayValue as any)\n });\n\n let [isFocused, setFocused] = useState(false);\n let [initialValue] = useState(displayValue);\n\n return {\n ...validationState,\n ...listState,\n ...triggerState,\n value: displayValue as ValueType<M>,\n defaultValue: defaultValue ?? (initialValue as ValueType<M>),\n setValue,\n selectedKey,\n setSelectedKey: setValue,\n selectedItem: selectedItems[0] ?? null,\n selectedItems,\n defaultSelectedKey:\n props.defaultSelectedKey ?? (props.selectionMode === 'single' ? (initialValue as Key) : null),\n focusStrategy,\n open(focusStrategy: FocusStrategy | null = null) {\n // Don't open if the collection is empty.\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.open();\n }\n },\n toggle(focusStrategy: FocusStrategy | null = null) {\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.toggle();\n }\n },\n isFocused,\n setFocused\n };\n}\n\nfunction convertValue(value: Key | Key[] | null | undefined) {\n if (value === undefined) {\n return undefined;\n }\n if (value === null) {\n return [];\n }\n return Array.isArray(value) ? value : [value];\n}\n"],"names":[],"version":3,"file":"useSelectState.cjs.map"}
|
|
@@ -57,6 +57,7 @@ function $1b2568040b4efc20$export$5159ec8b34d4ec12(props) {
|
|
|
57
57
|
setControlledValue(keys);
|
|
58
58
|
}
|
|
59
59
|
};
|
|
60
|
+
// oxlint-disable-next-line react/react-compiler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L,GAAG,eAAe;QAClB,GAAG,SAAS;QACZ,GAAG,YAAY;QACf,OAAO;QACP,cAAc,yBAAA,0BAAA,eAAiB;kBAC/B;qBACA;QACA,gBAAgB;QAChB,cAAc,CAAA,kBAAA,aAAa,CAAC,EAAE,cAAhB,6BAAA,kBAAoB;uBAClC;QACA,oBACE,CAAA,4BAAA,MAAM,kBAAkB,cAAxB,uCAAA,4BAA6B,MAAM,aAAa,KAAK,WAAY,eAAuB;uBAC1F;QACA,MAAK,gBAAsC,IAAI;YAC7C,yCAAyC;YACzC,IAAI,UAAU,UAAU,CAAC,IAAI,KAAK,KAAK,MAAM,qBAAqB,EAAE;gBAClE,iBAAiB;gBACjB,aAAa,IAAI;YACnB;QACF;QACA,QAAO,gBAAsC,IAAI;YAC/C,IAAI,UAAU,UAAU,CAAC,IAAI,KAAK,KAAK,MAAM,qBAAqB,EAAE;gBAClE,iBAAiB;gBACjB,aAAa,MAAM;YACrB;QACF;mBACA;oBACA;IACF;AACF;AAEA,SAAS,mCAAa,KAAqC;IACzD,IAAI,UAAU,WACZ,OAAO;IAET,IAAI,UAAU,MACZ,OAAO,EAAE;IAEX,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ;QAAC;KAAM;AAC/C","sources":["packages/react-stately/src/select/useSelectState.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 {\n CollectionBase,\n CollectionStateBase,\n FocusableProps,\n FocusStrategy,\n HelpTextProps,\n InputBase,\n Key,\n LabelableProps,\n Node,\n Selection,\n TextInputBase,\n Validation,\n ValueBase\n} from '@react-types/shared';\nimport {FormValidationState, useFormValidationState} from '../form/useFormValidationState';\nimport {ListState, useListState} from '../list/useListState';\nimport {OverlayTriggerState, useOverlayTriggerState} from '../overlays/useOverlayTriggerState';\nimport {useControlledState} from '../utils/useControlledState';\nimport {useMemo, useState} from 'react';\n\nexport type SelectionMode = 'single' | 'multiple';\nexport type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : readonly Key[];\nexport type ChangeValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];\ntype ValidationType<M extends SelectionMode> = M extends 'single' ? Key : Key[];\n\nexport interface SelectProps<T, M extends SelectionMode = 'single'>\n extends\n CollectionBase<T>,\n Omit<InputBase, 'isReadOnly'>,\n ValueBase<ValueType<M>, ChangeValueType<M>>,\n Validation<ValidationType<M>>,\n HelpTextProps,\n LabelableProps,\n TextInputBase,\n FocusableProps {\n /**\n * Whether single or multiple selection is enabled.\n *\n * @default 'single'\n */\n selectionMode?: M;\n /**\n * The currently selected key in the collection (controlled).\n *\n * @deprecated\n */\n selectedKey?: Key | null;\n /**\n * The initial selected key in the collection (uncontrolled).\n *\n * @deprecated\n */\n defaultSelectedKey?: Key | null;\n /**\n * Handler that is called when the selection changes.\n *\n * @deprecated\n */\n onSelectionChange?: (key: Key | null) => void;\n /** Sets the open state of the menu. */\n isOpen?: boolean;\n /** Sets the default open state of the menu. */\n defaultOpen?: boolean;\n /** Method that is called when the open state of the menu changes. */\n onOpenChange?: (isOpen: boolean) => void;\n /**\n * Whether the Select should close when an item is selected. Defaults to true if selectionMode is\n * single, false otherwise.\n */\n shouldCloseOnSelect?: boolean;\n /** Whether the select should be allowed to be open when the collection is empty. */\n allowsEmptyCollection?: boolean;\n}\n\nexport interface SelectStateOptions<T, M extends SelectionMode = 'single'>\n extends Omit<SelectProps<T, M>, 'children'>, CollectionStateBase<T> {}\n\nexport interface SelectState<T, M extends SelectionMode = 'single'>\n extends ListState<T>, OverlayTriggerState, FormValidationState {\n /**\n * The key for the first selected item.\n *\n * @deprecated\n */\n readonly selectedKey: Key | null;\n\n /**\n * The default selected key.\n *\n * @deprecated\n */\n readonly defaultSelectedKey: Key | null;\n\n /**\n * Sets the selected key.\n *\n * @deprecated\n */\n setSelectedKey(key: Key | null): void;\n\n /** The current select value. */\n readonly value: ValueType<M>;\n\n /** The default select value. */\n readonly defaultValue: ValueType<M>;\n\n /** Sets the select value. */\n setValue(value: Key | readonly Key[] | null): void;\n\n /**\n * The value of the first selected item.\n *\n * @deprecated\n */\n readonly selectedItem: Node<T> | null;\n\n /** The value of the selected items. */\n readonly selectedItems: Node<T>[];\n\n /** Whether the select is currently focused. */\n readonly isFocused: boolean;\n\n /** Sets whether the select is focused. */\n setFocused(isFocused: boolean): void;\n\n /** Controls which item will be auto focused when the menu opens. */\n readonly focusStrategy: FocusStrategy | null;\n\n /** Opens the menu. */\n open(focusStrategy?: FocusStrategy | null): void;\n\n /** Toggles the menu. */\n toggle(focusStrategy?: FocusStrategy | null): void;\n}\n\n/**\n * Provides state management for a select component. Handles building a collection\n * of items from props, handles the open state for the popup menu, and manages\n * multiple selection state.\n */\nexport function useSelectState<T, M extends SelectionMode = 'single'>(\n props: SelectStateOptions<T, M>\n): SelectState<T, M> {\n let {selectionMode = 'single' as M, shouldCloseOnSelect = selectionMode === 'single'} = props;\n let triggerState = useOverlayTriggerState(props);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy | null>(null);\n let defaultValue = useMemo(() => {\n return props.defaultValue !== undefined\n ? props.defaultValue\n : ((selectionMode === 'single' ? (props.defaultSelectedKey ?? null) : []) as ValueType<M>);\n }, [props.defaultValue, props.defaultSelectedKey, selectionMode]);\n let value = useMemo(() => {\n return props.value !== undefined\n ? props.value\n : ((selectionMode === 'single' ? props.selectedKey : undefined) as ValueType<M>);\n }, [props.value, props.selectedKey, selectionMode]);\n let [controlledValue, setControlledValue] = useControlledState<Key | readonly Key[] | null>(\n value,\n defaultValue,\n props.onChange as any\n );\n // Only display the first selected item if in single selection mode but the value is an array.\n let displayValue =\n selectionMode === 'single' && Array.isArray(controlledValue)\n ? controlledValue[0]\n : controlledValue;\n let setValue = (value: Key | Key[] | null) => {\n if (selectionMode === 'single') {\n let key = Array.isArray(value) ? (value[0] ?? null) : value;\n setControlledValue(key);\n if (key !== displayValue) {\n props.onSelectionChange?.(key);\n }\n } else {\n let keys: Key[] = [];\n if (Array.isArray(value)) {\n keys = value;\n } else if (value != null) {\n keys = [value];\n }\n\n setControlledValue(keys);\n }\n };\n\n let listState = useListState({\n ...props,\n selectionMode,\n disallowEmptySelection: selectionMode === 'single',\n allowDuplicateSelectionEvents: true,\n selectedKeys: useMemo(() => convertValue(displayValue), [displayValue]),\n onSelectionChange: (keys: Selection) => {\n // impossible, but TS doesn't know that\n if (keys === 'all') {\n return;\n }\n\n if (selectionMode === 'single') {\n let key = keys.values().next().value ?? null;\n setValue(key);\n } else {\n setValue([...keys]);\n }\n if (shouldCloseOnSelect) {\n triggerState.close();\n }\n\n validationState.commitValidation();\n }\n });\n\n let selectedKey = listState.selectionManager.firstSelectedKey;\n let selectedItems = useMemo(() => {\n return [...listState.selectionManager.selectedKeys]\n .map(key => listState.collection.getItem(key))\n .filter(item => item != null);\n }, [listState.selectionManager.selectedKeys, listState.collection]);\n\n let validationState = useFormValidationState({\n ...props,\n value: Array.isArray(displayValue) && displayValue.length === 0 ? null : (displayValue as any)\n });\n\n let [isFocused, setFocused] = useState(false);\n let [initialValue] = useState(displayValue);\n\n return {\n ...validationState,\n ...listState,\n ...triggerState,\n value: displayValue as ValueType<M>,\n defaultValue: defaultValue ?? (initialValue as ValueType<M>),\n setValue,\n selectedKey,\n setSelectedKey: setValue,\n selectedItem: selectedItems[0] ?? null,\n selectedItems,\n defaultSelectedKey:\n props.defaultSelectedKey ?? (props.selectionMode === 'single' ? (initialValue as Key) : null),\n focusStrategy,\n open(focusStrategy: FocusStrategy | null = null) {\n // Don't open if the collection is empty.\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.open();\n }\n },\n toggle(focusStrategy: FocusStrategy | null = null) {\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.toggle();\n }\n },\n isFocused,\n setFocused\n };\n}\n\nfunction convertValue(value: Key | Key[] | null | undefined) {\n if (value === undefined) {\n return undefined;\n }\n if (value === null) {\n return [];\n }\n return Array.isArray(value) ? value : [value];\n}\n"],"names":[],"version":3,"file":"useSelectState.js.map"}
|
|
1
|
+
{"mappings":";;;;;;AAAA;;;;;;;;;;CAUC;;;;;AA+IM,SAAS,0CACd,KAA+B;IAE/B,IAAI,iBAAC,gBAAgB,+BAAe,sBAAsB,kBAAkB,UAAS,GAAG;IACxF,IAAI,eAAe,CAAA,GAAA,yCAAqB,EAAE;IAC1C,IAAI,CAAC,eAAe,iBAAiB,GAAG,CAAA,GAAA,eAAO,EAAwB;IACvE,IAAI,eAAe,CAAA,GAAA,cAAM,EAAE;YAGW;QAFpC,OAAO,MAAM,YAAY,KAAK,YAC1B,MAAM,YAAY,GAChB,kBAAkB,WAAY,CAAA,4BAAA,MAAM,kBAAkB,cAAxB,uCAAA,4BAA4B,OAAQ,EAAE;IAC5E,GAAG;QAAC,MAAM,YAAY;QAAE,MAAM,kBAAkB;QAAE;KAAc;IAChE,IAAI,QAAQ,CAAA,GAAA,cAAM,EAAE;QAClB,OAAO,MAAM,KAAK,KAAK,YACnB,MAAM,KAAK,GACT,kBAAkB,WAAW,MAAM,WAAW,GAAG;IACzD,GAAG;QAAC,MAAM,KAAK;QAAE,MAAM,WAAW;QAAE;KAAc;IAClD,IAAI,CAAC,iBAAiB,mBAAmB,GAAG,CAAA,GAAA,yCAAiB,EAC3D,OACA,cACA,MAAM,QAAQ;IAEhB,8FAA8F;IAC9F,IAAI,eACF,kBAAkB,YAAY,MAAM,OAAO,CAAC,mBACxC,eAAe,CAAC,EAAE,GAClB;IACN,IAAI,WAAW,CAAC;QACd,IAAI,kBAAkB,UAAU;gBAI5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AC,iBAAiB,aAAa,MAAM,KAAK,IAAI,OAAQ;IAC5E;IAEA,IAAI,CAAC,WAAW,WAAW,GAAG,CAAA,GAAA,eAAO,EAAE;IACvC,IAAI,CAAC,aAAa,GAAG,CAAA,GAAA,eAAO,EAAE;QAWd,iBAGZ;IAZJ,OAAO;QACL,GAAG,eAAe;QAClB,GAAG,SAAS;QACZ,GAAG,YAAY;QACf,OAAO;QACP,cAAc,yBAAA,0BAAA,eAAiB;kBAC/B;qBACA;QACA,gBAAgB;QAChB,cAAc,CAAA,kBAAA,aAAa,CAAC,EAAE,cAAhB,6BAAA,kBAAoB;uBAClC;QACA,oBACE,CAAA,4BAAA,MAAM,kBAAkB,cAAxB,uCAAA,4BAA6B,MAAM,aAAa,KAAK,WAAY,eAAuB;uBAC1F;QACA,MAAK,gBAAsC,IAAI;YAC7C,yCAAyC;YACzC,IAAI,UAAU,UAAU,CAAC,IAAI,KAAK,KAAK,MAAM,qBAAqB,EAAE;gBAClE,iBAAiB;gBACjB,aAAa,IAAI;YACnB;QACF;QACA,QAAO,gBAAsC,IAAI;YAC/C,IAAI,UAAU,UAAU,CAAC,IAAI,KAAK,KAAK,MAAM,qBAAqB,EAAE;gBAClE,iBAAiB;gBACjB,aAAa,MAAM;YACrB;QACF;mBACA;oBACA;IACF;AACF;AAEA,SAAS,mCAAa,KAAqC;IACzD,IAAI,UAAU,WACZ,OAAO;IAET,IAAI,UAAU,MACZ,OAAO,EAAE;IAEX,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ;QAAC;KAAM;AAC/C","sources":["packages/react-stately/src/select/useSelectState.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 {\n CollectionBase,\n CollectionStateBase,\n FocusableProps,\n FocusStrategy,\n HelpTextProps,\n InputBase,\n Key,\n LabelableProps,\n Node,\n Selection,\n TextInputBase,\n Validation,\n ValueBase\n} from '@react-types/shared';\nimport {FormValidationState, useFormValidationState} from '../form/useFormValidationState';\nimport {ListState, useListState} from '../list/useListState';\nimport {OverlayTriggerState, useOverlayTriggerState} from '../overlays/useOverlayTriggerState';\nimport {useControlledState} from '../utils/useControlledState';\nimport {useMemo, useState} from 'react';\n\nexport type SelectionMode = 'single' | 'multiple';\nexport type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : readonly Key[];\nexport type ChangeValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];\ntype ValidationType<M extends SelectionMode> = M extends 'single' ? Key : Key[];\n\nexport interface SelectProps<T, M extends SelectionMode = 'single'>\n extends\n CollectionBase<T>,\n Omit<InputBase, 'isReadOnly'>,\n ValueBase<ValueType<M>, ChangeValueType<M>>,\n Validation<ValidationType<M>>,\n HelpTextProps,\n LabelableProps,\n TextInputBase,\n FocusableProps {\n /**\n * Whether single or multiple selection is enabled.\n *\n * @default 'single'\n */\n selectionMode?: M;\n /**\n * The currently selected key in the collection (controlled).\n *\n * @deprecated\n */\n selectedKey?: Key | null;\n /**\n * The initial selected key in the collection (uncontrolled).\n *\n * @deprecated\n */\n defaultSelectedKey?: Key | null;\n /**\n * Handler that is called when the selection changes.\n *\n * @deprecated\n */\n onSelectionChange?: (key: Key | null) => void;\n /** Sets the open state of the menu. */\n isOpen?: boolean;\n /** Sets the default open state of the menu. */\n defaultOpen?: boolean;\n /** Method that is called when the open state of the menu changes. */\n onOpenChange?: (isOpen: boolean) => void;\n /**\n * Whether the Select should close when an item is selected. Defaults to true if selectionMode is\n * single, false otherwise.\n */\n shouldCloseOnSelect?: boolean;\n /** Whether the select should be allowed to be open when the collection is empty. */\n allowsEmptyCollection?: boolean;\n}\n\nexport interface SelectStateOptions<T, M extends SelectionMode = 'single'>\n extends Omit<SelectProps<T, M>, 'children'>, CollectionStateBase<T> {}\n\nexport interface SelectState<T, M extends SelectionMode = 'single'>\n extends ListState<T>, OverlayTriggerState, FormValidationState {\n /**\n * The key for the first selected item.\n *\n * @deprecated\n */\n readonly selectedKey: Key | null;\n\n /**\n * The default selected key.\n *\n * @deprecated\n */\n readonly defaultSelectedKey: Key | null;\n\n /**\n * Sets the selected key.\n *\n * @deprecated\n */\n setSelectedKey(key: Key | null): void;\n\n /** The current select value. */\n readonly value: ValueType<M>;\n\n /** The default select value. */\n readonly defaultValue: ValueType<M>;\n\n /** Sets the select value. */\n setValue(value: Key | readonly Key[] | null): void;\n\n /**\n * The value of the first selected item.\n *\n * @deprecated\n */\n readonly selectedItem: Node<T> | null;\n\n /** The value of the selected items. */\n readonly selectedItems: Node<T>[];\n\n /** Whether the select is currently focused. */\n readonly isFocused: boolean;\n\n /** Sets whether the select is focused. */\n setFocused(isFocused: boolean): void;\n\n /** Controls which item will be auto focused when the menu opens. */\n readonly focusStrategy: FocusStrategy | null;\n\n /** Opens the menu. */\n open(focusStrategy?: FocusStrategy | null): void;\n\n /** Toggles the menu. */\n toggle(focusStrategy?: FocusStrategy | null): void;\n}\n\n/**\n * Provides state management for a select component. Handles building a collection\n * of items from props, handles the open state for the popup menu, and manages\n * multiple selection state.\n */\nexport function useSelectState<T, M extends SelectionMode = 'single'>(\n props: SelectStateOptions<T, M>\n): SelectState<T, M> {\n let {selectionMode = 'single' as M, shouldCloseOnSelect = selectionMode === 'single'} = props;\n let triggerState = useOverlayTriggerState(props);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy | null>(null);\n let defaultValue = useMemo(() => {\n return props.defaultValue !== undefined\n ? props.defaultValue\n : ((selectionMode === 'single' ? (props.defaultSelectedKey ?? null) : []) as ValueType<M>);\n }, [props.defaultValue, props.defaultSelectedKey, selectionMode]);\n let value = useMemo(() => {\n return props.value !== undefined\n ? props.value\n : ((selectionMode === 'single' ? props.selectedKey : undefined) as ValueType<M>);\n }, [props.value, props.selectedKey, selectionMode]);\n let [controlledValue, setControlledValue] = useControlledState<Key | readonly Key[] | null>(\n value,\n defaultValue,\n props.onChange as any\n );\n // Only display the first selected item if in single selection mode but the value is an array.\n let displayValue =\n selectionMode === 'single' && Array.isArray(controlledValue)\n ? controlledValue[0]\n : controlledValue;\n let setValue = (value: Key | Key[] | null) => {\n if (selectionMode === 'single') {\n let key = Array.isArray(value) ? (value[0] ?? null) : value;\n setControlledValue(key);\n if (key !== displayValue) {\n props.onSelectionChange?.(key);\n }\n } else {\n let keys: Key[] = [];\n if (Array.isArray(value)) {\n keys = value;\n } else if (value != null) {\n keys = [value];\n }\n\n setControlledValue(keys);\n }\n };\n\n // oxlint-disable-next-line react/react-compiler\n let listState = useListState({\n ...props,\n selectionMode,\n disallowEmptySelection: selectionMode === 'single',\n allowDuplicateSelectionEvents: true,\n selectedKeys: useMemo(() => convertValue(displayValue), [displayValue]),\n onSelectionChange: (keys: Selection) => {\n // impossible, but TS doesn't know that\n if (keys === 'all') {\n return;\n }\n\n if (selectionMode === 'single') {\n let key = keys.values().next().value ?? null;\n setValue(key);\n } else {\n setValue([...keys]);\n }\n if (shouldCloseOnSelect) {\n triggerState.close();\n }\n\n validationState.commitValidation();\n }\n });\n\n let selectedKey = listState.selectionManager.firstSelectedKey;\n let selectedItems = useMemo(() => {\n return [...listState.selectionManager.selectedKeys]\n .map(key => listState.collection.getItem(key))\n .filter(item => item != null);\n }, [listState.selectionManager.selectedKeys, listState.collection]);\n\n let validationState = useFormValidationState({\n ...props,\n value: Array.isArray(displayValue) && displayValue.length === 0 ? null : (displayValue as any)\n });\n\n let [isFocused, setFocused] = useState(false);\n let [initialValue] = useState(displayValue);\n\n return {\n ...validationState,\n ...listState,\n ...triggerState,\n value: displayValue as ValueType<M>,\n defaultValue: defaultValue ?? (initialValue as ValueType<M>),\n setValue,\n selectedKey,\n setSelectedKey: setValue,\n selectedItem: selectedItems[0] ?? null,\n selectedItems,\n defaultSelectedKey:\n props.defaultSelectedKey ?? (props.selectionMode === 'single' ? (initialValue as Key) : null),\n focusStrategy,\n open(focusStrategy: FocusStrategy | null = null) {\n // Don't open if the collection is empty.\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.open();\n }\n },\n toggle(focusStrategy: FocusStrategy | null = null) {\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.toggle();\n }\n },\n isFocused,\n setFocused\n };\n}\n\nfunction convertValue(value: Key | Key[] | null | undefined) {\n if (value === undefined) {\n return undefined;\n }\n if (value === null) {\n return [];\n }\n return Array.isArray(value) ? value : [value];\n}\n"],"names":[],"version":3,"file":"useSelectState.js.map"}
|
|
@@ -54,6 +54,7 @@ function $29256f53a2edafe9$export$5159ec8b34d4ec12(props) {
|
|
|
54
54
|
setControlledValue(keys);
|
|
55
55
|
}
|
|
56
56
|
};
|
|
57
|
+
// oxlint-disable-next-line react/react-compiler
|
|
57
58
|
let listState = (0, $b14b6f590b50af39$export$2f645645f7bca764)({
|
|
58
59
|
...props,
|
|
59
60
|
selectionMode: selectionMode,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;AAAA;;;;;;;;;;CAUC;;;;;AA+IM,SAAS,0CACd,KAA+B;IAE/B,IAAI,iBAAC,gBAAgB,+BAAe,sBAAsB,kBAAkB,UAAS,GAAG;IACxF,IAAI,eAAe,CAAA,GAAA,yCAAqB,EAAE;IAC1C,IAAI,CAAC,eAAe,iBAAiB,GAAG,CAAA,GAAA,eAAO,EAAwB;IACvE,IAAI,eAAe,CAAA,GAAA,cAAM,EAAE;QACzB,OAAO,MAAM,YAAY,KAAK,YAC1B,MAAM,YAAY,GAChB,kBAAkB,WAAY,MAAM,kBAAkB,IAAI,OAAQ,EAAE;IAC5E,GAAG;QAAC,MAAM,YAAY;QAAE,MA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ately/src/select/useSelectState.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 {\n CollectionBase,\n CollectionStateBase,\n FocusableProps,\n FocusStrategy,\n HelpTextProps,\n InputBase,\n Key,\n LabelableProps,\n Node,\n Selection,\n TextInputBase,\n Validation,\n ValueBase\n} from '@react-types/shared';\nimport {FormValidationState, useFormValidationState} from '../form/useFormValidationState';\nimport {ListState, useListState} from '../list/useListState';\nimport {OverlayTriggerState, useOverlayTriggerState} from '../overlays/useOverlayTriggerState';\nimport {useControlledState} from '../utils/useControlledState';\nimport {useMemo, useState} from 'react';\n\nexport type SelectionMode = 'single' | 'multiple';\nexport type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : readonly Key[];\nexport type ChangeValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];\ntype ValidationType<M extends SelectionMode> = M extends 'single' ? Key : Key[];\n\nexport interface SelectProps<T, M extends SelectionMode = 'single'>\n extends\n CollectionBase<T>,\n Omit<InputBase, 'isReadOnly'>,\n ValueBase<ValueType<M>, ChangeValueType<M>>,\n Validation<ValidationType<M>>,\n HelpTextProps,\n LabelableProps,\n TextInputBase,\n FocusableProps {\n /**\n * Whether single or multiple selection is enabled.\n *\n * @default 'single'\n */\n selectionMode?: M;\n /**\n * The currently selected key in the collection (controlled).\n *\n * @deprecated\n */\n selectedKey?: Key | null;\n /**\n * The initial selected key in the collection (uncontrolled).\n *\n * @deprecated\n */\n defaultSelectedKey?: Key | null;\n /**\n * Handler that is called when the selection changes.\n *\n * @deprecated\n */\n onSelectionChange?: (key: Key | null) => void;\n /** Sets the open state of the menu. */\n isOpen?: boolean;\n /** Sets the default open state of the menu. */\n defaultOpen?: boolean;\n /** Method that is called when the open state of the menu changes. */\n onOpenChange?: (isOpen: boolean) => void;\n /**\n * Whether the Select should close when an item is selected. Defaults to true if selectionMode is\n * single, false otherwise.\n */\n shouldCloseOnSelect?: boolean;\n /** Whether the select should be allowed to be open when the collection is empty. */\n allowsEmptyCollection?: boolean;\n}\n\nexport interface SelectStateOptions<T, M extends SelectionMode = 'single'>\n extends Omit<SelectProps<T, M>, 'children'>, CollectionStateBase<T> {}\n\nexport interface SelectState<T, M extends SelectionMode = 'single'>\n extends ListState<T>, OverlayTriggerState, FormValidationState {\n /**\n * The key for the first selected item.\n *\n * @deprecated\n */\n readonly selectedKey: Key | null;\n\n /**\n * The default selected key.\n *\n * @deprecated\n */\n readonly defaultSelectedKey: Key | null;\n\n /**\n * Sets the selected key.\n *\n * @deprecated\n */\n setSelectedKey(key: Key | null): void;\n\n /** The current select value. */\n readonly value: ValueType<M>;\n\n /** The default select value. */\n readonly defaultValue: ValueType<M>;\n\n /** Sets the select value. */\n setValue(value: Key | readonly Key[] | null): void;\n\n /**\n * The value of the first selected item.\n *\n * @deprecated\n */\n readonly selectedItem: Node<T> | null;\n\n /** The value of the selected items. */\n readonly selectedItems: Node<T>[];\n\n /** Whether the select is currently focused. */\n readonly isFocused: boolean;\n\n /** Sets whether the select is focused. */\n setFocused(isFocused: boolean): void;\n\n /** Controls which item will be auto focused when the menu opens. */\n readonly focusStrategy: FocusStrategy | null;\n\n /** Opens the menu. */\n open(focusStrategy?: FocusStrategy | null): void;\n\n /** Toggles the menu. */\n toggle(focusStrategy?: FocusStrategy | null): void;\n}\n\n/**\n * Provides state management for a select component. Handles building a collection\n * of items from props, handles the open state for the popup menu, and manages\n * multiple selection state.\n */\nexport function useSelectState<T, M extends SelectionMode = 'single'>(\n props: SelectStateOptions<T, M>\n): SelectState<T, M> {\n let {selectionMode = 'single' as M, shouldCloseOnSelect = selectionMode === 'single'} = props;\n let triggerState = useOverlayTriggerState(props);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy | null>(null);\n let defaultValue = useMemo(() => {\n return props.defaultValue !== undefined\n ? props.defaultValue\n : ((selectionMode === 'single' ? (props.defaultSelectedKey ?? null) : []) as ValueType<M>);\n }, [props.defaultValue, props.defaultSelectedKey, selectionMode]);\n let value = useMemo(() => {\n return props.value !== undefined\n ? props.value\n : ((selectionMode === 'single' ? props.selectedKey : undefined) as ValueType<M>);\n }, [props.value, props.selectedKey, selectionMode]);\n let [controlledValue, setControlledValue] = useControlledState<Key | readonly Key[] | null>(\n value,\n defaultValue,\n props.onChange as any\n );\n // Only display the first selected item if in single selection mode but the value is an array.\n let displayValue =\n selectionMode === 'single' && Array.isArray(controlledValue)\n ? controlledValue[0]\n : controlledValue;\n let setValue = (value: Key | Key[] | null) => {\n if (selectionMode === 'single') {\n let key = Array.isArray(value) ? (value[0] ?? null) : value;\n setControlledValue(key);\n if (key !== displayValue) {\n props.onSelectionChange?.(key);\n }\n } else {\n let keys: Key[] = [];\n if (Array.isArray(value)) {\n keys = value;\n } else if (value != null) {\n keys = [value];\n }\n\n setControlledValue(keys);\n }\n };\n\n let listState = useListState({\n ...props,\n selectionMode,\n disallowEmptySelection: selectionMode === 'single',\n allowDuplicateSelectionEvents: true,\n selectedKeys: useMemo(() => convertValue(displayValue), [displayValue]),\n onSelectionChange: (keys: Selection) => {\n // impossible, but TS doesn't know that\n if (keys === 'all') {\n return;\n }\n\n if (selectionMode === 'single') {\n let key = keys.values().next().value ?? null;\n setValue(key);\n } else {\n setValue([...keys]);\n }\n if (shouldCloseOnSelect) {\n triggerState.close();\n }\n\n validationState.commitValidation();\n }\n });\n\n let selectedKey = listState.selectionManager.firstSelectedKey;\n let selectedItems = useMemo(() => {\n return [...listState.selectionManager.selectedKeys]\n .map(key => listState.collection.getItem(key))\n .filter(item => item != null);\n }, [listState.selectionManager.selectedKeys, listState.collection]);\n\n let validationState = useFormValidationState({\n ...props,\n value: Array.isArray(displayValue) && displayValue.length === 0 ? null : (displayValue as any)\n });\n\n let [isFocused, setFocused] = useState(false);\n let [initialValue] = useState(displayValue);\n\n return {\n ...validationState,\n ...listState,\n ...triggerState,\n value: displayValue as ValueType<M>,\n defaultValue: defaultValue ?? (initialValue as ValueType<M>),\n setValue,\n selectedKey,\n setSelectedKey: setValue,\n selectedItem: selectedItems[0] ?? null,\n selectedItems,\n defaultSelectedKey:\n props.defaultSelectedKey ?? (props.selectionMode === 'single' ? (initialValue as Key) : null),\n focusStrategy,\n open(focusStrategy: FocusStrategy | null = null) {\n // Don't open if the collection is empty.\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.open();\n }\n },\n toggle(focusStrategy: FocusStrategy | null = null) {\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.toggle();\n }\n },\n isFocused,\n setFocused\n };\n}\n\nfunction convertValue(value: Key | Key[] | null | undefined) {\n if (value === undefined) {\n return undefined;\n }\n if (value === null) {\n return [];\n }\n return Array.isArray(value) ? value : [value];\n}\n"],"names":[],"version":3,"file":"useSelectState.mjs.map"}
|
|
1
|
+
{"mappings":";;;;;;AAAA;;;;;;;;;;CAUC;;;;;AA+IM,SAAS,0CACd,KAA+B;IAE/B,IAAI,iBAAC,gBAAgB,+BAAe,sBAAsB,kBAAkB,UAAS,GAAG;IACxF,IAAI,eAAe,CAAA,GAAA,yCAAqB,EAAE;IAC1C,IAAI,CAAC,eAAe,iBAAiB,GAAG,CAAA,GAAA,eAAO,EAAwB;IACvE,IAAI,eAAe,CAAA,GAAA,cAAM,EAAE;QACzB,OAAO,MAAM,YAAY,KAAK,YAC1B,MAAM,YAAY,GAChB,kBAAkB,WAAY,MAAM,kBAAkB,IAAI,OAAQ,EAAE;IAC5E,GAAG;QAAC,MAAM,YAAY;QAAE,MAAM,kBAAkB;QAAE;KAAc;IAChE,IAAI,QA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esContent":["/*\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 {\n CollectionBase,\n CollectionStateBase,\n FocusableProps,\n FocusStrategy,\n HelpTextProps,\n InputBase,\n Key,\n LabelableProps,\n Node,\n Selection,\n TextInputBase,\n Validation,\n ValueBase\n} from '@react-types/shared';\nimport {FormValidationState, useFormValidationState} from '../form/useFormValidationState';\nimport {ListState, useListState} from '../list/useListState';\nimport {OverlayTriggerState, useOverlayTriggerState} from '../overlays/useOverlayTriggerState';\nimport {useControlledState} from '../utils/useControlledState';\nimport {useMemo, useState} from 'react';\n\nexport type SelectionMode = 'single' | 'multiple';\nexport type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : readonly Key[];\nexport type ChangeValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];\ntype ValidationType<M extends SelectionMode> = M extends 'single' ? Key : Key[];\n\nexport interface SelectProps<T, M extends SelectionMode = 'single'>\n extends\n CollectionBase<T>,\n Omit<InputBase, 'isReadOnly'>,\n ValueBase<ValueType<M>, ChangeValueType<M>>,\n Validation<ValidationType<M>>,\n HelpTextProps,\n LabelableProps,\n TextInputBase,\n FocusableProps {\n /**\n * Whether single or multiple selection is enabled.\n *\n * @default 'single'\n */\n selectionMode?: M;\n /**\n * The currently selected key in the collection (controlled).\n *\n * @deprecated\n */\n selectedKey?: Key | null;\n /**\n * The initial selected key in the collection (uncontrolled).\n *\n * @deprecated\n */\n defaultSelectedKey?: Key | null;\n /**\n * Handler that is called when the selection changes.\n *\n * @deprecated\n */\n onSelectionChange?: (key: Key | null) => void;\n /** Sets the open state of the menu. */\n isOpen?: boolean;\n /** Sets the default open state of the menu. */\n defaultOpen?: boolean;\n /** Method that is called when the open state of the menu changes. */\n onOpenChange?: (isOpen: boolean) => void;\n /**\n * Whether the Select should close when an item is selected. Defaults to true if selectionMode is\n * single, false otherwise.\n */\n shouldCloseOnSelect?: boolean;\n /** Whether the select should be allowed to be open when the collection is empty. */\n allowsEmptyCollection?: boolean;\n}\n\nexport interface SelectStateOptions<T, M extends SelectionMode = 'single'>\n extends Omit<SelectProps<T, M>, 'children'>, CollectionStateBase<T> {}\n\nexport interface SelectState<T, M extends SelectionMode = 'single'>\n extends ListState<T>, OverlayTriggerState, FormValidationState {\n /**\n * The key for the first selected item.\n *\n * @deprecated\n */\n readonly selectedKey: Key | null;\n\n /**\n * The default selected key.\n *\n * @deprecated\n */\n readonly defaultSelectedKey: Key | null;\n\n /**\n * Sets the selected key.\n *\n * @deprecated\n */\n setSelectedKey(key: Key | null): void;\n\n /** The current select value. */\n readonly value: ValueType<M>;\n\n /** The default select value. */\n readonly defaultValue: ValueType<M>;\n\n /** Sets the select value. */\n setValue(value: Key | readonly Key[] | null): void;\n\n /**\n * The value of the first selected item.\n *\n * @deprecated\n */\n readonly selectedItem: Node<T> | null;\n\n /** The value of the selected items. */\n readonly selectedItems: Node<T>[];\n\n /** Whether the select is currently focused. */\n readonly isFocused: boolean;\n\n /** Sets whether the select is focused. */\n setFocused(isFocused: boolean): void;\n\n /** Controls which item will be auto focused when the menu opens. */\n readonly focusStrategy: FocusStrategy | null;\n\n /** Opens the menu. */\n open(focusStrategy?: FocusStrategy | null): void;\n\n /** Toggles the menu. */\n toggle(focusStrategy?: FocusStrategy | null): void;\n}\n\n/**\n * Provides state management for a select component. Handles building a collection\n * of items from props, handles the open state for the popup menu, and manages\n * multiple selection state.\n */\nexport function useSelectState<T, M extends SelectionMode = 'single'>(\n props: SelectStateOptions<T, M>\n): SelectState<T, M> {\n let {selectionMode = 'single' as M, shouldCloseOnSelect = selectionMode === 'single'} = props;\n let triggerState = useOverlayTriggerState(props);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy | null>(null);\n let defaultValue = useMemo(() => {\n return props.defaultValue !== undefined\n ? props.defaultValue\n : ((selectionMode === 'single' ? (props.defaultSelectedKey ?? null) : []) as ValueType<M>);\n }, [props.defaultValue, props.defaultSelectedKey, selectionMode]);\n let value = useMemo(() => {\n return props.value !== undefined\n ? props.value\n : ((selectionMode === 'single' ? props.selectedKey : undefined) as ValueType<M>);\n }, [props.value, props.selectedKey, selectionMode]);\n let [controlledValue, setControlledValue] = useControlledState<Key | readonly Key[] | null>(\n value,\n defaultValue,\n props.onChange as any\n );\n // Only display the first selected item if in single selection mode but the value is an array.\n let displayValue =\n selectionMode === 'single' && Array.isArray(controlledValue)\n ? controlledValue[0]\n : controlledValue;\n let setValue = (value: Key | Key[] | null) => {\n if (selectionMode === 'single') {\n let key = Array.isArray(value) ? (value[0] ?? null) : value;\n setControlledValue(key);\n if (key !== displayValue) {\n props.onSelectionChange?.(key);\n }\n } else {\n let keys: Key[] = [];\n if (Array.isArray(value)) {\n keys = value;\n } else if (value != null) {\n keys = [value];\n }\n\n setControlledValue(keys);\n }\n };\n\n // oxlint-disable-next-line react/react-compiler\n let listState = useListState({\n ...props,\n selectionMode,\n disallowEmptySelection: selectionMode === 'single',\n allowDuplicateSelectionEvents: true,\n selectedKeys: useMemo(() => convertValue(displayValue), [displayValue]),\n onSelectionChange: (keys: Selection) => {\n // impossible, but TS doesn't know that\n if (keys === 'all') {\n return;\n }\n\n if (selectionMode === 'single') {\n let key = keys.values().next().value ?? null;\n setValue(key);\n } else {\n setValue([...keys]);\n }\n if (shouldCloseOnSelect) {\n triggerState.close();\n }\n\n validationState.commitValidation();\n }\n });\n\n let selectedKey = listState.selectionManager.firstSelectedKey;\n let selectedItems = useMemo(() => {\n return [...listState.selectionManager.selectedKeys]\n .map(key => listState.collection.getItem(key))\n .filter(item => item != null);\n }, [listState.selectionManager.selectedKeys, listState.collection]);\n\n let validationState = useFormValidationState({\n ...props,\n value: Array.isArray(displayValue) && displayValue.length === 0 ? null : (displayValue as any)\n });\n\n let [isFocused, setFocused] = useState(false);\n let [initialValue] = useState(displayValue);\n\n return {\n ...validationState,\n ...listState,\n ...triggerState,\n value: displayValue as ValueType<M>,\n defaultValue: defaultValue ?? (initialValue as ValueType<M>),\n setValue,\n selectedKey,\n setSelectedKey: setValue,\n selectedItem: selectedItems[0] ?? null,\n selectedItems,\n defaultSelectedKey:\n props.defaultSelectedKey ?? (props.selectionMode === 'single' ? (initialValue as Key) : null),\n focusStrategy,\n open(focusStrategy: FocusStrategy | null = null) {\n // Don't open if the collection is empty.\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.open();\n }\n },\n toggle(focusStrategy: FocusStrategy | null = null) {\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.toggle();\n }\n },\n isFocused,\n setFocused\n };\n}\n\nfunction convertValue(value: Key | Key[] | null | undefined) {\n if (value === undefined) {\n return undefined;\n }\n if (value === null) {\n return [];\n }\n return Array.isArray(value) ? value : [value];\n}\n"],"names":[],"version":3,"file":"useSelectState.mjs.map"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;AA8LD,MAAM,0CAAoB;AAC1B,MAAM,0CAAoB;AAC1B,MAAM,2CAAqB;AAapB,SAAS,0CACd,KAA4B;IAE5B,MAAM,cACJ,aAAa,iBACb,WAAW,mDACX,WAAW,yCACX,iBAAiB,SAAS,QAC1B,OAAO,uDACP,cAAc,cACf,GAAG;IAEJ,gFAAgF;IAChF,IAAI,WAAW,CAAA,GAAA,oBAAM,EAAE;QACrB,IAAI,eAAe,AAAC,CAAA,WAAW,QAAO,IAAK;QAC3C,eAAe,CAAA,GAAA,yCAAc,EAAE,cAAc,GAAG,eAAe,MAAM;QACrE,OAAO,KAAK,GAAG,CAAC,cAA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AA,GAAA,yCAAc,EAAE,MAAM,CAAC,MAAM,GAAG,GAAG,UAAU,UAAU;IAC5E;IAEA,OAAO;QACL,QAAQ;QACR,eAAe,MAAM,YAAY,KAAK,YAAY,eAAe;QACjE,eAAe,CAAC,QAAkB,MAAM,CAAC,MAAM;QAC/C,eAAe;yBACf;QACA,iBAAiB,CAAC,QAAkB,WAAW,CAAC,MAAM;QACtD,kBAAkB;QAClB,cAAc;QACd,iBAAiB;QACjB,iBAAiB,CAAC,QAAkB,gBAAgB,MAAM,CAAC,MAAM;yBACjE;QACA,oBAAoB,CAAC,QAAkB,kBAAkB,MAAM,CAAC,MAAM;2BACtE;0BACA;0BACA;yBACA;yBACA;0BACA;wBACA;wBACA;cACA;kBACA;qBACA;oBACA;IACF;AACF;AAEA,SAAS,mCAAgB,KAAU,EAAE,KAAa,EAAE,KAAQ;IAC1D,IAAI,KAAK,CAAC,MAAM,KAAK,OACnB,OAAO;IAGT,OAAO;WAAI,MAAM,KAAK,CAAC,GAAG;QAAQ;WAAU,MAAM,KAAK,CAAC,QAAQ;KAAG;AACrE;AAEA,SAAS,mCAAa,KAAyB;IAC7C,IAAI,SAAS,MACX,OAAO;IAGT,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ;QAAC;KAAM;AAC/C;AAEA,SAAS,qCAAe,KAAK,EAAE,YAAY,EAAE,QAAQ;IACnD,OAAO,CAAC;QACN,IAAI,OAAO,UAAU,YAAY,OAAO,iBAAiB,UACvD,WAAW,QAAQ,CAAC,EAAE;aAEtB,WAAW;IAEf;AACF","sources":["packages/react-stately/src/slider/useSliderState.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 {clamp, snapValueToStep} from '../utils/number';\n\nimport {LabelableProps, Orientation, RangeInputBase, ValueBase} from '@react-types/shared';\nimport {useCallback, useMemo, useRef, useState} from 'react';\nimport {useControlledState} from '../utils/useControlledState';\n\nexport interface SliderProps<T = number | number[]>\n extends RangeInputBase<number>, ValueBase<T>, LabelableProps {\n /**\n * The orientation of the Slider.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation;\n /** Whether the whole Slider is disabled. */\n isDisabled?: boolean;\n /** Fired when the slider stops moving, due to being let go. */\n onChangeEnd?: (value: T) => void;\n // These are duplicated from ValueBase to define defaults for the docs.\n /**\n * The slider's minimum value.\n *\n * @default 0\n */\n minValue?: number;\n /**\n * The slider's maximum value.\n *\n * @default 100\n */\n maxValue?: number;\n /**\n * The slider's step value.\n *\n * @default 1\n */\n step?: number;\n}\n\nexport interface SliderState {\n /**\n * Values managed by the slider by thumb index.\n */\n readonly values: number[];\n /**\n * The default values for each thumb.\n */\n readonly defaultValues: number[];\n /**\n * Get the value for the specified thumb.\n *\n * @param index\n */\n getThumbValue(index: number): number;\n\n /**\n * Sets the value for the specified thumb.\n * The actual value set will be clamped and rounded according to min/max/step.\n *\n * @param index\n * @param value\n */\n setThumbValue(index: number, value: number): void;\n\n /**\n * Sets value for the specified thumb by percent offset (between 0 and 1).\n *\n * @param index\n * @param percent\n */\n setThumbPercent(index: number, percent: number): void;\n\n /**\n * Whether the specific thumb is being dragged.\n *\n * @param index\n */\n isThumbDragging(index: number): boolean;\n /**\n * Set is dragging on the specified thumb.\n *\n * @param index\n * @param dragging\n */\n setThumbDragging(index: number, dragging: boolean): void;\n\n /**\n * Currently-focused thumb index.\n */\n readonly focusedThumb: number | undefined;\n /**\n * Set focused true on specified thumb. This will remove focus from\n * any thumb that had it before.\n *\n * @param index\n */\n setFocusedThumb(index: number | undefined): void;\n\n /**\n * Returns the specified thumb's value as a percentage from 0 to 1.\n *\n * @param index\n */\n getThumbPercent(index: number): number;\n\n /**\n * Returns the value as a percent between the min and max of the slider.\n *\n * @param index\n */\n getValuePercent(value: number): number;\n\n /**\n * Returns the string label for the specified thumb's value, per props.formatOptions.\n *\n * @param index\n */\n getThumbValueLabel(index: number): string;\n\n /**\n * Returns the string label for the value, per props.formatOptions.\n *\n * @param value\n */\n getFormattedValue(value?: number | number[]): string;\n\n /**\n * Returns the min allowed value for the specified thumb.\n *\n * @param index\n */\n getThumbMinValue(index: number): number;\n\n /**\n * Returns the max allowed value for the specified thumb.\n *\n * @param index\n */\n getThumbMaxValue(index: number): number;\n\n /**\n * Converts a percent along track (between 0 and 1) to the corresponding value.\n *\n * @param percent\n */\n getPercentValue(percent: number): number;\n\n /**\n * Returns if the specified thumb is editable.\n *\n * @param index\n */\n isThumbEditable(index: number): boolean;\n\n /**\n * Set the specified thumb's editable state.\n *\n * @param index\n * @param editable\n */\n setThumbEditable(index: number, editable: boolean): void;\n\n /**\n * Increments the value of the thumb by the step or page amount.\n */\n incrementThumb(index: number, stepSize?: number): void;\n /**\n * Decrements the value of the thumb by the step or page amount.\n */\n decrementThumb(index: number, stepSize?: number): void;\n\n /**\n * The step amount for the slider.\n */\n readonly step: number;\n\n /**\n * The page size for the slider, used to do a bigger step.\n */\n readonly pageSize: number;\n\n /** The orientation of the slider. */\n readonly orientation: Orientation;\n\n /** Whether the slider is disabled. */\n readonly isDisabled: boolean;\n}\n\nconst DEFAULT_MIN_VALUE = 0;\nconst DEFAULT_MAX_VALUE = 100;\nconst DEFAULT_STEP_VALUE = 1;\n\nexport interface SliderStateOptions<T> extends SliderProps<T> {\n numberFormatter: Intl.NumberFormat;\n}\n\n/**\n * Provides state management for a slider component. Stores values for all thumbs,\n * formats values for localization, and provides methods to update the position\n * of any thumbs.\n *\n * @param props\n */\nexport function useSliderState<T extends number | number[]>(\n props: SliderStateOptions<T>\n): SliderState {\n const {\n isDisabled = false,\n minValue = DEFAULT_MIN_VALUE,\n maxValue = DEFAULT_MAX_VALUE,\n numberFormatter: formatter,\n step = DEFAULT_STEP_VALUE,\n orientation = 'horizontal'\n } = props;\n\n // Page step should be at least equal to step and always a multiple of the step.\n let pageSize = useMemo(() => {\n let calcPageSize = (maxValue - minValue) / 10;\n calcPageSize = snapValueToStep(calcPageSize, 0, calcPageSize + step, step);\n return Math.max(calcPageSize, step);\n }, [step, maxValue, minValue]);\n\n let restrictValues = useCallback(\n (values: number[] | undefined) =>\n values?.map((val, idx) => {\n let min = idx === 0 ? minValue : values[idx - 1];\n let max = idx === values.length - 1 ? maxValue : values[idx + 1];\n return snapValueToStep(val, min, max, step);\n }),\n [minValue, maxValue, step]\n );\n\n let value = useMemo(\n () => restrictValues(convertValue(props.value)),\n [props.value, restrictValues]\n );\n let defaultValue = useMemo(\n () => restrictValues(convertValue(props.defaultValue) ?? [minValue])!,\n [props.defaultValue, minValue, restrictValues]\n );\n let onChange = createOnChange(props.value, props.defaultValue, props.onChange);\n let onChangeEnd = createOnChange(props.value, props.defaultValue, props.onChangeEnd);\n\n const [values, setValuesState] = useControlledState<number[]>(value, defaultValue, onChange);\n let [initialValues] = useState(values);\n const [isDraggings, setDraggingsState] = useState<boolean[]>(\n new Array(values.length).fill(false)\n );\n const isEditablesRef = useRef<boolean[]>(new Array(values.length).fill(true));\n const [focusedIndex, setFocusedIndex] = useState<number | undefined>(undefined);\n\n const valuesRef = useRef<number[]>(values);\n const isDraggingsRef = useRef<boolean[]>(isDraggings);\n\n let setValues = (values: number[]) => {\n valuesRef.current = values;\n setValuesState(values);\n };\n\n let setDraggings = (draggings: boolean[]) => {\n isDraggingsRef.current = draggings;\n setDraggingsState(draggings);\n };\n\n function getValuePercent(value: number) {\n return (value - minValue) / (maxValue - minValue);\n }\n\n function getThumbMinValue(index: number) {\n return index === 0 ? minValue : values[index - 1];\n }\n function getThumbMaxValue(index: number) {\n return index === values.length - 1 ? maxValue : values[index + 1];\n }\n\n function isThumbEditable(index: number) {\n return isEditablesRef.current[index];\n }\n\n function setThumbEditable(index: number, editable: boolean) {\n isEditablesRef.current[index] = editable;\n }\n\n function updateValue(index: number, value: number) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n const thisMin = getThumbMinValue(index);\n const thisMax = getThumbMaxValue(index);\n\n // Round value to multiple of step, clamp value between min and max\n value = snapValueToStep(value, thisMin, thisMax, step);\n let newValues = replaceIndex(valuesRef.current, index, value);\n setValues(newValues);\n }\n\n function updateDragging(index: number, dragging: boolean) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n if (dragging) {\n valuesRef.current = values;\n }\n\n const wasDragging = isDraggingsRef.current[index];\n isDraggingsRef.current = replaceIndex(isDraggingsRef.current, index, dragging);\n setDraggings(isDraggingsRef.current);\n\n // Call onChangeEnd if no handles are dragging.\n if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) {\n onChangeEnd(valuesRef.current);\n }\n }\n\n let listFormatRef = useRef<Intl.ListFormat | null>(null);\n\n function getFormattedValue(valueParam: number | number[] = values) {\n if (typeof valueParam === 'number') {\n valueParam = [valueParam];\n }\n\n switch (valueParam.length) {\n case 0:\n return '';\n case 1:\n return formatter.format(valueParam[0]);\n case 2:\n return formatter.formatRange(valueParam[0], valueParam[1]);\n default: {\n let formatted = valueParam.map(value => formatter.format(value));\n let listFormat = listFormatRef.current;\n if (\n !listFormat ||\n listFormat.resolvedOptions().locale !== formatter.resolvedOptions().locale\n ) {\n listFormat = listFormatRef.current = new Intl.ListFormat(\n formatter.resolvedOptions().locale,\n {type: 'unit'}\n );\n }\n return listFormat.format(formatted);\n }\n }\n }\n\n function setThumbPercent(index: number, percent: number) {\n updateValue(index, getPercentValue(percent));\n }\n\n function getRoundedValue(value: number) {\n return Math.round((value - minValue) / step) * step + minValue;\n }\n\n function getPercentValue(percent: number) {\n const val = percent * (maxValue - minValue) + minValue;\n return clamp(getRoundedValue(val), minValue, maxValue);\n }\n\n function incrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] + s, minValue, maxValue, step));\n }\n\n function decrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] - s, minValue, maxValue, step));\n }\n\n return {\n values: values,\n defaultValues: props.defaultValue !== undefined ? defaultValue : initialValues,\n getThumbValue: (index: number) => values[index],\n setThumbValue: updateValue,\n setThumbPercent,\n isThumbDragging: (index: number) => isDraggings[index],\n setThumbDragging: updateDragging,\n focusedThumb: focusedIndex,\n setFocusedThumb: setFocusedIndex,\n getThumbPercent: (index: number) => getValuePercent(values[index]),\n getValuePercent,\n getThumbValueLabel: (index: number) => getFormattedValue(values[index]),\n getFormattedValue,\n getThumbMinValue,\n getThumbMaxValue,\n getPercentValue,\n isThumbEditable,\n setThumbEditable,\n incrementThumb,\n decrementThumb,\n step,\n pageSize,\n orientation,\n isDisabled\n };\n}\n\nfunction replaceIndex<T>(array: T[], index: number, value: T) {\n if (array[index] === value) {\n return array;\n }\n\n return [...array.slice(0, index), value, ...array.slice(index + 1)];\n}\n\nfunction convertValue(value?: number | number[]) {\n if (value == null) {\n return undefined;\n }\n\n return Array.isArray(value) ? value : [value];\n}\n\nfunction createOnChange(value, defaultValue, onChange) {\n return (newValue: number[]) => {\n if (typeof value === 'number' || typeof defaultValue === 'number') {\n onChange?.(newValue[0]);\n } else {\n onChange?.(newValue);\n }\n };\n}\n"],"names":[],"version":3,"file":"useSliderState.cjs.map"}
|
|
1
|
+
{"mappings":";;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;AA8LD,MAAM,0CAAoB;AAC1B,MAAM,0CAAoB;AAC1B,MAAM,2CAAqB;AAcpB,SAAS,0CACd,KAA4B;IAE5B,MAAM,cACJ,aAAa,iBACb,WAAW,mDACX,WAAW,yCACX,iBAAiB,SAAS,QAC1B,OAAO,uDACP,cAAc,cACf,GAAG;IAEJ,gFAAgF;IAChF,IAAI,WAAW,CAAA,GAAA,oBAAM,EAAE;QACrB,IAAI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,GAAG,UAAU,UAAU;IAC5E;IAEA,SAAS,eAAe,KAAa,EAAE,WAAmB,CAAC;QACzD,IAAI,IAAI,KAAK,GAAG,CAAC,UAAU;QAC3B,YAAY,OAAO,CAAA,GAAA,yCAAc,EAAE,MAAM,CAAC,MAAM,GAAG,GAAG,UAAU,UAAU;IAC5E;IAEA,OAAO;QACL,QAAQ;QACR,eAAe,MAAM,YAAY,KAAK,YAAY,eAAe;QACjE,eAAe,CAAC,QAAkB,MAAM,CAAC,MAAM;QAC/C,eAAe;yBACf;QACA,iBAAiB,CAAC,QAAkB,WAAW,CAAC,MAAM;QACtD,kBAAkB;QAClB,cAAc;QACd,iBAAiB;QACjB,iBAAiB,CAAC,QAAkB,gBAAgB,MAAM,CAAC,MAAM;yBACjE;QACA,oBAAoB,CAAC,QAAkB,kBAAkB,MAAM,CAAC,MAAM;2BACtE;0BACA;0BACA;yBACA;yBACA;0BACA;wBACA;wBACA;cACA;kBACA;qBACA;oBACA;IACF;AACF;AAEA,SAAS,mCAAgB,KAAU,EAAE,KAAa,EAAE,KAAQ;IAC1D,IAAI,KAAK,CAAC,MAAM,KAAK,OACnB,OAAO;IAGT,OAAO;WAAI,MAAM,KAAK,CAAC,GAAG;QAAQ;WAAU,MAAM,KAAK,CAAC,QAAQ;KAAG;AACrE;AAEA,SAAS,mCAAa,KAAyB;IAC7C,IAAI,SAAS,MACX,OAAO;IAGT,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ;QAAC;KAAM;AAC/C;AAEA,SAAS,qCAAe,KAAK,EAAE,YAAY,EAAE,QAAQ;IACnD,OAAO,CAAC;QACN,IAAI,OAAO,UAAU,YAAY,OAAO,iBAAiB,UACvD,WAAW,QAAQ,CAAC,EAAE;aAEtB,WAAW;IAEf;AACF","sources":["packages/react-stately/src/slider/useSliderState.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 {clamp, snapValueToStep} from '../utils/number';\n\nimport {LabelableProps, Orientation, RangeInputBase, ValueBase} from '@react-types/shared';\nimport {useCallback, useMemo, useRef, useState} from 'react';\nimport {useControlledState} from '../utils/useControlledState';\n\nexport interface SliderProps<T = number | number[]>\n extends RangeInputBase<number>, ValueBase<T>, LabelableProps {\n /**\n * The orientation of the Slider.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation;\n /** Whether the whole Slider is disabled. */\n isDisabled?: boolean;\n /** Fired when the slider stops moving, due to being let go. */\n onChangeEnd?: (value: T) => void;\n // These are duplicated from ValueBase to define defaults for the docs.\n /**\n * The slider's minimum value.\n *\n * @default 0\n */\n minValue?: number;\n /**\n * The slider's maximum value.\n *\n * @default 100\n */\n maxValue?: number;\n /**\n * The slider's step value.\n *\n * @default 1\n */\n step?: number;\n}\n\nexport interface SliderState {\n /**\n * Values managed by the slider by thumb index.\n */\n readonly values: number[];\n /**\n * The default values for each thumb.\n */\n readonly defaultValues: number[];\n /**\n * Get the value for the specified thumb.\n *\n * @param index\n */\n getThumbValue(index: number): number;\n\n /**\n * Sets the value for the specified thumb.\n * The actual value set will be clamped and rounded according to min/max/step.\n *\n * @param index\n * @param value\n */\n setThumbValue(index: number, value: number): void;\n\n /**\n * Sets value for the specified thumb by percent offset (between 0 and 1).\n *\n * @param index\n * @param percent\n */\n setThumbPercent(index: number, percent: number): void;\n\n /**\n * Whether the specific thumb is being dragged.\n *\n * @param index\n */\n isThumbDragging(index: number): boolean;\n /**\n * Set is dragging on the specified thumb.\n *\n * @param index\n * @param dragging\n */\n setThumbDragging(index: number, dragging: boolean): void;\n\n /**\n * Currently-focused thumb index.\n */\n readonly focusedThumb: number | undefined;\n /**\n * Set focused true on specified thumb. This will remove focus from\n * any thumb that had it before.\n *\n * @param index\n */\n setFocusedThumb(index: number | undefined): void;\n\n /**\n * Returns the specified thumb's value as a percentage from 0 to 1.\n *\n * @param index\n */\n getThumbPercent(index: number): number;\n\n /**\n * Returns the value as a percent between the min and max of the slider.\n *\n * @param index\n */\n getValuePercent(value: number): number;\n\n /**\n * Returns the string label for the specified thumb's value, per props.formatOptions.\n *\n * @param index\n */\n getThumbValueLabel(index: number): string;\n\n /**\n * Returns the string label for the value, per props.formatOptions.\n *\n * @param value\n */\n getFormattedValue(value?: number | number[]): string;\n\n /**\n * Returns the min allowed value for the specified thumb.\n *\n * @param index\n */\n getThumbMinValue(index: number): number;\n\n /**\n * Returns the max allowed value for the specified thumb.\n *\n * @param index\n */\n getThumbMaxValue(index: number): number;\n\n /**\n * Converts a percent along track (between 0 and 1) to the corresponding value.\n *\n * @param percent\n */\n getPercentValue(percent: number): number;\n\n /**\n * Returns if the specified thumb is editable.\n *\n * @param index\n */\n isThumbEditable(index: number): boolean;\n\n /**\n * Set the specified thumb's editable state.\n *\n * @param index\n * @param editable\n */\n setThumbEditable(index: number, editable: boolean): void;\n\n /**\n * Increments the value of the thumb by the step or page amount.\n */\n incrementThumb(index: number, stepSize?: number): void;\n /**\n * Decrements the value of the thumb by the step or page amount.\n */\n decrementThumb(index: number, stepSize?: number): void;\n\n /**\n * The step amount for the slider.\n */\n readonly step: number;\n\n /**\n * The page size for the slider, used to do a bigger step.\n */\n readonly pageSize: number;\n\n /** The orientation of the slider. */\n readonly orientation: Orientation;\n\n /** Whether the slider is disabled. */\n readonly isDisabled: boolean;\n}\n\nconst DEFAULT_MIN_VALUE = 0;\nconst DEFAULT_MAX_VALUE = 100;\nconst DEFAULT_STEP_VALUE = 1;\n\nexport interface SliderStateOptions<T> extends SliderProps<T> {\n numberFormatter: Intl.NumberFormat;\n}\n\n/**\n * Provides state management for a slider component. Stores values for all thumbs,\n * formats values for localization, and provides methods to update the position\n * of any thumbs.\n *\n * @param props\n */\n// oxlint-disable-next-line react/react-compiler\nexport function useSliderState<T extends number | number[]>(\n props: SliderStateOptions<T>\n): SliderState {\n const {\n isDisabled = false,\n minValue = DEFAULT_MIN_VALUE,\n maxValue = DEFAULT_MAX_VALUE,\n numberFormatter: formatter,\n step = DEFAULT_STEP_VALUE,\n orientation = 'horizontal'\n } = props;\n\n // Page step should be at least equal to step and always a multiple of the step.\n let pageSize = useMemo(() => {\n let calcPageSize = (maxValue - minValue) / 10;\n calcPageSize = snapValueToStep(calcPageSize, 0, calcPageSize + step, step);\n return Math.max(calcPageSize, step);\n }, [step, maxValue, minValue]);\n\n let restrictValues = useCallback(\n (values: number[] | undefined) =>\n values?.map((val, idx) => {\n let min = idx === 0 ? minValue : values[idx - 1];\n let max = idx === values.length - 1 ? maxValue : values[idx + 1];\n return snapValueToStep(val, min, max, step);\n }),\n [minValue, maxValue, step]\n );\n\n let value = useMemo(\n () => restrictValues(convertValue(props.value)),\n [props.value, restrictValues]\n );\n let defaultValue = useMemo(\n () => restrictValues(convertValue(props.defaultValue) ?? [minValue])!,\n [props.defaultValue, minValue, restrictValues]\n );\n let onChange = createOnChange(props.value, props.defaultValue, props.onChange);\n let onChangeEnd = createOnChange(props.value, props.defaultValue, props.onChangeEnd);\n\n const [values, setValuesState] = useControlledState<number[]>(value, defaultValue, onChange);\n let [initialValues] = useState(values);\n const [isDraggings, setDraggingsState] = useState<boolean[]>(\n new Array(values.length).fill(false)\n );\n const isEditablesRef = useRef<boolean[]>(new Array(values.length).fill(true));\n const [focusedIndex, setFocusedIndex] = useState<number | undefined>(undefined);\n\n const valuesRef = useRef<number[]>(values);\n const isDraggingsRef = useRef<boolean[]>(isDraggings);\n\n let setValues = (values: number[]) => {\n valuesRef.current = values;\n setValuesState(values);\n };\n\n let setDraggings = (draggings: boolean[]) => {\n isDraggingsRef.current = draggings;\n setDraggingsState(draggings);\n };\n\n function getValuePercent(value: number) {\n return (value - minValue) / (maxValue - minValue);\n }\n\n function getThumbMinValue(index: number) {\n return index === 0 ? minValue : values[index - 1];\n }\n function getThumbMaxValue(index: number) {\n return index === values.length - 1 ? maxValue : values[index + 1];\n }\n\n function isThumbEditable(index: number) {\n return isEditablesRef.current[index];\n }\n\n function setThumbEditable(index: number, editable: boolean) {\n isEditablesRef.current[index] = editable;\n }\n\n function updateValue(index: number, value: number) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n const thisMin = getThumbMinValue(index);\n const thisMax = getThumbMaxValue(index);\n\n // Round value to multiple of step, clamp value between min and max\n value = snapValueToStep(value, thisMin, thisMax, step);\n let newValues = replaceIndex(valuesRef.current, index, value);\n setValues(newValues);\n }\n\n function updateDragging(index: number, dragging: boolean) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n if (dragging) {\n valuesRef.current = values;\n }\n\n const wasDragging = isDraggingsRef.current[index];\n isDraggingsRef.current = replaceIndex(isDraggingsRef.current, index, dragging);\n setDraggings(isDraggingsRef.current);\n\n // Call onChangeEnd if no handles are dragging.\n if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) {\n onChangeEnd(valuesRef.current);\n }\n }\n\n let listFormatRef = useRef<Intl.ListFormat | null>(null);\n\n function getFormattedValue(valueParam: number | number[] = values) {\n if (typeof valueParam === 'number') {\n valueParam = [valueParam];\n }\n\n switch (valueParam.length) {\n case 0:\n return '';\n case 1:\n return formatter.format(valueParam[0]);\n case 2:\n return formatter.formatRange(valueParam[0], valueParam[1]);\n default: {\n let formatted = valueParam.map(value => formatter.format(value));\n let listFormat = listFormatRef.current;\n if (\n !listFormat ||\n listFormat.resolvedOptions().locale !== formatter.resolvedOptions().locale\n ) {\n listFormat = listFormatRef.current = new Intl.ListFormat(\n formatter.resolvedOptions().locale,\n {type: 'unit'}\n );\n }\n return listFormat.format(formatted);\n }\n }\n }\n\n function setThumbPercent(index: number, percent: number) {\n updateValue(index, getPercentValue(percent));\n }\n\n function getRoundedValue(value: number) {\n return Math.round((value - minValue) / step) * step + minValue;\n }\n\n function getPercentValue(percent: number) {\n const val = percent * (maxValue - minValue) + minValue;\n return clamp(getRoundedValue(val), minValue, maxValue);\n }\n\n function incrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] + s, minValue, maxValue, step));\n }\n\n function decrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] - s, minValue, maxValue, step));\n }\n\n return {\n values: values,\n defaultValues: props.defaultValue !== undefined ? defaultValue : initialValues,\n getThumbValue: (index: number) => values[index],\n setThumbValue: updateValue,\n setThumbPercent,\n isThumbDragging: (index: number) => isDraggings[index],\n setThumbDragging: updateDragging,\n focusedThumb: focusedIndex,\n setFocusedThumb: setFocusedIndex,\n getThumbPercent: (index: number) => getValuePercent(values[index]),\n getValuePercent,\n getThumbValueLabel: (index: number) => getFormattedValue(values[index]),\n getFormattedValue,\n getThumbMinValue,\n getThumbMaxValue,\n getPercentValue,\n isThumbEditable,\n setThumbEditable,\n incrementThumb,\n decrementThumb,\n step,\n pageSize,\n orientation,\n isDisabled\n };\n}\n\nfunction replaceIndex<T>(array: T[], index: number, value: T) {\n if (array[index] === value) {\n return array;\n }\n\n return [...array.slice(0, index), value, ...array.slice(index + 1)];\n}\n\nfunction convertValue(value?: number | number[]) {\n if (value == null) {\n return undefined;\n }\n\n return Array.isArray(value) ? value : [value];\n}\n\nfunction createOnChange(value, defaultValue, onChange) {\n return (newValue: number[]) => {\n if (typeof value === 'number' || typeof defaultValue === 'number') {\n onChange?.(newValue[0]);\n } else {\n onChange?.(newValue);\n }\n };\n}\n"],"names":[],"version":3,"file":"useSliderState.cjs.map"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;AAAA;;;;;;;;;;CAUC;;;AA8LD,MAAM,0CAAoB;AAC1B,MAAM,0CAAoB;AAC1B,MAAM,2CAAqB;AAapB,SAAS,0CACd,KAA4B;IAE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AU,YAAY,OAAO,iBAAiB,UACvD,qBAAA,+BAAA,SAAW,QAAQ,CAAC,EAAE;aAEtB,qBAAA,+BAAA,SAAW;IAEf;AACF","sources":["packages/react-stately/src/slider/useSliderState.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 {clamp, snapValueToStep} from '../utils/number';\n\nimport {LabelableProps, Orientation, RangeInputBase, ValueBase} from '@react-types/shared';\nimport {useCallback, useMemo, useRef, useState} from 'react';\nimport {useControlledState} from '../utils/useControlledState';\n\nexport interface SliderProps<T = number | number[]>\n extends RangeInputBase<number>, ValueBase<T>, LabelableProps {\n /**\n * The orientation of the Slider.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation;\n /** Whether the whole Slider is disabled. */\n isDisabled?: boolean;\n /** Fired when the slider stops moving, due to being let go. */\n onChangeEnd?: (value: T) => void;\n // These are duplicated from ValueBase to define defaults for the docs.\n /**\n * The slider's minimum value.\n *\n * @default 0\n */\n minValue?: number;\n /**\n * The slider's maximum value.\n *\n * @default 100\n */\n maxValue?: number;\n /**\n * The slider's step value.\n *\n * @default 1\n */\n step?: number;\n}\n\nexport interface SliderState {\n /**\n * Values managed by the slider by thumb index.\n */\n readonly values: number[];\n /**\n * The default values for each thumb.\n */\n readonly defaultValues: number[];\n /**\n * Get the value for the specified thumb.\n *\n * @param index\n */\n getThumbValue(index: number): number;\n\n /**\n * Sets the value for the specified thumb.\n * The actual value set will be clamped and rounded according to min/max/step.\n *\n * @param index\n * @param value\n */\n setThumbValue(index: number, value: number): void;\n\n /**\n * Sets value for the specified thumb by percent offset (between 0 and 1).\n *\n * @param index\n * @param percent\n */\n setThumbPercent(index: number, percent: number): void;\n\n /**\n * Whether the specific thumb is being dragged.\n *\n * @param index\n */\n isThumbDragging(index: number): boolean;\n /**\n * Set is dragging on the specified thumb.\n *\n * @param index\n * @param dragging\n */\n setThumbDragging(index: number, dragging: boolean): void;\n\n /**\n * Currently-focused thumb index.\n */\n readonly focusedThumb: number | undefined;\n /**\n * Set focused true on specified thumb. This will remove focus from\n * any thumb that had it before.\n *\n * @param index\n */\n setFocusedThumb(index: number | undefined): void;\n\n /**\n * Returns the specified thumb's value as a percentage from 0 to 1.\n *\n * @param index\n */\n getThumbPercent(index: number): number;\n\n /**\n * Returns the value as a percent between the min and max of the slider.\n *\n * @param index\n */\n getValuePercent(value: number): number;\n\n /**\n * Returns the string label for the specified thumb's value, per props.formatOptions.\n *\n * @param index\n */\n getThumbValueLabel(index: number): string;\n\n /**\n * Returns the string label for the value, per props.formatOptions.\n *\n * @param value\n */\n getFormattedValue(value?: number | number[]): string;\n\n /**\n * Returns the min allowed value for the specified thumb.\n *\n * @param index\n */\n getThumbMinValue(index: number): number;\n\n /**\n * Returns the max allowed value for the specified thumb.\n *\n * @param index\n */\n getThumbMaxValue(index: number): number;\n\n /**\n * Converts a percent along track (between 0 and 1) to the corresponding value.\n *\n * @param percent\n */\n getPercentValue(percent: number): number;\n\n /**\n * Returns if the specified thumb is editable.\n *\n * @param index\n */\n isThumbEditable(index: number): boolean;\n\n /**\n * Set the specified thumb's editable state.\n *\n * @param index\n * @param editable\n */\n setThumbEditable(index: number, editable: boolean): void;\n\n /**\n * Increments the value of the thumb by the step or page amount.\n */\n incrementThumb(index: number, stepSize?: number): void;\n /**\n * Decrements the value of the thumb by the step or page amount.\n */\n decrementThumb(index: number, stepSize?: number): void;\n\n /**\n * The step amount for the slider.\n */\n readonly step: number;\n\n /**\n * The page size for the slider, used to do a bigger step.\n */\n readonly pageSize: number;\n\n /** The orientation of the slider. */\n readonly orientation: Orientation;\n\n /** Whether the slider is disabled. */\n readonly isDisabled: boolean;\n}\n\nconst DEFAULT_MIN_VALUE = 0;\nconst DEFAULT_MAX_VALUE = 100;\nconst DEFAULT_STEP_VALUE = 1;\n\nexport interface SliderStateOptions<T> extends SliderProps<T> {\n numberFormatter: Intl.NumberFormat;\n}\n\n/**\n * Provides state management for a slider component. Stores values for all thumbs,\n * formats values for localization, and provides methods to update the position\n * of any thumbs.\n *\n * @param props\n */\nexport function useSliderState<T extends number | number[]>(\n props: SliderStateOptions<T>\n): SliderState {\n const {\n isDisabled = false,\n minValue = DEFAULT_MIN_VALUE,\n maxValue = DEFAULT_MAX_VALUE,\n numberFormatter: formatter,\n step = DEFAULT_STEP_VALUE,\n orientation = 'horizontal'\n } = props;\n\n // Page step should be at least equal to step and always a multiple of the step.\n let pageSize = useMemo(() => {\n let calcPageSize = (maxValue - minValue) / 10;\n calcPageSize = snapValueToStep(calcPageSize, 0, calcPageSize + step, step);\n return Math.max(calcPageSize, step);\n }, [step, maxValue, minValue]);\n\n let restrictValues = useCallback(\n (values: number[] | undefined) =>\n values?.map((val, idx) => {\n let min = idx === 0 ? minValue : values[idx - 1];\n let max = idx === values.length - 1 ? maxValue : values[idx + 1];\n return snapValueToStep(val, min, max, step);\n }),\n [minValue, maxValue, step]\n );\n\n let value = useMemo(\n () => restrictValues(convertValue(props.value)),\n [props.value, restrictValues]\n );\n let defaultValue = useMemo(\n () => restrictValues(convertValue(props.defaultValue) ?? [minValue])!,\n [props.defaultValue, minValue, restrictValues]\n );\n let onChange = createOnChange(props.value, props.defaultValue, props.onChange);\n let onChangeEnd = createOnChange(props.value, props.defaultValue, props.onChangeEnd);\n\n const [values, setValuesState] = useControlledState<number[]>(value, defaultValue, onChange);\n let [initialValues] = useState(values);\n const [isDraggings, setDraggingsState] = useState<boolean[]>(\n new Array(values.length).fill(false)\n );\n const isEditablesRef = useRef<boolean[]>(new Array(values.length).fill(true));\n const [focusedIndex, setFocusedIndex] = useState<number | undefined>(undefined);\n\n const valuesRef = useRef<number[]>(values);\n const isDraggingsRef = useRef<boolean[]>(isDraggings);\n\n let setValues = (values: number[]) => {\n valuesRef.current = values;\n setValuesState(values);\n };\n\n let setDraggings = (draggings: boolean[]) => {\n isDraggingsRef.current = draggings;\n setDraggingsState(draggings);\n };\n\n function getValuePercent(value: number) {\n return (value - minValue) / (maxValue - minValue);\n }\n\n function getThumbMinValue(index: number) {\n return index === 0 ? minValue : values[index - 1];\n }\n function getThumbMaxValue(index: number) {\n return index === values.length - 1 ? maxValue : values[index + 1];\n }\n\n function isThumbEditable(index: number) {\n return isEditablesRef.current[index];\n }\n\n function setThumbEditable(index: number, editable: boolean) {\n isEditablesRef.current[index] = editable;\n }\n\n function updateValue(index: number, value: number) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n const thisMin = getThumbMinValue(index);\n const thisMax = getThumbMaxValue(index);\n\n // Round value to multiple of step, clamp value between min and max\n value = snapValueToStep(value, thisMin, thisMax, step);\n let newValues = replaceIndex(valuesRef.current, index, value);\n setValues(newValues);\n }\n\n function updateDragging(index: number, dragging: boolean) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n if (dragging) {\n valuesRef.current = values;\n }\n\n const wasDragging = isDraggingsRef.current[index];\n isDraggingsRef.current = replaceIndex(isDraggingsRef.current, index, dragging);\n setDraggings(isDraggingsRef.current);\n\n // Call onChangeEnd if no handles are dragging.\n if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) {\n onChangeEnd(valuesRef.current);\n }\n }\n\n let listFormatRef = useRef<Intl.ListFormat | null>(null);\n\n function getFormattedValue(valueParam: number | number[] = values) {\n if (typeof valueParam === 'number') {\n valueParam = [valueParam];\n }\n\n switch (valueParam.length) {\n case 0:\n return '';\n case 1:\n return formatter.format(valueParam[0]);\n case 2:\n return formatter.formatRange(valueParam[0], valueParam[1]);\n default: {\n let formatted = valueParam.map(value => formatter.format(value));\n let listFormat = listFormatRef.current;\n if (\n !listFormat ||\n listFormat.resolvedOptions().locale !== formatter.resolvedOptions().locale\n ) {\n listFormat = listFormatRef.current = new Intl.ListFormat(\n formatter.resolvedOptions().locale,\n {type: 'unit'}\n );\n }\n return listFormat.format(formatted);\n }\n }\n }\n\n function setThumbPercent(index: number, percent: number) {\n updateValue(index, getPercentValue(percent));\n }\n\n function getRoundedValue(value: number) {\n return Math.round((value - minValue) / step) * step + minValue;\n }\n\n function getPercentValue(percent: number) {\n const val = percent * (maxValue - minValue) + minValue;\n return clamp(getRoundedValue(val), minValue, maxValue);\n }\n\n function incrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] + s, minValue, maxValue, step));\n }\n\n function decrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] - s, minValue, maxValue, step));\n }\n\n return {\n values: values,\n defaultValues: props.defaultValue !== undefined ? defaultValue : initialValues,\n getThumbValue: (index: number) => values[index],\n setThumbValue: updateValue,\n setThumbPercent,\n isThumbDragging: (index: number) => isDraggings[index],\n setThumbDragging: updateDragging,\n focusedThumb: focusedIndex,\n setFocusedThumb: setFocusedIndex,\n getThumbPercent: (index: number) => getValuePercent(values[index]),\n getValuePercent,\n getThumbValueLabel: (index: number) => getFormattedValue(values[index]),\n getFormattedValue,\n getThumbMinValue,\n getThumbMaxValue,\n getPercentValue,\n isThumbEditable,\n setThumbEditable,\n incrementThumb,\n decrementThumb,\n step,\n pageSize,\n orientation,\n isDisabled\n };\n}\n\nfunction replaceIndex<T>(array: T[], index: number, value: T) {\n if (array[index] === value) {\n return array;\n }\n\n return [...array.slice(0, index), value, ...array.slice(index + 1)];\n}\n\nfunction convertValue(value?: number | number[]) {\n if (value == null) {\n return undefined;\n }\n\n return Array.isArray(value) ? value : [value];\n}\n\nfunction createOnChange(value, defaultValue, onChange) {\n return (newValue: number[]) => {\n if (typeof value === 'number' || typeof defaultValue === 'number') {\n onChange?.(newValue[0]);\n } else {\n onChange?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AS,mCAAa,KAAyB;IAC7C,IAAI,SAAS,MACX,OAAO;IAGT,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ;QAAC;KAAM;AAC/C;AAEA,SAAS,qCAAe,KAAK,EAAE,YAAY,EAAE,QAAQ;IACnD,OAAO,CAAC;QACN,IAAI,OAAO,UAAU,YAAY,OAAO,iBAAiB,UACvD,qBAAA,+BAAA,SAAW,QAAQ,CAAC,EAAE;aAEtB,qBAAA,+BAAA,SAAW;IAEf;AACF","sources":["packages/react-stately/src/slider/useSliderState.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 {clamp, snapValueToStep} from '../utils/number';\n\nimport {LabelableProps, Orientation, RangeInputBase, ValueBase} from '@react-types/shared';\nimport {useCallback, useMemo, useRef, useState} from 'react';\nimport {useControlledState} from '../utils/useControlledState';\n\nexport interface SliderProps<T = number | number[]>\n extends RangeInputBase<number>, ValueBase<T>, LabelableProps {\n /**\n * The orientation of the Slider.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation;\n /** Whether the whole Slider is disabled. */\n isDisabled?: boolean;\n /** Fired when the slider stops moving, due to being let go. */\n onChangeEnd?: (value: T) => void;\n // These are duplicated from ValueBase to define defaults for the docs.\n /**\n * The slider's minimum value.\n *\n * @default 0\n */\n minValue?: number;\n /**\n * The slider's maximum value.\n *\n * @default 100\n */\n maxValue?: number;\n /**\n * The slider's step value.\n *\n * @default 1\n */\n step?: number;\n}\n\nexport interface SliderState {\n /**\n * Values managed by the slider by thumb index.\n */\n readonly values: number[];\n /**\n * The default values for each thumb.\n */\n readonly defaultValues: number[];\n /**\n * Get the value for the specified thumb.\n *\n * @param index\n */\n getThumbValue(index: number): number;\n\n /**\n * Sets the value for the specified thumb.\n * The actual value set will be clamped and rounded according to min/max/step.\n *\n * @param index\n * @param value\n */\n setThumbValue(index: number, value: number): void;\n\n /**\n * Sets value for the specified thumb by percent offset (between 0 and 1).\n *\n * @param index\n * @param percent\n */\n setThumbPercent(index: number, percent: number): void;\n\n /**\n * Whether the specific thumb is being dragged.\n *\n * @param index\n */\n isThumbDragging(index: number): boolean;\n /**\n * Set is dragging on the specified thumb.\n *\n * @param index\n * @param dragging\n */\n setThumbDragging(index: number, dragging: boolean): void;\n\n /**\n * Currently-focused thumb index.\n */\n readonly focusedThumb: number | undefined;\n /**\n * Set focused true on specified thumb. This will remove focus from\n * any thumb that had it before.\n *\n * @param index\n */\n setFocusedThumb(index: number | undefined): void;\n\n /**\n * Returns the specified thumb's value as a percentage from 0 to 1.\n *\n * @param index\n */\n getThumbPercent(index: number): number;\n\n /**\n * Returns the value as a percent between the min and max of the slider.\n *\n * @param index\n */\n getValuePercent(value: number): number;\n\n /**\n * Returns the string label for the specified thumb's value, per props.formatOptions.\n *\n * @param index\n */\n getThumbValueLabel(index: number): string;\n\n /**\n * Returns the string label for the value, per props.formatOptions.\n *\n * @param value\n */\n getFormattedValue(value?: number | number[]): string;\n\n /**\n * Returns the min allowed value for the specified thumb.\n *\n * @param index\n */\n getThumbMinValue(index: number): number;\n\n /**\n * Returns the max allowed value for the specified thumb.\n *\n * @param index\n */\n getThumbMaxValue(index: number): number;\n\n /**\n * Converts a percent along track (between 0 and 1) to the corresponding value.\n *\n * @param percent\n */\n getPercentValue(percent: number): number;\n\n /**\n * Returns if the specified thumb is editable.\n *\n * @param index\n */\n isThumbEditable(index: number): boolean;\n\n /**\n * Set the specified thumb's editable state.\n *\n * @param index\n * @param editable\n */\n setThumbEditable(index: number, editable: boolean): void;\n\n /**\n * Increments the value of the thumb by the step or page amount.\n */\n incrementThumb(index: number, stepSize?: number): void;\n /**\n * Decrements the value of the thumb by the step or page amount.\n */\n decrementThumb(index: number, stepSize?: number): void;\n\n /**\n * The step amount for the slider.\n */\n readonly step: number;\n\n /**\n * The page size for the slider, used to do a bigger step.\n */\n readonly pageSize: number;\n\n /** The orientation of the slider. */\n readonly orientation: Orientation;\n\n /** Whether the slider is disabled. */\n readonly isDisabled: boolean;\n}\n\nconst DEFAULT_MIN_VALUE = 0;\nconst DEFAULT_MAX_VALUE = 100;\nconst DEFAULT_STEP_VALUE = 1;\n\nexport interface SliderStateOptions<T> extends SliderProps<T> {\n numberFormatter: Intl.NumberFormat;\n}\n\n/**\n * Provides state management for a slider component. Stores values for all thumbs,\n * formats values for localization, and provides methods to update the position\n * of any thumbs.\n *\n * @param props\n */\n// oxlint-disable-next-line react/react-compiler\nexport function useSliderState<T extends number | number[]>(\n props: SliderStateOptions<T>\n): SliderState {\n const {\n isDisabled = false,\n minValue = DEFAULT_MIN_VALUE,\n maxValue = DEFAULT_MAX_VALUE,\n numberFormatter: formatter,\n step = DEFAULT_STEP_VALUE,\n orientation = 'horizontal'\n } = props;\n\n // Page step should be at least equal to step and always a multiple of the step.\n let pageSize = useMemo(() => {\n let calcPageSize = (maxValue - minValue) / 10;\n calcPageSize = snapValueToStep(calcPageSize, 0, calcPageSize + step, step);\n return Math.max(calcPageSize, step);\n }, [step, maxValue, minValue]);\n\n let restrictValues = useCallback(\n (values: number[] | undefined) =>\n values?.map((val, idx) => {\n let min = idx === 0 ? minValue : values[idx - 1];\n let max = idx === values.length - 1 ? maxValue : values[idx + 1];\n return snapValueToStep(val, min, max, step);\n }),\n [minValue, maxValue, step]\n );\n\n let value = useMemo(\n () => restrictValues(convertValue(props.value)),\n [props.value, restrictValues]\n );\n let defaultValue = useMemo(\n () => restrictValues(convertValue(props.defaultValue) ?? [minValue])!,\n [props.defaultValue, minValue, restrictValues]\n );\n let onChange = createOnChange(props.value, props.defaultValue, props.onChange);\n let onChangeEnd = createOnChange(props.value, props.defaultValue, props.onChangeEnd);\n\n const [values, setValuesState] = useControlledState<number[]>(value, defaultValue, onChange);\n let [initialValues] = useState(values);\n const [isDraggings, setDraggingsState] = useState<boolean[]>(\n new Array(values.length).fill(false)\n );\n const isEditablesRef = useRef<boolean[]>(new Array(values.length).fill(true));\n const [focusedIndex, setFocusedIndex] = useState<number | undefined>(undefined);\n\n const valuesRef = useRef<number[]>(values);\n const isDraggingsRef = useRef<boolean[]>(isDraggings);\n\n let setValues = (values: number[]) => {\n valuesRef.current = values;\n setValuesState(values);\n };\n\n let setDraggings = (draggings: boolean[]) => {\n isDraggingsRef.current = draggings;\n setDraggingsState(draggings);\n };\n\n function getValuePercent(value: number) {\n return (value - minValue) / (maxValue - minValue);\n }\n\n function getThumbMinValue(index: number) {\n return index === 0 ? minValue : values[index - 1];\n }\n function getThumbMaxValue(index: number) {\n return index === values.length - 1 ? maxValue : values[index + 1];\n }\n\n function isThumbEditable(index: number) {\n return isEditablesRef.current[index];\n }\n\n function setThumbEditable(index: number, editable: boolean) {\n isEditablesRef.current[index] = editable;\n }\n\n function updateValue(index: number, value: number) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n const thisMin = getThumbMinValue(index);\n const thisMax = getThumbMaxValue(index);\n\n // Round value to multiple of step, clamp value between min and max\n value = snapValueToStep(value, thisMin, thisMax, step);\n let newValues = replaceIndex(valuesRef.current, index, value);\n setValues(newValues);\n }\n\n function updateDragging(index: number, dragging: boolean) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n if (dragging) {\n valuesRef.current = values;\n }\n\n const wasDragging = isDraggingsRef.current[index];\n isDraggingsRef.current = replaceIndex(isDraggingsRef.current, index, dragging);\n setDraggings(isDraggingsRef.current);\n\n // Call onChangeEnd if no handles are dragging.\n if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) {\n onChangeEnd(valuesRef.current);\n }\n }\n\n let listFormatRef = useRef<Intl.ListFormat | null>(null);\n\n function getFormattedValue(valueParam: number | number[] = values) {\n if (typeof valueParam === 'number') {\n valueParam = [valueParam];\n }\n\n switch (valueParam.length) {\n case 0:\n return '';\n case 1:\n return formatter.format(valueParam[0]);\n case 2:\n return formatter.formatRange(valueParam[0], valueParam[1]);\n default: {\n let formatted = valueParam.map(value => formatter.format(value));\n let listFormat = listFormatRef.current;\n if (\n !listFormat ||\n listFormat.resolvedOptions().locale !== formatter.resolvedOptions().locale\n ) {\n listFormat = listFormatRef.current = new Intl.ListFormat(\n formatter.resolvedOptions().locale,\n {type: 'unit'}\n );\n }\n return listFormat.format(formatted);\n }\n }\n }\n\n function setThumbPercent(index: number, percent: number) {\n updateValue(index, getPercentValue(percent));\n }\n\n function getRoundedValue(value: number) {\n return Math.round((value - minValue) / step) * step + minValue;\n }\n\n function getPercentValue(percent: number) {\n const val = percent * (maxValue - minValue) + minValue;\n return clamp(getRoundedValue(val), minValue, maxValue);\n }\n\n function incrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] + s, minValue, maxValue, step));\n }\n\n function decrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] - s, minValue, maxValue, step));\n }\n\n return {\n values: values,\n defaultValues: props.defaultValue !== undefined ? defaultValue : initialValues,\n getThumbValue: (index: number) => values[index],\n setThumbValue: updateValue,\n setThumbPercent,\n isThumbDragging: (index: number) => isDraggings[index],\n setThumbDragging: updateDragging,\n focusedThumb: focusedIndex,\n setFocusedThumb: setFocusedIndex,\n getThumbPercent: (index: number) => getValuePercent(values[index]),\n getValuePercent,\n getThumbValueLabel: (index: number) => getFormattedValue(values[index]),\n getFormattedValue,\n getThumbMinValue,\n getThumbMaxValue,\n getPercentValue,\n isThumbEditable,\n setThumbEditable,\n incrementThumb,\n decrementThumb,\n step,\n pageSize,\n orientation,\n isDisabled\n };\n}\n\nfunction replaceIndex<T>(array: T[], index: number, value: T) {\n if (array[index] === value) {\n return array;\n }\n\n return [...array.slice(0, index), value, ...array.slice(index + 1)];\n}\n\nfunction convertValue(value?: number | number[]) {\n if (value == null) {\n return undefined;\n }\n\n return Array.isArray(value) ? value : [value];\n}\n\nfunction createOnChange(value, defaultValue, onChange) {\n return (newValue: number[]) => {\n if (typeof value === 'number' || typeof defaultValue === 'number') {\n onChange?.(newValue[0]);\n } else {\n onChange?.(newValue);\n }\n };\n}\n"],"names":[],"version":3,"file":"useSliderState.js.map"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;AAAA;;;;;;;;;;CAUC;;;AA8LD,MAAM,0CAAoB;AAC1B,MAAM,0CAAoB;AAC1B,MAAM,2CAAqB;AAapB,SAAS,0CACd,KAA4B;IAE5B,MAAM,cACJ,aAAa,iBACb,WAAW,mDACX,WAAW,yCACX,iBAAiB,SAAS,QAC1B,OAAO,uDACP,cAAc,cACf,GAAG;IAEJ,gFAAgF;IAChF,IAAI,WAAW,CAAA,GAAA,cAAM,EAAE;QACrB,IAAI,eAAe,AAAC,CAAA,WAAW,QAAO,IAAK;QAC3C,eAAe,CAAA,GAAA,yCAAc,EAAE,cAAc,GAAG,eAAe,MAAM;QACrE,OAAO,KAAK,GAAG,CAAC,cAAc;IAChC,GAAG;QAAC;QAAM;QAAU;KAAS;IAE7B,IAAI,iBAAiB,CAAA,GAAA,kBAAU,EAC7B,CAAC,SACC,QAAQ,IAAI,CAAC,KAAK;YAChB,IAAI,MAAM,QAAQ,IAAI,WAAW,MAAM,CAAC,MAAM,EAAE;YAChD,IAAI,MAAM,QAAQ,OAAO,MAAM,GAAG,IAAI,WAAW,MAAM,CAAC,MAAM,EAAE;YAChE,OAAO,CAAA,GAAA,yCAAc,EAAE,KAAK,KAAK,KAAK;QACxC,IACF;QAAC;QAAU;QAAU;KAAK;IAG5B,IAAI,QAAQ,CAAA,GAAA,cAAM,EAChB,IAAM,eAAe,mCAAa,MAAM,KAAK,IAC7C;QAAC,MAAM,KAAK;QAAE;KAAe;IAE/B,IAAI,eAAe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AgB,KAAU,EAAE,KAAa,EAAE,KAAQ;IAC1D,IAAI,KAAK,CAAC,MAAM,KAAK,OACnB,OAAO;IAGT,OAAO;WAAI,MAAM,KAAK,CAAC,GAAG;QAAQ;WAAU,MAAM,KAAK,CAAC,QAAQ;KAAG;AACrE;AAEA,SAAS,mCAAa,KAAyB;IAC7C,IAAI,SAAS,MACX,OAAO;IAGT,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ;QAAC;KAAM;AAC/C;AAEA,SAAS,qCAAe,KAAK,EAAE,YAAY,EAAE,QAAQ;IACnD,OAAO,CAAC;QACN,IAAI,OAAO,UAAU,YAAY,OAAO,iBAAiB,UACvD,WAAW,QAAQ,CAAC,EAAE;aAEtB,WAAW;IAEf;AACF","sources":["packages/react-stately/src/slider/useSliderState.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 {clamp, snapValueToStep} from '../utils/number';\n\nimport {LabelableProps, Orientation, RangeInputBase, ValueBase} from '@react-types/shared';\nimport {useCallback, useMemo, useRef, useState} from 'react';\nimport {useControlledState} from '../utils/useControlledState';\n\nexport interface SliderProps<T = number | number[]>\n extends RangeInputBase<number>, ValueBase<T>, LabelableProps {\n /**\n * The orientation of the Slider.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation;\n /** Whether the whole Slider is disabled. */\n isDisabled?: boolean;\n /** Fired when the slider stops moving, due to being let go. */\n onChangeEnd?: (value: T) => void;\n // These are duplicated from ValueBase to define defaults for the docs.\n /**\n * The slider's minimum value.\n *\n * @default 0\n */\n minValue?: number;\n /**\n * The slider's maximum value.\n *\n * @default 100\n */\n maxValue?: number;\n /**\n * The slider's step value.\n *\n * @default 1\n */\n step?: number;\n}\n\nexport interface SliderState {\n /**\n * Values managed by the slider by thumb index.\n */\n readonly values: number[];\n /**\n * The default values for each thumb.\n */\n readonly defaultValues: number[];\n /**\n * Get the value for the specified thumb.\n *\n * @param index\n */\n getThumbValue(index: number): number;\n\n /**\n * Sets the value for the specified thumb.\n * The actual value set will be clamped and rounded according to min/max/step.\n *\n * @param index\n * @param value\n */\n setThumbValue(index: number, value: number): void;\n\n /**\n * Sets value for the specified thumb by percent offset (between 0 and 1).\n *\n * @param index\n * @param percent\n */\n setThumbPercent(index: number, percent: number): void;\n\n /**\n * Whether the specific thumb is being dragged.\n *\n * @param index\n */\n isThumbDragging(index: number): boolean;\n /**\n * Set is dragging on the specified thumb.\n *\n * @param index\n * @param dragging\n */\n setThumbDragging(index: number, dragging: boolean): void;\n\n /**\n * Currently-focused thumb index.\n */\n readonly focusedThumb: number | undefined;\n /**\n * Set focused true on specified thumb. This will remove focus from\n * any thumb that had it before.\n *\n * @param index\n */\n setFocusedThumb(index: number | undefined): void;\n\n /**\n * Returns the specified thumb's value as a percentage from 0 to 1.\n *\n * @param index\n */\n getThumbPercent(index: number): number;\n\n /**\n * Returns the value as a percent between the min and max of the slider.\n *\n * @param index\n */\n getValuePercent(value: number): number;\n\n /**\n * Returns the string label for the specified thumb's value, per props.formatOptions.\n *\n * @param index\n */\n getThumbValueLabel(index: number): string;\n\n /**\n * Returns the string label for the value, per props.formatOptions.\n *\n * @param value\n */\n getFormattedValue(value?: number | number[]): string;\n\n /**\n * Returns the min allowed value for the specified thumb.\n *\n * @param index\n */\n getThumbMinValue(index: number): number;\n\n /**\n * Returns the max allowed value for the specified thumb.\n *\n * @param index\n */\n getThumbMaxValue(index: number): number;\n\n /**\n * Converts a percent along track (between 0 and 1) to the corresponding value.\n *\n * @param percent\n */\n getPercentValue(percent: number): number;\n\n /**\n * Returns if the specified thumb is editable.\n *\n * @param index\n */\n isThumbEditable(index: number): boolean;\n\n /**\n * Set the specified thumb's editable state.\n *\n * @param index\n * @param editable\n */\n setThumbEditable(index: number, editable: boolean): void;\n\n /**\n * Increments the value of the thumb by the step or page amount.\n */\n incrementThumb(index: number, stepSize?: number): void;\n /**\n * Decrements the value of the thumb by the step or page amount.\n */\n decrementThumb(index: number, stepSize?: number): void;\n\n /**\n * The step amount for the slider.\n */\n readonly step: number;\n\n /**\n * The page size for the slider, used to do a bigger step.\n */\n readonly pageSize: number;\n\n /** The orientation of the slider. */\n readonly orientation: Orientation;\n\n /** Whether the slider is disabled. */\n readonly isDisabled: boolean;\n}\n\nconst DEFAULT_MIN_VALUE = 0;\nconst DEFAULT_MAX_VALUE = 100;\nconst DEFAULT_STEP_VALUE = 1;\n\nexport interface SliderStateOptions<T> extends SliderProps<T> {\n numberFormatter: Intl.NumberFormat;\n}\n\n/**\n * Provides state management for a slider component. Stores values for all thumbs,\n * formats values for localization, and provides methods to update the position\n * of any thumbs.\n *\n * @param props\n */\nexport function useSliderState<T extends number | number[]>(\n props: SliderStateOptions<T>\n): SliderState {\n const {\n isDisabled = false,\n minValue = DEFAULT_MIN_VALUE,\n maxValue = DEFAULT_MAX_VALUE,\n numberFormatter: formatter,\n step = DEFAULT_STEP_VALUE,\n orientation = 'horizontal'\n } = props;\n\n // Page step should be at least equal to step and always a multiple of the step.\n let pageSize = useMemo(() => {\n let calcPageSize = (maxValue - minValue) / 10;\n calcPageSize = snapValueToStep(calcPageSize, 0, calcPageSize + step, step);\n return Math.max(calcPageSize, step);\n }, [step, maxValue, minValue]);\n\n let restrictValues = useCallback(\n (values: number[] | undefined) =>\n values?.map((val, idx) => {\n let min = idx === 0 ? minValue : values[idx - 1];\n let max = idx === values.length - 1 ? maxValue : values[idx + 1];\n return snapValueToStep(val, min, max, step);\n }),\n [minValue, maxValue, step]\n );\n\n let value = useMemo(\n () => restrictValues(convertValue(props.value)),\n [props.value, restrictValues]\n );\n let defaultValue = useMemo(\n () => restrictValues(convertValue(props.defaultValue) ?? [minValue])!,\n [props.defaultValue, minValue, restrictValues]\n );\n let onChange = createOnChange(props.value, props.defaultValue, props.onChange);\n let onChangeEnd = createOnChange(props.value, props.defaultValue, props.onChangeEnd);\n\n const [values, setValuesState] = useControlledState<number[]>(value, defaultValue, onChange);\n let [initialValues] = useState(values);\n const [isDraggings, setDraggingsState] = useState<boolean[]>(\n new Array(values.length).fill(false)\n );\n const isEditablesRef = useRef<boolean[]>(new Array(values.length).fill(true));\n const [focusedIndex, setFocusedIndex] = useState<number | undefined>(undefined);\n\n const valuesRef = useRef<number[]>(values);\n const isDraggingsRef = useRef<boolean[]>(isDraggings);\n\n let setValues = (values: number[]) => {\n valuesRef.current = values;\n setValuesState(values);\n };\n\n let setDraggings = (draggings: boolean[]) => {\n isDraggingsRef.current = draggings;\n setDraggingsState(draggings);\n };\n\n function getValuePercent(value: number) {\n return (value - minValue) / (maxValue - minValue);\n }\n\n function getThumbMinValue(index: number) {\n return index === 0 ? minValue : values[index - 1];\n }\n function getThumbMaxValue(index: number) {\n return index === values.length - 1 ? maxValue : values[index + 1];\n }\n\n function isThumbEditable(index: number) {\n return isEditablesRef.current[index];\n }\n\n function setThumbEditable(index: number, editable: boolean) {\n isEditablesRef.current[index] = editable;\n }\n\n function updateValue(index: number, value: number) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n const thisMin = getThumbMinValue(index);\n const thisMax = getThumbMaxValue(index);\n\n // Round value to multiple of step, clamp value between min and max\n value = snapValueToStep(value, thisMin, thisMax, step);\n let newValues = replaceIndex(valuesRef.current, index, value);\n setValues(newValues);\n }\n\n function updateDragging(index: number, dragging: boolean) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n if (dragging) {\n valuesRef.current = values;\n }\n\n const wasDragging = isDraggingsRef.current[index];\n isDraggingsRef.current = replaceIndex(isDraggingsRef.current, index, dragging);\n setDraggings(isDraggingsRef.current);\n\n // Call onChangeEnd if no handles are dragging.\n if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) {\n onChangeEnd(valuesRef.current);\n }\n }\n\n let listFormatRef = useRef<Intl.ListFormat | null>(null);\n\n function getFormattedValue(valueParam: number | number[] = values) {\n if (typeof valueParam === 'number') {\n valueParam = [valueParam];\n }\n\n switch (valueParam.length) {\n case 0:\n return '';\n case 1:\n return formatter.format(valueParam[0]);\n case 2:\n return formatter.formatRange(valueParam[0], valueParam[1]);\n default: {\n let formatted = valueParam.map(value => formatter.format(value));\n let listFormat = listFormatRef.current;\n if (\n !listFormat ||\n listFormat.resolvedOptions().locale !== formatter.resolvedOptions().locale\n ) {\n listFormat = listFormatRef.current = new Intl.ListFormat(\n formatter.resolvedOptions().locale,\n {type: 'unit'}\n );\n }\n return listFormat.format(formatted);\n }\n }\n }\n\n function setThumbPercent(index: number, percent: number) {\n updateValue(index, getPercentValue(percent));\n }\n\n function getRoundedValue(value: number) {\n return Math.round((value - minValue) / step) * step + minValue;\n }\n\n function getPercentValue(percent: number) {\n const val = percent * (maxValue - minValue) + minValue;\n return clamp(getRoundedValue(val), minValue, maxValue);\n }\n\n function incrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] + s, minValue, maxValue, step));\n }\n\n function decrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] - s, minValue, maxValue, step));\n }\n\n return {\n values: values,\n defaultValues: props.defaultValue !== undefined ? defaultValue : initialValues,\n getThumbValue: (index: number) => values[index],\n setThumbValue: updateValue,\n setThumbPercent,\n isThumbDragging: (index: number) => isDraggings[index],\n setThumbDragging: updateDragging,\n focusedThumb: focusedIndex,\n setFocusedThumb: setFocusedIndex,\n getThumbPercent: (index: number) => getValuePercent(values[index]),\n getValuePercent,\n getThumbValueLabel: (index: number) => getFormattedValue(values[index]),\n getFormattedValue,\n getThumbMinValue,\n getThumbMaxValue,\n getPercentValue,\n isThumbEditable,\n setThumbEditable,\n incrementThumb,\n decrementThumb,\n step,\n pageSize,\n orientation,\n isDisabled\n };\n}\n\nfunction replaceIndex<T>(array: T[], index: number, value: T) {\n if (array[index] === value) {\n return array;\n }\n\n return [...array.slice(0, index), value, ...array.slice(index + 1)];\n}\n\nfunction convertValue(value?: number | number[]) {\n if (value == null) {\n return undefined;\n }\n\n return Array.isArray(value) ? value : [value];\n}\n\nfunction createOnChange(value, defaultValue, onChange) {\n return (newValue: number[]) => {\n if (typeof value === 'number' || typeof defaultValue === 'number') {\n onChange?.(newValue[0]);\n } else {\n onChange?.(newValue);\n }\n };\n}\n"],"names":[],"version":3,"file":"useSliderState.mjs.map"}
|
|
1
|
+
{"mappings":";;;;AAAA;;;;;;;;;;CAUC;;;AA8LD,MAAM,0CAAoB;AAC1B,MAAM,0CAAoB;AAC1B,MAAM,2CAAqB;AAcpB,SAAS,0CACd,KAA4B;IAE5B,MAAM,cACJ,aAAa,iBACb,WAAW,mDACX,WAAW,yCACX,iBAAiB,SAAS,QAC1B,OAAO,uDACP,cAAc,cACf,GAAG;IAEJ,gFAAgF;IAChF,IAAI,WAAW,CAAA,GAAA,cAAM,EAAE;QACrB,IAAI,eAAe,AAAC,CAAA,WAAW,QAAO,IAAK;QAC3C,eAAe,CAAA,GAAA,yCAAc,EAAE,cAAc,GAAG,eAAe,MAAM;QACrE,OAAO,KAAK,GAAG,CAAC,cAAc;IAChC,GAAG;QAAC;QAAM;QAAU;KAAS;IAE7B,IAAI,iBAAiB,CAAA,GAAA,kBAAU,EAC7B,CAAC,SACC,QAAQ,IAAI,CAAC,KAAK;YAChB,IAAI,MAAM,QAAQ,IAAI,WAAW,MAAM,CAAC,MAAM,EAAE;YAChD,IAAI,MAAM,QAAQ,OAAO,MAAM,GAAG,IAAI,WAAW,MAAM,CAAC,MAAM,EAAE;YAChE,OAAO,CAAA,GAAA,yCAAc,EAAE,KAAK,KAAK,KAAK;QACxC,IACF;QAAC;QAAU;QAAU;KAAK;IAG5B,IAAI,QAA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AAM,CAAC,MAAM;2BACtE;0BACA;0BACA;yBACA;yBACA;0BACA;wBACA;wBACA;cACA;kBACA;qBACA;oBACA;IACF;AACF;AAEA,SAAS,mCAAgB,KAAU,EAAE,KAAa,EAAE,KAAQ;IAC1D,IAAI,KAAK,CAAC,MAAM,KAAK,OACnB,OAAO;IAGT,OAAO;WAAI,MAAM,KAAK,CAAC,GAAG;QAAQ;WAAU,MAAM,KAAK,CAAC,QAAQ;KAAG;AACrE;AAEA,SAAS,mCAAa,KAAyB;IAC7C,IAAI,SAAS,MACX,OAAO;IAGT,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ;QAAC;KAAM;AAC/C;AAEA,SAAS,qCAAe,KAAK,EAAE,YAAY,EAAE,QAAQ;IACnD,OAAO,CAAC;QACN,IAAI,OAAO,UAAU,YAAY,OAAO,iBAAiB,UACvD,WAAW,QAAQ,CAAC,EAAE;aAEtB,WAAW;IAEf;AACF","sources":["packages/react-stately/src/slider/useSliderState.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 {clamp, snapValueToStep} from '../utils/number';\n\nimport {LabelableProps, Orientation, RangeInputBase, ValueBase} from '@react-types/shared';\nimport {useCallback, useMemo, useRef, useState} from 'react';\nimport {useControlledState} from '../utils/useControlledState';\n\nexport interface SliderProps<T = number | number[]>\n extends RangeInputBase<number>, ValueBase<T>, LabelableProps {\n /**\n * The orientation of the Slider.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation;\n /** Whether the whole Slider is disabled. */\n isDisabled?: boolean;\n /** Fired when the slider stops moving, due to being let go. */\n onChangeEnd?: (value: T) => void;\n // These are duplicated from ValueBase to define defaults for the docs.\n /**\n * The slider's minimum value.\n *\n * @default 0\n */\n minValue?: number;\n /**\n * The slider's maximum value.\n *\n * @default 100\n */\n maxValue?: number;\n /**\n * The slider's step value.\n *\n * @default 1\n */\n step?: number;\n}\n\nexport interface SliderState {\n /**\n * Values managed by the slider by thumb index.\n */\n readonly values: number[];\n /**\n * The default values for each thumb.\n */\n readonly defaultValues: number[];\n /**\n * Get the value for the specified thumb.\n *\n * @param index\n */\n getThumbValue(index: number): number;\n\n /**\n * Sets the value for the specified thumb.\n * The actual value set will be clamped and rounded according to min/max/step.\n *\n * @param index\n * @param value\n */\n setThumbValue(index: number, value: number): void;\n\n /**\n * Sets value for the specified thumb by percent offset (between 0 and 1).\n *\n * @param index\n * @param percent\n */\n setThumbPercent(index: number, percent: number): void;\n\n /**\n * Whether the specific thumb is being dragged.\n *\n * @param index\n */\n isThumbDragging(index: number): boolean;\n /**\n * Set is dragging on the specified thumb.\n *\n * @param index\n * @param dragging\n */\n setThumbDragging(index: number, dragging: boolean): void;\n\n /**\n * Currently-focused thumb index.\n */\n readonly focusedThumb: number | undefined;\n /**\n * Set focused true on specified thumb. This will remove focus from\n * any thumb that had it before.\n *\n * @param index\n */\n setFocusedThumb(index: number | undefined): void;\n\n /**\n * Returns the specified thumb's value as a percentage from 0 to 1.\n *\n * @param index\n */\n getThumbPercent(index: number): number;\n\n /**\n * Returns the value as a percent between the min and max of the slider.\n *\n * @param index\n */\n getValuePercent(value: number): number;\n\n /**\n * Returns the string label for the specified thumb's value, per props.formatOptions.\n *\n * @param index\n */\n getThumbValueLabel(index: number): string;\n\n /**\n * Returns the string label for the value, per props.formatOptions.\n *\n * @param value\n */\n getFormattedValue(value?: number | number[]): string;\n\n /**\n * Returns the min allowed value for the specified thumb.\n *\n * @param index\n */\n getThumbMinValue(index: number): number;\n\n /**\n * Returns the max allowed value for the specified thumb.\n *\n * @param index\n */\n getThumbMaxValue(index: number): number;\n\n /**\n * Converts a percent along track (between 0 and 1) to the corresponding value.\n *\n * @param percent\n */\n getPercentValue(percent: number): number;\n\n /**\n * Returns if the specified thumb is editable.\n *\n * @param index\n */\n isThumbEditable(index: number): boolean;\n\n /**\n * Set the specified thumb's editable state.\n *\n * @param index\n * @param editable\n */\n setThumbEditable(index: number, editable: boolean): void;\n\n /**\n * Increments the value of the thumb by the step or page amount.\n */\n incrementThumb(index: number, stepSize?: number): void;\n /**\n * Decrements the value of the thumb by the step or page amount.\n */\n decrementThumb(index: number, stepSize?: number): void;\n\n /**\n * The step amount for the slider.\n */\n readonly step: number;\n\n /**\n * The page size for the slider, used to do a bigger step.\n */\n readonly pageSize: number;\n\n /** The orientation of the slider. */\n readonly orientation: Orientation;\n\n /** Whether the slider is disabled. */\n readonly isDisabled: boolean;\n}\n\nconst DEFAULT_MIN_VALUE = 0;\nconst DEFAULT_MAX_VALUE = 100;\nconst DEFAULT_STEP_VALUE = 1;\n\nexport interface SliderStateOptions<T> extends SliderProps<T> {\n numberFormatter: Intl.NumberFormat;\n}\n\n/**\n * Provides state management for a slider component. Stores values for all thumbs,\n * formats values for localization, and provides methods to update the position\n * of any thumbs.\n *\n * @param props\n */\n// oxlint-disable-next-line react/react-compiler\nexport function useSliderState<T extends number | number[]>(\n props: SliderStateOptions<T>\n): SliderState {\n const {\n isDisabled = false,\n minValue = DEFAULT_MIN_VALUE,\n maxValue = DEFAULT_MAX_VALUE,\n numberFormatter: formatter,\n step = DEFAULT_STEP_VALUE,\n orientation = 'horizontal'\n } = props;\n\n // Page step should be at least equal to step and always a multiple of the step.\n let pageSize = useMemo(() => {\n let calcPageSize = (maxValue - minValue) / 10;\n calcPageSize = snapValueToStep(calcPageSize, 0, calcPageSize + step, step);\n return Math.max(calcPageSize, step);\n }, [step, maxValue, minValue]);\n\n let restrictValues = useCallback(\n (values: number[] | undefined) =>\n values?.map((val, idx) => {\n let min = idx === 0 ? minValue : values[idx - 1];\n let max = idx === values.length - 1 ? maxValue : values[idx + 1];\n return snapValueToStep(val, min, max, step);\n }),\n [minValue, maxValue, step]\n );\n\n let value = useMemo(\n () => restrictValues(convertValue(props.value)),\n [props.value, restrictValues]\n );\n let defaultValue = useMemo(\n () => restrictValues(convertValue(props.defaultValue) ?? [minValue])!,\n [props.defaultValue, minValue, restrictValues]\n );\n let onChange = createOnChange(props.value, props.defaultValue, props.onChange);\n let onChangeEnd = createOnChange(props.value, props.defaultValue, props.onChangeEnd);\n\n const [values, setValuesState] = useControlledState<number[]>(value, defaultValue, onChange);\n let [initialValues] = useState(values);\n const [isDraggings, setDraggingsState] = useState<boolean[]>(\n new Array(values.length).fill(false)\n );\n const isEditablesRef = useRef<boolean[]>(new Array(values.length).fill(true));\n const [focusedIndex, setFocusedIndex] = useState<number | undefined>(undefined);\n\n const valuesRef = useRef<number[]>(values);\n const isDraggingsRef = useRef<boolean[]>(isDraggings);\n\n let setValues = (values: number[]) => {\n valuesRef.current = values;\n setValuesState(values);\n };\n\n let setDraggings = (draggings: boolean[]) => {\n isDraggingsRef.current = draggings;\n setDraggingsState(draggings);\n };\n\n function getValuePercent(value: number) {\n return (value - minValue) / (maxValue - minValue);\n }\n\n function getThumbMinValue(index: number) {\n return index === 0 ? minValue : values[index - 1];\n }\n function getThumbMaxValue(index: number) {\n return index === values.length - 1 ? maxValue : values[index + 1];\n }\n\n function isThumbEditable(index: number) {\n return isEditablesRef.current[index];\n }\n\n function setThumbEditable(index: number, editable: boolean) {\n isEditablesRef.current[index] = editable;\n }\n\n function updateValue(index: number, value: number) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n const thisMin = getThumbMinValue(index);\n const thisMax = getThumbMaxValue(index);\n\n // Round value to multiple of step, clamp value between min and max\n value = snapValueToStep(value, thisMin, thisMax, step);\n let newValues = replaceIndex(valuesRef.current, index, value);\n setValues(newValues);\n }\n\n function updateDragging(index: number, dragging: boolean) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n if (dragging) {\n valuesRef.current = values;\n }\n\n const wasDragging = isDraggingsRef.current[index];\n isDraggingsRef.current = replaceIndex(isDraggingsRef.current, index, dragging);\n setDraggings(isDraggingsRef.current);\n\n // Call onChangeEnd if no handles are dragging.\n if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) {\n onChangeEnd(valuesRef.current);\n }\n }\n\n let listFormatRef = useRef<Intl.ListFormat | null>(null);\n\n function getFormattedValue(valueParam: number | number[] = values) {\n if (typeof valueParam === 'number') {\n valueParam = [valueParam];\n }\n\n switch (valueParam.length) {\n case 0:\n return '';\n case 1:\n return formatter.format(valueParam[0]);\n case 2:\n return formatter.formatRange(valueParam[0], valueParam[1]);\n default: {\n let formatted = valueParam.map(value => formatter.format(value));\n let listFormat = listFormatRef.current;\n if (\n !listFormat ||\n listFormat.resolvedOptions().locale !== formatter.resolvedOptions().locale\n ) {\n listFormat = listFormatRef.current = new Intl.ListFormat(\n formatter.resolvedOptions().locale,\n {type: 'unit'}\n );\n }\n return listFormat.format(formatted);\n }\n }\n }\n\n function setThumbPercent(index: number, percent: number) {\n updateValue(index, getPercentValue(percent));\n }\n\n function getRoundedValue(value: number) {\n return Math.round((value - minValue) / step) * step + minValue;\n }\n\n function getPercentValue(percent: number) {\n const val = percent * (maxValue - minValue) + minValue;\n return clamp(getRoundedValue(val), minValue, maxValue);\n }\n\n function incrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] + s, minValue, maxValue, step));\n }\n\n function decrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] - s, minValue, maxValue, step));\n }\n\n return {\n values: values,\n defaultValues: props.defaultValue !== undefined ? defaultValue : initialValues,\n getThumbValue: (index: number) => values[index],\n setThumbValue: updateValue,\n setThumbPercent,\n isThumbDragging: (index: number) => isDraggings[index],\n setThumbDragging: updateDragging,\n focusedThumb: focusedIndex,\n setFocusedThumb: setFocusedIndex,\n getThumbPercent: (index: number) => getValuePercent(values[index]),\n getValuePercent,\n getThumbValueLabel: (index: number) => getFormattedValue(values[index]),\n getFormattedValue,\n getThumbMinValue,\n getThumbMaxValue,\n getPercentValue,\n isThumbEditable,\n setThumbEditable,\n incrementThumb,\n decrementThumb,\n step,\n pageSize,\n orientation,\n isDisabled\n };\n}\n\nfunction replaceIndex<T>(array: T[], index: number, value: T) {\n if (array[index] === value) {\n return array;\n }\n\n return [...array.slice(0, index), value, ...array.slice(index + 1)];\n}\n\nfunction convertValue(value?: number | number[]) {\n if (value == null) {\n return undefined;\n }\n\n return Array.isArray(value) ? value : [value];\n}\n\nfunction createOnChange(value, defaultValue, onChange) {\n return (newValue: number[]) => {\n if (typeof value === 'number' || typeof defaultValue === 'number') {\n onChange?.(newValue[0]);\n } else {\n onChange?.(newValue);\n }\n };\n}\n"],"names":[],"version":3,"file":"useSliderState.mjs.map"}
|
|
@@ -44,6 +44,10 @@ function $4f38c929ec2b5900$export$f556054ce4358701(minWidth, tableWidth) {
|
|
|
44
44
|
return minWidth != null ? $4f38c929ec2b5900$export$7bbad27896f7ae9f(minWidth, tableWidth) : 0;
|
|
45
45
|
}
|
|
46
46
|
function $4f38c929ec2b5900$export$55d50dc687385491(availableWidth, columns, changedColumns, getDefaultWidth, getDefaultMinWidth) {
|
|
47
|
+
let originalWidth = availableWidth;
|
|
48
|
+
let flooredWidth = Math.floor(availableWidth);
|
|
49
|
+
let hasFractionalWidth = availableWidth - flooredWidth > 0;
|
|
50
|
+
availableWidth = flooredWidth;
|
|
47
51
|
let hasNonFrozenItems = false;
|
|
48
52
|
let flexItems = columns.map((column, index)=>{
|
|
49
53
|
let width = changedColumns.get(column.key) != null ? changedColumns.get(column.key) ?? '1fr' : column.width ?? column.defaultWidth ?? getDefaultWidth?.(index) ?? '1fr';
|
|
@@ -157,7 +161,15 @@ function $4f38c929ec2b5900$export$55d50dc687385491(availableWidth, columns, chan
|
|
|
157
161
|
else if (!item.frozen) hasNonFrozenItems = true;
|
|
158
162
|
});
|
|
159
163
|
}
|
|
160
|
-
|
|
164
|
+
let columnSizes = $4f38c929ec2b5900$var$cascadeRounding(flexItems);
|
|
165
|
+
// Give the leftover sub-pixel width to the last column so the columns sum
|
|
166
|
+
// exactly to the (possibly fractional) available width.
|
|
167
|
+
if (hasFractionalWidth && columnSizes.length > 0) {
|
|
168
|
+
let tableFractionalWidth = originalWidth.toString().split('.')[1];
|
|
169
|
+
let columnWidth = columnSizes[columnSizes.length - 1].toString();
|
|
170
|
+
columnSizes[columnSizes.length - 1] = Number(columnWidth + '.' + tableFractionalWidth);
|
|
171
|
+
}
|
|
172
|
+
return columnSizes;
|
|
161
173
|
}
|
|
162
174
|
function $4f38c929ec2b5900$var$cascadeRounding(flexItems) {
|
|
163
175
|
/*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC,GAMM,SAAS,0CAAS,KAAyB;IAChD,OAAO,SAAS,QAAS,CAAA,CAAC,MAAM,UAAoB,OAAO,OAAO,KAAK,CAAC,oBAAoB,IAAG;AACjG;AAEO,SAAS,0CAAoB,KAAyB;IAC3D,IAAI,CAAC,SAAS,OAAO,UAAU,UAC7B,OAAO;IAET,IAAI,QAAQ,MAAM,KAAK,CAAC;IACxB,6DAA6D;IAC7D,IAAI,CAAC,OAAO;QACV,IAAI,QAAQ,GAAG,CAAC,QAAQ,KAAK,cAC3B,QAAQ,IAAI,CACV,CAAC,OAAO,EAAE,MAAM,6GAA6G,CAAC,EAC9H;QAGJ,OAAO;IACT;IACA,OAAO,WAAW,KAAK,CAAC,EAAE;AAC5B;AAEO,SAAS,0CAAiB,KAAsB,EAAE,UAAkB;IACzE,IAAI,OAAO,UAAU,UAAU;QAC7B,IAAI,QAAQ,MAAM,KAAK,CAAC;QACxB,IAAI,CAAC,OACH,MAAM,IAAI,MAAM;QAElB,OAAO,aAAc,CAAA,WAAW,KAAK,CAAC,EAAE,IAAI,GAAE;IAChD;IACA,OAAO;AACT;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c;QAC/B,IAAI,UAAU,KAAK,KAAK,CAAC,QAAQ,WAAW;QAC5C,WAAW;QACX,YAAY;QACZ,aAAa,IAAI,CAAC;IACpB;IAEA,OAAO;AACT","sources":["packages/react-stately/src/table/TableUtils.ts"],"sourcesContent":["/*\n * Copyright 2022 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 {ColumnSize} from './Column';\nimport {Key} from '@react-types/shared';\n\n// numbers and percents are considered static. *fr units or a lack of units are considered dynamic.\nexport function isStatic(width?: ColumnSize | null): boolean {\n return width != null && (!isNaN(width as number) || String(width).match(/^(\\d+)(?=%$)/) !== null);\n}\n\nexport function parseFractionalUnit(width?: ColumnSize | null): number {\n if (!width || typeof width === 'number') {\n return 1;\n }\n let match = width.match(/^(.+)(?=fr$)/);\n // if width is the incorrect format, just default it to a 1fr\n if (!match) {\n if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `width: ${width} is not a supported format, width should be a number (ex. 150), percentage (ex. '50%') or fr unit (ex. '2fr')`,\n \"defaulting to '1fr'\"\n );\n }\n return 1;\n }\n return parseFloat(match[0]);\n}\n\nexport function parseStaticWidth(width: number | string, tableWidth: number): number {\n if (typeof width === 'string') {\n let match = width.match(/^(\\d+)(?=%$)/);\n if (!match) {\n throw new Error('Only percentages or numbers are supported for static column widths');\n }\n return tableWidth * (parseFloat(match[0]) / 100);\n }\n return width;\n}\n\nexport function getMaxWidth(\n maxWidth: number | string | null | undefined,\n tableWidth: number\n): number {\n return maxWidth != null ? parseStaticWidth(maxWidth, tableWidth) : Number.MAX_SAFE_INTEGER;\n}\n\n// cannot support FR units, we'd need to know everything else in the table to do that\nexport function getMinWidth(minWidth: number | string, tableWidth: number): number {\n return minWidth != null ? parseStaticWidth(minWidth, tableWidth) : 0;\n}\n\nexport interface IColumn {\n minWidth?: number | string;\n maxWidth?: number | string;\n width?: number | string;\n defaultWidth?: number | string;\n key: Key;\n}\n\ninterface FlexItem {\n frozen: boolean;\n baseSize: number;\n hypotheticalMainSize: number;\n min: number;\n max: number;\n flex: number;\n targetMainSize: number;\n violation: number;\n}\n\n/**\n * Implements the flex algorithm described in https://www.w3.org/TR/css-flexbox-1/#layout-algorithm\n * It makes a few constraint/assumptions: 1. All basis values are 0 unless it is a static width,\n * then the basis is the static width 2. All flex grow and shrink values are equal to the FR\n * specified on the column, grow and shrink for the same column are equal 3. We only have one row An\n * example of the setup can be seen here https://jsfiddle.net/snowystinger/wv0ymjaf/61/ where I let\n * the browser figure out the flex of the columns. Note: We differ in one key aspect, all of our\n * column widths must be whole numbers, so we avoid browser sub pixel rounding errors. To do this,\n * we use a cascading rounding algorithm to ensure that the sum of the widths is maintained while\n * distributing the rounding remainder across the columns.\n *\n * As noted in the chrome source code, this algorithm is very accurate, but has the potential to be\n * quadratic. They have deemed this to be acceptable because the number of elements is usually small\n * and the flex factors are usually not high variance. I believe we can make the same assumptions.\n * Particularly once resizing is started, it will convert all columns to the left to static widths,\n * so it will cut down on the number of FR columns.\n *\n * There are likely faster ways to do this, I've chosen to stick to the spec as closely as possible\n * for readability, accuracy, and for the note that this behaving quadratically is unlikely to be a\n * problem.\n *\n * @param availableWidth - The visible width of the table.\n * @param columns - The table defined columns.\n * @param changedColumns - Any columns we want to override, for example, during resizing.\n * @param getDefaultWidth - A function that returns the default width of a column by its index.\n * @param getDefaultMinWidth - A function that returns the default min width of a column by its\n * index.\n */\nexport function calculateColumnSizes(\n availableWidth: number,\n columns: IColumn[],\n changedColumns: Map<Key, ColumnSize>,\n getDefaultWidth?: (index: number) => ColumnSize | null | undefined,\n getDefaultMinWidth?: (index: number) => ColumnSize | null | undefined\n): number[] {\n let hasNonFrozenItems = false;\n let flexItems: FlexItem[] = columns.map((column, index) => {\n let width: ColumnSize = (\n changedColumns.get(column.key) != null\n ? (changedColumns.get(column.key) ?? '1fr')\n : (column.width ?? column.defaultWidth ?? getDefaultWidth?.(index) ?? '1fr')\n ) as ColumnSize;\n let frozen = false;\n let baseSize = 0;\n let flex = 0;\n let targetMainSize = 0;\n if (isStatic(width)) {\n baseSize = parseStaticWidth(width, availableWidth);\n frozen = true;\n } else {\n flex = parseFractionalUnit(width);\n if (flex <= 0) {\n frozen = true;\n }\n }\n\n let min = getMinWidth(column.minWidth ?? getDefaultMinWidth?.(index) ?? 0, availableWidth);\n let max = getMaxWidth(column.maxWidth, availableWidth);\n let hypotheticalMainSize = Math.max(min, Math.min(baseSize, max));\n\n // 9.7.1\n // We don't make use of flex basis, it's always 0, so we are always in 'grow' mode.\n // 9.7.2\n if (frozen) {\n targetMainSize = hypotheticalMainSize;\n } else if (baseSize > hypotheticalMainSize) {\n frozen = true;\n targetMainSize = hypotheticalMainSize;\n }\n\n // 9.7.3\n if (!frozen) {\n hasNonFrozenItems = true;\n }\n return {\n frozen,\n baseSize,\n hypotheticalMainSize,\n min,\n max,\n flex,\n targetMainSize,\n violation: 0\n };\n });\n\n // 9.7.4\n // 9.7.4.a\n while (hasNonFrozenItems) {\n // 9.7.4.b\n /**\n * Calculate the remaining free space as for initial free space,\n * above (9.7.3). If the sum of the unfrozen flex items’ flex factors is\n * less than one, multiply the initial free space by this sum (of flex factors).\n * If the magnitude of this value is less than the magnitude of\n * the remaining free space, use this as the remaining free space.\n */\n let usedWidth = 0;\n let flexFactors = 0;\n flexItems.forEach(item => {\n if (item.frozen) {\n usedWidth += item.targetMainSize;\n } else {\n usedWidth += item.baseSize;\n flexFactors += item.flex;\n }\n });\n\n let remainingFreeSpace = availableWidth - usedWidth;\n // we only support integer FR's, and because of hasNonFrozenItems, we know that flexFactors > 0\n // so no need to check for flexFactors < 1\n // 9.7.4.c\n /**\n * If the remaining free space is zero\n * - Do nothing.\n * Else // remember, we're always in grow mode\n * - Find the ratio of the item’s flex grow factor to the\n * sum of the flex grow factors of all unfrozen items on\n * the line. Set the item’s target main size to its flex\n * base size plus a fraction of the remaining free space\n * proportional to the ratio.\n */\n if (remainingFreeSpace > 0) {\n flexItems.forEach(item => {\n if (!item.frozen) {\n let ratio = item.flex / flexFactors;\n item.targetMainSize = item.baseSize + ratio * remainingFreeSpace;\n }\n });\n }\n\n // 9.7.4.d\n /**\n * Fix min/max violations. Clamp each non-frozen item’s\n * target main size by its used min and max main sizes\n * and floor its content-box size at zero. If the item’s\n * target main size was made smaller by this, it’s a max\n * violation. If the item’s target main size was made\n * larger by this, it’s a min violation.\n */\n let totalViolation = 0;\n flexItems.forEach(item => {\n item.violation = 0;\n if (!item.frozen) {\n let {min, max, targetMainSize} = item;\n item.targetMainSize = Math.max(min, Math.min(targetMainSize, max));\n\n item.violation = item.targetMainSize - targetMainSize;\n totalViolation += item.violation;\n }\n });\n\n // 9.7.4.e\n /**\n * Freeze over-flexed items. The total violation is the\n * sum of the adjustments from the previous step\n * ∑(clamped size - unclamped size). If the total violation is:\n * Zero\n * - Freeze all items.\n *\n * Positive\n * - Freeze all the items with min violations.\n *\n * Negative\n * - Freeze all the items with max violations.\n */\n hasNonFrozenItems = false;\n flexItems.forEach(item => {\n if (totalViolation === 0 || Math.sign(totalViolation) === Math.sign(item.violation)) {\n item.frozen = true;\n } else if (!item.frozen) {\n hasNonFrozenItems = true;\n }\n });\n }\n\n return cascadeRounding(flexItems);\n}\n\nfunction cascadeRounding(flexItems: FlexItem[]): number[] {\n /*\n Given an array of floats that sum to an integer, this rounds the floats\n and returns an array of integers with the same sum.\n */\n\n let fpTotal = 0;\n let intTotal = 0;\n let roundedArray: number[] = [];\n flexItems.forEach(function (item) {\n let float = item.targetMainSize;\n let integer = Math.round(float + fpTotal) - intTotal;\n fpTotal += float;\n intTotal += integer;\n roundedArray.push(integer);\n });\n\n return roundedArray;\n}\n"],"names":[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AAW;QACX,YAAY;QACZ,aAAa,IAAI,CAAC;IACpB;IAEA,OAAO;AACT","sources":["packages/react-stately/src/table/TableUtils.ts"],"sourcesContent":["/*\n * Copyright 2022 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 {ColumnSize} from './Column';\nimport {Key} from '@react-types/shared';\n\n// numbers and percents are considered static. *fr units or a lack of units are considered dynamic.\nexport function isStatic(width?: ColumnSize | null): boolean {\n return width != null && (!isNaN(width as number) || String(width).match(/^(\\d+)(?=%$)/) !== null);\n}\n\nexport function parseFractionalUnit(width?: ColumnSize | null): number {\n if (!width || typeof width === 'number') {\n return 1;\n }\n let match = width.match(/^(.+)(?=fr$)/);\n // if width is the incorrect format, just default it to a 1fr\n if (!match) {\n if (process.env.NODE_ENV !== 'production') {\n console.warn(\n `width: ${width} is not a supported format, width should be a number (ex. 150), percentage (ex. '50%') or fr unit (ex. '2fr')`,\n \"defaulting to '1fr'\"\n );\n }\n return 1;\n }\n return parseFloat(match[0]);\n}\n\nexport function parseStaticWidth(width: number | string, tableWidth: number): number {\n if (typeof width === 'string') {\n let match = width.match(/^(\\d+)(?=%$)/);\n if (!match) {\n throw new Error('Only percentages or numbers are supported for static column widths');\n }\n return tableWidth * (parseFloat(match[0]) / 100);\n }\n return width;\n}\n\nexport function getMaxWidth(\n maxWidth: number | string | null | undefined,\n tableWidth: number\n): number {\n return maxWidth != null ? parseStaticWidth(maxWidth, tableWidth) : Number.MAX_SAFE_INTEGER;\n}\n\n// cannot support FR units, we'd need to know everything else in the table to do that\nexport function getMinWidth(minWidth: number | string, tableWidth: number): number {\n return minWidth != null ? parseStaticWidth(minWidth, tableWidth) : 0;\n}\n\nexport interface IColumn {\n minWidth?: number | string;\n maxWidth?: number | string;\n width?: number | string;\n defaultWidth?: number | string;\n key: Key;\n}\n\ninterface FlexItem {\n frozen: boolean;\n baseSize: number;\n hypotheticalMainSize: number;\n min: number;\n max: number;\n flex: number;\n targetMainSize: number;\n violation: number;\n}\n\n/**\n * Implements the flex algorithm described in https://www.w3.org/TR/css-flexbox-1/#layout-algorithm\n * It makes a few constraint/assumptions: 1. All basis values are 0 unless it is a static width,\n * then the basis is the static width 2. All flex grow and shrink values are equal to the FR\n * specified on the column, grow and shrink for the same column are equal 3. We only have one row An\n * example of the setup can be seen here https://jsfiddle.net/snowystinger/wv0ymjaf/61/ where I let\n * the browser figure out the flex of the columns. Note: We differ in one key aspect, all of our\n * column widths must be whole numbers, so we avoid browser sub pixel rounding errors. To do this,\n * we use a cascading rounding algorithm to ensure that the sum of the widths is maintained while\n * distributing the rounding remainder across the columns.\n *\n * As noted in the chrome source code, this algorithm is very accurate, but has the potential to be\n * quadratic. They have deemed this to be acceptable because the number of elements is usually small\n * and the flex factors are usually not high variance. I believe we can make the same assumptions.\n * Particularly once resizing is started, it will convert all columns to the left to static widths,\n * so it will cut down on the number of FR columns.\n *\n * There are likely faster ways to do this, I've chosen to stick to the spec as closely as possible\n * for readability, accuracy, and for the note that this behaving quadratically is unlikely to be a\n * problem.\n *\n * @param availableWidth - The visible width of the table.\n * @param columns - The table defined columns.\n * @param changedColumns - Any columns we want to override, for example, during resizing.\n * @param getDefaultWidth - A function that returns the default width of a column by its index.\n * @param getDefaultMinWidth - A function that returns the default min width of a column by its\n * index.\n */\nexport function calculateColumnSizes(\n availableWidth: number,\n columns: IColumn[],\n changedColumns: Map<Key, ColumnSize>,\n getDefaultWidth?: (index: number) => ColumnSize | null | undefined,\n getDefaultMinWidth?: (index: number) => ColumnSize | null | undefined\n): number[] {\n let originalWidth = availableWidth;\n let flooredWidth = Math.floor(availableWidth);\n let hasFractionalWidth = availableWidth - flooredWidth > 0;\n availableWidth = flooredWidth;\n let hasNonFrozenItems = false;\n let flexItems: FlexItem[] = columns.map((column, index) => {\n let width: ColumnSize = (\n changedColumns.get(column.key) != null\n ? (changedColumns.get(column.key) ?? '1fr')\n : (column.width ?? column.defaultWidth ?? getDefaultWidth?.(index) ?? '1fr')\n ) as ColumnSize;\n let frozen = false;\n let baseSize = 0;\n let flex = 0;\n let targetMainSize = 0;\n if (isStatic(width)) {\n baseSize = parseStaticWidth(width, availableWidth);\n frozen = true;\n } else {\n flex = parseFractionalUnit(width);\n if (flex <= 0) {\n frozen = true;\n }\n }\n\n let min = getMinWidth(column.minWidth ?? getDefaultMinWidth?.(index) ?? 0, availableWidth);\n let max = getMaxWidth(column.maxWidth, availableWidth);\n let hypotheticalMainSize = Math.max(min, Math.min(baseSize, max));\n\n // 9.7.1\n // We don't make use of flex basis, it's always 0, so we are always in 'grow' mode.\n // 9.7.2\n if (frozen) {\n targetMainSize = hypotheticalMainSize;\n } else if (baseSize > hypotheticalMainSize) {\n frozen = true;\n targetMainSize = hypotheticalMainSize;\n }\n\n // 9.7.3\n if (!frozen) {\n hasNonFrozenItems = true;\n }\n return {\n frozen,\n baseSize,\n hypotheticalMainSize,\n min,\n max,\n flex,\n targetMainSize,\n violation: 0\n };\n });\n\n // 9.7.4\n // 9.7.4.a\n while (hasNonFrozenItems) {\n // 9.7.4.b\n /**\n * Calculate the remaining free space as for initial free space,\n * above (9.7.3). If the sum of the unfrozen flex items’ flex factors is\n * less than one, multiply the initial free space by this sum (of flex factors).\n * If the magnitude of this value is less than the magnitude of\n * the remaining free space, use this as the remaining free space.\n */\n let usedWidth = 0;\n let flexFactors = 0;\n flexItems.forEach(item => {\n if (item.frozen) {\n usedWidth += item.targetMainSize;\n } else {\n usedWidth += item.baseSize;\n flexFactors += item.flex;\n }\n });\n\n let remainingFreeSpace = availableWidth - usedWidth;\n // we only support integer FR's, and because of hasNonFrozenItems, we know that flexFactors > 0\n // so no need to check for flexFactors < 1\n // 9.7.4.c\n /**\n * If the remaining free space is zero\n * - Do nothing.\n * Else // remember, we're always in grow mode\n * - Find the ratio of the item’s flex grow factor to the\n * sum of the flex grow factors of all unfrozen items on\n * the line. Set the item’s target main size to its flex\n * base size plus a fraction of the remaining free space\n * proportional to the ratio.\n */\n if (remainingFreeSpace > 0) {\n flexItems.forEach(item => {\n if (!item.frozen) {\n let ratio = item.flex / flexFactors;\n item.targetMainSize = item.baseSize + ratio * remainingFreeSpace;\n }\n });\n }\n\n // 9.7.4.d\n /**\n * Fix min/max violations. Clamp each non-frozen item’s\n * target main size by its used min and max main sizes\n * and floor its content-box size at zero. If the item’s\n * target main size was made smaller by this, it’s a max\n * violation. If the item’s target main size was made\n * larger by this, it’s a min violation.\n */\n let totalViolation = 0;\n flexItems.forEach(item => {\n item.violation = 0;\n if (!item.frozen) {\n let {min, max, targetMainSize} = item;\n item.targetMainSize = Math.max(min, Math.min(targetMainSize, max));\n\n item.violation = item.targetMainSize - targetMainSize;\n totalViolation += item.violation;\n }\n });\n\n // 9.7.4.e\n /**\n * Freeze over-flexed items. The total violation is the\n * sum of the adjustments from the previous step\n * ∑(clamped size - unclamped size). If the total violation is:\n * Zero\n * - Freeze all items.\n *\n * Positive\n * - Freeze all the items with min violations.\n *\n * Negative\n * - Freeze all the items with max violations.\n */\n hasNonFrozenItems = false;\n flexItems.forEach(item => {\n if (totalViolation === 0 || Math.sign(totalViolation) === Math.sign(item.violation)) {\n item.frozen = true;\n } else if (!item.frozen) {\n hasNonFrozenItems = true;\n }\n });\n }\n\n let columnSizes = cascadeRounding(flexItems);\n\n // Give the leftover sub-pixel width to the last column so the columns sum\n // exactly to the (possibly fractional) available width.\n if (hasFractionalWidth && columnSizes.length > 0) {\n let tableFractionalWidth = originalWidth.toString().split('.')[1];\n let columnWidth = columnSizes[columnSizes.length - 1].toString();\n columnSizes[columnSizes.length - 1] = Number(columnWidth + '.' + tableFractionalWidth);\n }\n\n return columnSizes;\n}\n\nfunction cascadeRounding(flexItems: FlexItem[]): number[] {\n /*\n Given an array of floats that sum to an integer, this rounds the floats\n and returns an array of integers with the same sum.\n */\n\n let fpTotal = 0;\n let intTotal = 0;\n let roundedArray: number[] = [];\n flexItems.forEach(function (item) {\n let float = item.targetMainSize;\n let integer = Math.round(float + fpTotal) - intTotal;\n fpTotal += float;\n intTotal += integer;\n roundedArray.push(integer);\n });\n\n return roundedArray;\n}\n"],"names":[],"version":3,"file":"TableUtils.cjs.map"}
|
|
@@ -36,6 +36,10 @@ function $2f907784468a29c9$export$f556054ce4358701(minWidth, tableWidth) {
|
|
|
36
36
|
return minWidth != null ? $2f907784468a29c9$export$7bbad27896f7ae9f(minWidth, tableWidth) : 0;
|
|
37
37
|
}
|
|
38
38
|
function $2f907784468a29c9$export$55d50dc687385491(availableWidth, columns, changedColumns, getDefaultWidth, getDefaultMinWidth) {
|
|
39
|
+
let originalWidth = availableWidth;
|
|
40
|
+
let flooredWidth = Math.floor(availableWidth);
|
|
41
|
+
let hasFractionalWidth = availableWidth - flooredWidth > 0;
|
|
42
|
+
availableWidth = flooredWidth;
|
|
39
43
|
let hasNonFrozenItems = false;
|
|
40
44
|
let flexItems = columns.map((column, index)=>{
|
|
41
45
|
var _changedColumns_get, _column_width, _ref, _ref1;
|
|
@@ -151,7 +155,15 @@ function $2f907784468a29c9$export$55d50dc687385491(availableWidth, columns, chan
|
|
|
151
155
|
else if (!item.frozen) hasNonFrozenItems = true;
|
|
152
156
|
});
|
|
153
157
|
}
|
|
154
|
-
|
|
158
|
+
let columnSizes = $2f907784468a29c9$var$cascadeRounding(flexItems);
|
|
159
|
+
// Give the leftover sub-pixel width to the last column so the columns sum
|
|
160
|
+
// exactly to the (possibly fractional) available width.
|
|
161
|
+
if (hasFractionalWidth && columnSizes.length > 0) {
|
|
162
|
+
let tableFractionalWidth = originalWidth.toString().split('.')[1];
|
|
163
|
+
let columnWidth = columnSizes[columnSizes.length - 1].toString();
|
|
164
|
+
columnSizes[columnSizes.length - 1] = Number(columnWidth + '.' + tableFractionalWidth);
|
|
165
|
+
}
|
|
166
|
+
return columnSizes;
|
|
155
167
|
}
|
|
156
168
|
function $2f907784468a29c9$var$cascadeRounding(flexItems) {
|
|
157
169
|
/*
|