{"version":3,"file":"use-radio-group.cjs","names":["createContext","useFieldProps","useControllableState","getRootProps: PropGetter","props","mergeRefs","visuallyHiddenAttributes"],"sources":["../../../../src/components/radio/use-radio-group.ts"],"sourcesContent":["\"use client\"\n\nimport type { ChangeEvent } from \"react\"\nimport type { HTMLProps, PropGetter } from \"../../core\"\nimport type { FieldProps } from \"../field\"\nimport { useCallback, useId } from \"react\"\nimport { useControllableState } from \"../../hooks/use-controllable-state\"\nimport {\n  createContext,\n  cx,\n  dataAttr,\n  handlerAll,\n  isObject,\n  isUndefined,\n  mergeRefs,\n  visuallyHiddenAttributes,\n} from \"../../utils\"\nimport { useFieldProps } from \"../field\"\n\ninterface RadioGroupContext extends Omit<UseRadioGroupReturn, \"getRootProps\"> {}\n\nconst [RadioGroupContext, useRadioGroupContext] =\n  createContext<RadioGroupContext>({\n    name: \"RadioGroupContext\",\n    strict: false,\n  })\n\nexport { RadioGroupContext, useRadioGroupContext }\n\nexport interface UseRadioGroupProps<Y extends string = string>\n  extends Omit<HTMLProps, \"defaultValue\" | \"onChange\" | \"value\">,\n    FieldProps {\n  /**\n   * The initial value of the radio group.\n   */\n  defaultValue?: Y\n  /**\n   * The value of the radio group.\n   */\n  value?: Y\n  /**\n   * The callback fired when any children radio is checked or unchecked.\n   */\n  onChange?: (value: Y) => void\n}\n\nexport const useRadioGroup = <Y extends string = string>(\n  props: UseRadioGroupProps<Y> = {},\n) => {\n  const uuid = useId()\n  const {\n    context: { labelId } = {},\n    props: {\n      id = uuid,\n      defaultValue,\n      disabled,\n      readOnly,\n      required,\n      value: valueProp,\n      onChange: onChangeProp,\n      ...rest\n    },\n    ariaProps: { \"aria-describedby\": ariaDescribedbyProp, ...ariaProps },\n    dataProps,\n    eventProps,\n  } = useFieldProps(props)\n  const interactive = !(readOnly || disabled)\n  const [value, setValue] = useControllableState({\n    defaultValue,\n    value: valueProp,\n    onChange: onChangeProp,\n  })\n\n  const onChange = useCallback(\n    (valueOrEv: ChangeEvent<HTMLInputElement> | Y) => {\n      if (!interactive) return\n\n      if (isObject(valueOrEv)) {\n        setValue(valueOrEv.target.value as Y)\n      } else {\n        setValue(valueOrEv)\n      }\n    },\n    [interactive, setValue],\n  )\n\n  const getRootProps: PropGetter = useCallback(\n    ({\n      ref,\n      \"aria-describedby\": ariaDescribedby,\n      \"aria-labelledby\": ariaLabelledby,\n      ...props\n    } = {}) => ({\n      ...dataProps,\n      id,\n      \"aria-describedby\": cx(ariaDescribedbyProp, ariaDescribedby),\n      \"aria-labelledby\": cx(labelId, ariaLabelledby),\n      role: \"radiogroup\",\n      ...rest,\n      ...props,\n      ref: mergeRefs(ref, rest.ref),\n    }),\n    [ariaDescribedbyProp, dataProps, id, labelId, rest],\n  )\n\n  const getInputProps: PropGetter<\"input\"> = useCallback(\n    (props = {}) => {\n      const checked = !isUndefined(value) && value === props.value\n\n      return {\n        ...dataProps,\n        ...ariaProps,\n        type: \"radio\",\n        name: id,\n        style: visuallyHiddenAttributes.style,\n        \"aria-checked\": checked,\n        \"data-checked\": dataAttr(checked),\n        checked,\n        disabled,\n        readOnly,\n        required,\n        ...props,\n        onBlur: handlerAll(props.onBlur, eventProps.onBlur),\n        onChange: handlerAll(props.onChange, onChange),\n        onFocus: handlerAll(props.onFocus, eventProps.onFocus),\n      }\n    },\n    [\n      ariaProps,\n      dataProps,\n      disabled,\n      eventProps,\n      id,\n      onChange,\n      readOnly,\n      required,\n      value,\n    ],\n  )\n\n  const getLabelProps: PropGetter<\"label\"> = useCallback(\n    (props) => ({\n      ...dataProps,\n      ...props,\n    }),\n    [dataProps],\n  )\n\n  return {\n    name: id,\n    value,\n    getInputProps,\n    getLabelProps,\n    getRootProps,\n    onChange,\n  }\n}\n\nexport type UseRadioGroupReturn = ReturnType<typeof useRadioGroup>\n"],"mappings":";;;;;;;;;;;;;;AAqBA,MAAM,CAAC,mBAAmB,wBACxBA,8BAAiC;CAC/B,MAAM;CACN,QAAQ;CACT,CAAC;AAqBJ,MAAa,iBACX,QAA+B,EAA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