Skip to content

Commit 494e4ec

Browse files
committed
refactor(components): standardize custom tag names
BREAKING CHANGE: `El` prop is now `tag`. This standardizes the way custom elements should render their html “tags”.
1 parent 0a61071 commit 494e4ec

6 files changed

Lines changed: 29 additions & 45 deletions

File tree

docs/lib/Components/ButtonsPage.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default class ButtonsPage extends React.Component {
3434
3535
// Pass in a Component to override default button element
3636
// example: react-router Link
37-
El: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
37+
tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
3838
3939
onClick: PropTypes.func,
4040
size: PropTypes.string

lib/Button.jsx

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ const propTypes = {
66
block: PropTypes.bool,
77
color: PropTypes.string,
88
disabled: PropTypes.bool,
9-
El: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
9+
tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
1010
onClick: PropTypes.func,
1111
size: PropTypes.string
1212
};
1313

1414
const defaultProps = {
15-
color: 'primary'
15+
color: 'primary',
16+
tag: 'button'
1617
};
1718

1819
class Button extends React.Component {
@@ -33,17 +34,15 @@ class Button extends React.Component {
3334
}
3435

3536
render() {
36-
const {
37+
let {
3738
active,
3839
block,
39-
children,
4040
className,
4141
color,
42-
El,
4342
size,
43+
tag: Tag,
4444
...attributes
4545
} = this.props;
46-
let Tag = 'button';
4746

4847
const classes = classNames(
4948
className,
@@ -54,18 +53,12 @@ class Button extends React.Component {
5453
{ active, disabled: this.props.disabled }
5554
);
5655

57-
if (El) {
58-
Tag = El;
59-
} else if (attributes.href) {
56+
if (attributes.href && Tag === 'button') {
6057
Tag = 'a';
6158
}
6259

6360
return (
64-
<Tag {...attributes}
65-
className={classes}
66-
onClick={this.onClick}>
67-
{children}
68-
</Tag>
61+
<Tag {...attributes} className={classes} onClick={this.onClick}/>
6962
);
7063
}
7164
}

lib/Dropdown.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class Dropdown extends React.Component {
155155
className,
156156
dropup,
157157
group,
158-
'tag': TagName,
158+
'tag': Tag,
159159
...attributes
160160
} = omit(this.props, ['children', 'isOpen']);
161161

@@ -170,10 +170,10 @@ class Dropdown extends React.Component {
170170
);
171171

172172
return (
173-
<TagName {...attributes}
173+
<Tag {...attributes}
174174
className={classes}>
175175
{this.renderChildren()}
176-
</TagName>
176+
</Tag>
177177
);
178178
}
179179
}

lib/DropdownItem.jsx

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import React, { PropTypes } from 'react';
22
import classNames from 'classnames';
3-
import omit from 'lodash.omit';
43

54
const propTypes = {
65
children: PropTypes.node,
76
disabled: PropTypes.bool,
87
divider: PropTypes.bool,
9-
El: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
8+
tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
109
header: PropTypes.bool,
1110
isOpen: PropTypes.bool,
1211
toggle: PropTypes.func
1312
};
1413

14+
const defaultProps = {
15+
tag: 'button'
16+
};
17+
1518
class DropdownItem extends React.Component {
1619
constructor(props) {
1720
super(props);
@@ -41,13 +44,11 @@ class DropdownItem extends React.Component {
4144
}
4245

4346
render() {
44-
let Tagname = 'button';
4547
const tabIndex = this.getTabIndex();
46-
const {
48+
let {
4749
className,
48-
children,
4950
divider,
50-
El,
51+
tag: Tag,
5152
header,
5253
...props } = this.props;
5354

@@ -61,34 +62,24 @@ class DropdownItem extends React.Component {
6162
}
6263
);
6364

64-
if (El) {
65-
return (
66-
<El {...props}
67-
tabIndex={tabIndex}
68-
className={classes}
69-
onClick={this.onClick}>
70-
{children}
71-
</El>
72-
);
73-
}
74-
75-
if (header) {
76-
Tagname = 'h6';
77-
} else if (divider) {
78-
Tagname = 'div';
65+
if (Tag === 'button') {
66+
if (header) {
67+
Tag = 'h6';
68+
} else if (divider) {
69+
Tag = 'div';
70+
}
7971
}
8072

8173
return (
82-
<Tagname {...props}
74+
<Tag {...props}
8375
tabIndex={tabIndex}
8476
className={classes}
85-
onClick={this.onClick}>
86-
{children}
87-
</Tagname>
77+
onClick={this.onClick}/>
8878
);
8979
}
9080
}
9181

9282
DropdownItem.propTypes = propTypes;
83+
DropdownItem.defaultProps = defaultProps;
9384

9485
export default DropdownItem;

test/Button.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Button', () => {
1212

1313
it('should render custom element', () => {
1414
const Link = (props) => <a href="/home" {...props}>{props.children}</a>;
15-
const wrapper = mount(<Button El={Link}>Home</Button>);
15+
const wrapper = mount(<Button tag={Link}>Home</Button>);
1616

1717
expect(wrapper.find('a').length).toBe(1);
1818
expect(wrapper.text()).toBe('Home');

test/DropdownItem.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('DropdownItem', () => {
2222

2323
it('should render custom element', () => {
2424
const Link = (props) => <a href="/home" {...props}>{props.children}</a>;
25-
const wrapper = mount(<DropdownItem isOpen={isOpen} toggle={toggle} El={Link}>Home</DropdownItem>);
25+
const wrapper = mount(<DropdownItem isOpen={isOpen} toggle={toggle} tag={Link}>Home</DropdownItem>);
2626

2727
expect(wrapper.find('a').length).toBe(1);
2828
expect(wrapper.find('a').hasClass('dropdown-item')).toBe(true);

0 commit comments

Comments
 (0)