♻️ Improve the RadioButton component

This commit is contained in:
Philipp Stracker 2025-01-23 18:20:25 +01:00
parent 60cedb3fe0
commit 34b8e2de90
No known key found for this signature in database

View file

@ -1,16 +1,39 @@
const RadioButton = ( { id, name, value, currentValue, handleRdbState } ) => {
import { useCallback } from '@wordpress/element';
const RadioButton = ( {
id,
name,
value,
currentValue,
checked = null,
onChange,
handleRdbState,
} ) => {
const handleChange = useCallback( () => {
if ( onChange ) {
onChange( value );
} else if ( handleRdbState ) {
console.warn(
'Deprecated prop, use "onChange" instead of "handleRdbState"'
);
handleRdbState( value );
}
}, [ handleRdbState, onChange, value ] );
const radioProps = {
className: 'ppcp-r__radio-value',
type: 'radio',
id,
name,
value,
onChange: handleChange,
checked: null === checked ? value === currentValue : checked,
};
return (
<div className="ppcp-r__radio">
{ /* todo: Can we remove the wrapper div? */ }
<input
className="ppcp-r__radio-value"
type="radio"
id={ id }
checked={ value === currentValue }
name={ name }
value={ value }
onChange={ () => handleRdbState( value ) }
/>
<input { ...radioProps } />
<span className="ppcp-r__radio-presentation"></span>
</div>
);