/** * WordPress dependencies */ import { BaseControl, TextControl } from '@safe-wordpress/components'; import { useInstanceId } from '@safe-wordpress/compose'; /** * External dependencies */ import clsx from 'clsx'; import { noop } from 'lodash'; import { isEmpty } from '@nab/utils'; /** * Internal dependencies */ import './style.scss'; export type RadioControlProps< TValue extends string > = { readonly className?: string; readonly disabled?: boolean; readonly extraValue?: string; readonly help?: string; readonly label?: string; readonly options: ReadonlyArray< RadioControlOption< TValue > >; readonly selected: string; readonly onChange: ( val: TValue ) => void; readonly onExtraChange?: ( val: string ) => void; }; export type RadioControlOption< TValue extends string > = { readonly value: TValue; readonly label: string; readonly extra?: string; }; export const RadioControl = < TValue extends string >( { className, disabled, extraValue, help, label, selected, onChange, onExtraChange, options = [], }: RadioControlProps< TValue > ): JSX.Element | null => { const instanceId = useInstanceId( RadioControl ); const id = `inspector-radio-control-${ instanceId }`; if ( isEmpty( options ) ) { return null; } return ( { options.map( ( option, index ) => (
onChange( event.target.value as TValue ) } checked={ option.value === selected } disabled={ disabled } aria-describedby={ !! help ? `${ id }__help` : undefined } />
{ !! option.extra && option.value === selected && (
) }
) ) }
); };