2023-09-06 06:21:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-05-31 03:49:36 +00:00
|
|
|
import clsx from 'clsx';
|
2023-09-06 06:21:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './choice.scss';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
className?: string;
|
|
|
|
selected: boolean;
|
|
|
|
title: string;
|
|
|
|
subtitle?: string;
|
|
|
|
name: string;
|
|
|
|
value: string;
|
|
|
|
onChange: ( value: string ) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Choice = ( {
|
|
|
|
className,
|
|
|
|
selected,
|
|
|
|
title,
|
|
|
|
subtitle,
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
}: Props ) => {
|
|
|
|
const changeHandler = () => {
|
|
|
|
onChange( value );
|
|
|
|
};
|
|
|
|
const inputId = 'woocommerce-' + value.replace( /_/g, '-' );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
role="radio"
|
2024-05-31 03:49:36 +00:00
|
|
|
className={ clsx( 'woocommerce-cys-choice-container', className ) }
|
2023-09-06 06:21:09 +00:00
|
|
|
onClick={ changeHandler }
|
|
|
|
onKeyDown={ ( e ) => {
|
|
|
|
if ( e.key === 'Enter' ) {
|
|
|
|
changeHandler();
|
|
|
|
}
|
|
|
|
} }
|
|
|
|
data-selected={ selected ? selected : null }
|
|
|
|
tabIndex={ 0 }
|
|
|
|
>
|
|
|
|
<div className="woocommerce-cys-choice">
|
|
|
|
<input
|
|
|
|
className="woocommerce-cys-choice-input"
|
|
|
|
id={ inputId }
|
|
|
|
name={ name }
|
|
|
|
type="radio"
|
|
|
|
value={ value }
|
|
|
|
checked={ !! selected }
|
|
|
|
onChange={ changeHandler }
|
|
|
|
data-selected={ selected ? selected : null }
|
|
|
|
// Stop the input from being focused when the parent div is clicked
|
|
|
|
tabIndex={ -1 }
|
|
|
|
></input>
|
|
|
|
<label htmlFor={ inputId } className="choice__title">
|
|
|
|
{ title }
|
|
|
|
</label>
|
|
|
|
{ subtitle && <p className="choice__subtitle">{ subtitle }</p> }
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|