2022-12-06 17:23:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { withInstanceId } from '@wordpress/compose';
|
2024-04-10 09:54:05 +00:00
|
|
|
import { useMemo } from '@wordpress/element';
|
2022-12-06 17:23:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-10-23 14:59:19 +00:00
|
|
|
import { RadioControlOption } from '../radio-control';
|
2022-12-06 17:23:46 +00:00
|
|
|
|
2023-11-13 17:12:46 +00:00
|
|
|
export interface RadioControlAccordionProps {
|
2022-12-06 17:23:46 +00:00
|
|
|
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;
|
2024-04-10 09:54:05 +00:00
|
|
|
// Should the selected option be highlighted with a border?
|
|
|
|
highlightChecked?: boolean;
|
2022-12-06 17:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const RadioControlAccordion = ( {
|
|
|
|
className,
|
|
|
|
instanceId,
|
|
|
|
id,
|
|
|
|
selected,
|
|
|
|
onChange,
|
|
|
|
options = [],
|
2024-04-10 09:54:05 +00:00
|
|
|
highlightChecked = false,
|
2022-12-06 17:23:46 +00:00
|
|
|
}: RadioControlAccordionProps ): JSX.Element | null => {
|
|
|
|
const radioControlId = id || instanceId;
|
|
|
|
|
2024-04-10 09:54:05 +00:00
|
|
|
const selectedOptionNumber = useMemo( () => {
|
|
|
|
return options.findIndex( ( option ) => option.value === selected );
|
|
|
|
}, [ options, selected ] );
|
|
|
|
|
2022-12-06 17:23:46 +00:00
|
|
|
if ( ! options.length ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={ classnames(
|
|
|
|
'wc-block-components-radio-control',
|
2024-04-10 09:54:05 +00:00
|
|
|
{
|
|
|
|
'wc-block-components-radio-control--highlight-checked':
|
|
|
|
highlightChecked,
|
|
|
|
'wc-block-components-radio-control--highlight-checked--first-selected':
|
|
|
|
highlightChecked && selectedOptionNumber === 0,
|
|
|
|
'wc-block-components-radio-control--highlight-checked--last-selected':
|
|
|
|
highlightChecked &&
|
|
|
|
selectedOptionNumber === options.length - 1,
|
|
|
|
},
|
2022-12-06 17:23:46 +00:00
|
|
|
className
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
{ options.map( ( option ) => {
|
|
|
|
const hasOptionContent =
|
|
|
|
typeof option === 'object' && 'content' in option;
|
|
|
|
const checked = option.value === selected;
|
|
|
|
return (
|
|
|
|
<div
|
2024-04-10 09:54:05 +00:00
|
|
|
className={ classnames(
|
|
|
|
'wc-block-components-radio-control-accordion-option',
|
|
|
|
{
|
|
|
|
'wc-block-components-radio-control-accordion-option--checked-option-highlighted':
|
|
|
|
checked && highlightChecked,
|
|
|
|
}
|
|
|
|
) }
|
2022-12-06 17:23:46 +00:00
|
|
|
key={ option.value }
|
|
|
|
>
|
|
|
|
<RadioControlOption
|
|
|
|
name={ `radio-control-${ radioControlId }` }
|
|
|
|
checked={ checked }
|
|
|
|
option={ option }
|
|
|
|
onChange={ ( value ) => {
|
|
|
|
onChange( value );
|
|
|
|
if ( typeof option.onChange === 'function' ) {
|
|
|
|
option.onChange( value );
|
|
|
|
}
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
{ hasOptionContent && checked && (
|
|
|
|
<div
|
|
|
|
className={ classnames(
|
|
|
|
'wc-block-components-radio-control-accordion-content',
|
|
|
|
{
|
|
|
|
'wc-block-components-radio-control-accordion-content-hide':
|
|
|
|
! checked,
|
|
|
|
}
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
{ option.content }
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default withInstanceId( RadioControlAccordion );
|
|
|
|
export { RadioControlAccordion };
|