UNPKG

@react-aria/select

Version:
167 lines (155 loc) • 7.79 kB
var $63cuq$reactariautils = require("@react-aria/utils"); var $63cuq$react = require("react"); var $63cuq$reactariaselection = require("@react-aria/selection"); var $63cuq$reactariainteractions = require("@react-aria/interactions"); var $63cuq$reactariai18n = require("@react-aria/i18n"); var $63cuq$reactarialabel = require("@react-aria/label"); var $63cuq$reactariamenu = require("@react-aria/menu"); function $parcel$export(e, n, v, s) { Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true}); } $parcel$export(module.exports, "selectData", () => $23a1e93dc7fa8653$export$703601b7e90536f8); $parcel$export(module.exports, "useSelect", () => $23a1e93dc7fa8653$export$e64b2f635402ca43); /* * 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 $23a1e93dc7fa8653$export$703601b7e90536f8 = new WeakMap(); function $23a1e93dc7fa8653$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, $63cuq$reactariai18n.useCollator)({ usage: 'search', sensitivity: 'base' }); let delegate = (0, $63cuq$react.useMemo)(()=>keyboardDelegate || new (0, $63cuq$reactariaselection.ListKeyboardDelegate)(state.collection, state.disabledKeys, ref, collator), [ keyboardDelegate, state.collection, state.disabledKeys, collator ]); let { menuTriggerProps: menuTriggerProps, menuProps: menuProps } = (0, $63cuq$reactariamenu.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, $63cuq$reactariaselection.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, $63cuq$reactarialabel.useField)({ ...props, labelElementType: 'span', isInvalid: isInvalid, errorMessage: props.errorMessage || validationErrors }); typeSelectProps.onKeyDown = typeSelectProps.onKeyDownCapture; delete typeSelectProps.onKeyDownCapture; let domProps = (0, $63cuq$reactariautils.filterDOMProps)(props, { labelable: true }); let triggerProps = (0, $63cuq$reactariautils.mergeProps)(typeSelectProps, menuTriggerProps, fieldProps); let valueId = (0, $63cuq$reactariautils.useId)(); $23a1e93dc7fa8653$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, $63cuq$reactariainteractions.setInteractionModality)('keyboard'); } } }, triggerProps: (0, $63cuq$reactariautils.mergeProps)(domProps, { ...triggerProps, isDisabled: isDisabled, onKeyDown: (0, $63cuq$reactariautils.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 }; } //# sourceMappingURL=useSelect.main.js.map