Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 4x 4x 4x 14x 1x 1x 1x 14x 5x 5x 5x 3x 14x 14x 14x 14x 19x | import _, { omit } from 'lodash'; import React from 'react'; import PropTypes from 'prop-types'; import { lucidClassNames } from '../../util/style-helpers'; import { filterTypes, getFirst, StandardProps, } from '../../util/component-types'; import { buildModernHybridComponent } from '../../util/state-management'; import { IButtonProps, Button } from '../Button/Button'; import { ButtonGroupDumb as ButtonGroup } from '../ButtonGroup/ButtonGroup'; import ChevronIcon from '../Icon/ChevronIcon/ChevronIcon'; import { IDropMenuState, IDropMenuProps, DropMenuDumb as DropMenu, } from '../DropMenu/DropMenu'; import * as reducers from './SplitButton.reducers'; const cx = lucidClassNames.bind('&-SplitButton'); const { bool, func, node, oneOf, shape, string } = PropTypes; /** SplitButton Button Child Component */ export interface ISplitButtonButtonChildProps extends StandardProps { /** Disables selection of the \`Button\`. */ isDisabled?: boolean; } const ButtonChild = (_props: ISplitButtonButtonChildProps): null => null; ButtonChild.displayName = 'SplitButton.ButtonChild'; ButtonChild.peek = { description: ` 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. `, }; ButtonChild.propTypes = { /** The children to render within the \`Button\`. */ children: node, /** Disables selection of the \`Button\`. */ isDisabled: bool, /** Called when the user clicks the \`Button\`. Signature: \`({ props, event }) => {}\` */ onClick: func, }; /** SplitButton */ export interface ISplitButtonProps extends StandardProps { /** Sets the direction the flyout menu will render relative to the SplitButton. */ direction: 'up' | 'down'; /** Style variations of the SplitButton. */ kind?: 'primary' | 'danger'; /** Size variations of the SplitButton. */ size?: 'short' | 'small' | 'large'; /** Form element type variations of SplitButton. Passed through to DOM Button. */ type?: string; /** *Child Element* - props pass through to the underlying DropMenu component */ DropMenu: IDropMenuProps; } class SplitButton extends React.Component<ISplitButtonProps> { static displayName = 'SplitButton'; static Button = ButtonChild; static peek = { description: `\`SplitButton\` allows you to combine a single main \`Button\` together with a list of additional \`Buttons\` with actions which will be rendered within a \`DropMenu\`.`, categories: ['controls', 'buttons'], madeFrom: ['Button', 'DropMenu'], }; static reducers = reducers; static 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', 'danger']), /** Size variations of the SplitButton. */ size: oneOf(['short', 'small', 'large']), /** Form element type variations of SplitButton. Passed through to DOM Button. */ type: string, }; static defaultProps = { direction: 'down' as const, type: 'button' as const, DropMenu: DropMenu.defaultProps, }; // Handles select events in the DropMenu handleSelect = ( optionIndex: number | null, { event, }: { event: | React.KeyboardEvent<Element> | React.MouseEvent<Element, MouseEvent>; } ): void => { const buttonChildProps = _.map( filterTypes(this.props.children, SplitButton.Button), 'props' ); if (optionIndex !== null) { this.handleButtonClick(buttonChildProps[optionIndex + 1], event); } }; // Handles clicks on the Primary Button handleClick = ({ event, }: { event: React.MouseEvent<HTMLButtonElement>; }): void => { 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 within handleClick and handleSelect handleButtonClick = (buttonProps: IButtonProps, event: any): void => { const { DropMenu: { onCollapse }, } = this.props; onCollapse && onCollapse({ props: this.props.DropMenu, event }); 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} {...omit(passThroughs, [ 'DropMenu', 'children', 'className', 'direction', 'kind', 'size', 'type', 'initialState', 'callbackId', ])} direction={direction} className={cx('&', className)} onSelect={this.handleSelect} > <DropMenu.Control> <ButtonGroup> <Button {...primaryButtonProps} className={cx( '&-Button-primary', _.get(primaryButtonProps, 'className') )} kind={kind} type={type} size={size} onClick={this.handleClick} /> <Button className={cx('&-Button-drop')} size={size} hasOnlyIcon={true} isActive={isExpanded} kind={kind} isDisabled={_.every( [primaryButtonProps, ...buttonChildProps], 'isDisabled' )} > <ChevronIcon className={cx('&-ChevronIcon')} direction={direction} size={10} /> </Button> </ButtonGroup> </DropMenu.Control> {_.map(buttonChildProps, (buttonChildProp, index) => ( <DropMenu.Option {...buttonChildProp} key={index} /> ))} </DropMenu> ); } } export default buildModernHybridComponent< ISplitButtonProps, IDropMenuState, typeof SplitButton >(SplitButton as any, { reducers }); export { SplitButton as SplitButtonDumb }; |