/**
* External dependencies
*
* @format
*/
import { Button, IconButton, Dropdown, NavigableMenu } from '@wordpress/components';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import { noop } from 'lodash';
/**
* Internal dependencies
*/
import './style.scss';
const SplitButton = ( {
isPrimary,
mainIcon,
mainLabel,
onClick,
menuLabel,
controls,
className,
} ) => {
if ( ! controls || ! controls.length ) {
return null;
}
const MainButtonComponent = ( mainIcon && IconButton ) || Button;
const classes = classnames( 'woocommerce-split-button', className, {
'is-primary': isPrimary,
'has-label': mainLabel,
} );
return (
{ mainLabel }
{
return (
);
} }
renderContent={ ( { onClose } ) => {
const renderControl = ( control, index ) => {
const ButtonComponent = ( control.icon && IconButton ) || Button;
return (
{
event.stopPropagation();
onClose();
if ( control.onClick ) {
control.onClick();
}
} }
className="woocommerce-split-button__menu-item"
icon={ control.icon || '' }
role="menuitem"
>
{ control.label }
);
};
return (
{ controls.map( renderControl ) }
);
} }
/>
);
};
SplitButton.propTypes = {
isPrimary: PropTypes.bool,
mainIcon: PropTypes.node,
mainLabel: PropTypes.string,
onClick: PropTypes.func,
menuLabel: PropTypes.string,
controls: PropTypes.arrayOf(
PropTypes.shape( {
icon: PropTypes.node,
label: PropTypes.string,
onClick: PropTypes.func,
} )
).isRequired,
className: PropTypes.string,
};
SplitButton.defaultProps = {
isPrimary: false,
onClick: noop,
};
export default SplitButton;