11import classNames from 'classnames' ;
22import PropTypes from 'prop-types' ;
3- import React from 'react' ;
3+ import React , { cloneElement } from 'react' ;
4+ import Button from './Button' ;
5+ import {
6+ btnSizes ,
7+ btnStyles
8+ } from './constants' ;
49import styles from './index.styl' ;
510
6- const ButtonGroup = ( { className , btnSize , vertical, dropdownOpen, ...props } ) => {
11+ const ButtonGroup = ( { btnSize , btnStyle , vertical, dropdownOpen, children , className , ...props } ) => {
712 const classes = {
813 [ styles . btnGroup ] : true ,
914 [ styles . btnGroupLg ] : btnSize === 'large' || btnSize === 'lg' ,
@@ -13,32 +18,41 @@ const ButtonGroup = ({ className, btnSize, vertical, dropdownOpen, ...props }) =
1318 [ styles . btnGroupVertical ] : vertical ,
1419 [ styles . open ] : dropdownOpen
1520 } ;
21+
1622 return (
1723 < div
1824 { ...props }
1925 className = { classNames ( className , classes ) }
20- />
26+ >
27+ { React . Children . map ( children , child => {
28+ if ( React . isValidElement ( child ) && child . type === Button ) {
29+ const childProps = { } ;
30+ if ( btnSizes . indexOf ( btnSize ) >= 0 ) {
31+ childProps . btnSize = btnSize ;
32+ }
33+ if ( btnStyles . indexOf ( btnStyle ) >= 0 ) {
34+ childProps . btnStyle = btnStyle ;
35+ }
36+ return cloneElement ( child , childProps ) ;
37+ }
38+
39+ return child ;
40+ } ) }
41+ </ div >
2142 ) ;
2243} ;
2344
2445ButtonGroup . propTypes = {
25- btnSize : PropTypes . oneOf ( [
26- 'lg' ,
27- 'md' ,
28- 'sm' ,
29- 'xs' ,
30- 'large' ,
31- 'medium' ,
32- 'small' ,
33- 'extra-small'
34- ] ) ,
46+ btnSize : PropTypes . oneOf ( btnSizes ) ,
47+ btnStyle : PropTypes . oneOf ( btnStyles ) ,
3548 vertical : PropTypes . bool ,
3649
3750 // Apply styles for use in a Dropdown.
3851 // This prop will be set automatically when the ButtonGroup is used inside a Dropdown.
3952 dropdownOpen : PropTypes . bool
4053} ;
4154ButtonGroup . defaultProps = {
55+ btnSize : 'md' ,
4256 vertical : false ,
4357 dropdownOpen : false
4458} ;
0 commit comments