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C,WAAW;IAAI;IACrD,IAAI,WAA+B;IACnC,IAAI,MAAM,aAAa,IAAI,MACzB;QAAA,IAAI,MAAM,aAAa,KAAK,OAC1B,WAAW;IACb,OACK,IAAI,MAAM,gBAAgB,KAAK,SAAS,MAAM,gBAAgB,IAAI,MACvE,WAAW;IAEb,IAAI,YACF,WAAW;IAGb,IAAI,QAAC,IAAI,QAAE,IAAI,iBAAE,aAAa,kBAAE,cAAc,sBAAE,kBAAkB,EAAC,GAAG,CAAA,GAAA,wCAAa,EAAE,GAAG,CAAC;IACzF,CAAA,GAAA,sCAAW,EAAE,KAAK,MAAM,oBAAoB,EAAE,MAAM,gBAAgB;IACpE,CAAA,GAAA,2CAAgB,EAAE;4BAAC;IAAkB,GAAG,OAAO;IAE/C,IAAI,mBAAmB,CAAA,GAAA,oCAAS;IAEhC,OAAO;QACL,YAAY,CAAA,GAAA,oCAAS,EACnB,YACA,CAAA,GAAA,oBAAM,EACJ,IAAO,CAAA;gBACL,SAAS,CAAA,IAAK,EAAE,cAAc;gBAE9B,0DAA0D;gBAC1D,iFAAiF;gBACjF,aAAa,CAAA,IAAK,EAAE,cAAc;YACpC,CAAA,GACA,EAAE;QAGN,YAAY,CAAA,GAAA,oCAAS,EAAE,UAAU;YAC/B,GAAG,YAAY;YACf,MAAM;kBACN;kBACA;sBACA;YACA,UAAU;YACV,UAAU,MAAM,UAAU,IAAI,uBAAuB;qBACrD;mBACA;sBACA;YACA,oBACE;gBACE,KAAK,CAAC,mBAAmB;gBACzB,iBAAiB,EAAE;gBACnB,MAAM,SAAS,GAAG,iBAAiB;gBACnC;aACD,CACE,MAAM,CAAC,SACP,IAAI,CAAC,QAAQ;QACpB;0BACA;oBACA;QACA,YAAY;QACZ,WAAW,aAAa;IAC1B;AACF","sources":["packages/react-aria/src/radio/useRadio.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 AriaLabelingProps,\n DOMAttributesWithRef,\n DOMProps,\n FocusableProps,\n PressEvents,\n RefObject\n} from '@react-types/shared';\nimport {filterDOMProps} from '../utils/filterDOMProps';\nimport {InputHTMLAttributes, LabelHTMLAttributes, ReactNode, useMemo} from 'react';\nimport {mergeProps} from '../utils/mergeProps';\nimport {radioGroupData} from './utils';\nimport {RadioGroupState} from 'react-stately/useRadioGroupState';\nimport {useFocusable} from '../interactions/useFocusable';\nimport {useFormReset} from '../utils/useFormReset';\nimport {useFormValidation} from '../form/useFormValidation';\nimport {usePress} from '../interactions/usePress';\nimport {useSlotId2} from '../utils/useSlot';\n\nexport interface RadioProps extends FocusableProps {\n /**\n * The value of the radio button, used when submitting an HTML form.\n * See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio#Value).\n */\n value: string;\n /**\n * The label for the Radio. Accepts any renderable node.\n */\n children?: ReactNode;\n /**\n * Whether the radio button is disabled or not.\n * Shows that a selection exists, but is not available in that circumstance.\n */\n isDisabled?: boolean;\n}\n\nexport interface AriaRadioProps extends RadioProps, DOMProps, AriaLabelingProps, PressEvents {}\n\nexport interface RadioAria {\n /** Props for the label wrapper element. */\n labelProps: LabelHTMLAttributes<HTMLLabelElement>;\n /** Props for the input element. */\n inputProps: InputHTMLAttributes<HTMLInputElement>;\n /** Props for the checkbox description element, if any. */\n descriptionProps: DOMAttributesWithRef<HTMLElement>;\n /** Whether the radio is disabled. */\n isDisabled: boolean;\n /** Whether the radio is currently selected. */\n isSelected: boolean;\n /** Whether the radio is in a pressed state. */\n isPressed: boolean;\n}\n\n/**\n * Provides the behavior and accessibility implementation for an individual\n * radio button in a radio group.\n *\n * @param props - Props for the radio.\n * @param state - State for the radio group, as returned by `useRadioGroupState`.\n * @param ref - Ref to the HTML input element.\n */\nexport function useRadio(\n props: AriaRadioProps,\n state: RadioGroupState,\n ref: RefObject<HTMLInputElement | null>\n): RadioAria {\n let {\n value,\n children,\n 'aria-label': ariaLabel,\n 'aria-labelledby': ariaLabelledby,\n onPressStart,\n onPressEnd,\n onPressChange,\n onPress,\n onPressUp,\n onClick\n } = props;\n\n const isDisabled = props.isDisabled || state.isDisabled;\n\n let hasChildren = children != null;\n let hasAriaLabel = ariaLabel != null || ariaLabelledby != null;\n if (!hasChildren && !hasAriaLabel && process.env.NODE_ENV !== 'production') {\n console.warn(\n 'If you do not provide children, you must specify an aria-label for accessibility'\n );\n }\n\n let checked = state.selectedValue === value;\n\n let onChange = e => {\n e.stopPropagation();\n state.setSelectedValue(value);\n };\n\n // Handle press state for keyboard interactions and cases where labelProps is not used.\n let {pressProps, isPressed} = usePress({\n onPressStart,\n onPressEnd,\n onPressChange,\n onPress,\n onPressUp,\n onClick,\n isDisabled\n });\n\n // Handle press state on the label.\n let {pressProps: labelProps, isPressed: isLabelPressed} = usePress({\n onPressStart,\n onPressEnd,\n onPressChange,\n onPressUp,\n onClick,\n isDisabled,\n onPress(e) {\n onPress?.(e);\n state.setSelectedValue(value);\n ref.current?.focus();\n }\n });\n\n let {focusableProps} = useFocusable(\n mergeProps(props, {\n onFocus: () => state.setLastFocusedValue(value)\n }),\n ref\n );\n let interactions = mergeProps(pressProps, focusableProps);\n let domProps = filterDOMProps(props, {labelable: true});\n let tabIndex: number | undefined = -1;\n if (state.selectedValue != null) {\n if (state.selectedValue === value) {\n tabIndex = 0;\n }\n } else if (state.lastFocusedValue === value || state.lastFocusedValue == null) {\n tabIndex = 0;\n }\n if (isDisabled) {\n tabIndex = undefined;\n }\n\n let {name, form, descriptionId, errorMessageId, validationBehavior} = radioGroupData.get(state)!;\n useFormReset(ref, state.defaultSelectedValue, state.setSelectedValue);\n useFormValidation({validationBehavior}, state, ref);\n\n let descriptionProps = useSlotId2();\n\n return {\n labelProps: mergeProps(\n labelProps,\n useMemo(\n () => ({\n onClick: e => e.preventDefault(),\n\n // Prevent label from being focused when mouse down on it.\n // Note, this does not prevent the input from being focused in the `click` event.\n onMouseDown: e => e.preventDefault()\n }),\n []\n )\n ),\n inputProps: mergeProps(domProps, {\n ...interactions,\n type: 'radio',\n name,\n form,\n tabIndex,\n disabled: isDisabled,\n required: state.isRequired && validationBehavior === 'native',\n checked,\n value,\n onChange,\n 'aria-describedby':\n [\n props['aria-describedby'],\n descriptionProps.id,\n state.isInvalid ? errorMessageId : null,\n descriptionId\n ]\n .filter(Boolean)\n .join(' ') || undefined\n }),\n descriptionProps,\n isDisabled,\n isSelected: checked,\n isPressed: isPressed || isLabelPressed\n };\n}\n"],"names":[],"version":3,"file":"useRadio.cjs.map"}