2019-08-23 16:35:34 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import Label from '../label';
|
2019-08-27 10:24:50 +00:00
|
|
|
import withComponentId from '../../hocs/with-component-id';
|
2019-08-23 16:35:34 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component used for 'Order by' selectors, which renders a label
|
|
|
|
* and a <select> with the options provided in the props.
|
|
|
|
*/
|
2019-09-05 15:09:31 +00:00
|
|
|
const OrderSelect = ( {
|
|
|
|
className,
|
|
|
|
componentId,
|
|
|
|
defaultValue,
|
|
|
|
label,
|
|
|
|
onChange,
|
|
|
|
options,
|
|
|
|
screenReaderLabel,
|
|
|
|
readOnly,
|
|
|
|
value,
|
|
|
|
} ) => {
|
2019-08-23 16:35:34 +00:00
|
|
|
const selectId = `wc-block-order-select__select-${ componentId }`;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<p className={ classNames( 'wc-block-order-select', className ) }>
|
|
|
|
<Label
|
|
|
|
label={ label }
|
|
|
|
screenReaderLabel={ screenReaderLabel }
|
|
|
|
wrapperElement="label"
|
|
|
|
wrapperProps={ {
|
|
|
|
className: 'wc-block-order-select__label',
|
|
|
|
htmlFor: selectId,
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
<select // eslint-disable-line jsx-a11y/no-onchange
|
|
|
|
id={ selectId }
|
|
|
|
className="wc-block-order-select__select"
|
|
|
|
defaultValue={ defaultValue }
|
|
|
|
onChange={ onChange }
|
|
|
|
readOnly={ readOnly }
|
|
|
|
value={ value }
|
|
|
|
>
|
|
|
|
{ options.map( ( option ) => (
|
|
|
|
<option key={ option.key } value={ option.key }>
|
|
|
|
{ option.label }
|
|
|
|
</option>
|
|
|
|
) ) }
|
|
|
|
</select>
|
|
|
|
</p>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
OrderSelect.propTypes = {
|
|
|
|
defaultValue: PropTypes.string,
|
|
|
|
label: PropTypes.string,
|
|
|
|
onChange: PropTypes.func,
|
2019-09-05 15:09:31 +00:00
|
|
|
options: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
|
|
|
key: PropTypes.string.isRequired,
|
|
|
|
label: PropTypes.string.isRequired,
|
|
|
|
} )
|
|
|
|
),
|
2019-08-23 16:35:34 +00:00
|
|
|
readOnly: PropTypes.bool,
|
|
|
|
screenReaderLabel: PropTypes.string,
|
|
|
|
value: PropTypes.string,
|
2019-08-27 10:24:50 +00:00
|
|
|
// from withComponentId
|
|
|
|
componentId: PropTypes.number.isRequired,
|
2019-08-23 16:35:34 +00:00
|
|
|
};
|
|
|
|
|
2019-08-27 10:24:50 +00:00
|
|
|
export default withComponentId( OrderSelect );
|