@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
104 lines (100 loc) • 3.75 kB
JavaScript
"use client";
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
const require_dom = require('../../utils/dom.cjs');
const require_ref = require('../../utils/ref.cjs');
const require_utils_index = require('../../utils/index.cjs');
const require_hooks_use_controllable_state_index = require('../../hooks/use-controllable-state/index.cjs');
const require_use_field_props = require('../field/use-field-props.cjs');
const require_use_radio_group = require('./use-radio-group.cjs');
let react = require("react");
react = require_rolldown_runtime.__toESM(react);
//#region src/components/radio/use-radio.ts
const useRadio = ({ "aria-describedby": ariaDescribedbyProp,...props } = {}) => {
const group = require_use_radio_group.useRadioGroupContext();
const { name: groupName, value: groupValue, getInputProps: getGroupInputProps, getLabelProps } = group ?? {};
const uuid = (0, react.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 } = require_use_field_props.useFieldProps({
id: group ? uuid : void 0,
...props,
notSupportReadOnly: true
});
const interactive = !(readOnly || disabled);
const resolvedAriaDescribedby = group ? ariaDescribedbyProp : ariaDescribedby;
const [checked, setChecked] = require_hooks_use_controllable_state_index.useControllableEventState({
defaultValue: defaultChecked,
value: !(0, require_utils_index.utils_exports.isUndefined)(groupValue) && !(0, require_utils_index.utils_exports.isUndefined)(value) ? groupValue === value : checkedProp,
onChange: onChangeProp
});
const onChange = (0, react.useCallback)((ev) => {
if (!interactive) return ev.defaultPrevented = true;
setChecked(ev);
}, [setChecked, interactive]);
const getRootProps = (0, react.useCallback)((props$1 = {}) => {
const sharedProps = {
...dataProps,
"data-checked": (0, require_utils_index.utils_exports.dataAttr)(checked),
...rest,
...props$1
};
return getLabelProps?.(sharedProps) ?? sharedProps;
}, [
dataProps,
getLabelProps,
checked,
rest
]);
const getInputProps = (0, react.useCallback)(({ "aria-describedby": ariaDescribedby$1,...props$1 } = {}) => {
const sharedProps = {
...dataProps,
...ariaProps,
id,
type: "radio",
name,
style: require_dom.visuallyHiddenAttributes.style,
"aria-checked": checked,
"aria-describedby": (0, require_utils_index.utils_exports.cx)(resolvedAriaDescribedby, ariaDescribedby$1),
"data-checked": (0, require_utils_index.utils_exports.dataAttr)(checked),
checked,
disabled,
readOnly,
required,
value,
...props$1,
ref: require_ref.mergeRefs(props$1.ref, ref),
onBlur: (0, require_utils_index.utils_exports.handlerAll)(props$1.onBlur, eventProps.onBlur),
onChange: (0, require_utils_index.utils_exports.handlerAll)(props$1.onChange, onChange),
onFocus: (0, require_utils_index.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: (0, react.useCallback)((props$1 = {}) => ({
...dataProps,
"aria-hidden": "true",
"data-checked": (0, require_utils_index.utils_exports.dataAttr)(checked),
...props$1
}), [dataProps, checked]),
getInputProps,
getRootProps,
onChange
};
};
//#endregion
exports.useRadio = useRadio;
//# sourceMappingURL=use-radio.cjs.map