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,kBAAM,gCAAC;YAAO,KAAK;YAAG,OAAO,SAAS;;SAK9D,IAAI,MAAM;QACf,IAAI,OAAO,CAAA,GAAA,yCAAS,EAAE,GAAG,CAAC,UAAU,CAAC;QACrC,IAAI,sBAAC,kBAAkB,EAAC,GAAG;QAE3B,8EAA8E;QAC9E,IAAI,OAAO,MAAM,KAAK,GACpB,SAAS;YAAC;SAAK;QAGjB,IAAI,MAAM,OAAO,GAAG,CAAC,CAAC,OAAO;YAC3B,IAAI,aAAoD;gBACtD,MAAM;gBACN,cAAc,YAAY,YAAY;sBACtC;sBACA;gBACA,UAAU;gBACV,OAAO,SAAS;YAClB;YAEA,IAAI,uBAAuB,UACzB,qEAAqE;YACrE,iFAAiF;YACjF,qBACE,gCAAC;gBACC,KAAK;gBACJ,GAAG,UAAU;gBACd,KAAK,MAAM,IAAI,WAAW;gBAC1B,OAAO;oBAAC,SAAS;gBAAM;gBACvB,MAAK;gBACL,UAAU,MAAM,IAAI,YAAY,QAAQ,GAAG;gBAC3C,UAAU;gBACR,0BAA0B,GAC5B;;YAKN,qBAAO,gCAAC;gBAAM,KAAK;gBAAI,GAAG,UAAU;gBAAE,KAAK,MAAM,IAAI,WAAW;;QAClE;QAEA,qBAAO,kEAAG;IACZ;IAEA,OAAO;AACT","sources":["packages/react-aria/src/select/HiddenSelect.tsx"],"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 {FocusableElement, Key, RefObject} from '@react-types/shared';\nimport {getEventTarget} from '../utils/shadowdom/DOMFunctions';\nimport React, {InputHTMLAttributes, JSX, ReactNode, useCallback, useRef} from 'react';\nimport {selectData} from './useSelect';\nimport {SelectionMode, SelectState} from 'react-stately/useSelectState';\nimport {useFormReset} from '../utils/useFormReset';\nimport {useFormValidation} from '../form/useFormValidation';\nimport {useVisuallyHidden} from '../visually-hidden/VisuallyHidden';\n\nexport interface AriaHiddenSelectProps {\n /**\n * Describes the type of autocomplete functionality the input should provide if any. See\n * [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefautocomplete).\n */\n autoComplete?: string;\n\n /** The text label for the select. */\n label?: ReactNode;\n\n /** HTML form input name. */\n name?: string;\n\n /**\n * The `<form>` element to associate the input with.\n * The value of this attribute must be the id of a `<form>` in the same document.\n * See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#form).\n */\n form?: string;\n\n /** Sets the disabled state of the select and input. */\n isDisabled?: boolean;\n}\n\nexport interface HiddenSelectProps<\n T,\n M extends SelectionMode = 'single'\n> extends AriaHiddenSelectProps {\n /** State for the select. */\n state: SelectState<T, M>;\n\n /** A ref to the trigger element. */\n triggerRef: RefObject<FocusableElement | null>;\n}\n\nexport interface AriaHiddenSelectOptions extends AriaHiddenSelectProps {\n /** A ref to the hidden `<select>` element. */\n selectRef?: RefObject<HTMLSelectElement | HTMLInputElement | null>;\n}\n\nexport interface HiddenSelectAria {\n /** Props for the container element. */\n containerProps: React.HTMLAttributes<FocusableElement>;\n\n /** Props for the hidden input element. */\n inputProps: React.InputHTMLAttributes<HTMLInputElement>;\n\n /** Props for the hidden select element. */\n selectProps: React.SelectHTMLAttributes<HTMLSelectElement>;\n}\n\n/**\n * Provides the behavior and accessibility implementation for a hidden `<select>` element, which\n * can be used in combination with `useSelect` to support browser form autofill, mobile form\n * navigation, and native HTML form submission.\n */\nexport function useHiddenSelect<T, M extends SelectionMode = 'single'>(\n props: AriaHiddenSelectOptions,\n state: SelectState<T, M>,\n triggerRef: RefObject<FocusableElement | null>\n): HiddenSelectAria {\n let data = selectData.get(state) || {};\n let {autoComplete, name = data.name, form = data.form, isDisabled = data.isDisabled} = props;\n let {validationBehavior, isRequired} = data;\n let {visuallyHiddenProps} = useVisuallyHidden({\n style: {\n // Prevent page scrolling.\n position: 'fixed',\n top: 0,\n left: 0\n }\n });\n\n useFormReset(props.selectRef, state.defaultValue, state.setValue);\n useFormValidation(\n {\n validationBehavior,\n focus: () => triggerRef.current?.focus()\n },\n state,\n props.selectRef\n );\n\n let setValue = state.setValue;\n let onChange = useCallback(\n (e: React.ChangeEvent<HTMLSelectElement>) => {\n let eventTarget = getEventTarget(e);\n if (eventTarget.multiple) {\n setValue(Array.from(eventTarget.selectedOptions, option => option.value) as any);\n } else {\n setValue(e.currentTarget.value as any);\n }\n },\n [setValue]\n );\n\n // In Safari, the <select> cannot have `display: none` or `hidden` for autofill to work.\n // In Firefox, there must be a <label> to identify the <select> whereas other browsers\n // seem to identify it just by surrounding text.\n // The solution is to use <VisuallyHidden> to hide the elements, which clips the elements to a\n // 1px rectangle. In addition, we hide from screen readers with aria-hidden, and make the <select>\n // non tabbable with tabIndex={-1}.\n return {\n containerProps: {\n ...visuallyHiddenProps,\n 'aria-hidden': true,\n // @ts-ignore\n ['data-react-aria-prevent-focus']: true,\n // @ts-ignore\n ['data-a11y-ignore']: 'aria-hidden-focus'\n },\n inputProps: {\n style: {display: 'none'}\n },\n selectProps: {\n tabIndex: -1,\n autoComplete,\n disabled: isDisabled,\n multiple: state.selectionManager.selectionMode === 'multiple',\n required: validationBehavior === 'native' && isRequired,\n name,\n form,\n value: (state.value as string | string[]) ?? '',\n onChange,\n onInput: onChange\n }\n };\n}\n\n/**\n * Renders a hidden native `<select>` element, which can be used to support browser\n * form autofill, mobile form navigation, and native form submission.\n */\nexport function HiddenSelect<T, M extends SelectionMode = 'single'>(\n props: HiddenSelectProps<T, M>\n): JSX.Element | null {\n let {state, triggerRef, label, name, form, isDisabled} = props;\n let selectRef = useRef(null);\n let inputRef = useRef(null);\n let {containerProps, selectProps} = useHiddenSelect(\n {...props, selectRef: state.collection.size <= 300 ? selectRef : inputRef},\n state,\n triggerRef\n );\n\n let values: (Key | null)[] = Array.isArray(state.value) ? state.value : [state.value];\n\n // If used in a <form>, use a hidden input so the value can be submitted to a server.\n // If the collection isn't too big, use a hidden <select> element for this so that browser\n // autofill will work. Otherwise, use an <input type=\"hidden\">.\n if (state.collection.size <= 300) {\n return (\n <div {...containerProps} data-testid=\"hidden-select-container\">\n <label>\n {label}\n <select {...selectProps} ref={selectRef}>\n <option value=\"\" label={'\\u00A0'}>\n {'\\u00A0'}\n </option>\n {[...state.collection.getKeys()].map(key => {\n let item = state.collection.getItem(key);\n if (item && item.type === 'item') {\n return (\n <option key={item.key} value={item.key}>\n {item.textValue}\n </option>\n );\n }\n })}\n {/* The collection may be empty during the initial render. */}\n {/* Rendering options for the current values ensures the select has a value immediately, */}\n {/* making FormData reads consistent. */}\n {state.collection.size === 0 &&\n name &&\n values.map((value, i) => <option key={i} value={value ?? ''} />)}\n </select>\n </label>\n </div>\n );\n } else if (name) {\n let data = selectData.get(state) || {};\n let {validationBehavior} = data;\n\n // Always render at least one hidden input to ensure required form submission.\n if (values.length === 0) {\n values = [null];\n }\n\n let res = values.map((value, i) => {\n let inputProps: InputHTMLAttributes<HTMLInputElement> = {\n type: 'hidden',\n autoComplete: selectProps.autoComplete,\n name,\n form,\n disabled: isDisabled,\n value: value ?? ''\n };\n\n if (validationBehavior === 'native') {\n // Use a hidden <input type=\"text\"> rather than <input type=\"hidden\">\n // so that an empty value blocks HTML form submission when the field is required.\n return (\n <input\n key={i}\n {...inputProps}\n ref={i === 0 ? inputRef : null}\n style={{display: 'none'}}\n type=\"text\"\n required={i === 0 ? selectProps.required : false}\n onChange={() => {\n /** Ignore react warning. */\n }}\n />\n );\n }\n\n return <input key={i} {...inputProps} ref={i === 0 ? inputRef : null} />;\n });\n\n return <>{res}</>;\n }\n\n return null;\n}\n"],"names":[],"version":3,"file":"HiddenSelect.mjs.map"}