Skip to content

Commit 5860e66

Browse files
committed
Remove jss and support class properties
1 parent 8ae5112 commit 5860e66

File tree

6 files changed

+93
-106
lines changed

6 files changed

+93
-106
lines changed

.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"react"
55
],
66
"plugins": [
7-
"transform-object-rest-spread"
7+
"transform-object-rest-spread",
8+
"transform-class-properties"
89
]
910
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"babel-eslint": "^6.0.4",
4646
"babel-jest": "^15.0.0",
4747
"babel-loader": "^6.2.4",
48+
"babel-plugin-transform-class-properties": "^6.11.5",
4849
"babel-plugin-transform-object-rest-spread": "^6.8.0",
4950
"babel-preset-latest": "^6.14.0",
5051
"babel-preset-react": "^6.5.0",
@@ -66,8 +67,7 @@
6667
"webpack-dev-server": "^1.14.1"
6768
},
6869
"dependencies": {
69-
"classnames": "^2.2.0",
70-
"js-stylesheet": "^0.0.1"
70+
"classnames": "^2.2.0"
7171
},
7272
"jest": {
7373
"testPathDirs": [

src/components/Tab.js

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
import React, { PropTypes, Component } from 'react';
1+
import React, { Component, PropTypes } from 'react';
22
import cx from 'classnames';
33

4-
class Tab extends Component {
4+
export default class Tab extends Component {
5+
6+
static defaultProps = {
7+
activeTabClassName: 'ReactTabs__Tab--selected',
8+
disabledTabClassName: 'ReactTabs__Tab--disabled',
9+
focus: false,
10+
id: null,
11+
panelId: null,
12+
selected: false,
13+
};
514

615
static propTypes = {
7-
className: PropTypes.string,
8-
id: PropTypes.string,
9-
tabRef: PropTypes.func,
10-
focus: PropTypes.bool,
11-
selected: PropTypes.bool,
12-
disabled: PropTypes.bool,
1316
activeTabClassName: PropTypes.string,
1417
disabledTabClassName: PropTypes.string,
15-
panelId: PropTypes.string,
1618
children: PropTypes.oneOfType([
1719
PropTypes.array,
1820
PropTypes.object,
1921
PropTypes.string,
2022
]),
21-
};
22-
23-
static defaultProps = {
24-
focus: false,
25-
selected: false,
26-
id: null,
27-
panelId: null,
28-
activeTabClassName: 'ReactTabs__Tab--selected',
29-
disabledTabClassName: 'ReactTabs__Tab--disabled',
23+
className: PropTypes.string,
24+
disabled: PropTypes.bool,
25+
focus: PropTypes.bool,
26+
id: PropTypes.string,
27+
panelId: PropTypes.string,
28+
selected: PropTypes.bool,
29+
tabRef: PropTypes.func,
3030
};
3131

3232
componentDidMount() {
@@ -45,18 +45,17 @@ class Tab extends Component {
4545

4646
render() {
4747
const {
48-
selected,
49-
disabled,
50-
panelId,
5148
activeTabClassName,
52-
disabledTabClassName,
53-
className,
5449
children,
50+
className,
51+
disabled,
52+
disabledTabClassName,
53+
focus, // eslint-disable-line no-unused-vars
5554
id,
55+
panelId,
56+
selected,
5657
...attributes } = this.props;
5758

58-
delete attributes.focus;
59-
6059
return (
6160
<li
6261
{...attributes}
@@ -81,5 +80,3 @@ class Tab extends Component {
8180
);
8281
}
8382
}
84-
85-
export default Tab;

src/components/TabList.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,47 @@
1-
import React, { PropTypes, Component } from 'react';
1+
import React, { Component, PropTypes } from 'react';
22
import cx from 'classnames';
33
import Tab from './Tab';
44

5-
function renderChildren(props) {
6-
return React.Children.map(props.children, (child) => {
7-
// if child is not a tab we don't need to clone it
8-
// since we don't need to add custom props
9-
10-
if (child.type !== Tab) {
11-
return child;
12-
}
13-
14-
const clonedProps = {
15-
activeTabClassName: props.activeTabClassName,
16-
disabledTabClassName: props.disabledTabClassName,
17-
};
18-
19-
return React.cloneElement(child, clonedProps);
20-
});
21-
}
22-
23-
class TabList extends Component {
5+
export default class TabList extends Component {
246

257
static propTypes = {
26-
className: PropTypes.string,
278
activeTabClassName: PropTypes.string,
28-
disabledTabClassName: PropTypes.string,
299
children: PropTypes.oneOfType([
3010
PropTypes.object,
3111
PropTypes.array,
3212
]),
13+
className: PropTypes.string,
14+
disabledTabClassName: PropTypes.string,
3315
};
3416

35-
render() {
17+
renderChildren() {
3618
const {
37-
className,
3819
activeTabClassName,
39-
disabledTabClassName,
4020
children,
21+
disabledTabClassName,
22+
} = this.props;
23+
24+
return React.Children.map(children, (child) => {
25+
// if child is not a tab we don't need to clone it
26+
// since we don't need to add custom props
27+
28+
if (child.type === Tab) {
29+
return React.cloneElement(child, {
30+
activeTabClassName,
31+
disabledTabClassName,
32+
});
33+
}
34+
35+
return child;
36+
});
37+
}
38+
39+
render() {
40+
const {
41+
activeTabClassName, // eslint-disable-line no-unused-vars
42+
children, // eslint-disable-line no-unused-vars
43+
className,
44+
disabledTabClassName, // eslint-disable-line no-unused-vars
4145
...attributes } = this.props;
4246

4347
return (
@@ -49,10 +53,8 @@ class TabList extends Component {
4953
)}
5054
role="tablist"
5155
>
52-
{renderChildren({ activeTabClassName, disabledTabClassName, children })}
56+
{this.renderChildren()}
5357
</ul>
5458
);
5559
}
5660
}
57-
58-
export default TabList;

src/components/TabPanel.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
import React, { PropTypes, Component } from 'react';
1+
import React, { Component, PropTypes } from 'react';
22
import cx from 'classnames';
33

4-
class TabPanel extends Component {
4+
export default class TabPanel extends Component {
5+
6+
static contextTypes = {
7+
forceRenderTabPanel: PropTypes.bool,
8+
};
9+
10+
static defaultProps = {
11+
id: null,
12+
selected: false,
13+
tabId: null,
14+
};
515

616
static propTypes = {
717
children: PropTypes.oneOfType([
@@ -16,18 +26,16 @@ class TabPanel extends Component {
1626
tabId: PropTypes.string,
1727
};
1828

19-
static contextTypes = {
20-
forceRenderTabPanel: PropTypes.bool,
21-
};
22-
23-
static defaultProps = {
24-
selected: false,
25-
id: null,
26-
tabId: null,
27-
};
28-
2929
render() {
30-
const { className, children, selected, id, tabId, style, ...attributes } = this.props;
30+
const {
31+
children,
32+
className,
33+
id,
34+
selected,
35+
style,
36+
tabId,
37+
...attributes } = this.props;
38+
3139
return (
3240
<div
3341
{...attributes}
@@ -48,5 +56,3 @@ class TabPanel extends Component {
4856
);
4957
}
5058
}
51-
52-
export default TabPanel;

src/components/Tabs.js

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { PropTypes, cloneElement, Component } from 'react';
22
import cx from 'classnames';
3-
import jss from 'js-stylesheet';
43
import uuid from '../helpers/uuid';
54
import childrenPropType from '../helpers/childrenPropType';
65
import Tab from './Tab';
@@ -15,38 +14,30 @@ function isTabDisabled(node) {
1514
return node.getAttribute('aria-disabled') === 'true';
1615
}
1716

18-
let useDefaultStyles = true;
19-
20-
class Tabs extends Component {
21-
22-
static propTypes = {
23-
className: PropTypes.string,
24-
selectedIndex: PropTypes.number, // eslint-disable-line react/no-unused-prop-types
25-
onSelect: PropTypes.func,
26-
focus: PropTypes.bool, // eslint-disable-line react/no-unused-prop-types
27-
children: childrenPropType,
28-
forceRenderTabPanel: PropTypes.bool,
29-
};
17+
export default class Tabs extends Component {
3018

3119
static childContextTypes = {
3220
forceRenderTabPanel: PropTypes.bool,
3321
};
3422

3523
static defaultProps = {
36-
selectedIndex: -1,
3724
focus: false,
3825
forceRenderTabPanel: false,
26+
selectedIndex: -1,
3927
};
4028

41-
static setUseDefaultStyles = (use) => {
42-
useDefaultStyles = use;
29+
static propTypes = {
30+
children: childrenPropType,
31+
className: PropTypes.string,
32+
focus: PropTypes.bool, // eslint-disable-line react/no-unused-prop-types
33+
forceRenderTabPanel: PropTypes.bool,
34+
onSelect: PropTypes.func,
35+
selectedIndex: PropTypes.number, // eslint-disable-line react/no-unused-prop-types
4336
};
4437

4538
constructor(props) {
4639
super(props);
4740
this.state = this.copyPropsToState(this.props, this.state);
48-
this.handleClick = this.handleClick.bind(this);
49-
this.handleKeyDown = this.handleKeyDown.bind(this);
5041
}
5142

5243
getChildContext() {
@@ -55,12 +46,6 @@ class Tabs extends Component {
5546
};
5647
}
5748

58-
componentDidMount() {
59-
if (useDefaultStyles) {
60-
jss(require('../helpers/styles')); // eslint-disable-line global-require
61-
}
62-
}
63-
6449
componentWillReceiveProps(newProps) {
6550
// Use a transactional update to prevent race conditions
6651
// when reading the state in copyPropsToState
@@ -210,9 +195,7 @@ class Tabs extends Component {
210195

211196
// Reset index for panels
212197
index = 0;
213-
}
214-
// Clone TabPanel components to have refs
215-
else {
198+
} else {
216199
const id = panelIds[index];
217200
const tabId = tabIds[index];
218201
const selected = state.selectedIndex === index;
@@ -232,7 +215,7 @@ class Tabs extends Component {
232215

233216
tabNodes: [];
234217

235-
handleKeyDown(e) {
218+
handleKeyDown = (e) => {
236219
if (this.isTabFromContainer(e.target)) {
237220
let index = this.state.selectedIndex;
238221
let preventDefault = false;
@@ -256,9 +239,9 @@ class Tabs extends Component {
256239

257240
this.setSelected(index, true);
258241
}
259-
}
242+
};
260243

261-
handleClick(e) {
244+
handleClick = (e) => {
262245
let node = e.target;
263246
do { // eslint-disable-line no-cond-assign
264247
if (this.isTabFromContainer(node)) {
@@ -271,7 +254,7 @@ class Tabs extends Component {
271254
return;
272255
}
273256
} while ((node = node.parentNode) !== null);
274-
}
257+
};
275258

276259
// This is an anti-pattern, so sue me
277260
copyPropsToState(props, state) {
@@ -371,5 +354,3 @@ class Tabs extends Component {
371354
);
372355
}
373356
}
374-
375-
export default Tabs;

0 commit comments

Comments
 (0)