UNPKG

@react-aria/select

Version:
161 lines (151 loc) • 7.65 kB
import {filterDOMProps as $aUnai$filterDOMProps, mergeProps as $aUnai$mergeProps, useId as $aUnai$useId, chain as $aUnai$chain} from "@react-aria/utils"; import {useMemo as $aUnai$useMemo} from "react"; import {ListKeyboardDelegate as $aUnai$ListKeyboardDelegate, useTypeSelect as $aUnai$useTypeSelect} from "@react-aria/selection"; import {setInteractionModality as $aUnai$setInteractionModality} from "@react-aria/interactions"; import {useCollator as $aUnai$useCollator} from "@react-aria/i18n"; import {useField as $aUnai$useField} from "@react-aria/label"; import {useMenuTrigger as $aUnai$useMenuTrigger} from "@react-aria/menu"; /* * Copyright 2020 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ const $58aed456727eb0f3$export$703601b7e90536f8 = new WeakMap(); function $58aed456727eb0f3$export$e64b2f635402ca43(props, state, ref) { let { keyboardDelegate: keyboardDelegate, isDisabled: isDisabled, isRequired: isRequired, name: name, validationBehavior: validationBehavior = 'aria' } = props; // By default, a KeyboardDelegate is provided which uses the DOM to query layout information (e.g. for page up/page down). // When virtualized, the layout object will be passed in as a prop and override this. let collator = (0, $aUnai$useCollator)({ usage: 'search', sensitivity: 'base' }); let delegate = (0, $aUnai$useMemo)(()=>keyboardDelegate || new (0, $aUnai$ListKeyboardDelegate)(state.collection, state.disabledKeys, ref, collator), [ keyboardDelegate, state.collection, state.disabledKeys, collator ]); let { menuTriggerProps: menuTriggerProps, menuProps: menuProps } = (0, $aUnai$useMenuTrigger)({ isDisabled: isDisabled, type: 'listbox' }, state, ref); let onKeyDown = (e)=>{ switch(e.key){ case 'ArrowLeft': { var _delegate_getKeyAbove, _delegate_getFirstKey; // prevent scrolling containers e.preventDefault(); let key = state.selectedKey != null ? (_delegate_getKeyAbove = delegate.getKeyAbove) === null || _delegate_getKeyAbove === void 0 ? void 0 : _delegate_getKeyAbove.call(delegate, state.selectedKey) : (_delegate_getFirstKey = delegate.getFirstKey) === null || _delegate_getFirstKey === void 0 ? void 0 : _delegate_getFirstKey.call(delegate); if (key) state.setSelectedKey(key); break; } case 'ArrowRight': { var _delegate_getKeyBelow, _delegate_getFirstKey1; // prevent scrolling containers e.preventDefault(); let key = state.selectedKey != null ? (_delegate_getKeyBelow = delegate.getKeyBelow) === null || _delegate_getKeyBelow === void 0 ? void 0 : _delegate_getKeyBelow.call(delegate, state.selectedKey) : (_delegate_getFirstKey1 = delegate.getFirstKey) === null || _delegate_getFirstKey1 === void 0 ? void 0 : _delegate_getFirstKey1.call(delegate); if (key) state.setSelectedKey(key); break; } } }; let { typeSelectProps: typeSelectProps } = (0, $aUnai$useTypeSelect)({ keyboardDelegate: delegate, selectionManager: state.selectionManager, onTypeSelect (key) { state.setSelectedKey(key); } }); let { isInvalid: isInvalid, validationErrors: validationErrors, validationDetails: validationDetails } = state.displayValidation; let { labelProps: labelProps, fieldProps: fieldProps, descriptionProps: descriptionProps, errorMessageProps: errorMessageProps } = (0, $aUnai$useField)({ ...props, labelElementType: 'span', isInvalid: isInvalid, errorMessage: props.errorMessage || validationErrors }); typeSelectProps.onKeyDown = typeSelectProps.onKeyDownCapture; delete typeSelectProps.onKeyDownCapture; let domProps = (0, $aUnai$filterDOMProps)(props, { labelable: true }); let triggerProps = (0, $aUnai$mergeProps)(typeSelectProps, menuTriggerProps, fieldProps); let valueId = (0, $aUnai$useId)(); $58aed456727eb0f3$export$703601b7e90536f8.set(state, { isDisabled: isDisabled, isRequired: isRequired, name: name, validationBehavior: validationBehavior }); return { labelProps: { ...labelProps, onClick: ()=>{ if (!props.isDisabled) { var _ref_current; (_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.focus(); // Show the focus ring so the user knows where focus went (0, $aUnai$setInteractionModality)('keyboard'); } } }, triggerProps: (0, $aUnai$mergeProps)(domProps, { ...triggerProps, isDisabled: isDisabled, onKeyDown: (0, $aUnai$chain)(triggerProps.onKeyDown, onKeyDown, props.onKeyDown), onKeyUp: props.onKeyUp, 'aria-labelledby': [ valueId, triggerProps['aria-labelledby'], triggerProps['aria-label'] && !triggerProps['aria-labelledby'] ? triggerProps.id : null ].filter(Boolean).join(' '), onFocus (e) { if (state.isFocused) return; if (props.onFocus) props.onFocus(e); if (props.onFocusChange) props.onFocusChange(true); state.setFocused(true); }, onBlur (e) { if (state.isOpen) return; if (props.onBlur) props.onBlur(e); if (props.onFocusChange) props.onFocusChange(false); state.setFocused(false); } }), valueProps: { id: valueId }, menuProps: { ...menuProps, autoFocus: state.focusStrategy || true, shouldSelectOnPressUp: true, shouldFocusOnHover: true, disallowEmptySelection: true, linkBehavior: 'selection', onBlur: (e)=>{ if (e.currentTarget.contains(e.relatedTarget)) return; if (props.onBlur) props.onBlur(e); if (props.onFocusChange) props.onFocusChange(false); state.setFocused(false); }, 'aria-labelledby': [ fieldProps['aria-labelledby'], triggerProps['aria-label'] && !fieldProps['aria-labelledby'] ? triggerProps.id : null ].filter(Boolean).join(' ') }, descriptionProps: descriptionProps, errorMessageProps: errorMessageProps, isInvalid: isInvalid, validationErrors: validationErrors, validationDetails: validationDetails }; } export {$58aed456727eb0f3$export$703601b7e90536f8 as selectData, $58aed456727eb0f3$export$e64b2f635402ca43 as useSelect}; //# sourceMappingURL=useSelect.module.js.map