Skip to content

Use es6 class #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions src/components/Tab.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, { PropTypes } from 'react';
import React, { PropTypes, Component } from 'react';
import { findDOMNode } from 'react-dom';
import cx from 'classnames';

module.exports = React.createClass({
displayName: 'Tab',
class Tab extends Component {

propTypes: {
static propTypes = {
className: PropTypes.string,
id: PropTypes.string,
focus: PropTypes.bool,
Expand All @@ -19,32 +18,30 @@ module.exports = React.createClass({
PropTypes.object,
PropTypes.string,
]),
},
};

getDefaultProps() {
return {
focus: false,
selected: false,
id: null,
panelId: null,
activeTabClassName: 'ReactTabs__Tab--selected',
disabledTabClassName: 'ReactTabs__Tab--disabled',
};
},
static defaultProps = {
focus: false,
selected: false,
id: null,
panelId: null,
activeTabClassName: 'ReactTabs__Tab--selected',
disabledTabClassName: 'ReactTabs__Tab--disabled',
};

componentDidMount() {
this.checkFocus();
},
}

componentDidUpdate() {
this.checkFocus();
},
}

checkFocus() {
if (this.props.selected && this.props.focus) {
findDOMNode(this).focus();
}
},
}

render() {
const {
Expand Down Expand Up @@ -81,5 +78,7 @@ module.exports = React.createClass({
{children}
</li>
);
},
});
}
}

export default Tab;
15 changes: 8 additions & 7 deletions src/components/TabList.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { PropTypes } from 'react';
import React, { PropTypes, Component } from 'react';
import cx from 'classnames';
import Tab from './Tab';

Expand All @@ -20,18 +20,17 @@ function renderChildren(props) {
});
}

module.exports = React.createClass({
displayName: 'TabList',
class TabList extends Component {

propTypes: {
static propTypes = {
className: PropTypes.string,
activeTabClassName: PropTypes.string,
disabledTabClassName: PropTypes.string,
children: PropTypes.oneOfType([
PropTypes.object,
PropTypes.array,
]),
},
};

render() {
const {
Expand All @@ -53,5 +52,7 @@ module.exports = React.createClass({
{renderChildren({ activeTabClassName, disabledTabClassName, children })}
</ul>
);
},
});
}
}

export default TabList;
32 changes: 15 additions & 17 deletions src/components/TabPanel.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { PropTypes } from 'react';
import React, { PropTypes, Component } from 'react';
import cx from 'classnames';

module.exports = React.createClass({
displayName: 'TabPanel',
class TabPanel extends Component {

propTypes: {
static propTypes = {
children: PropTypes.oneOfType([
PropTypes.array,
PropTypes.object,
Expand All @@ -15,23 +14,20 @@ module.exports = React.createClass({
selected: PropTypes.bool,
style: PropTypes.object,
tabId: PropTypes.string,
},
};

contextTypes: {
static contextTypes = {
forceRenderTabPanel: PropTypes.bool,
},
};

getDefaultProps() {
return {
selected: false,
id: null,
tabId: null,
};
},
static defaultProps = {
selected: false,
id: null,
tabId: null,
};

render() {
const { className, children, selected, id, tabId, style, ...attributes } = this.props;

return (
<div
{...attributes}
Expand All @@ -50,5 +46,7 @@ module.exports = React.createClass({
{(this.context.forceRenderTabPanel || selected) ? children : null}
</div>
);
},
});
}
}

export default TabPanel;
80 changes: 40 additions & 40 deletions src/components/Tabs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { PropTypes, cloneElement } from 'react';
import React, { PropTypes, cloneElement, Component } from 'react';
import { findDOMNode } from 'react-dom';
import cx from 'classnames';
import jss from 'js-stylesheet';
Expand All @@ -18,58 +18,56 @@ function isTabDisabled(node) {

let useDefaultStyles = true;

module.exports = React.createClass({
displayName: 'Tabs',
class Tabs extends Component {

propTypes: {
static propTypes = {
className: PropTypes.string,
selectedIndex: PropTypes.number,
onSelect: PropTypes.func,
focus: PropTypes.bool,
children: childrenPropType,
forceRenderTabPanel: PropTypes.bool,
},
};

childContextTypes: {
static childContextTypes = {
forceRenderTabPanel: PropTypes.bool,
},
};

statics: {
setUseDefaultStyles(use) {
useDefaultStyles = use;
},
},
static defaultProps = {
selectedIndex: -1,
focus: false,
forceRenderTabPanel: false,
};

getDefaultProps() {
return {
selectedIndex: -1,
focus: false,
forceRenderTabPanel: false,
};
},
static setUseDefaultStyles = (use) => {
useDefaultStyles = use;
};

getInitialState() {
return this.copyPropsToState(this.props, this.state);
},
constructor(props) {
super(props);
this.state = this.copyPropsToState(this.props, this.state);
this.handleClick = this.handleClick.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
}

getChildContext() {
return {
forceRenderTabPanel: this.props.forceRenderTabPanel,
};
},
}

componentDidMount() {
if (useDefaultStyles) {
jss(require('../helpers/styles.js')); // eslint-disable-line global-require
}
},
}

componentWillReceiveProps(newProps) {
// Use a transactional update to prevent race conditions
// when reading the state in copyPropsToState
// See https://github.com/reactjs/react-tabs/issues/51
this.setState(state => this.copyPropsToState(newProps, state));
},
}

setSelected(index, focus) {
// Don't do anything if nothing has changed
Expand All @@ -92,7 +90,7 @@ module.exports = React.createClass({
// Update selected index
this.setState({ selectedIndex: index, focus: focus === true });
}
},
}

getNextTab(index) {
const count = this.getTabsCount();
Expand All @@ -115,7 +113,7 @@ module.exports = React.createClass({

// No tabs are disabled, return index
return index;
},
}

getPrevTab(index) {
let i = index;
Expand All @@ -139,29 +137,29 @@ module.exports = React.createClass({

// No tabs are disabled, return index
return index;
},
}

getTabsCount() {
return this.props.children && this.props.children[0] ?
React.Children.count(this.props.children[0].props.children) :
0;
},
}

getPanelsCount() {
return React.Children.count(this.props.children.slice(1));
},
}

getTabList() {
return this.refs.tablist;
},
}

getTab(index) {
return this.refs[`tabs-${index}`];
},
}

getPanel(index) {
return this.refs[`panels-${index}`];
},
}

getChildren() {
let index = 0;
Expand Down Expand Up @@ -246,7 +244,7 @@ module.exports = React.createClass({

return result;
});
},
}

handleKeyDown(e) {
if (this.isTabFromContainer(e.target)) {
Expand All @@ -272,7 +270,7 @@ module.exports = React.createClass({

this.setSelected(index, true);
}
},
}

handleClick(e) {
let node = e.target;
Expand All @@ -287,7 +285,7 @@ module.exports = React.createClass({
return;
}
} while ((node = node.parentNode) !== null);
},
}

// This is an anti-pattern, so sue me
copyPropsToState(props, state) {
Expand All @@ -313,7 +311,7 @@ module.exports = React.createClass({
selectedIndex,
focus: props.focus,
};
},
}

/**
* Determine if a node from event.target is a Tab element for the current Tabs container.
Expand All @@ -337,7 +335,7 @@ module.exports = React.createClass({
} while (nodeAncestor);

return false;
},
}

render() {
// This fixes an issue with focus management.
Expand Down Expand Up @@ -385,5 +383,7 @@ module.exports = React.createClass({
{this.getChildren()}
</div>
);
},
});
}
}

export default Tabs;
3 changes: 2 additions & 1 deletion src/helpers/childrenPropType.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import Tab from '../components/Tab';
import TabList from '../components/TabList';
import TabPanel from '../components/TabPanel';

module.exports = function childrenPropTypes(props, propName) {
let error;
Expand All @@ -27,7 +28,7 @@ module.exports = function childrenPropTypes(props, propName) {
tabsCount++;
}
});
} else if (child.type.displayName === 'TabPanel') {
} else if (child.type === TabPanel) {
panelsCount++;
} else {
error = new Error(
Expand Down