{"version":3,"sources":["../src/use-radio-group.ts"],"sourcesContent":["import type { PropGetter } from \"@yamada-ui/core\"\nimport type { Dict } from \"@yamada-ui/utils\"\nimport type { ChangeEvent } from \"react\"\nimport { useControllableState } from \"@yamada-ui/use-controllable-state\"\nimport { isObject, mergeRefs, useCallbackRef } from \"@yamada-ui/utils\"\nimport { useCallback, useId, useRef } from \"react\"\n\nconst isEvent = (value: any): value is { target: HTMLInputElement } =>\n  value && isObject(value) && isObject(value.target)\n\nexport interface UseRadioGroupProps<Y extends number | string = string> {\n  /**\n   * The top-level id string that will be applied to the radios.\n   * The index of the radio will be appended to this top-level id.\n   */\n  id?: string\n  /**\n   * The HTML `name` attribute used for forms.\n   */\n  name?: string\n  /**\n   * The initial value of the radio group.\n   */\n  defaultValue?: Y\n  /**\n   * If `true`, input elements will receive `checked` attribute instead of `isChecked`.\n   *\n   * This assumes, you're using native radio inputs.\n   *\n   * @default false\n   *\n   * @deprecated It will be deprecated in version 2.0.\n   */\n  isNative?: boolean\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 = <\n  Y extends number | string,\n  M extends Dict = Dict,\n>({\n  id,\n  name,\n  defaultValue,\n  isNative,\n  value: valueProp,\n  onChange: onChangeProp,\n  ...props\n}: M & UseRadioGroupProps<Y>) => {\n  const uuid = useId()\n\n  id ??= uuid\n  name ??= `radio-${id}`\n\n  const containerRef = useRef<any>(null)\n  const onChangeRef = useCallbackRef(onChangeProp)\n  const [value, setValue] = useControllableState({\n    defaultValue,\n    value: valueProp,\n    onChange: onChangeRef,\n  })\n\n  const onFocus = useCallback(() => {\n    const container = containerRef.current\n\n    if (!container) return\n\n    let query = `input:not(:disabled):checked`\n\n    let firstInput = container.querySelector(query) as HTMLElement | undefined\n\n    if (firstInput) {\n      firstInput.focus()\n    } else {\n      query = `input:not(:disabled)`\n      firstInput = container.querySelector(query) as HTMLElement | undefined\n\n      firstInput?.focus()\n    }\n  }, [])\n\n  const onChange = useCallback(\n    (evOrValue: ChangeEvent<HTMLInputElement> | Y) => {\n      const nextValue = (\n        isEvent(evOrValue) ? evOrValue.target.value : evOrValue\n      ) as Y\n\n      setValue(nextValue)\n    },\n    [setValue],\n  )\n\n  const getContainerProps: PropGetter = useCallback(\n    (props = {}, ref = null) => ({\n      role: \"radiogroup\",\n      ...props,\n      ref: mergeRefs(ref, containerRef),\n    }),\n    [],\n  )\n\n  const getRadioProps: PropGetter<\n    { value?: Y },\n    {\n      checked?: boolean\n      isChecked?: boolean\n      value?: Y\n    }\n  > = useCallback(\n    (props = {}, ref = null) => {\n      const checked = props.value === value\n\n      return {\n        ...props,\n        ref,\n        name,\n        \"aria-checked\": checked,\n        [isNative ? \"checked\" : \"isChecked\"]:\n          // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n          value != null ? checked : undefined,\n        // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n        ...(!isNative ? { checked: value != null ? checked : undefined } : {}),\n        onChange,\n      }\n    },\n    [name, value, onChange, isNative],\n  )\n\n  return {\n    id,\n    name,\n    props,\n    setValue,\n    value,\n    getContainerProps,\n    getRadioProps,\n    onChange,\n    onFocus,\n  }\n}\n\nexport type UseRadioGroupReturn<Y extends number | string = string> =\n  ReturnType<typeof useRadioGroup<Y>>\n"],"mappings":";;;AAGA,SAAS,4BAA4B;AACrC,SAAS,UAAU,WAAW,sBAAsB;AACpD,SAAS,aAAa,OAAO,cAAc;AAE3C,IAAM,UAAU,CAAC,UACf,SAAS,SAAS,KAAK,KAAK,SAAS,MAAM,MAAM;AAoC5C,IAAM,gBAAgB,CAG3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,OAAO;AAAA,EACP,UAAU;AAAA,EACV,GAAG;AACL,MAAiC;AAC/B,QAAM,OAAO,MAAM;AAEnB,yBAAO;AACP,+BAAS,SAAS,EAAE;AAEpB,QAAM,eAAe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