@primer/react
Version:
An implementation of GitHub's Primer Design System using React
102 lines (101 loc) • 2.93 kB
JavaScript
import { RadioGroupContext } from "../RadioGroup/RadioGroupContext.js";
import shared_module_css_default from "../Checkbox/shared.module.css.js";
import Radio_module_css_default from "./Radio.module.css.js";
import { c } from "react-compiler-runtime";
import { clsx } from "clsx";
import { jsx } from "react/jsx-runtime";
import React, { useContext } from "react";
//#region src/Radio/Radio.tsx
/**
* An accessible, native radio component for selecting one option from a list.
*/
const Radio = /*#__PURE__*/ React.forwardRef((t0, ref) => {
const $ = c(26);
let checked;
let className;
let disabled;
let nameProp;
let onChange;
let required;
let rest;
let t1;
let value;
if ($[0] !== t0) {
({checked, disabled, name: nameProp, onChange, required, value, className, "aria-hidden": t1, ...rest} = t0);
$[0] = t0;
$[1] = checked;
$[2] = className;
$[3] = disabled;
$[4] = nameProp;
$[5] = onChange;
$[6] = required;
$[7] = rest;
$[8] = t1;
$[9] = value;
} else {
checked = $[1];
className = $[2];
disabled = $[3];
nameProp = $[4];
onChange = $[5];
required = $[6];
rest = $[7];
t1 = $[8];
value = $[9];
}
const ariaHidden = t1 === void 0 ? false : t1;
const radioGroupContext = useContext(RadioGroupContext);
let t2;
if ($[10] !== onChange || $[11] !== radioGroupContext) {
t2 = (e) => {
!(radioGroupContext === null || radioGroupContext === void 0) && radioGroupContext.onChange && radioGroupContext.onChange(e);
onChange && onChange(e);
};
$[10] = onChange;
$[11] = radioGroupContext;
$[12] = t2;
} else t2 = $[12];
const handleOnChange = t2;
const name = nameProp || (radioGroupContext === null || radioGroupContext === void 0 ? void 0 : radioGroupContext.name);
if (!name && !ariaHidden) console.warn("A radio input must have a `name` attribute. Pass `name` as a prop directly to each Radio, or nest them in a `RadioGroup` component with a `name` prop");
const t3 = checked ? "true" : "false";
let t4;
if ($[13] !== className) {
t4 = clsx(className, shared_module_css_default.Input, Radio_module_css_default.Radio);
$[13] = className;
$[14] = t4;
} else t4 = $[14];
let t5;
if ($[15] !== checked || $[16] !== disabled || $[17] !== handleOnChange || $[18] !== name || $[19] !== ref || $[20] !== required || $[21] !== rest || $[22] !== t3 || $[23] !== t4 || $[24] !== value) {
t5 = /*#__PURE__*/ jsx("input", {
type: "radio",
value,
name,
ref,
disabled,
checked,
"aria-checked": t3,
required,
onChange: handleOnChange,
className: t4,
...rest,
"data-component": "Radio"
});
$[15] = checked;
$[16] = disabled;
$[17] = handleOnChange;
$[18] = name;
$[19] = ref;
$[20] = required;
$[21] = rest;
$[22] = t3;
$[23] = t4;
$[24] = value;
$[25] = t5;
} else t5 = $[25];
return t5;
});
Radio.displayName = "Radio";
Radio.__SLOT__ = Symbol("Radio");
//#endregion
export { Radio as default };