@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
102 lines (98 loc) • 3.24 kB
JavaScript
"use client";
import { visuallyHiddenAttributes } from "../../utils/dom.js";
import { mergeRefs } from "../../utils/ref.js";
import { utils_exports } from "../../utils/index.js";
import { useControllableEventState } from "../../hooks/use-controllable-state/index.js";
import { useFieldProps } from "../field/use-field-props.js";
import { useRadioGroupContext } from "./use-radio-group.js";
import { useCallback, useId } from "react";
//#region src/components/radio/use-radio.ts
const useRadio = ({ "aria-describedby": ariaDescribedbyProp,...props } = {}) => {
const group = useRadioGroupContext();
const { name: groupName, value: groupValue, getInputProps: getGroupInputProps, getLabelProps } = group ?? {};
const uuid = useId();
const { props: { id, ref, name = groupName, checked: checkedProp, defaultChecked = false, disabled, readOnly, required, value, onChange: onChangeProp,...rest }, ariaProps: { "aria-describedby": ariaDescribedby,...ariaProps }, dataProps, eventProps } = useFieldProps({
id: group ? uuid : void 0,
...props,
notSupportReadOnly: true
});
const interactive = !(readOnly || disabled);
const resolvedAriaDescribedby = group ? ariaDescribedbyProp : ariaDescribedby;
const [checked, setChecked] = useControllableEventState({
defaultValue: defaultChecked,
value: !(0, utils_exports.isUndefined)(groupValue) && !(0, utils_exports.isUndefined)(value) ? groupValue === value : checkedProp,
onChange: onChangeProp
});
const onChange = useCallback((ev) => {
if (!interactive) return ev.defaultPrevented = true;
setChecked(ev);
}, [setChecked, interactive]);
const getRootProps = useCallback((props$1 = {}) => {
const sharedProps = {
...dataProps,
"data-checked": (0, utils_exports.dataAttr)(checked),
...rest,
...props$1
};
return getLabelProps?.(sharedProps) ?? sharedProps;
}, [
dataProps,
getLabelProps,
checked,
rest
]);
const getInputProps = useCallback(({ "aria-describedby": ariaDescribedby$1,...props$1 } = {}) => {
const sharedProps = {
...dataProps,
...ariaProps,
id,
type: "radio",
name,
style: visuallyHiddenAttributes.style,
"aria-checked": checked,
"aria-describedby": (0, utils_exports.cx)(resolvedAriaDescribedby, ariaDescribedby$1),
"data-checked": (0, utils_exports.dataAttr)(checked),
checked,
disabled,
readOnly,
required,
value,
...props$1,
ref: mergeRefs(props$1.ref, ref),
onBlur: (0, utils_exports.handlerAll)(props$1.onBlur, eventProps.onBlur),
onChange: (0, utils_exports.handlerAll)(props$1.onChange, onChange),
onFocus: (0, utils_exports.handlerAll)(props$1.onFocus, eventProps.onFocus)
};
return getGroupInputProps?.(sharedProps) ?? sharedProps;
}, [
id,
name,
checked,
resolvedAriaDescribedby,
disabled,
readOnly,
required,
value,
dataProps,
ariaProps,
ref,
eventProps,
onChange,
getGroupInputProps
]);
return {
checked,
getIndicatorProps: useCallback((props$1 = {}) => ({
...dataProps,
"aria-hidden": "true",
"data-checked": (0, utils_exports.dataAttr)(checked),
...props$1
}), [dataProps, checked]),
getInputProps,
getRootProps,
onChange
};
};
//#endregion
export { useRadio };
//# sourceMappingURL=use-radio.js.map