2019-12-16 22:13:41 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classnames from 'classnames';
|
2020-03-26 12:39:54 +00:00
|
|
|
import { withInstanceId } from '@woocommerce/base-hocs/with-instance-id';
|
2019-12-16 22:13:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-01-10 14:37:27 +00:00
|
|
|
import RadioControlOption from './option';
|
2019-12-16 22:13:41 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
const RadioControl = ( {
|
|
|
|
className,
|
2020-03-10 14:40:30 +00:00
|
|
|
instanceId,
|
2020-01-14 20:21:29 +00:00
|
|
|
id,
|
2019-12-16 22:13:41 +00:00
|
|
|
selected,
|
|
|
|
onChange,
|
|
|
|
options = [],
|
|
|
|
} ) => {
|
2020-03-10 14:40:30 +00:00
|
|
|
const radioControlId = id || instanceId;
|
2020-01-14 20:21:29 +00:00
|
|
|
|
2019-12-16 22:13:41 +00:00
|
|
|
return (
|
|
|
|
options.length && (
|
|
|
|
<div
|
2020-06-17 09:53:42 +00:00
|
|
|
className={ classnames(
|
|
|
|
'wc-block-components-radio-control',
|
|
|
|
className
|
|
|
|
) }
|
2019-12-16 22:13:41 +00:00
|
|
|
>
|
2020-01-10 14:37:27 +00:00
|
|
|
{ options.map( ( option ) => (
|
|
|
|
<RadioControlOption
|
|
|
|
key={ option.value }
|
2020-01-14 20:21:29 +00:00
|
|
|
name={ `radio-control-${ radioControlId }` }
|
2020-01-10 14:37:27 +00:00
|
|
|
checked={ option.value === selected }
|
|
|
|
option={ option }
|
2020-05-11 13:29:57 +00:00
|
|
|
onChange={ ( value ) => {
|
|
|
|
onChange( value );
|
|
|
|
if ( typeof option.onChange === 'function' ) {
|
|
|
|
option.onChange( value );
|
|
|
|
}
|
|
|
|
} }
|
2020-01-10 14:37:27 +00:00
|
|
|
/>
|
|
|
|
) ) }
|
2019-12-16 22:13:41 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-03-10 14:40:30 +00:00
|
|
|
export default withInstanceId( RadioControl );
|
2020-02-14 03:43:13 +00:00
|
|
|
export { RadioControlOption };
|
|
|
|
export { default as RadioControlOptionLayout } from './option-layout';
|