Skip to content

Commit e8cbe7b

Browse files
committed
Add support for passing btnSize and btnStyle to ButtonGroup component
1 parent 127e5c7 commit e8cbe7b

File tree

8 files changed

+85
-60
lines changed

8 files changed

+85
-60
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ import { Button } from './components/Buttons';
106106
#### Flat button group
107107

108108
```js
109-
<ButtonGroup>
110-
<Button btnStyle="flat" active>Left</Button>
111-
<Button btnStyle="flat">Middle</Button>
112-
<Button btnStyle="flat">Right</Button>
109+
<ButtonGroup btnStyle="flat" btnSize="md">
110+
<Button active>Left</Button>
111+
<Button>Middle</Button>
112+
<Button>Right</Button>
113113
</ButtonGroup>
114-
<ButtonGroup>
115-
<Button btnStyle="flat" compact><i className="fa fa-pie-chart" /></Button>
116-
<Button btnStyle="flat" compact><i className="fa fa-line-chart" /></Button>
117-
<Button btnStyle="flat" compact><i className="fa fa-table" /></Button>
114+
<ButtonGroup btnStyle="flat" btnSize="md">
115+
<Button compact><i className="fa fa-pie-chart" /></Button>
116+
<Button compact><i className="fa fa-line-chart" /></Button>
117+
<Button compact><i className="fa fa-table" /></Button>
118118
</ButtonGroup>
119119
```
120120

examples/ButtonGroups.jsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ export default () => {
2020
<br /><br />
2121

2222
<p><strong>Flat button group</strong></p>
23-
<ButtonGroup>
24-
<Button btnStyle="flat" active>Left</Button>
25-
<Button btnStyle="flat">Middle</Button>
26-
<Button btnStyle="flat">Right</Button>
23+
<ButtonGroup btnSize="md" btnStyle="flat">
24+
<Button active>Left</Button>
25+
<Button>Middle</Button>
26+
<Button>Right</Button>
2727
</ButtonGroup>
28-
<ButtonGroup>
29-
<Button btnStyle="flat" compact><i className="fa fa-pie-chart" /></Button>
30-
<Button btnStyle="flat" compact><i className="fa fa-line-chart" /></Button>
31-
<Button btnStyle="flat" compact><i className="fa fa-table" /></Button>
28+
<ButtonGroup btnSize="md" btnStyle="flat">
29+
<Button compact><i className="fa fa-pie-chart" /></Button>
30+
<Button compact><i className="fa fa-line-chart" /></Button>
31+
<Button compact><i className="fa fa-table" /></Button>
3232
</ButtonGroup>
3333
</div>
3434
</div>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@trendmicro/react-buttons",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "Trend Micro Components: React Buttons",
55
"main": "lib/index.js",
66
"files": [

src/Button.jsx

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import classNames from 'classnames';
22
import PropTypes from 'prop-types';
33
import React, { PureComponent } from 'react';
4+
import {
5+
btnSizes,
6+
btnStyles
7+
} from './constants';
48
import styles from './index.styl';
59

610
class Button extends PureComponent {
@@ -14,25 +18,8 @@ class Button extends PureComponent {
1418
'reset',
1519
'submit'
1620
]),
17-
btnSize: PropTypes.oneOf([
18-
'lg',
19-
'md',
20-
'sm',
21-
'xs',
22-
'large',
23-
'medium',
24-
'small',
25-
'extra-small'
26-
]),
27-
btnStyle: PropTypes.oneOf([
28-
'default',
29-
'primary',
30-
'danger',
31-
'emphasis', // alias of "danger"
32-
'border',
33-
'flat', // alias of "border"
34-
'link'
35-
]),
21+
btnSize: PropTypes.oneOf(btnSizes),
22+
btnStyle: PropTypes.oneOf(btnStyles),
3623
active: PropTypes.bool,
3724
hover: PropTypes.bool,
3825
focus: PropTypes.bool,

src/ButtonGroup.jsx

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import classNames from 'classnames';
22
import 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';
49
import 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

2445
ButtonGroup.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
};
4154
ButtonGroup.defaultProps = {
55+
btnSize: 'md',
4256
vertical: false,
4357
dropdownOpen: false
4458
};

src/btn-size.styl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
border-radius: 3px;
77

88
&.btn-compact {
9-
width: 58px;
9+
padding-left: 0;
10+
padding-right: 0;
11+
min-width: 58px;
1012
}
1113
}
1214

@@ -16,6 +18,12 @@
1618
font-size: 13px;
1719
line-height: 20px;
1820
border-radius: 3px;
21+
22+
&.btn-compact {
23+
padding-left: 0;
24+
padding-right: 0;
25+
min-width: 42px;
26+
}
1927
}
2028

2129
.btn-sm,
@@ -26,7 +34,9 @@
2634
border-radius: 3px;
2735

2836
&.btn-compact {
29-
width: 36px;
37+
padding-left: 0;
38+
padding-right: 0;
39+
min-width: 36px;
3040
}
3141
}
3242

@@ -38,6 +48,8 @@
3848
border-radius: 3px;
3949

4050
&.btn-compact {
41-
width: 28px;
51+
padding-left: 0;
52+
padding-right: 0;
53+
min-width: 28px;
4254
}
4355
}

src/btn.styl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,6 @@
6060
margin-left: 8px;
6161
}
6262

63-
// compact button
64-
&.btn-compact {
65-
min-width: 0;
66-
padding-left: 0;
67-
padding-right: 0;
68-
width: 42px;
69-
}
70-
7163
// Loading class within a button
7264
> [class*="loader-container"] {
7365
display: inline-block;

src/constants.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const btnSizes = [
2+
'lg',
3+
'md',
4+
'sm',
5+
'xs',
6+
'large',
7+
'medium',
8+
'small',
9+
'extra-small'
10+
];
11+
12+
export const btnStyles = [
13+
'default',
14+
'primary',
15+
'danger',
16+
'emphasis', // alias of "danger"
17+
'border',
18+
'flat', // alias of "border"
19+
'link'
20+
];

0 commit comments

Comments
 (0)