2018-06-29 00:34:17 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Button } from '@wordpress/components';
|
2021-07-14 20:38:57 +00:00
|
|
|
import { createElement } from '@wordpress/element';
|
2020-03-02 22:22:32 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2018-06-29 00:34:17 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A button useful for a launcher of a dropdown component. The button is 100% width of its container and displays
|
|
|
|
* single or multiple lines rendered as `<span/>` elments.
|
|
|
|
*
|
2020-02-14 02:23:21 +00:00
|
|
|
* @param {Object} props Props passed to component.
|
|
|
|
* @return {Object} -
|
2018-08-31 17:27:21 +00:00
|
|
|
*/
|
2020-02-14 02:23:21 +00:00
|
|
|
const DropdownButton = ( props ) => {
|
2018-06-29 00:34:17 +00:00
|
|
|
const { labels, isOpen, ...otherProps } = props;
|
|
|
|
const buttonClasses = classnames( 'woocommerce-dropdown-button', {
|
|
|
|
'is-open': isOpen,
|
|
|
|
'is-multi-line': labels.length > 1,
|
|
|
|
} );
|
|
|
|
return (
|
2020-02-14 02:23:21 +00:00
|
|
|
<Button
|
|
|
|
className={ buttonClasses }
|
|
|
|
aria-expanded={ isOpen }
|
|
|
|
{ ...otherProps }
|
|
|
|
>
|
2018-06-29 00:34:17 +00:00
|
|
|
<div className="woocommerce-dropdown-button__labels">
|
2020-07-28 02:32:58 +00:00
|
|
|
{ labels.map( ( label, i ) => (
|
|
|
|
<span key={ i }>{ decodeEntities( label ) }</span>
|
|
|
|
) ) }
|
2018-06-29 00:34:17 +00:00
|
|
|
</div>
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
DropdownButton.propTypes = {
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* An array of elements to be rendered as the content of the button.
|
|
|
|
*/
|
2018-11-14 16:52:47 +00:00
|
|
|
labels: PropTypes.array.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Boolean describing if the dropdown in open or not.
|
|
|
|
*/
|
2018-06-29 00:34:17 +00:00
|
|
|
isOpen: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DropdownButton;
|