UNPKG

@wordpress/components

Version:
127 lines (122 loc) 4.2 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.RadioControl = RadioControl; exports.default = void 0; var _clsx = _interopRequireDefault(require("clsx")); var _compose = require("@wordpress/compose"); var _baseControl = _interopRequireDefault(require("../base-control")); var _vStack = require("../v-stack"); var _baseControlStyles = require("../base-control/styles/base-control-styles"); var _visuallyHidden = require("../visually-hidden"); var _jsxRuntime = require("react/jsx-runtime"); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ 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, hideLabelFromVision, options = [], id: preferredId, ...additionalProps } = props; const id = (0, _compose.useInstanceId)(RadioControl, 'inspector-radio-control', preferredId); const onChangeValue = event => onChange(event.target.value); if (!options?.length) { return null; } return /*#__PURE__*/(0, _jsxRuntime.jsxs)("fieldset", { id: id, className: (0, _clsx.default)(className, 'components-radio-control'), "aria-describedby": !!help ? generateHelpId(id) : undefined, children: [hideLabelFromVision ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_visuallyHidden.VisuallyHidden, { as: "legend", children: label }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_baseControl.default.VisualLabel, { as: "legend", children: label }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_vStack.VStack, { spacing: 3, className: (0, _clsx.default)('components-radio-control__group-wrapper', { 'has-help': !!help }), children: options.map((option, index) => /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { className: "components-radio-control__option", children: [/*#__PURE__*/(0, _jsxRuntime.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) : undefined, ...additionalProps }), /*#__PURE__*/(0, _jsxRuntime.jsx)("label", { className: "components-radio-control__label", htmlFor: generateOptionId(id, index), children: option.label }), !!option.description ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_baseControlStyles.StyledHelp, { __nextHasNoMarginBottom: true, id: generateOptionDescriptionId(id, index), className: "components-radio-control__option-description", children: option.description }) : null] }, generateOptionId(id, index))) }), !!help && /*#__PURE__*/(0, _jsxRuntime.jsx)(_baseControlStyles.StyledHelp, { __nextHasNoMarginBottom: true, id: generateHelpId(id), className: "components-base-control__help", children: help })] }); } var _default = exports.default = RadioControl; //# sourceMappingURL=index.js.map