woocommerce/plugins/woocommerce-admin/client/components/segmented-selection/index.js

64 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-05-17 04:44:58 +00:00
/** @format */
/**
* External dependencies
*/
import { Component, Fragment } from '@wordpress/element';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import { partial, uniqueId } from 'lodash';
/**
* Internal dependencies
*/
import './style.scss';
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 (
<Fragment key={ value }>
<input
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 }>
<span className="woocommerce-segmented-selection__label">{ label }</span>
2018-05-17 04:44:58 +00:00
</label>
</Fragment>
);
} ) }
</div>
</fieldset>
);
}
}
SegmentedSelection.propTypes = {
className: PropTypes.string,
options: PropTypes.arrayOf(
PropTypes.shape( {
value: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
} )
).isRequired,
selected: PropTypes.string,
onSelect: PropTypes.func.isRequired,
name: PropTypes.string.isRequired,
legend: PropTypes.string.isRequired,
};
export default SegmentedSelection;