@react-aria/selection 3.17.5 → 3.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ListKeyboardDelegate.main.js +200 -0
- package/dist/ListKeyboardDelegate.main.js.map +1 -0
- package/dist/ListKeyboardDelegate.mjs +195 -0
- package/dist/ListKeyboardDelegate.module.js +195 -0
- package/dist/ListKeyboardDelegate.module.js.map +1 -0
- package/dist/import.mjs +5 -794
- package/dist/main.js +10 -799
- package/dist/main.js.map +1 -1
- package/dist/module.js +5 -794
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/useSelectableCollection.main.js +304 -0
- package/dist/useSelectableCollection.main.js.map +1 -0
- package/dist/useSelectableCollection.mjs +299 -0
- package/dist/useSelectableCollection.module.js +299 -0
- package/dist/useSelectableCollection.module.js.map +1 -0
- package/dist/useSelectableItem.main.js +213 -0
- package/dist/useSelectableItem.main.js.map +1 -0
- package/dist/useSelectableItem.mjs +208 -0
- package/dist/useSelectableItem.module.js +208 -0
- package/dist/useSelectableItem.module.js.map +1 -0
- package/dist/useSelectableList.main.js +61 -0
- package/dist/useSelectableList.main.js.map +1 -0
- package/dist/useSelectableList.mjs +56 -0
- package/dist/useSelectableList.module.js +56 -0
- package/dist/useSelectableList.module.js.map +1 -0
- package/dist/useTypeSelect.main.js +73 -0
- package/dist/useTypeSelect.main.js.map +1 -0
- package/dist/useTypeSelect.mjs +68 -0
- package/dist/useTypeSelect.module.js +68 -0
- package/dist/useTypeSelect.module.js.map +1 -0
- package/dist/utils.main.js +32 -0
- package/dist/utils.main.js.map +1 -0
- package/dist/utils.mjs +26 -0
- package/dist/utils.module.js +26 -0
- package/dist/utils.module.js.map +1 -0
- package/package.json +8 -8
- package/src/ListKeyboardDelegate.ts +15 -7
- package/src/useSelectableCollection.ts +5 -3
- package/src/useSelectableItem.ts +4 -2
- package/src/useSelectableList.ts +7 -1
|
@@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AC,eACH,CAAA,GAAA,qBAAa,EAAE,UAAU,OAAO,EAAE;gBAEpC,iFAAiF;gBACjF,IAAI,aAAa,WACf,CAAA,GAAA,yBAAiB,EAAE,SAAS;oBAAC,mBAAmB,IAAI,OAAO;gBAAA;YAE/D;QACF;QAEA,+FAA+F;QAC/F,IAAI,QAAQ,SAAS,IAAI,QAAQ,UAAU,IAAI,QAAQ,eAAe,OAAO,IAAI,MAC/E,CAAA,GAAA,kBAAU,EAAE,IAAI,OAAO;QAGzB,eAAe,OAAO,GAAG,QAAQ,UAAU;QAC3C,aAAa,OAAO,GAAG;IACzB,GAAG;QAAC;QAAe;QAAW,QAAQ,UAAU;QAAE,QAAQ,SAAS;QAAE;KAAI;IAEzE,IAAI,WAAW;mBACb;iBACA;gBACA;QACA,aAAY,CAAC;YACX,8CAA8C;YAC9C,IAAI,UAAU,OAAO,KAAK,EAAE,MAAM,EAChC,wEAAwE;YACxE,EAAE,cAAc;QAEpB;IACF;IAEA,IAAI,mBAAC,eAAe,EAAC,GAAG,CAAA,GAAA,yCAAY,EAAE;QACpC,kBAAkB;QAClB,kBAAkB;IACpB;IAEA,IAAI,CAAC,mBACH,WAAW,CAAA,GAAA,iBAAS,EAAE,iBAAiB;IAGzC,oFAAoF;IACpF,+FAA+F;IAC/F,8FAA8F;IAC9F,gDAAgD;IAChD,IAAI;IACJ,IAAI,CAAC,uBACH,WAAW,QAAQ,UAAU,IAAI,OAAO,IAAI;IAG9C,OAAO;QACL,iBAAiB;YACf,GAAG,QAAQ;sBACX;QACF;IACF;AACF","sources":["packages/@react-aria/selection/src/useSelectableCollection.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 {DOMAttributes, FocusableElement, FocusStrategy, Key, KeyboardDelegate} from '@react-types/shared';\nimport {flushSync} from 'react-dom';\nimport {FocusEvent, KeyboardEvent, RefObject, useEffect, useRef} from 'react';\nimport {focusSafely, getFocusableTreeWalker} from '@react-aria/focus';\nimport {focusWithoutScrolling, mergeProps, scrollIntoView, scrollIntoViewport, useEvent, useRouter} from '@react-aria/utils';\nimport {getInteractionModality} from '@react-aria/interactions';\nimport {isCtrlKeyPressed, isNonContiguousSelectionModifier} from './utils';\nimport {MultipleSelectionManager} from '@react-stately/selection';\nimport {useLocale} from '@react-aria/i18n';\nimport {useTypeSelect} from './useTypeSelect';\n\nexport interface AriaSelectableCollectionOptions {\n /**\n * An interface for reading and updating multiple selection state.\n */\n selectionManager: MultipleSelectionManager,\n /**\n * A delegate object that implements behavior for keyboard focus movement.\n */\n keyboardDelegate: KeyboardDelegate,\n /**\n * The ref attached to the element representing the collection.\n */\n ref: RefObject<HTMLElement>,\n /**\n * Whether the collection or one of its items should be automatically focused upon render.\n * @default false\n */\n autoFocus?: boolean | FocusStrategy,\n /**\n * Whether focus should wrap around when the end/start is reached.\n * @default false\n */\n shouldFocusWrap?: boolean,\n /**\n * Whether the collection allows empty selection.\n * @default false\n */\n disallowEmptySelection?: boolean,\n /**\n * Whether the collection allows the user to select all items via keyboard shortcut.\n * @default false\n */\n disallowSelectAll?: boolean,\n /**\n * Whether selection should occur automatically on focus.\n * @default false\n */\n selectOnFocus?: boolean,\n /**\n * Whether typeahead is disabled.\n * @default false\n */\n disallowTypeAhead?: boolean,\n /**\n * Whether the collection items should use virtual focus instead of being focused directly.\n */\n shouldUseVirtualFocus?: boolean,\n /**\n * Whether navigation through tab key is enabled.\n */\n allowsTabNavigation?: boolean,\n /**\n * Whether the collection items are contained in a virtual scroller.\n */\n isVirtualized?: boolean,\n /**\n * The ref attached to the scrollable body. Used to provide automatic scrolling on item focus for non-virtualized collections.\n * If not provided, defaults to the collection ref.\n */\n scrollRef?: RefObject<HTMLElement>,\n /**\n * The behavior of links in the collection.\n * - 'action': link behaves like onAction.\n * - 'selection': link follows selection interactions (e.g. if URL drives selection).\n * - 'override': links override all other interactions (link items are not selectable).\n * @default 'action'\n */\n linkBehavior?: 'action' | 'selection' | 'override'\n}\n\nexport interface SelectableCollectionAria {\n /** Props for the collection element. */\n collectionProps: DOMAttributes\n}\n\n/**\n * Handles interactions with selectable collections.\n */\nexport function useSelectableCollection(options: AriaSelectableCollectionOptions): SelectableCollectionAria {\n let {\n selectionManager: manager,\n keyboardDelegate: delegate,\n ref,\n autoFocus = false,\n shouldFocusWrap = false,\n disallowEmptySelection = false,\n disallowSelectAll = false,\n selectOnFocus = manager.selectionBehavior === 'replace',\n disallowTypeAhead = false,\n shouldUseVirtualFocus,\n allowsTabNavigation = false,\n isVirtualized,\n // If no scrollRef is provided, assume the collection ref is the scrollable region\n scrollRef = ref,\n linkBehavior = 'action'\n } = options;\n let {direction} = useLocale();\n let router = useRouter();\n\n let onKeyDown = (e: KeyboardEvent) => {\n // Prevent option + tab from doing anything since it doesn't move focus to the cells, only buttons/checkboxes\n if (e.altKey && e.key === 'Tab') {\n e.preventDefault();\n }\n\n // Keyboard events bubble through portals. Don't handle keyboard events\n // for elements outside the collection (e.g. menus).\n if (!ref.current.contains(e.target as Element)) {\n return;\n }\n\n const navigateToKey = (key: Key | undefined, childFocus?: FocusStrategy) => {\n if (key != null) {\n if (manager.isLink(key) && linkBehavior === 'selection' && selectOnFocus && !isNonContiguousSelectionModifier(e)) {\n // Set focused key and re-render synchronously to bring item into view if needed.\n flushSync(() => {\n manager.setFocusedKey(key, childFocus);\n });\n\n let item = scrollRef.current.querySelector(`[data-key=\"${CSS.escape(key.toString())}\"]`);\n let itemProps = manager.getItemProps(key);\n router.open(item, e, itemProps.href, itemProps.routerOptions);\n\n return;\n }\n\n manager.setFocusedKey(key, childFocus);\n\n if (manager.isLink(key) && linkBehavior === 'override') {\n return;\n }\n\n if (e.shiftKey && manager.selectionMode === 'multiple') {\n manager.extendSelection(key);\n } else if (selectOnFocus && !isNonContiguousSelectionModifier(e)) {\n manager.replaceSelection(key);\n }\n }\n };\n\n switch (e.key) {\n case 'ArrowDown': {\n if (delegate.getKeyBelow) {\n e.preventDefault();\n let nextKey = manager.focusedKey != null\n ? delegate.getKeyBelow(manager.focusedKey)\n : delegate.getFirstKey?.();\n if (nextKey == null && shouldFocusWrap) {\n nextKey = delegate.getFirstKey?.(manager.focusedKey);\n }\n navigateToKey(nextKey);\n }\n break;\n }\n case 'ArrowUp': {\n if (delegate.getKeyAbove) {\n e.preventDefault();\n let nextKey = manager.focusedKey != null\n ? delegate.getKeyAbove(manager.focusedKey)\n : delegate.getLastKey?.();\n if (nextKey == null && shouldFocusWrap) {\n nextKey = delegate.getLastKey?.(manager.focusedKey);\n }\n navigateToKey(nextKey);\n }\n break;\n }\n case 'ArrowLeft': {\n if (delegate.getKeyLeftOf) {\n e.preventDefault();\n let nextKey = delegate.getKeyLeftOf(manager.focusedKey);\n if (nextKey == null && shouldFocusWrap) {\n nextKey = direction === 'rtl' ? delegate.getFirstKey?.(manager.focusedKey) : delegate.getLastKey?.(manager.focusedKey);\n }\n navigateToKey(nextKey, direction === 'rtl' ? 'first' : 'last');\n }\n break;\n }\n case 'ArrowRight': {\n if (delegate.getKeyRightOf) {\n e.preventDefault();\n let nextKey = delegate.getKeyRightOf(manager.focusedKey);\n if (nextKey == null && shouldFocusWrap) {\n nextKey = direction === 'rtl' ? delegate.getLastKey?.(manager.focusedKey) : delegate.getFirstKey?.(manager.focusedKey);\n }\n navigateToKey(nextKey, direction === 'rtl' ? 'last' : 'first');\n }\n break;\n }\n case 'Home':\n if (delegate.getFirstKey) {\n e.preventDefault();\n let firstKey = delegate.getFirstKey(manager.focusedKey, isCtrlKeyPressed(e));\n manager.setFocusedKey(firstKey);\n if (isCtrlKeyPressed(e) && e.shiftKey && manager.selectionMode === 'multiple') {\n manager.extendSelection(firstKey);\n } else if (selectOnFocus) {\n manager.replaceSelection(firstKey);\n }\n }\n break;\n case 'End':\n if (delegate.getLastKey) {\n e.preventDefault();\n let lastKey = delegate.getLastKey(manager.focusedKey, isCtrlKeyPressed(e));\n manager.setFocusedKey(lastKey);\n if (isCtrlKeyPressed(e) && e.shiftKey && manager.selectionMode === 'multiple') {\n manager.extendSelection(lastKey);\n } else if (selectOnFocus) {\n manager.replaceSelection(lastKey);\n }\n }\n break;\n case 'PageDown':\n if (delegate.getKeyPageBelow) {\n e.preventDefault();\n let nextKey = delegate.getKeyPageBelow(manager.focusedKey);\n navigateToKey(nextKey);\n }\n break;\n case 'PageUp':\n if (delegate.getKeyPageAbove) {\n e.preventDefault();\n let nextKey = delegate.getKeyPageAbove(manager.focusedKey);\n navigateToKey(nextKey);\n }\n break;\n case 'a':\n if (isCtrlKeyPressed(e) && manager.selectionMode === 'multiple' && disallowSelectAll !== true) {\n e.preventDefault();\n manager.selectAll();\n }\n break;\n case 'Escape':\n if (!disallowEmptySelection && manager.selectedKeys.size !== 0) {\n e.stopPropagation();\n e.preventDefault();\n manager.clearSelection();\n }\n break;\n case 'Tab': {\n if (!allowsTabNavigation) {\n // There may be elements that are \"tabbable\" inside a collection (e.g. in a grid cell).\n // However, collections should be treated as a single tab stop, with arrow key navigation internally.\n // We don't control the rendering of these, so we can't override the tabIndex to prevent tabbing.\n // Instead, we handle the Tab key, and move focus manually to the first/last tabbable element\n // in the collection, so that the browser default behavior will apply starting from that element\n // rather than the currently focused one.\n if (e.shiftKey) {\n ref.current.focus();\n } else {\n let walker = getFocusableTreeWalker(ref.current, {tabbable: true});\n let next: FocusableElement;\n let last: FocusableElement;\n do {\n last = walker.lastChild() as FocusableElement;\n if (last) {\n next = last;\n }\n } while (last);\n\n if (next && !next.contains(document.activeElement)) {\n focusWithoutScrolling(next);\n }\n }\n break;\n }\n }\n }\n };\n\n // Store the scroll position so we can restore it later.\n let scrollPos = useRef({top: 0, left: 0});\n useEvent(scrollRef, 'scroll', isVirtualized ? null : () => {\n scrollPos.current = {\n top: scrollRef.current.scrollTop,\n left: scrollRef.current.scrollLeft\n };\n });\n\n let onFocus = (e: FocusEvent) => {\n if (manager.isFocused) {\n // If a focus event bubbled through a portal, reset focus state.\n if (!e.currentTarget.contains(e.target)) {\n manager.setFocused(false);\n }\n\n return;\n }\n\n // Focus events can bubble through portals. Ignore these events.\n if (!e.currentTarget.contains(e.target)) {\n return;\n }\n\n manager.setFocused(true);\n\n if (manager.focusedKey == null) {\n let navigateToFirstKey = (key: Key | undefined) => {\n if (key != null) {\n manager.setFocusedKey(key);\n if (selectOnFocus) {\n manager.replaceSelection(key);\n }\n }\n };\n // If the user hasn't yet interacted with the collection, there will be no focusedKey set.\n // Attempt to detect whether the user is tabbing forward or backward into the collection\n // and either focus the first or last item accordingly.\n let relatedTarget = e.relatedTarget as Element;\n if (relatedTarget && (e.currentTarget.compareDocumentPosition(relatedTarget) & Node.DOCUMENT_POSITION_FOLLOWING)) {\n navigateToFirstKey(manager.lastSelectedKey ?? delegate.getLastKey());\n } else {\n navigateToFirstKey(manager.firstSelectedKey ?? delegate.getFirstKey());\n }\n } else if (!isVirtualized) {\n // Restore the scroll position to what it was before.\n scrollRef.current.scrollTop = scrollPos.current.top;\n scrollRef.current.scrollLeft = scrollPos.current.left;\n }\n\n if (!isVirtualized && manager.focusedKey != null) {\n // Refocus and scroll the focused item into view if it exists within the scrollable region.\n let element = scrollRef.current.querySelector(`[data-key=\"${CSS.escape(manager.focusedKey.toString())}\"]`) as HTMLElement;\n if (element) {\n // This prevents a flash of focus on the first/last element in the collection, or the collection itself.\n if (!element.contains(document.activeElement)) {\n focusWithoutScrolling(element);\n }\n\n let modality = getInteractionModality();\n if (modality === 'keyboard') {\n scrollIntoViewport(element, {containingElement: ref.current});\n }\n }\n }\n };\n\n let onBlur = (e) => {\n // Don't set blurred and then focused again if moving focus within the collection.\n if (!e.currentTarget.contains(e.relatedTarget as HTMLElement)) {\n manager.setFocused(false);\n }\n };\n\n const autoFocusRef = useRef(autoFocus);\n useEffect(() => {\n if (autoFocusRef.current) {\n let focusedKey = null;\n\n // Check focus strategy to determine which item to focus\n if (autoFocus === 'first') {\n focusedKey = delegate.getFirstKey();\n } if (autoFocus === 'last') {\n focusedKey = delegate.getLastKey();\n }\n\n // If there are any selected keys, make the first one the new focus target\n let selectedKeys = manager.selectedKeys;\n if (selectedKeys.size) {\n for (let key of selectedKeys) {\n if (manager.canSelectItem(key)) {\n focusedKey = key;\n break;\n }\n }\n }\n\n manager.setFocused(true);\n manager.setFocusedKey(focusedKey);\n\n // If no default focus key is selected, focus the collection itself.\n if (focusedKey == null && !shouldUseVirtualFocus) {\n focusSafely(ref.current);\n }\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n // If not virtualized, scroll the focused element into view when the focusedKey changes.\n // When virtualized, Virtualizer handles this internally.\n let lastFocusedKey = useRef(manager.focusedKey);\n useEffect(() => {\n let modality = getInteractionModality();\n if (manager.isFocused && manager.focusedKey != null && scrollRef?.current) {\n let element = scrollRef.current.querySelector(`[data-key=\"${CSS.escape(manager.focusedKey.toString())}\"]`) as HTMLElement;\n if (element && (modality === 'keyboard' || autoFocusRef.current)) {\n if (!isVirtualized) {\n scrollIntoView(scrollRef.current, element);\n }\n // Avoid scroll in iOS VO, since it may cause overlay to close (i.e. RAC submenu)\n if (modality !== 'virtual') {\n scrollIntoViewport(element, {containingElement: ref.current});\n }\n }\n }\n\n // If the focused key becomes null (e.g. the last item is deleted), focus the whole collection.\n if (manager.isFocused && manager.focusedKey == null && lastFocusedKey.current != null) {\n focusSafely(ref.current);\n }\n\n lastFocusedKey.current = manager.focusedKey;\n autoFocusRef.current = false;\n }, [isVirtualized, scrollRef, manager.focusedKey, manager.isFocused, ref]);\n\n let handlers = {\n onKeyDown,\n onFocus,\n onBlur,\n onMouseDown(e) {\n // Ignore events that bubbled through portals.\n if (scrollRef.current === e.target) {\n // Prevent focus going to the collection when clicking on the scrollbar.\n e.preventDefault();\n }\n }\n };\n\n let {typeSelectProps} = useTypeSelect({\n keyboardDelegate: delegate,\n selectionManager: manager\n });\n\n if (!disallowTypeAhead) {\n handlers = mergeProps(typeSelectProps, handlers);\n }\n\n // If nothing is focused within the collection, make the collection itself tabbable.\n // This will be marshalled to either the first or last item depending on where focus came from.\n // If using virtual focus, don't set a tabIndex at all so that VoiceOver on iOS 14 doesn't try\n // to move real DOM focus to the element anyway.\n let tabIndex: number;\n if (!shouldUseVirtualFocus) {\n tabIndex = manager.focusedKey == null ? 0 : -1;\n }\n\n return {\n collectionProps: {\n ...handlers,\n tabIndex\n }\n };\n}\n"],"names":[],"version":3,"file":"useSelectableCollection.module.js.map"}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
var $ee0bdf4faa47f2a8$exports = require("./utils.main.js");
|
|
2
|
+
var $i4XHw$reactariafocus = require("@react-aria/focus");
|
|
3
|
+
var $i4XHw$reactariautils = require("@react-aria/utils");
|
|
4
|
+
var $i4XHw$reactariainteractions = require("@react-aria/interactions");
|
|
5
|
+
var $i4XHw$react = require("react");
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
function $parcel$export(e, n, v, s) {
|
|
9
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
$parcel$export(module.exports, "useSelectableItem", () => $433b1145b0781e10$export$ecf600387e221c37);
|
|
13
|
+
/*
|
|
14
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
15
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
16
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
17
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
18
|
+
*
|
|
19
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
20
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
21
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
22
|
+
* governing permissions and limitations under the License.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
function $433b1145b0781e10$export$ecf600387e221c37(options) {
|
|
29
|
+
let { selectionManager: manager, key: key, ref: ref, shouldSelectOnPressUp: shouldSelectOnPressUp, shouldUseVirtualFocus: shouldUseVirtualFocus, focus: focus, isDisabled: isDisabled, onAction: onAction, allowsDifferentPressOrigin: allowsDifferentPressOrigin, linkBehavior: linkBehavior = 'action' } = options;
|
|
30
|
+
let router = (0, $i4XHw$reactariautils.useRouter)();
|
|
31
|
+
let onSelect = (e)=>{
|
|
32
|
+
if (e.pointerType === 'keyboard' && (0, $ee0bdf4faa47f2a8$exports.isNonContiguousSelectionModifier)(e)) manager.toggleSelection(key);
|
|
33
|
+
else {
|
|
34
|
+
if (manager.selectionMode === 'none') return;
|
|
35
|
+
if (manager.isLink(key)) {
|
|
36
|
+
if (linkBehavior === 'selection') {
|
|
37
|
+
let itemProps = manager.getItemProps(key);
|
|
38
|
+
router.open(ref.current, e, itemProps.href, itemProps.routerOptions);
|
|
39
|
+
// Always set selected keys back to what they were so that select and combobox close.
|
|
40
|
+
manager.setSelectedKeys(manager.selectedKeys);
|
|
41
|
+
return;
|
|
42
|
+
} else if (linkBehavior === 'override' || linkBehavior === 'none') return;
|
|
43
|
+
}
|
|
44
|
+
if (manager.selectionMode === 'single') {
|
|
45
|
+
if (manager.isSelected(key) && !manager.disallowEmptySelection) manager.toggleSelection(key);
|
|
46
|
+
else manager.replaceSelection(key);
|
|
47
|
+
} else if (e && e.shiftKey) manager.extendSelection(key);
|
|
48
|
+
else if (manager.selectionBehavior === 'toggle' || e && ((0, $ee0bdf4faa47f2a8$exports.isCtrlKeyPressed)(e) || e.pointerType === 'touch' || e.pointerType === 'virtual')) // if touch or virtual (VO) then we just want to toggle, otherwise it's impossible to multi select because they don't have modifier keys
|
|
49
|
+
manager.toggleSelection(key);
|
|
50
|
+
else manager.replaceSelection(key);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
// Focus the associated DOM node when this item becomes the focusedKey
|
|
54
|
+
(0, $i4XHw$react.useEffect)(()=>{
|
|
55
|
+
let isFocused = key === manager.focusedKey;
|
|
56
|
+
if (isFocused && manager.isFocused && !shouldUseVirtualFocus) {
|
|
57
|
+
if (focus) focus();
|
|
58
|
+
else if (document.activeElement !== ref.current) (0, $i4XHw$reactariafocus.focusSafely)(ref.current);
|
|
59
|
+
}
|
|
60
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
61
|
+
}, [
|
|
62
|
+
ref,
|
|
63
|
+
key,
|
|
64
|
+
manager.focusedKey,
|
|
65
|
+
manager.childFocusStrategy,
|
|
66
|
+
manager.isFocused,
|
|
67
|
+
shouldUseVirtualFocus
|
|
68
|
+
]);
|
|
69
|
+
isDisabled = isDisabled || manager.isDisabled(key);
|
|
70
|
+
// Set tabIndex to 0 if the element is focused, or -1 otherwise so that only the last focused
|
|
71
|
+
// item is tabbable. If using virtual focus, don't set a tabIndex at all so that VoiceOver
|
|
72
|
+
// on iOS 14 doesn't try to move real DOM focus to the item anyway.
|
|
73
|
+
let itemProps = {};
|
|
74
|
+
if (!shouldUseVirtualFocus && !isDisabled) itemProps = {
|
|
75
|
+
tabIndex: key === manager.focusedKey ? 0 : -1,
|
|
76
|
+
onFocus (e) {
|
|
77
|
+
if (e.target === ref.current) manager.setFocusedKey(key);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
else if (isDisabled) itemProps.onMouseDown = (e)=>{
|
|
81
|
+
// Prevent focus going to the body when clicking on a disabled item.
|
|
82
|
+
e.preventDefault();
|
|
83
|
+
};
|
|
84
|
+
// With checkbox selection, onAction (i.e. navigation) becomes primary, and occurs on a single click of the row.
|
|
85
|
+
// Clicking the checkbox enters selection mode, after which clicking anywhere on any row toggles selection for that row.
|
|
86
|
+
// With highlight selection, onAction is secondary, and occurs on double click. Single click selects the row.
|
|
87
|
+
// With touch, onAction occurs on single tap, and long press enters selection mode.
|
|
88
|
+
let isLinkOverride = manager.isLink(key) && linkBehavior === 'override';
|
|
89
|
+
let hasLinkAction = manager.isLink(key) && linkBehavior !== 'selection' && linkBehavior !== 'none';
|
|
90
|
+
let allowsSelection = !isDisabled && manager.canSelectItem(key) && !isLinkOverride;
|
|
91
|
+
let allowsActions = (onAction || hasLinkAction) && !isDisabled;
|
|
92
|
+
let hasPrimaryAction = allowsActions && (manager.selectionBehavior === 'replace' ? !allowsSelection : !allowsSelection || manager.isEmpty);
|
|
93
|
+
let hasSecondaryAction = allowsActions && allowsSelection && manager.selectionBehavior === 'replace';
|
|
94
|
+
let hasAction = hasPrimaryAction || hasSecondaryAction;
|
|
95
|
+
let modality = (0, $i4XHw$react.useRef)(null);
|
|
96
|
+
let longPressEnabled = hasAction && allowsSelection;
|
|
97
|
+
let longPressEnabledOnPressStart = (0, $i4XHw$react.useRef)(false);
|
|
98
|
+
let hadPrimaryActionOnPressStart = (0, $i4XHw$react.useRef)(false);
|
|
99
|
+
let performAction = (e)=>{
|
|
100
|
+
if (onAction) onAction();
|
|
101
|
+
if (hasLinkAction) {
|
|
102
|
+
let itemProps = manager.getItemProps(key);
|
|
103
|
+
router.open(ref.current, e, itemProps.href, itemProps.routerOptions);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
// By default, selection occurs on pointer down. This can be strange if selecting an
|
|
107
|
+
// item causes the UI to disappear immediately (e.g. menus).
|
|
108
|
+
// If shouldSelectOnPressUp is true, we use onPressUp instead of onPressStart.
|
|
109
|
+
// onPress requires a pointer down event on the same element as pointer up. For menus,
|
|
110
|
+
// we want to be able to have the pointer down on the trigger that opens the menu and
|
|
111
|
+
// the pointer up on the menu item rather than requiring a separate press.
|
|
112
|
+
// For keyboard events, selection still occurs on key down.
|
|
113
|
+
let itemPressProps = {};
|
|
114
|
+
if (shouldSelectOnPressUp) {
|
|
115
|
+
itemPressProps.onPressStart = (e)=>{
|
|
116
|
+
modality.current = e.pointerType;
|
|
117
|
+
longPressEnabledOnPressStart.current = longPressEnabled;
|
|
118
|
+
if (e.pointerType === 'keyboard' && (!hasAction || $433b1145b0781e10$var$isSelectionKey())) onSelect(e);
|
|
119
|
+
};
|
|
120
|
+
// If allowsDifferentPressOrigin, make selection happen on pressUp (e.g. open menu on press down, selection on menu item happens on press up.)
|
|
121
|
+
// Otherwise, have selection happen onPress (prevents listview row selection when clicking on interactable elements in the row)
|
|
122
|
+
if (!allowsDifferentPressOrigin) itemPressProps.onPress = (e)=>{
|
|
123
|
+
if (hasPrimaryAction || hasSecondaryAction && e.pointerType !== 'mouse') {
|
|
124
|
+
if (e.pointerType === 'keyboard' && !$433b1145b0781e10$var$isActionKey()) return;
|
|
125
|
+
performAction(e);
|
|
126
|
+
} else if (e.pointerType !== 'keyboard' && allowsSelection) onSelect(e);
|
|
127
|
+
};
|
|
128
|
+
else {
|
|
129
|
+
itemPressProps.onPressUp = hasPrimaryAction ? null : (e)=>{
|
|
130
|
+
if (e.pointerType !== 'keyboard' && allowsSelection) onSelect(e);
|
|
131
|
+
};
|
|
132
|
+
itemPressProps.onPress = hasPrimaryAction ? performAction : null;
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
itemPressProps.onPressStart = (e)=>{
|
|
136
|
+
modality.current = e.pointerType;
|
|
137
|
+
longPressEnabledOnPressStart.current = longPressEnabled;
|
|
138
|
+
hadPrimaryActionOnPressStart.current = hasPrimaryAction;
|
|
139
|
+
// Select on mouse down unless there is a primary action which will occur on mouse up.
|
|
140
|
+
// For keyboard, select on key down. If there is an action, the Space key selects on key down,
|
|
141
|
+
// and the Enter key performs onAction on key up.
|
|
142
|
+
if (allowsSelection && (e.pointerType === 'mouse' && !hasPrimaryAction || e.pointerType === 'keyboard' && (!allowsActions || $433b1145b0781e10$var$isSelectionKey()))) onSelect(e);
|
|
143
|
+
};
|
|
144
|
+
itemPressProps.onPress = (e)=>{
|
|
145
|
+
// Selection occurs on touch up. Primary actions always occur on pointer up.
|
|
146
|
+
// Both primary and secondary actions occur on Enter key up. The only exception
|
|
147
|
+
// is secondary actions, which occur on double click with a mouse.
|
|
148
|
+
if (e.pointerType === 'touch' || e.pointerType === 'pen' || e.pointerType === 'virtual' || e.pointerType === 'keyboard' && hasAction && $433b1145b0781e10$var$isActionKey() || e.pointerType === 'mouse' && hadPrimaryActionOnPressStart.current) {
|
|
149
|
+
if (hasAction) performAction(e);
|
|
150
|
+
else if (allowsSelection) onSelect(e);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
itemProps['data-key'] = key;
|
|
155
|
+
itemPressProps.preventFocusOnPress = shouldUseVirtualFocus;
|
|
156
|
+
let { pressProps: pressProps, isPressed: isPressed } = (0, $i4XHw$reactariainteractions.usePress)(itemPressProps);
|
|
157
|
+
// Double clicking with a mouse with selectionBehavior = 'replace' performs an action.
|
|
158
|
+
let onDoubleClick = hasSecondaryAction ? (e)=>{
|
|
159
|
+
if (modality.current === 'mouse') {
|
|
160
|
+
e.stopPropagation();
|
|
161
|
+
e.preventDefault();
|
|
162
|
+
performAction(e);
|
|
163
|
+
}
|
|
164
|
+
} : undefined;
|
|
165
|
+
// Long pressing an item with touch when selectionBehavior = 'replace' switches the selection behavior
|
|
166
|
+
// to 'toggle'. This changes the single tap behavior from performing an action (i.e. navigating) to
|
|
167
|
+
// selecting, and may toggle the appearance of a UI affordance like checkboxes on each item.
|
|
168
|
+
let { longPressProps: longPressProps } = (0, $i4XHw$reactariainteractions.useLongPress)({
|
|
169
|
+
isDisabled: !longPressEnabled,
|
|
170
|
+
onLongPress (e) {
|
|
171
|
+
if (e.pointerType === 'touch') {
|
|
172
|
+
onSelect(e);
|
|
173
|
+
manager.setSelectionBehavior('toggle');
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
// Prevent native drag and drop on long press if we also select on long press.
|
|
178
|
+
// Once the user is in selection mode, they can long press again to drag.
|
|
179
|
+
// Use a capturing listener to ensure this runs before useDrag, regardless of
|
|
180
|
+
// the order the props get merged.
|
|
181
|
+
let onDragStartCapture = (e)=>{
|
|
182
|
+
if (modality.current === 'touch' && longPressEnabledOnPressStart.current) e.preventDefault();
|
|
183
|
+
};
|
|
184
|
+
// Prevent default on link clicks so that we control exactly
|
|
185
|
+
// when they open (to match selection behavior).
|
|
186
|
+
let onClick = manager.isLink(key) ? (e)=>{
|
|
187
|
+
if (!(0, $i4XHw$reactariautils.openLink).isOpening) e.preventDefault();
|
|
188
|
+
} : undefined;
|
|
189
|
+
return {
|
|
190
|
+
itemProps: (0, $i4XHw$reactariautils.mergeProps)(itemProps, allowsSelection || hasPrimaryAction ? pressProps : {}, longPressEnabled ? longPressProps : {}, {
|
|
191
|
+
onDoubleClick: onDoubleClick,
|
|
192
|
+
onDragStartCapture: onDragStartCapture,
|
|
193
|
+
onClick: onClick
|
|
194
|
+
}),
|
|
195
|
+
isPressed: isPressed,
|
|
196
|
+
isSelected: manager.isSelected(key),
|
|
197
|
+
isFocused: manager.isFocused && manager.focusedKey === key,
|
|
198
|
+
isDisabled: isDisabled,
|
|
199
|
+
allowsSelection: allowsSelection,
|
|
200
|
+
hasAction: hasAction
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function $433b1145b0781e10$var$isActionKey() {
|
|
204
|
+
let event = window.event;
|
|
205
|
+
return (event === null || event === void 0 ? void 0 : event.key) === 'Enter';
|
|
206
|
+
}
|
|
207
|
+
function $433b1145b0781e10$var$isSelectionKey() {
|
|
208
|
+
let event = window.event;
|
|
209
|
+
return (event === null || event === void 0 ? void 0 : event.key) === ' ' || (event === null || event === void 0 ? void 0 : event.code) === 'Space';
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
//# sourceMappingURL=useSelectableItem.main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;;AAk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CAAA,GAAA,gCAAS,EAClB,WACA,mBAAmB,mBAAmB,aAAa,CAAC,GACpD,mBAAmB,iBAAiB,CAAC,GACrC;2BAAC;gCAAe;qBAAoB;QAAO;mBAE7C;QACA,YAAY,QAAQ,UAAU,CAAC;QAC/B,WAAW,QAAQ,SAAS,IAAI,QAAQ,UAAU,KAAK;oBACvD;yBACA;mBACA;IACF;AACF;AAEA,SAAS;IACP,IAAI,QAAQ,OAAO,KAAK;IACxB,OAAO,CAAA,kBAAA,4BAAA,MAAO,GAAG,MAAK;AACxB;AAEA,SAAS;IACP,IAAI,QAAQ,OAAO,KAAK;IACxB,OAAO,CAAA,kBAAA,4BAAA,MAAO,GAAG,MAAK,OAAO,CAAA,kBAAA,4BAAA,MAAO,IAAI,MAAK;AAC/C","sources":["packages/@react-aria/selection/src/useSelectableItem.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 {DOMAttributes, FocusableElement, Key, LongPressEvent, PressEvent} from '@react-types/shared';\nimport {focusSafely} from '@react-aria/focus';\nimport {isCtrlKeyPressed, isNonContiguousSelectionModifier} from './utils';\nimport {mergeProps, openLink, useRouter} from '@react-aria/utils';\nimport {MultipleSelectionManager} from '@react-stately/selection';\nimport {PressProps, useLongPress, usePress} from '@react-aria/interactions';\nimport {RefObject, useEffect, useRef} from 'react';\n\nexport interface SelectableItemOptions {\n /**\n * An interface for reading and updating multiple selection state.\n */\n selectionManager: MultipleSelectionManager,\n /**\n * A unique key for the item.\n */\n key: Key,\n /**\n * Ref to the item.\n */\n ref: RefObject<FocusableElement>,\n /**\n * By default, selection occurs on pointer down. This can be strange if selecting an\n * item causes the UI to disappear immediately (e.g. menus).\n */\n shouldSelectOnPressUp?: boolean,\n /**\n * Whether selection requires the pointer/mouse down and up events to occur on the same target or triggers selection on\n * the target of the pointer/mouse up event.\n */\n allowsDifferentPressOrigin?: boolean,\n /**\n * Whether the option is contained in a virtual scroller.\n */\n isVirtualized?: boolean,\n /**\n * Function to focus the item.\n */\n focus?: () => void,\n /**\n * Whether the option should use virtual focus instead of being focused directly.\n */\n shouldUseVirtualFocus?: boolean,\n /** Whether the item is disabled. */\n isDisabled?: boolean,\n /**\n * Handler that is called when a user performs an action on the item. The exact user event depends on\n * the collection's `selectionBehavior` prop and the interaction modality.\n */\n onAction?: () => void,\n /**\n * The behavior of links in the collection.\n * - 'action': link behaves like onAction.\n * - 'selection': link follows selection interactions (e.g. if URL drives selection).\n * - 'override': links override all other interactions (link items are not selectable).\n * - 'none': links are disabled for both selection and actions (e.g. handled elsewhere).\n * @default 'action'\n */\n linkBehavior?: 'action' | 'selection' | 'override' | 'none'\n}\n\nexport interface SelectableItemStates {\n /** Whether the item is currently in a pressed state. */\n isPressed: boolean,\n /** Whether the item is currently selected. */\n isSelected: boolean,\n /** Whether the item is currently focused. */\n isFocused: boolean,\n /**\n * Whether the item is non-interactive, i.e. both selection and actions are disabled and the item may\n * not be focused. Dependent on `disabledKeys` and `disabledBehavior`.\n */\n isDisabled: boolean,\n /**\n * Whether the item may be selected, dependent on `selectionMode`, `disabledKeys`, and `disabledBehavior`.\n */\n allowsSelection: boolean,\n /**\n * Whether the item has an action, dependent on `onAction`, `disabledKeys`,\n * and `disabledBehavior`. It may also change depending on the current selection state\n * of the list (e.g. when selection is primary). This can be used to enable or disable hover\n * styles or other visual indications of interactivity.\n */\n hasAction: boolean\n}\n\nexport interface SelectableItemAria extends SelectableItemStates {\n /**\n * Props to be spread on the item root node.\n */\n itemProps: DOMAttributes\n}\n\n/**\n * Handles interactions with an item in a selectable collection.\n */\nexport function useSelectableItem(options: SelectableItemOptions): SelectableItemAria {\n let {\n selectionManager: manager,\n key,\n ref,\n shouldSelectOnPressUp,\n shouldUseVirtualFocus,\n focus,\n isDisabled,\n onAction,\n allowsDifferentPressOrigin,\n linkBehavior = 'action'\n } = options;\n let router = useRouter();\n\n let onSelect = (e: PressEvent | LongPressEvent | PointerEvent) => {\n if (e.pointerType === 'keyboard' && isNonContiguousSelectionModifier(e)) {\n manager.toggleSelection(key);\n } else {\n if (manager.selectionMode === 'none') {\n return;\n }\n\n if (manager.isLink(key)) {\n if (linkBehavior === 'selection') {\n let itemProps = manager.getItemProps(key);\n router.open(ref.current, e, itemProps.href, itemProps.routerOptions);\n // Always set selected keys back to what they were so that select and combobox close.\n manager.setSelectedKeys(manager.selectedKeys);\n return;\n } else if (linkBehavior === 'override' || linkBehavior === 'none') {\n return;\n }\n }\n\n if (manager.selectionMode === 'single') {\n if (manager.isSelected(key) && !manager.disallowEmptySelection) {\n manager.toggleSelection(key);\n } else {\n manager.replaceSelection(key);\n }\n } else if (e && e.shiftKey) {\n manager.extendSelection(key);\n } else if (manager.selectionBehavior === 'toggle' || (e && (isCtrlKeyPressed(e) || e.pointerType === 'touch' || e.pointerType === 'virtual'))) {\n // if touch or virtual (VO) then we just want to toggle, otherwise it's impossible to multi select because they don't have modifier keys\n manager.toggleSelection(key);\n } else {\n manager.replaceSelection(key);\n }\n }\n };\n\n // Focus the associated DOM node when this item becomes the focusedKey\n useEffect(() => {\n let isFocused = key === manager.focusedKey;\n if (isFocused && manager.isFocused && !shouldUseVirtualFocus) {\n if (focus) {\n focus();\n } else if (document.activeElement !== ref.current) {\n focusSafely(ref.current);\n }\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [ref, key, manager.focusedKey, manager.childFocusStrategy, manager.isFocused, shouldUseVirtualFocus]);\n\n isDisabled = isDisabled || manager.isDisabled(key);\n // Set tabIndex to 0 if the element is focused, or -1 otherwise so that only the last focused\n // item is tabbable. If using virtual focus, don't set a tabIndex at all so that VoiceOver\n // on iOS 14 doesn't try to move real DOM focus to the item anyway.\n let itemProps: SelectableItemAria['itemProps'] = {};\n if (!shouldUseVirtualFocus && !isDisabled) {\n itemProps = {\n tabIndex: key === manager.focusedKey ? 0 : -1,\n onFocus(e) {\n if (e.target === ref.current) {\n manager.setFocusedKey(key);\n }\n }\n };\n } else if (isDisabled) {\n itemProps.onMouseDown = (e) => {\n // Prevent focus going to the body when clicking on a disabled item.\n e.preventDefault();\n };\n }\n\n // With checkbox selection, onAction (i.e. navigation) becomes primary, and occurs on a single click of the row.\n // Clicking the checkbox enters selection mode, after which clicking anywhere on any row toggles selection for that row.\n // With highlight selection, onAction is secondary, and occurs on double click. Single click selects the row.\n // With touch, onAction occurs on single tap, and long press enters selection mode.\n let isLinkOverride = manager.isLink(key) && linkBehavior === 'override';\n let hasLinkAction = manager.isLink(key) && linkBehavior !== 'selection' && linkBehavior !== 'none';\n let allowsSelection = !isDisabled && manager.canSelectItem(key) && !isLinkOverride;\n let allowsActions = (onAction || hasLinkAction) && !isDisabled;\n let hasPrimaryAction = allowsActions && (\n manager.selectionBehavior === 'replace'\n ? !allowsSelection\n : !allowsSelection || manager.isEmpty\n );\n let hasSecondaryAction = allowsActions && allowsSelection && manager.selectionBehavior === 'replace';\n let hasAction = hasPrimaryAction || hasSecondaryAction;\n let modality = useRef(null);\n\n let longPressEnabled = hasAction && allowsSelection;\n let longPressEnabledOnPressStart = useRef(false);\n let hadPrimaryActionOnPressStart = useRef(false);\n\n let performAction = (e) => {\n if (onAction) {\n onAction();\n }\n\n if (hasLinkAction) {\n let itemProps = manager.getItemProps(key);\n router.open(ref.current, e, itemProps.href, itemProps.routerOptions);\n }\n };\n\n // By default, selection occurs on pointer down. This can be strange if selecting an\n // item causes the UI to disappear immediately (e.g. menus).\n // If shouldSelectOnPressUp is true, we use onPressUp instead of onPressStart.\n // onPress requires a pointer down event on the same element as pointer up. For menus,\n // we want to be able to have the pointer down on the trigger that opens the menu and\n // the pointer up on the menu item rather than requiring a separate press.\n // For keyboard events, selection still occurs on key down.\n let itemPressProps: PressProps = {};\n if (shouldSelectOnPressUp) {\n itemPressProps.onPressStart = (e) => {\n modality.current = e.pointerType;\n longPressEnabledOnPressStart.current = longPressEnabled;\n if (e.pointerType === 'keyboard' && (!hasAction || isSelectionKey())) {\n onSelect(e);\n }\n };\n\n // If allowsDifferentPressOrigin, make selection happen on pressUp (e.g. open menu on press down, selection on menu item happens on press up.)\n // Otherwise, have selection happen onPress (prevents listview row selection when clicking on interactable elements in the row)\n if (!allowsDifferentPressOrigin) {\n itemPressProps.onPress = (e) => {\n if (hasPrimaryAction || (hasSecondaryAction && e.pointerType !== 'mouse')) {\n if (e.pointerType === 'keyboard' && !isActionKey()) {\n return;\n }\n\n performAction(e);\n } else if (e.pointerType !== 'keyboard' && allowsSelection) {\n onSelect(e);\n }\n };\n } else {\n itemPressProps.onPressUp = hasPrimaryAction ? null : (e) => {\n if (e.pointerType !== 'keyboard' && allowsSelection) {\n onSelect(e);\n }\n };\n\n itemPressProps.onPress = hasPrimaryAction ? performAction : null;\n }\n } else {\n itemPressProps.onPressStart = (e) => {\n modality.current = e.pointerType;\n longPressEnabledOnPressStart.current = longPressEnabled;\n hadPrimaryActionOnPressStart.current = hasPrimaryAction;\n\n // Select on mouse down unless there is a primary action which will occur on mouse up.\n // For keyboard, select on key down. If there is an action, the Space key selects on key down,\n // and the Enter key performs onAction on key up.\n if (\n allowsSelection && (\n (e.pointerType === 'mouse' && !hasPrimaryAction) ||\n (e.pointerType === 'keyboard' && (!allowsActions || isSelectionKey()))\n )\n ) {\n onSelect(e);\n }\n };\n\n itemPressProps.onPress = (e) => {\n // Selection occurs on touch up. Primary actions always occur on pointer up.\n // Both primary and secondary actions occur on Enter key up. The only exception\n // is secondary actions, which occur on double click with a mouse.\n if (\n e.pointerType === 'touch' ||\n e.pointerType === 'pen' ||\n e.pointerType === 'virtual' ||\n (e.pointerType === 'keyboard' && hasAction && isActionKey()) ||\n (e.pointerType === 'mouse' && hadPrimaryActionOnPressStart.current)\n ) {\n if (hasAction) {\n performAction(e);\n } else if (allowsSelection) {\n onSelect(e);\n }\n }\n };\n }\n\n itemProps['data-key'] = key;\n itemPressProps.preventFocusOnPress = shouldUseVirtualFocus;\n let {pressProps, isPressed} = usePress(itemPressProps);\n\n // Double clicking with a mouse with selectionBehavior = 'replace' performs an action.\n let onDoubleClick = hasSecondaryAction ? (e) => {\n if (modality.current === 'mouse') {\n e.stopPropagation();\n e.preventDefault();\n performAction(e);\n }\n } : undefined;\n\n // Long pressing an item with touch when selectionBehavior = 'replace' switches the selection behavior\n // to 'toggle'. This changes the single tap behavior from performing an action (i.e. navigating) to\n // selecting, and may toggle the appearance of a UI affordance like checkboxes on each item.\n let {longPressProps} = useLongPress({\n isDisabled: !longPressEnabled,\n onLongPress(e) {\n if (e.pointerType === 'touch') {\n onSelect(e);\n manager.setSelectionBehavior('toggle');\n }\n }\n });\n\n // Prevent native drag and drop on long press if we also select on long press.\n // Once the user is in selection mode, they can long press again to drag.\n // Use a capturing listener to ensure this runs before useDrag, regardless of\n // the order the props get merged.\n let onDragStartCapture = e => {\n if (modality.current === 'touch' && longPressEnabledOnPressStart.current) {\n e.preventDefault();\n }\n };\n\n // Prevent default on link clicks so that we control exactly\n // when they open (to match selection behavior).\n let onClick = manager.isLink(key) ? e => {\n if (!(openLink as any).isOpening) {\n e.preventDefault();\n }\n } : undefined;\n\n return {\n itemProps: mergeProps(\n itemProps,\n allowsSelection || hasPrimaryAction ? pressProps : {},\n longPressEnabled ? longPressProps : {},\n {onDoubleClick, onDragStartCapture, onClick}\n ),\n isPressed,\n isSelected: manager.isSelected(key),\n isFocused: manager.isFocused && manager.focusedKey === key,\n isDisabled,\n allowsSelection,\n hasAction\n };\n}\n\nfunction isActionKey() {\n let event = window.event as KeyboardEvent;\n return event?.key === 'Enter';\n}\n\nfunction isSelectionKey() {\n let event = window.event as KeyboardEvent;\n return event?.key === ' ' || event?.code === 'Space';\n}\n"],"names":[],"version":3,"file":"useSelectableItem.main.js.map"}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import {isCtrlKeyPressed as $feb5ffebff200149$export$16792effe837dba3, isNonContiguousSelectionModifier as $feb5ffebff200149$export$d3e3bd3e26688c04} from "./utils.mjs";
|
|
2
|
+
import {focusSafely as $581M0$focusSafely} from "@react-aria/focus";
|
|
3
|
+
import {useRouter as $581M0$useRouter, openLink as $581M0$openLink, mergeProps as $581M0$mergeProps} from "@react-aria/utils";
|
|
4
|
+
import {usePress as $581M0$usePress, useLongPress as $581M0$useLongPress} from "@react-aria/interactions";
|
|
5
|
+
import {useEffect as $581M0$useEffect, useRef as $581M0$useRef} from "react";
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
9
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
11
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
14
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
15
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
16
|
+
* governing permissions and limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
function $880e95eb8b93ba9a$export$ecf600387e221c37(options) {
|
|
23
|
+
let { selectionManager: manager, key: key, ref: ref, shouldSelectOnPressUp: shouldSelectOnPressUp, shouldUseVirtualFocus: shouldUseVirtualFocus, focus: focus, isDisabled: isDisabled, onAction: onAction, allowsDifferentPressOrigin: allowsDifferentPressOrigin, linkBehavior: linkBehavior = 'action' } = options;
|
|
24
|
+
let router = (0, $581M0$useRouter)();
|
|
25
|
+
let onSelect = (e)=>{
|
|
26
|
+
if (e.pointerType === 'keyboard' && (0, $feb5ffebff200149$export$d3e3bd3e26688c04)(e)) manager.toggleSelection(key);
|
|
27
|
+
else {
|
|
28
|
+
if (manager.selectionMode === 'none') return;
|
|
29
|
+
if (manager.isLink(key)) {
|
|
30
|
+
if (linkBehavior === 'selection') {
|
|
31
|
+
let itemProps = manager.getItemProps(key);
|
|
32
|
+
router.open(ref.current, e, itemProps.href, itemProps.routerOptions);
|
|
33
|
+
// Always set selected keys back to what they were so that select and combobox close.
|
|
34
|
+
manager.setSelectedKeys(manager.selectedKeys);
|
|
35
|
+
return;
|
|
36
|
+
} else if (linkBehavior === 'override' || linkBehavior === 'none') return;
|
|
37
|
+
}
|
|
38
|
+
if (manager.selectionMode === 'single') {
|
|
39
|
+
if (manager.isSelected(key) && !manager.disallowEmptySelection) manager.toggleSelection(key);
|
|
40
|
+
else manager.replaceSelection(key);
|
|
41
|
+
} else if (e && e.shiftKey) manager.extendSelection(key);
|
|
42
|
+
else if (manager.selectionBehavior === 'toggle' || e && ((0, $feb5ffebff200149$export$16792effe837dba3)(e) || e.pointerType === 'touch' || e.pointerType === 'virtual')) // if touch or virtual (VO) then we just want to toggle, otherwise it's impossible to multi select because they don't have modifier keys
|
|
43
|
+
manager.toggleSelection(key);
|
|
44
|
+
else manager.replaceSelection(key);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
// Focus the associated DOM node when this item becomes the focusedKey
|
|
48
|
+
(0, $581M0$useEffect)(()=>{
|
|
49
|
+
let isFocused = key === manager.focusedKey;
|
|
50
|
+
if (isFocused && manager.isFocused && !shouldUseVirtualFocus) {
|
|
51
|
+
if (focus) focus();
|
|
52
|
+
else if (document.activeElement !== ref.current) (0, $581M0$focusSafely)(ref.current);
|
|
53
|
+
}
|
|
54
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
55
|
+
}, [
|
|
56
|
+
ref,
|
|
57
|
+
key,
|
|
58
|
+
manager.focusedKey,
|
|
59
|
+
manager.childFocusStrategy,
|
|
60
|
+
manager.isFocused,
|
|
61
|
+
shouldUseVirtualFocus
|
|
62
|
+
]);
|
|
63
|
+
isDisabled = isDisabled || manager.isDisabled(key);
|
|
64
|
+
// Set tabIndex to 0 if the element is focused, or -1 otherwise so that only the last focused
|
|
65
|
+
// item is tabbable. If using virtual focus, don't set a tabIndex at all so that VoiceOver
|
|
66
|
+
// on iOS 14 doesn't try to move real DOM focus to the item anyway.
|
|
67
|
+
let itemProps = {};
|
|
68
|
+
if (!shouldUseVirtualFocus && !isDisabled) itemProps = {
|
|
69
|
+
tabIndex: key === manager.focusedKey ? 0 : -1,
|
|
70
|
+
onFocus (e) {
|
|
71
|
+
if (e.target === ref.current) manager.setFocusedKey(key);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
else if (isDisabled) itemProps.onMouseDown = (e)=>{
|
|
75
|
+
// Prevent focus going to the body when clicking on a disabled item.
|
|
76
|
+
e.preventDefault();
|
|
77
|
+
};
|
|
78
|
+
// With checkbox selection, onAction (i.e. navigation) becomes primary, and occurs on a single click of the row.
|
|
79
|
+
// Clicking the checkbox enters selection mode, after which clicking anywhere on any row toggles selection for that row.
|
|
80
|
+
// With highlight selection, onAction is secondary, and occurs on double click. Single click selects the row.
|
|
81
|
+
// With touch, onAction occurs on single tap, and long press enters selection mode.
|
|
82
|
+
let isLinkOverride = manager.isLink(key) && linkBehavior === 'override';
|
|
83
|
+
let hasLinkAction = manager.isLink(key) && linkBehavior !== 'selection' && linkBehavior !== 'none';
|
|
84
|
+
let allowsSelection = !isDisabled && manager.canSelectItem(key) && !isLinkOverride;
|
|
85
|
+
let allowsActions = (onAction || hasLinkAction) && !isDisabled;
|
|
86
|
+
let hasPrimaryAction = allowsActions && (manager.selectionBehavior === 'replace' ? !allowsSelection : !allowsSelection || manager.isEmpty);
|
|
87
|
+
let hasSecondaryAction = allowsActions && allowsSelection && manager.selectionBehavior === 'replace';
|
|
88
|
+
let hasAction = hasPrimaryAction || hasSecondaryAction;
|
|
89
|
+
let modality = (0, $581M0$useRef)(null);
|
|
90
|
+
let longPressEnabled = hasAction && allowsSelection;
|
|
91
|
+
let longPressEnabledOnPressStart = (0, $581M0$useRef)(false);
|
|
92
|
+
let hadPrimaryActionOnPressStart = (0, $581M0$useRef)(false);
|
|
93
|
+
let performAction = (e)=>{
|
|
94
|
+
if (onAction) onAction();
|
|
95
|
+
if (hasLinkAction) {
|
|
96
|
+
let itemProps = manager.getItemProps(key);
|
|
97
|
+
router.open(ref.current, e, itemProps.href, itemProps.routerOptions);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
// By default, selection occurs on pointer down. This can be strange if selecting an
|
|
101
|
+
// item causes the UI to disappear immediately (e.g. menus).
|
|
102
|
+
// If shouldSelectOnPressUp is true, we use onPressUp instead of onPressStart.
|
|
103
|
+
// onPress requires a pointer down event on the same element as pointer up. For menus,
|
|
104
|
+
// we want to be able to have the pointer down on the trigger that opens the menu and
|
|
105
|
+
// the pointer up on the menu item rather than requiring a separate press.
|
|
106
|
+
// For keyboard events, selection still occurs on key down.
|
|
107
|
+
let itemPressProps = {};
|
|
108
|
+
if (shouldSelectOnPressUp) {
|
|
109
|
+
itemPressProps.onPressStart = (e)=>{
|
|
110
|
+
modality.current = e.pointerType;
|
|
111
|
+
longPressEnabledOnPressStart.current = longPressEnabled;
|
|
112
|
+
if (e.pointerType === 'keyboard' && (!hasAction || $880e95eb8b93ba9a$var$isSelectionKey())) onSelect(e);
|
|
113
|
+
};
|
|
114
|
+
// If allowsDifferentPressOrigin, make selection happen on pressUp (e.g. open menu on press down, selection on menu item happens on press up.)
|
|
115
|
+
// Otherwise, have selection happen onPress (prevents listview row selection when clicking on interactable elements in the row)
|
|
116
|
+
if (!allowsDifferentPressOrigin) itemPressProps.onPress = (e)=>{
|
|
117
|
+
if (hasPrimaryAction || hasSecondaryAction && e.pointerType !== 'mouse') {
|
|
118
|
+
if (e.pointerType === 'keyboard' && !$880e95eb8b93ba9a$var$isActionKey()) return;
|
|
119
|
+
performAction(e);
|
|
120
|
+
} else if (e.pointerType !== 'keyboard' && allowsSelection) onSelect(e);
|
|
121
|
+
};
|
|
122
|
+
else {
|
|
123
|
+
itemPressProps.onPressUp = hasPrimaryAction ? null : (e)=>{
|
|
124
|
+
if (e.pointerType !== 'keyboard' && allowsSelection) onSelect(e);
|
|
125
|
+
};
|
|
126
|
+
itemPressProps.onPress = hasPrimaryAction ? performAction : null;
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
itemPressProps.onPressStart = (e)=>{
|
|
130
|
+
modality.current = e.pointerType;
|
|
131
|
+
longPressEnabledOnPressStart.current = longPressEnabled;
|
|
132
|
+
hadPrimaryActionOnPressStart.current = hasPrimaryAction;
|
|
133
|
+
// Select on mouse down unless there is a primary action which will occur on mouse up.
|
|
134
|
+
// For keyboard, select on key down. If there is an action, the Space key selects on key down,
|
|
135
|
+
// and the Enter key performs onAction on key up.
|
|
136
|
+
if (allowsSelection && (e.pointerType === 'mouse' && !hasPrimaryAction || e.pointerType === 'keyboard' && (!allowsActions || $880e95eb8b93ba9a$var$isSelectionKey()))) onSelect(e);
|
|
137
|
+
};
|
|
138
|
+
itemPressProps.onPress = (e)=>{
|
|
139
|
+
// Selection occurs on touch up. Primary actions always occur on pointer up.
|
|
140
|
+
// Both primary and secondary actions occur on Enter key up. The only exception
|
|
141
|
+
// is secondary actions, which occur on double click with a mouse.
|
|
142
|
+
if (e.pointerType === 'touch' || e.pointerType === 'pen' || e.pointerType === 'virtual' || e.pointerType === 'keyboard' && hasAction && $880e95eb8b93ba9a$var$isActionKey() || e.pointerType === 'mouse' && hadPrimaryActionOnPressStart.current) {
|
|
143
|
+
if (hasAction) performAction(e);
|
|
144
|
+
else if (allowsSelection) onSelect(e);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
itemProps['data-key'] = key;
|
|
149
|
+
itemPressProps.preventFocusOnPress = shouldUseVirtualFocus;
|
|
150
|
+
let { pressProps: pressProps, isPressed: isPressed } = (0, $581M0$usePress)(itemPressProps);
|
|
151
|
+
// Double clicking with a mouse with selectionBehavior = 'replace' performs an action.
|
|
152
|
+
let onDoubleClick = hasSecondaryAction ? (e)=>{
|
|
153
|
+
if (modality.current === 'mouse') {
|
|
154
|
+
e.stopPropagation();
|
|
155
|
+
e.preventDefault();
|
|
156
|
+
performAction(e);
|
|
157
|
+
}
|
|
158
|
+
} : undefined;
|
|
159
|
+
// Long pressing an item with touch when selectionBehavior = 'replace' switches the selection behavior
|
|
160
|
+
// to 'toggle'. This changes the single tap behavior from performing an action (i.e. navigating) to
|
|
161
|
+
// selecting, and may toggle the appearance of a UI affordance like checkboxes on each item.
|
|
162
|
+
let { longPressProps: longPressProps } = (0, $581M0$useLongPress)({
|
|
163
|
+
isDisabled: !longPressEnabled,
|
|
164
|
+
onLongPress (e) {
|
|
165
|
+
if (e.pointerType === 'touch') {
|
|
166
|
+
onSelect(e);
|
|
167
|
+
manager.setSelectionBehavior('toggle');
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
// Prevent native drag and drop on long press if we also select on long press.
|
|
172
|
+
// Once the user is in selection mode, they can long press again to drag.
|
|
173
|
+
// Use a capturing listener to ensure this runs before useDrag, regardless of
|
|
174
|
+
// the order the props get merged.
|
|
175
|
+
let onDragStartCapture = (e)=>{
|
|
176
|
+
if (modality.current === 'touch' && longPressEnabledOnPressStart.current) e.preventDefault();
|
|
177
|
+
};
|
|
178
|
+
// Prevent default on link clicks so that we control exactly
|
|
179
|
+
// when they open (to match selection behavior).
|
|
180
|
+
let onClick = manager.isLink(key) ? (e)=>{
|
|
181
|
+
if (!(0, $581M0$openLink).isOpening) e.preventDefault();
|
|
182
|
+
} : undefined;
|
|
183
|
+
return {
|
|
184
|
+
itemProps: (0, $581M0$mergeProps)(itemProps, allowsSelection || hasPrimaryAction ? pressProps : {}, longPressEnabled ? longPressProps : {}, {
|
|
185
|
+
onDoubleClick: onDoubleClick,
|
|
186
|
+
onDragStartCapture: onDragStartCapture,
|
|
187
|
+
onClick: onClick
|
|
188
|
+
}),
|
|
189
|
+
isPressed: isPressed,
|
|
190
|
+
isSelected: manager.isSelected(key),
|
|
191
|
+
isFocused: manager.isFocused && manager.focusedKey === key,
|
|
192
|
+
isDisabled: isDisabled,
|
|
193
|
+
allowsSelection: allowsSelection,
|
|
194
|
+
hasAction: hasAction
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
function $880e95eb8b93ba9a$var$isActionKey() {
|
|
198
|
+
let event = window.event;
|
|
199
|
+
return (event === null || event === void 0 ? void 0 : event.key) === 'Enter';
|
|
200
|
+
}
|
|
201
|
+
function $880e95eb8b93ba9a$var$isSelectionKey() {
|
|
202
|
+
let event = window.event;
|
|
203
|
+
return (event === null || event === void 0 ? void 0 : event.key) === ' ' || (event === null || event === void 0 ? void 0 : event.code) === 'Space';
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
export {$880e95eb8b93ba9a$export$ecf600387e221c37 as useSelectableItem};
|
|
208
|
+
//# sourceMappingURL=useSelectableItem.module.js.map
|