UNPKG

@wordpress/components

Version:
105 lines (104 loc) • 3.6 kB
import clsx from "clsx"; import { useInstanceId } from "@wordpress/compose"; import { Stack, Text, VisuallyHidden } from "@wordpress/ui"; import BaseControl from "../base-control/index.mjs"; import { StyledHelp } from "../base-control/styles/base-control-styles.mjs"; import { jsx, jsxs } from "react/jsx-runtime"; // packages/components/src/radio-control/index.tsx function generateOptionDescriptionId(radioGroupId, index) { return `${radioGroupId}-${index}-option-description`; } function generateOptionId(radioGroupId, index) { return `${radioGroupId}-${index}`; } function generateHelpId(radioGroupId) { return `${radioGroupId}__help`; } /** * Render a user interface to select the user type using radio inputs. * * ```jsx * import { RadioControl } from '@wordpress/components'; * import { useState } from '@wordpress/element'; * * const MyRadioControl = () => { * const [ option, setOption ] = useState( 'a' ); * * return ( * <RadioControl * label="User type" * help="The type of the current user" * selected={ option } * options={ [ * { label: 'Author', value: 'a' }, * { label: 'Editor', value: 'e' }, * ] } * onChange={ ( value ) => setOption( value ) } * /> * ); * }; * ``` */ function RadioControl(props) { const { label, className, selected, help, onChange, onClick, hideLabelFromVision, disabled, 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, role: "radiogroup", className: clsx(className, "components-radio-control"), disabled, "aria-describedby": !!help ? generateHelpId(id) : void 0, children: [hideLabelFromVision ? /* @__PURE__ */ jsx(VisuallyHidden, { render: /* @__PURE__ */ jsx("legend", {}), children: label }) : /* @__PURE__ */ jsx(BaseControl.VisualLabel, { as: "legend", children: label }), /* @__PURE__ */ jsxs(Stack, { direction: "column", gap: "md", align: "stretch", className: clsx("components-radio-control__group-wrapper", { "has-help": !!help }), children: [!!help && /* @__PURE__ */ jsx(Text, { id: generateHelpId(id), variant: "body-sm", className: "components-radio-control__help", children: help }), 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, disabled: option.disabled, 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, { id: generateOptionDescriptionId(id, index), className: "components-radio-control__option-description", children: option.description }) : null ] }, generateOptionId(id, index)))] })] }); } export { RadioControl, RadioControl as default }; //# sourceMappingURL=index.mjs.map