/** * External dependencies */ import clsx from 'clsx'; import { withInstanceId } from '@wordpress/compose'; import { useMemo } from '@wordpress/element'; /** * Internal dependencies */ import { RadioControlOption } from '../radio-control'; export interface RadioControlAccordionProps { className?: string; instanceId: number; id: string; onChange: ( value: string ) => void; options: Array< { value: string; label: string | JSX.Element; onChange?: ( value: string ) => void; name: string; content: JSX.Element; } >; selected: string | null; // Should the selected option be highlighted with a border? highlightChecked?: boolean; } const RadioControlAccordion = ( { className, instanceId, id, selected, onChange, options = [], highlightChecked = false, }: RadioControlAccordionProps ): JSX.Element | null => { const radioControlId = id || instanceId; const selectedOptionNumber = useMemo( () => { return options.findIndex( ( option ) => option.value === selected ); }, [ options, selected ] ); if ( ! options.length ) { return null; } return (
{ options.map( ( option ) => { const hasOptionContent = typeof option === 'object' && 'content' in option; const checked = option.value === selected; return (
{ onChange( value ); if ( typeof option.onChange === 'function' ) { option.onChange( value ); } } } /> { hasOptionContent && checked && (
{ option.content }
) }
); } ) }
); }; export default withInstanceId( RadioControlAccordion ); export { RadioControlAccordion };