Skip to content

Commit 3d30e1d

Browse files
committed
Move separate update logic into render
1 parent de7c5e6 commit 3d30e1d

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/components/Tab.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,13 @@ import React, { PropTypes } from 'react';
22
import { findDOMNode } from 'react-dom';
33
import cx from 'classnames';
44

5-
function syncNodeAttributes(node, props) {
6-
if (props.selected) {
7-
node.setAttribute('tabindex', '0');
8-
node.setAttribute('selected', 'selected');
9-
if (props.focus) {
10-
node.focus();
11-
}
12-
} else {
13-
node.removeAttribute('tabindex');
14-
node.removeAttribute('selected');
15-
}
16-
}
17-
185
module.exports = React.createClass({
196
displayName: 'Tab',
207

218
propTypes: {
229
className: PropTypes.string,
2310
id: PropTypes.string,
11+
focus: PropTypes.bool,
2412
selected: PropTypes.bool,
2513
disabled: PropTypes.bool,
2614
panelId: PropTypes.string,
@@ -41,11 +29,17 @@ module.exports = React.createClass({
4129
},
4230

4331
componentDidMount() {
44-
syncNodeAttributes(findDOMNode(this), this.props);
32+
this.checkFocus();
4533
},
4634

4735
componentDidUpdate() {
48-
syncNodeAttributes(findDOMNode(this), this.props);
36+
this.checkFocus();
37+
},
38+
39+
checkFocus() {
40+
if (this.props.selected && this.props.focus) {
41+
findDOMNode(this).focus();
42+
}
4943
},
5044

5145
render() {
@@ -68,6 +62,7 @@ module.exports = React.createClass({
6862
aria-expanded={selected ? 'true' : 'false'}
6963
aria-disabled={disabled ? 'true' : 'false'}
7064
aria-controls={panelId}
65+
tabIndex={selected ? '0' : null}
7166
>
7267
{children}
7368
</li>

0 commit comments

Comments
 (0)