|
| 1 | +import * as React from 'react'; |
| 2 | + |
| 3 | +import { Theme, withTheme } from '../../theme'; |
| 4 | +import { Box } from '../Layout'; |
| 5 | +import { ButtonProps } from './Button'; |
| 6 | + |
| 7 | +export type ButtonGroupDirection = 'vertical' | 'horizontal'; |
| 8 | + |
| 9 | +export interface ButtonGroupProps { |
| 10 | + /** |
| 11 | + * @default vertical |
| 12 | + */ |
| 13 | + direction?: ButtonGroupDirection; |
| 14 | + children: Array<React.ReactElement<ButtonProps>>; |
| 15 | + theme: Theme; |
| 16 | +} |
| 17 | + |
| 18 | +const directionToFlexDirectionMap: { [direction: string]: 'row' | 'column' } = { |
| 19 | + horizontal: 'row', |
| 20 | + vertical: 'column', |
| 21 | +}; |
| 22 | + |
| 23 | +const ButtonGroup: React.SFC<ButtonGroupProps> = props => { |
| 24 | + const { children, direction = 'vertical', theme } = props; |
| 25 | + |
| 26 | + const childrenLength = React.Children.count(children); |
| 27 | + |
| 28 | + const finalChildren = React.Children.map(children, (child, index) => { |
| 29 | + if (!React.isValidElement(child)) { |
| 30 | + return child; |
| 31 | + } |
| 32 | + |
| 33 | + // @ts-ignore |
| 34 | + const button = child as React.ReactElement<ButtonProps>; |
| 35 | + const buttonSize = button.props.size || 'medium'; |
| 36 | + const buttonBorderRadius = |
| 37 | + theme.themeVariables.controlBorderRadius[buttonSize]; |
| 38 | + |
| 39 | + return React.cloneElement(button, { |
| 40 | + dangerouslySetInlineStyle: |
| 41 | + direction === 'vertical' |
| 42 | + ? { |
| 43 | + buttonStyle: { |
| 44 | + borderBottomWidth: 1, |
| 45 | + borderColor: theme.themeVariables.colors.border.default, |
| 46 | + borderRadius: 0, |
| 47 | + borderWidth: 0, |
| 48 | + elevation: 0, |
| 49 | + |
| 50 | + ...(index === 0 && { |
| 51 | + borderTopLeftRadius: buttonBorderRadius, |
| 52 | + borderTopRightRadius: buttonBorderRadius, |
| 53 | + }), |
| 54 | + ...(childrenLength - 1 === index && { |
| 55 | + borderBottomLeftRadius: buttonBorderRadius, |
| 56 | + borderBottomRightRadius: buttonBorderRadius, |
| 57 | + borderBottomWidth: 0, |
| 58 | + }), |
| 59 | + }, |
| 60 | + } |
| 61 | + : { |
| 62 | + buttonStyle: { |
| 63 | + borderColor: theme.themeVariables.colors.border.default, |
| 64 | + borderLeftWidth: 0, |
| 65 | + borderRadius: 0, |
| 66 | + elevation: 0, |
| 67 | + |
| 68 | + ...(index === 0 && { |
| 69 | + borderBottomLeftRadius: buttonBorderRadius, |
| 70 | + borderLeftWidth: 2, |
| 71 | + borderTopLeftRadius: buttonBorderRadius, |
| 72 | + }), |
| 73 | + ...(childrenLength - 1 === index && { |
| 74 | + borderBottomRightRadius: buttonBorderRadius, |
| 75 | + borderRightWidth: 2, |
| 76 | + borderTopRightRadius: buttonBorderRadius, |
| 77 | + }), |
| 78 | + }, |
| 79 | + }, |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + return ( |
| 84 | + <Box flexDirection={directionToFlexDirectionMap[direction]}> |
| 85 | + {finalChildren} |
| 86 | + </Box> |
| 87 | + ); |
| 88 | +}; |
| 89 | + |
| 90 | +export default withTheme(ButtonGroup); |
0 commit comments