UNPKG

@wordpress/components

Version:
134 lines (133 loc) • 5.24 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // \0rolldown/runtime.js var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); let clsx = require("clsx"); clsx = __toESM(clsx); let _wordpress_compose = require("@wordpress/compose"); let _wordpress_ui = require("@wordpress/ui"); let ___base_control_index_tsx = require("../base-control/index.cjs"); ___base_control_index_tsx = __toESM(___base_control_index_tsx); let ___base_control_styles_base_control_styles_tsx = require("../base-control/styles/base-control-styles.cjs"); let react_jsx_runtime = require("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 = (0, _wordpress_compose.useInstanceId)(RadioControl, "inspector-radio-control", preferredId); const onChangeValue = (event) => onChange(event.target.value); if (!options?.length) return null; return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("fieldset", { id, role: "radiogroup", className: (0, clsx.default)(className, "components-radio-control"), disabled, "aria-describedby": !!help ? generateHelpId(id) : void 0, children: [hideLabelFromVision ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.VisuallyHidden, { render: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("legend", {}), children: label }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___base_control_index_tsx.default.VisualLabel, { as: "legend", children: label }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_ui.Stack, { direction: "column", gap: "md", align: "stretch", className: (0, clsx.default)("components-radio-control__group-wrapper", { "has-help": !!help }), children: [!!help && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Text, { id: generateHelpId(id), variant: "body-sm", className: "components-radio-control__help", children: help }), options.map((option, index) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { className: "components-radio-control__option", children: [ /* @__PURE__ */ (0, react_jsx_runtime.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__ */ (0, react_jsx_runtime.jsx)("label", { className: "components-radio-control__label", htmlFor: generateOptionId(id, index), children: option.label }), !!option.description ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___base_control_styles_base_control_styles_tsx.StyledHelp, { id: generateOptionDescriptionId(id, index), className: "components-radio-control__option-description", children: option.description }) : null ] }, generateOptionId(id, index)))] })] }); } exports.RadioControl = RadioControl; exports.default = RadioControl; //# sourceMappingURL=index.cjs.map