@base-ui-components/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
188 lines (186 loc) • 6.14 kB
JavaScript
'use client';
import * as React from 'react';
import { useMergedRefs } from '@base-ui-components/utils/useMergedRefs';
import { useIsoLayoutEffect } from '@base-ui-components/utils/useIsoLayoutEffect';
import { visuallyHidden } from '@base-ui-components/utils/visuallyHidden';
import { createChangeEventDetails } from "../../utils/createBaseUIEventDetails.js";
import { REASONS } from "../../utils/reasons.js";
import { EMPTY_OBJECT } from "../../utils/constants.js";
import { NOOP } from "../../utils/noop.js";
import { stateAttributesMapping } from "../utils/stateAttributesMapping.js";
import { useBaseUiId } from "../../utils/useBaseUiId.js";
import { useRenderElement } from "../../utils/useRenderElement.js";
import { useButton } from "../../use-button/index.js";
import { ACTIVE_COMPOSITE_ITEM } from "../../composite/constants.js";
import { CompositeItem } from "../../composite/item/CompositeItem.js";
import { useFieldRootContext } from "../../field/root/FieldRootContext.js";
import { useFieldItemContext } from "../../field/item/FieldItemContext.js";
import { useLabelableContext } from "../../labelable-provider/LabelableContext.js";
import { useLabelableId } from "../../labelable-provider/useLabelableId.js";
import { useRadioGroupContext } from "../../radio-group/RadioGroupContext.js";
import { RadioRootContext } from "./RadioRootContext.js";
/**
* Represents the radio button itself.
* Renders a `<span>` element and a hidden `<input>` beside.
*
* Documentation: [Base UI Radio](https://base-ui.com/react/components/radio)
*/
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export const RadioRoot = /*#__PURE__*/React.forwardRef(function RadioRoot(componentProps, forwardedRef) {
const {
render,
className,
disabled: disabledProp = false,
readOnly: readOnlyProp = false,
required: requiredProp = false,
value,
inputRef: inputRefProp,
nativeButton = false,
id: idProp,
...elementProps
} = componentProps;
const {
disabled: disabledGroup,
readOnly: readOnlyGroup,
required: requiredGroup,
checkedValue,
setCheckedValue,
touched,
setTouched,
validation,
registerControlRef
} = useRadioGroupContext();
const {
setDirty,
validityData,
setTouched: setFieldTouched,
setFilled,
state: fieldState,
disabled: fieldDisabled
} = useFieldRootContext();
const fieldItemContext = useFieldItemContext();
const {
labelId,
getDescriptionProps
} = useLabelableContext();
const disabled = fieldDisabled || fieldItemContext.disabled || disabledGroup || disabledProp;
const readOnly = readOnlyGroup || readOnlyProp;
const required = requiredGroup || requiredProp;
const checked = checkedValue === value;
const radioRef = React.useRef(null);
const inputRef = React.useRef(null);
const mergedInputRef = useMergedRefs(inputRefProp, inputRef);
useIsoLayoutEffect(() => {
if (inputRef.current?.checked) {
setFilled(true);
}
}, [setFilled]);
const id = useBaseUiId();
const inputId = useLabelableId({
id: idProp,
implicit: false,
controlRef: radioRef
});
const rootProps = {
role: 'radio',
'aria-checked': checked,
'aria-required': required || undefined,
'aria-disabled': disabled || undefined,
'aria-readonly': readOnly || undefined,
'aria-labelledby': labelId,
[ACTIVE_COMPOSITE_ITEM]: checked ? '' : undefined,
id,
disabled,
onKeyDown(event) {
if (event.key === 'Enter') {
event.preventDefault();
}
},
onClick(event) {
if (event.defaultPrevented || disabled || readOnly) {
return;
}
event.preventDefault();
inputRef.current?.click();
},
onFocus(event) {
if (event.defaultPrevented || disabled || readOnly || !touched) {
return;
}
inputRef.current?.click();
setTouched(false);
}
};
const {
getButtonProps,
buttonRef
} = useButton({
disabled,
native: nativeButton
});
const inputProps = React.useMemo(() => ({
type: 'radio',
ref: mergedInputRef,
id: inputId,
tabIndex: -1,
style: visuallyHidden,
'aria-hidden': true,
disabled,
checked,
required,
readOnly,
onChange(event) {
// Workaround for https://github.com/facebook/react/issues/9023
if (event.nativeEvent.defaultPrevented) {
return;
}
if (disabled || readOnly || value === undefined) {
return;
}
const details = createChangeEventDetails(REASONS.none, event.nativeEvent);
if (details.isCanceled) {
return;
}
setFieldTouched(true);
setDirty(value !== validityData.initialValue);
setFilled(true);
setCheckedValue(value, details);
},
onFocus() {
radioRef.current?.focus();
}
}), [checked, disabled, inputId, mergedInputRef, readOnly, required, setCheckedValue, setDirty, setFieldTouched, setFilled, validityData.initialValue, value]);
const state = React.useMemo(() => ({
...fieldState,
required,
disabled,
readOnly,
checked
}), [fieldState, disabled, readOnly, checked, required]);
const contextValue = React.useMemo(() => state, [state]);
const isRadioGroup = setCheckedValue !== NOOP;
const refs = [forwardedRef, registerControlRef, radioRef, buttonRef];
const props = [rootProps, getDescriptionProps, validation?.getValidationProps ?? EMPTY_OBJECT, elementProps, getButtonProps];
const element = useRenderElement('span', componentProps, {
enabled: !isRadioGroup,
state,
ref: refs,
props,
stateAttributesMapping
});
return /*#__PURE__*/_jsxs(RadioRootContext.Provider, {
value: contextValue,
children: [isRadioGroup ? /*#__PURE__*/_jsx(CompositeItem, {
tag: "span",
render: render,
className: className,
state: state,
refs: refs,
props: props,
stateAttributesMapping: stateAttributesMapping
}) : element, /*#__PURE__*/_jsx("input", {
...inputProps
})]
});
});
if (process.env.NODE_ENV !== "production") RadioRoot.displayName = "RadioRoot";