UNPKG

@wordpress/components

Version:
1 lines • 6.23 kB
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/radio-control/index.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport type { ChangeEvent } from 'react';\nimport { useInstanceId } from '@wordpress/compose';\nimport { Stack, Text, VisuallyHidden } from '@wordpress/ui';\nimport BaseControl from '../base-control';\nimport type { WordPressComponentProps } from '../context';\nimport type { RadioControlProps } from './types';\nimport { StyledHelp } from '../base-control/styles/base-control-styles';\n\nfunction generateOptionDescriptionId( radioGroupId: string, index: number ) {\n\treturn `${ radioGroupId }-${ index }-option-description`;\n}\n\nfunction generateOptionId( radioGroupId: string, index: number ) {\n\treturn `${ radioGroupId }-${ index }`;\n}\n\nfunction generateHelpId( radioGroupId: string ) {\n\treturn `${ radioGroupId }__help`;\n}\n\n/**\n * Render a user interface to select the user type using radio inputs.\n *\n * ```jsx\n * import { RadioControl } from '@wordpress/components';\n * import { useState } from '@wordpress/element';\n *\n * const MyRadioControl = () => {\n * const [ option, setOption ] = useState( 'a' );\n *\n * return (\n * <RadioControl\n * label=\"User type\"\n * help=\"The type of the current user\"\n * selected={ option }\n * options={ [\n * { label: 'Author', value: 'a' },\n * { label: 'Editor', value: 'e' },\n * ] }\n * onChange={ ( value ) => setOption( value ) }\n * />\n * );\n * };\n * ```\n */\nexport function RadioControl(\n\tprops: WordPressComponentProps< RadioControlProps, 'input', false >\n) {\n\tconst {\n\t\tlabel,\n\t\tclassName,\n\t\tselected,\n\t\thelp,\n\t\tonChange,\n\t\tonClick,\n\t\thideLabelFromVision,\n\t\tdisabled,\n\t\toptions = [],\n\t\tid: preferredId,\n\t\t...additionalProps\n\t} = props;\n\tconst id = useInstanceId(\n\t\tRadioControl,\n\t\t'inspector-radio-control',\n\t\tpreferredId\n\t);\n\n\tconst onChangeValue = ( event: ChangeEvent< HTMLInputElement > ) =>\n\t\tonChange( event.target.value );\n\n\tif ( ! options?.length ) {\n\t\treturn null;\n\t}\n\n\treturn (\n\t\t<fieldset\n\t\t\tid={ id }\n\t\t\trole=\"radiogroup\"\n\t\t\tclassName={ clsx( className, 'components-radio-control' ) }\n\t\t\tdisabled={ disabled }\n\t\t\taria-describedby={ !! help ? generateHelpId( id ) : undefined }\n\t\t>\n\t\t\t{ hideLabelFromVision ? (\n\t\t\t\t<VisuallyHidden render={ <legend /> }>{ label }</VisuallyHidden>\n\t\t\t) : (\n\t\t\t\t<BaseControl.VisualLabel as=\"legend\">\n\t\t\t\t\t{ label }\n\t\t\t\t</BaseControl.VisualLabel>\n\t\t\t) }\n\n\t\t\t<Stack\n\t\t\t\tdirection=\"column\"\n\t\t\t\tgap=\"md\"\n\t\t\t\talign=\"stretch\"\n\t\t\t\tclassName={ clsx( 'components-radio-control__group-wrapper', {\n\t\t\t\t\t'has-help': !! help,\n\t\t\t\t} ) }\n\t\t\t>\n\t\t\t\t{ !! help && (\n\t\t\t\t\t<Text\n\t\t\t\t\t\tid={ generateHelpId( id ) }\n\t\t\t\t\t\tvariant=\"body-sm\"\n\t\t\t\t\t\tclassName=\"components-radio-control__help\"\n\t\t\t\t\t>\n\t\t\t\t\t\t{ help }\n\t\t\t\t\t</Text>\n\t\t\t\t) }\n\t\t\t\t{ options.map( ( option, index ) => (\n\t\t\t\t\t<div\n\t\t\t\t\t\tkey={ generateOptionId( id, index ) }\n\t\t\t\t\t\tclassName=\"components-radio-control__option\"\n\t\t\t\t\t>\n\t\t\t\t\t\t<input\n\t\t\t\t\t\t\tid={ generateOptionId( id, index ) }\n\t\t\t\t\t\t\tclassName=\"components-radio-control__input\"\n\t\t\t\t\t\t\ttype=\"radio\"\n\t\t\t\t\t\t\tname={ id }\n\t\t\t\t\t\t\tvalue={ option.value }\n\t\t\t\t\t\t\tdisabled={ option.disabled }\n\t\t\t\t\t\t\tonChange={ onChangeValue }\n\t\t\t\t\t\t\tchecked={ option.value === selected }\n\t\t\t\t\t\t\taria-describedby={\n\t\t\t\t\t\t\t\t!! option.description\n\t\t\t\t\t\t\t\t\t? generateOptionDescriptionId( id, index )\n\t\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tonClick={ ( event ) => {\n\t\t\t\t\t\t\t\t// Compat code for Safari to ensure that the radio is focused when clicked.\n\t\t\t\t\t\t\t\tevent.currentTarget.focus();\n\n\t\t\t\t\t\t\t\tonClick?.( event );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t\t{ ...additionalProps }\n\t\t\t\t\t\t/>\n\t\t\t\t\t\t<label\n\t\t\t\t\t\t\tclassName=\"components-radio-control__label\"\n\t\t\t\t\t\t\thtmlFor={ generateOptionId( id, index ) }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ option.label }\n\t\t\t\t\t\t</label>\n\t\t\t\t\t\t{ !! option.description ? (\n\t\t\t\t\t\t\t<StyledHelp\n\t\t\t\t\t\t\t\tid={ generateOptionDescriptionId( id, index ) }\n\t\t\t\t\t\t\t\tclassName=\"components-radio-control__option-description\"\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t{ option.description }\n\t\t\t\t\t\t\t</StyledHelp>\n\t\t\t\t\t\t) : null }\n\t\t\t\t\t</div>\n\t\t\t\t) ) }\n\t\t\t</Stack>\n\t\t</fieldset>\n\t);\n}\n\nexport default RadioControl;\n"],"mappings":";;;;;;;AASA,SAAS,4BAA6B,cAAsB,OAAgB;CAC3E,OAAO,GAAI,aAAc,GAAI,MAAO;AACrC;AAEA,SAAS,iBAAkB,cAAsB,OAAgB;CAChE,OAAO,GAAI,aAAc,GAAI;AAC9B;AAEA,SAAS,eAAgB,cAAuB;CAC/C,OAAO,GAAI,aAAc;AAC1B;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,SAAgB,aACf,OACC;CACD,MAAM,EACL,OACA,WACA,UACA,MACA,UACA,SACA,qBACA,UACA,UAAU,CAAC,GACX,IAAI,aACJ,GAAG,oBACA;CACJ,MAAM,KAAK,cACV,cACA,2BACA,WACD;CAEA,MAAM,iBAAkB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