@wordpress/components
Version:
UI components for WordPress.
91 lines (84 loc) • 2.35 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import { createElement } from "@wordpress/element";
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
/**
* Internal dependencies
*/
import BaseControl from '../base-control';
import { VStack } from '../v-stack';
/**
* 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 ) }
* />
* );
* };
* ```
*/
export function RadioControl(props) {
const {
label,
className,
selected,
help,
onChange,
hideLabelFromVision,
options = [],
...additionalProps
} = props;
const instanceId = useInstanceId(RadioControl);
const id = `inspector-radio-control-${instanceId}`;
const onChangeValue = event => onChange(event.target.value);
if (!(options !== null && options !== void 0 && options.length)) {
return null;
}
return createElement(BaseControl, {
__nextHasNoMarginBottom: true,
label: label,
id: id,
hideLabelFromVision: hideLabelFromVision,
help: help,
className: classnames(className, 'components-radio-control')
}, createElement(VStack, {
spacing: 1
}, options.map((option, index) => createElement("div", {
key: `${id}-${index}`,
className: "components-radio-control__option"
}, createElement("input", _extends({
id: `${id}-${index}`,
className: "components-radio-control__input",
type: "radio",
name: id,
value: option.value,
onChange: onChangeValue,
checked: option.value === selected,
"aria-describedby": !!help ? `${id}__help` : undefined
}, additionalProps)), createElement("label", {
htmlFor: `${id}-${index}`
}, option.label)))));
}
export default RadioControl;
//# sourceMappingURL=index.js.map