Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 184064d

Browse files
author
Nate Ross
committed
Fixing TabList so it can have a string selectedKey or an integer selectedKey.
1 parent d3a3324 commit 184064d

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

lib/TabList.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default class TabList extends React.Component {
3636
}
3737
}
3838

39-
onClickItem(selectedKey) {
39+
handleClickItem(selectedKey) {
4040
this.setSelectedKey(selectedKey);
4141
}
4242

@@ -59,12 +59,12 @@ export default class TabList extends React.Component {
5959
return React.Children.map(children, (child, index) => {
6060
// If there is no key provide, use the index as default key
6161
const key = child.key || String(index);
62-
const selected = selectedKey === key;
62+
const selected = String(selectedKey) === key;
6363

6464
const props = {
6565
selected,
6666
tabIndex: index,
67-
onClick: this.onClickItem.bind(this, key)
67+
onClick: this.handleClickItem.bind(this, key)
6868
};
6969

7070
return React.cloneElement(child, props);

test/tablist-test.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,42 @@ describe('TabList', () => {
7171
expect(spy).toHaveBeenCalledWith('two');
7272
});
7373

74-
it('supports selectedKey', () => {
75-
const tree = shallow(
76-
<TabList selectedKey="1">
74+
describe('selectedKey', () => {
75+
const renderTabListWithSelectedKey = key => shallow(
76+
<TabList selectedKey={ key }>
7777
<div className="one">a</div>
7878
<div className="two">b</div>
7979
</TabList>
8080
);
81-
const child = tree.find('[selected=true]');
8281

83-
expect(child.length).toBe(1);
84-
expect(child.node.props.className).toBe('two');
82+
const assertChildTwoSelected = tree => {
83+
const child = tree.find('[selected=true]');
84+
expect(child.length).toBe(1);
85+
expect(child.node.props.className).toBe('two');
86+
};
87+
88+
it('supports string index', () => {
89+
const tree = renderTabListWithSelectedKey('1');
90+
assertChildTwoSelected(tree);
91+
});
92+
93+
it('supports integer index', () => {
94+
const tree = renderTabListWithSelectedKey(1);
95+
assertChildTwoSelected(tree);
96+
});
97+
98+
it('support child keys', () => {
99+
const tree = shallow(
100+
<TabList selectedKey="bar">
101+
<div key="foo" className="one">a</div>
102+
<div key="bar" className="two">b</div>
103+
</TabList>
104+
);
105+
assertChildTwoSelected(tree);
106+
});
85107
});
86108

109+
87110
it('supports defaultSelectedKey', () => {
88111
const tree = shallow(
89112
<TabList defaultSelectedKey="1">

0 commit comments

Comments
 (0)