All files / components/SplitButton SplitButton.jsx

100% Statements 18/18
100% Branches 2/2
100% Functions 1/1
100% Lines 18/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217                    124x                   124x                   124x                                                                                                                                                                                 248x                 1x     1x 1x         4x 4x               5x   5x   5x 3x                         14x       14x   14x   14x                                                                 24x                      
import _ from 'lodash';
import React from 'react';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, filterTypes, getFirst, omitProps }  from '../../util/component-types';
import Button from '../Button/Button';
import ButtonGroup from '../ButtonGroup/ButtonGroup';
import CaretIcon from '../Icon/CaretIcon/CaretIcon';
import DropMenu from '../DropMenu/DropMenu';
import * as reducers from './SplitButton.reducers';
 
const cx = lucidClassNames.bind('&-SplitButton');
 
const {
	any,
	bool,
	func,
	node,
	oneOf,
	shape,
	string,
} = React.PropTypes;
 
/**
 *
 * {"categories": ["controls", "buttons"], "madeFrom": ["Button", "DropMenu"]}
 *
 * SplitButtons allow you to combine a single main `Button` together with a
 * list of additional Buttons with actions which will be rendered within a
 * DropMenu.
 */
const SplitButton = createClass({
	displayName: 'SplitButton',
 
	components: {
		/**
		 * One of many potential `Button`s to render in this `SplitButton`. The
		 * first `Button` will be used as the Primary button, while all others will
		 * be rendered within the `DropMenu` below.
		 */
		Button: createClass({
			displayName: 'SplitButton.Button',
			propTypes: {
				/**
				 * The children to render within the `Button`.
				 */
				children: any,
				/**
				 * Disables selection of the `Button`.
				 */
				isDisabled: bool,
				/**
				 * Called when the user clicks the `Button`.
				 *
				 * Signature: `({ event, props }) => {}`
				 */
				onClick: func,
			},
		}),
	},
 
	reducers,
 
	propTypes: {
		/**
		 * Object of DropMenu props which are passed through to the underlying
		 * DropMenu component.
		 */
		DropMenu: shape(DropMenu.propTypes),
 
		/**
		 * All children should be `ButtonGroup.Button`s and they support the same
		 * props as `Button`s.
		 */
		children: node,
 
		/**
		 * Appended to the component-specific class names set on the root
		 * element. Value is run through the `classnames` library.
		 */
		className: string,
 
		/**
		 * Sets the direction the flyout menu will render relative to the
		 * SplitButton.
		 */
		direction: oneOf([
			'up',
			'down',
		]),
 
		/**
		 * Style variations of the SplitButton.
		 */
		kind: oneOf([
			'primary',
			'success',
			'warning',
			'danger',
			'info',
		]),
 
		/**
		 * Size variations of the SplitButton.
		 */
		size: oneOf([
			'short',
			'small',
			'large',
		]),
 
		/**
		 * Form element type variations of SplitButton. Defaults to 'button' to avoid
		 * being triggered by 'Enter' anywhere on the page. Passed through to DOM
		 * Button.
		 */
		type: string,
	},
 
	getDefaultProps() {
		return {
			direction: 'down',
			type: 'button',
			DropMenu: DropMenu.getDefaultProps(),
		};
	},
 
	// Handles clicks on the Primary Button
	handleClick({event}) {
		const clickedButtonProps = _.get(getFirst(this.props, SplitButton.Button), 'props');
 
		// Stop propagation to prevent this `Click` from expanding the `DropMenu`
		event.stopPropagation();
		this.handleButtonClick(clickedButtonProps, event);
	},
 
	// Handles clicks in the DropMenu
	handleSelect(optionIndex, {event}) {
		const buttonChildProps = _.map(filterTypes(this.props.children, SplitButton.Button), 'props');
		this.handleButtonClick(buttonChildProps[optionIndex + 1], event);
	},
 
	handleButtonClick(buttonProps, event) {
		const {
			DropMenu: {
				onCollapse,
			},
		} = this.props;
 
		onCollapse();
 
		if (_.has(buttonProps, 'onClick')) {
			buttonProps.onClick({event, props: buttonProps});
		}
	},
 
	render() {
		const {
			className,
			kind,
			direction,
			type,
			size,
			DropMenu: dropMenuProps,
			...passThroughs
		} = this.props;
 
		const {
			isExpanded,
		} = dropMenuProps;
 
		const [primaryButtonProps, ...buttonChildProps] = _.map(filterTypes(this.props.children, SplitButton.Button), 'props');
 
		return (
			<DropMenu
				{...dropMenuProps}
				{...omitProps(passThroughs, SplitButton)}
				direction={direction}
				className={cx('&', className)}
				onSelect={this.handleSelect}
			>
				<DropMenu.Control>
					<ButtonGroup>
						<Button
							{...primaryButtonProps}
							className={cx('&-Button-primary')}
							kind={kind}
							type={type}
							size={size}
							onClick={this.handleClick}
						/>
						<Button
							className={cx('&-Button-drop')}
							hasOnlyIcon
							isActive={isExpanded}
							kind={kind}
						>
							<CaretIcon
								className={cx('&-CaretIcon')}
								direction={direction}
								size={8}
							/>
						</Button>
					</ButtonGroup>
				</DropMenu.Control>
				{_.map(buttonChildProps, (buttonChildProp, index) => (
					<DropMenu.Option
						{...buttonChildProp}
						key={index}
					/>
				))}
			</DropMenu>
		);
	},
});
 
export default SplitButton;