UNPKG

@wordpress/components

Version:
92 lines (91 loc) 3.19 kB
// packages/components/src/radio-control/index.tsx import clsx from "clsx"; import { useInstanceId } from "@wordpress/compose"; import BaseControl from "../base-control"; import { VStack } from "../v-stack"; import { StyledHelp } from "../base-control/styles/base-control-styles"; import { VisuallyHidden } from "../visually-hidden"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; function generateOptionDescriptionId(radioGroupId, index) { return `${radioGroupId}-${index}-option-description`; } function generateOptionId(radioGroupId, index) { return `${radioGroupId}-${index}`; } function generateHelpId(radioGroupId) { return `${radioGroupId}__help`; } function RadioControl(props) { const { label, className, selected, help, onChange, onClick, hideLabelFromVision, options = [], id: preferredId, ...additionalProps } = props; const id = useInstanceId(RadioControl, "inspector-radio-control", preferredId); const onChangeValue = (event) => onChange(event.target.value); if (!options?.length) { return null; } return /* @__PURE__ */ _jsxs("fieldset", { id, className: clsx(className, "components-radio-control"), "aria-describedby": !!help ? generateHelpId(id) : void 0, children: [hideLabelFromVision ? /* @__PURE__ */ _jsx(VisuallyHidden, { as: "legend", children: label }) : /* @__PURE__ */ _jsx(BaseControl.VisualLabel, { as: "legend", children: label }), /* @__PURE__ */ _jsx(VStack, { spacing: 3, className: clsx("components-radio-control__group-wrapper", { "has-help": !!help }), children: options.map((option, index) => /* @__PURE__ */ _jsxs("div", { className: "components-radio-control__option", children: [/* @__PURE__ */ _jsx("input", { id: generateOptionId(id, index), className: "components-radio-control__input", type: "radio", name: id, value: option.value, onChange: onChangeValue, checked: option.value === selected, "aria-describedby": !!option.description ? generateOptionDescriptionId(id, index) : void 0, onClick: (event) => { event.currentTarget.focus(); onClick?.(event); }, ...additionalProps }), /* @__PURE__ */ _jsx("label", { className: "components-radio-control__label", htmlFor: generateOptionId(id, index), children: option.label }), !!option.description ? /* @__PURE__ */ _jsx(StyledHelp, { __nextHasNoMarginBottom: true, id: generateOptionDescriptionId(id, index), className: "components-radio-control__option-description", children: option.description }) : null] }, generateOptionId(id, index))) }), !!help && /* @__PURE__ */ _jsx(StyledHelp, { __nextHasNoMarginBottom: true, id: generateHelpId(id), className: "components-base-control__help", children: help })] }); } var radio_control_default = RadioControl; export { RadioControl, radio_control_default as default }; //# sourceMappingURL=index.js.map