2018-05-17 04:44:58 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-09-07 09:43:52 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
2018-05-17 04:44:58 +00:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { partial, uniqueId } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Create a panel of styled selectable options rendering stylized checkboxes and labels
|
|
|
|
*/
|
2018-05-17 04:44:58 +00:00
|
|
|
class SegmentedSelection extends Component {
|
|
|
|
render() {
|
|
|
|
const { className, options, selected, onSelect, name, legend } = this.props;
|
|
|
|
return (
|
2018-05-28 10:55:19 +00:00
|
|
|
<fieldset className="woocommerce-segmented-selection">
|
2018-05-17 04:44:58 +00:00
|
|
|
<legend className="screen-reader-text">{ legend }</legend>
|
2018-05-28 10:55:19 +00:00
|
|
|
<div className={ classnames( className, 'woocommerce-segmented-selection__container' ) }>
|
2018-05-17 04:44:58 +00:00
|
|
|
{ options.map( ( { value, label } ) => {
|
|
|
|
if ( ! value || ! label ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const id = uniqueId( `${ value }_` );
|
|
|
|
return (
|
2018-09-07 09:43:52 +00:00
|
|
|
<div className="woocommerce-segmented-selection__item" key={ value }>
|
2018-07-11 15:23:16 +00:00
|
|
|
{ /* eslint-disable jsx-a11y/label-has-for */ }
|
2018-05-17 04:44:58 +00:00
|
|
|
<input
|
2018-06-01 14:35:18 +00:00
|
|
|
className="woocommerce-segmented-selection__input"
|
2018-05-17 04:44:58 +00:00
|
|
|
type="radio"
|
|
|
|
name={ name }
|
|
|
|
id={ id }
|
|
|
|
checked={ selected === value }
|
2018-05-28 10:55:19 +00:00
|
|
|
onChange={ partial( onSelect, { [ name ]: value } ) }
|
2018-05-17 04:44:58 +00:00
|
|
|
/>
|
|
|
|
<label htmlFor={ id }>
|
2018-06-01 14:35:18 +00:00
|
|
|
<span className="woocommerce-segmented-selection__label">{ label }</span>
|
2018-05-17 04:44:58 +00:00
|
|
|
</label>
|
2018-07-11 15:23:16 +00:00
|
|
|
{ /* eslint-enable jsx-a11y/label-has-for */ }
|
2018-09-07 09:43:52 +00:00
|
|
|
</div>
|
2018-05-17 04:44:58 +00:00
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</div>
|
|
|
|
</fieldset>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SegmentedSelection.propTypes = {
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Additional CSS classes.
|
|
|
|
*/
|
2018-05-17 04:44:58 +00:00
|
|
|
className: PropTypes.string,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* An Array of options to render. The array needs to be composed of objects with properties `label` and `value`.
|
|
|
|
*/
|
2018-05-17 04:44:58 +00:00
|
|
|
options: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
|
|
|
value: PropTypes.string.isRequired,
|
|
|
|
label: PropTypes.string.isRequired,
|
|
|
|
} )
|
|
|
|
).isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Value of selected item.
|
|
|
|
*/
|
2018-05-17 04:44:58 +00:00
|
|
|
selected: PropTypes.string,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Callback to be executed after selection
|
|
|
|
*/
|
2018-05-17 04:44:58 +00:00
|
|
|
onSelect: PropTypes.func.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* This will be the key in the key and value arguments supplied to `onSelect`.
|
|
|
|
*/
|
2018-05-17 04:44:58 +00:00
|
|
|
name: PropTypes.string.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Create a legend visible to screen readers.
|
|
|
|
*/
|
2018-05-17 04:44:58 +00:00
|
|
|
legend: PropTypes.string.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SegmentedSelection;
|