/** * 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'; /** * A component for displaying a button with a main action plus a secondary set of actions behind a menu toggle. * * @return { object } - */ 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 = { /** * Whether the button is styled as a primary button. */ isPrimary: PropTypes.bool, /** * Icon for the main button. */ mainIcon: PropTypes.node, /** * Label for the main button. */ mainLabel: PropTypes.string, /** * Function to activate when the the main button is clicked. */ onClick: PropTypes.func, /** * Label to display for the menu of actions, used as a heading on the mobile popover and for accessible text. */ menuLabel: PropTypes.string, /** * An array of additional actions. Accepts additional icon, label, and onClick props. */ controls: PropTypes.arrayOf( PropTypes.shape( { /** * Icon used in button, passed to `IconButton`. Can be either string (dashicon name) or Gridicon. */ icon: PropTypes.oneOfType( [ PropTypes.string, PropTypes.element ] ), /** * Label displayed for this button. */ label: PropTypes.string.isRequired, /** * Click handler for this button. */ onClick: PropTypes.func, } ) ).isRequired, /** * Additional CSS classes. */ className: PropTypes.string, }; SplitButton.defaultProps = { isPrimary: false, onClick: noop, }; export default SplitButton;