Skip to content

Commit dbe6289

Browse files
authored
Merge pull request #5 from marcuslindfeldt/cr/default-tab
Change how default tab behaves
2 parents 0d87be6 + 9239b13 commit dbe6289

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/TabProvider.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class TabProvider extends Component {
3535
}
3636

3737
componentWillReceiveProps(nextProps) {
38-
if (!this.selection.isSelected(nextProps.defaultTab)) {
38+
if (this.props.defaultTab !== nextProps.defaultTab && !this.selection.isSelected(nextProps.defaultTab)) {
3939
this.selection.select(nextProps.defaultTab);
4040
}
4141
}

src/__tests__/TabProvider.js

+62-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,48 @@ test('<TabProvider /> should update to new tab on click', () => {
5656
expect(tabs.find('#second').prop('aria-expanded')).toBe(false);
5757
});
5858

59+
test('<TabProvider /> should not reset to default tab when parent updates', () => {
60+
class TestComponent extends React.Component {
61+
state = {
62+
state: 'one',
63+
}
64+
65+
render() {
66+
return (
67+
<TabProvider defaultTab="second">
68+
<div className="rwt__tabs">
69+
<TabList>
70+
<Tab tabFor="first"><span>Tab 1</span></Tab>
71+
<Tab tabFor="second"><span>Tab 2</span></Tab>
72+
</TabList>
73+
<TabPanel tabId="first"><p>TabPanel 1</p></TabPanel>
74+
<TabPanel tabId="second"><p>TabPanel 2</p></TabPanel>
75+
</div>
76+
</TabProvider>
77+
);
78+
}
79+
}
80+
const tabs = mount((
81+
<TestComponent />
82+
));
83+
84+
expect(tabs.find('#second-tab').prop('aria-selected')).toBe(true);
85+
expect(tabs.find('#second').prop('aria-expanded')).toBe(true);
86+
87+
tabs.find('#first-tab').simulate('click');
88+
89+
expect(tabs.find('#first-tab').prop('aria-selected')).toBe(true);
90+
expect(tabs.find('#first').prop('aria-expanded')).toBe(true);
91+
92+
tabs.setState({ state: 'two' });
93+
94+
expect(tabs.find('#first-tab').prop('aria-selected')).toBe(true);
95+
expect(tabs.find('#first').prop('aria-expanded')).toBe(true);
96+
97+
expect(tabs.find('#second-tab').prop('aria-selected')).toBe(false);
98+
expect(tabs.find('#second').prop('aria-expanded')).toBe(false);
99+
});
100+
59101
test('<TabProvider /> should call onChange callback on selection', () => {
60102
const onChange = jest.fn();
61103

@@ -77,7 +119,7 @@ test('<TabProvider /> should call onChange callback on selection', () => {
77119
expect(onChange).toHaveBeenCalledWith('first');
78120
});
79121

80-
test('<TabProvider /> should select correct tab when selected prop updates', () => {
122+
test('<TabProvider /> should select correct tab when default tab prop changes', () => {
81123
const onChange = jest.fn();
82124

83125
const tabs = mount((
@@ -95,11 +137,29 @@ test('<TabProvider /> should select correct tab when selected prop updates', ()
95137

96138
expect(tabs.find('#second-tab').prop('aria-selected')).toBe(true);
97139
expect(tabs.find('#second').prop('aria-expanded')).toBe(true);
98-
tabs.find('#first-tab').simulate('click');
140+
tabs.setProps({ defaultTab: 'first' });
99141
expect(tabs.find('#first-tab').prop('aria-selected')).toBe(true);
100142
expect(tabs.find('#first').prop('aria-expanded')).toBe(true);
101143
expect(tabs.find('#second-tab').prop('aria-selected')).toBe(false);
102144
expect(tabs.find('#second').prop('aria-expanded')).toBe(false);
145+
});
146+
147+
test('<TabProvider /> should not change tab when props are unchanged', () => {
148+
const onChange = jest.fn();
149+
150+
const tabs = mount((
151+
<TabProvider defaultTab="second" onChange={onChange}>
152+
<div className="rwt__tabs">
153+
<TabList>
154+
<Tab tabFor="first"><span>Tab 1</span></Tab>
155+
<Tab tabFor="second"><span>Tab 2</span></Tab>
156+
</TabList>
157+
<TabPanel tabId="first"><p>TabPanel 1</p></TabPanel>
158+
<TabPanel tabId="second"><p>TabPanel 2</p></TabPanel>
159+
</div>
160+
</TabProvider>
161+
));
162+
103163
tabs.setProps({ defaultTab: 'second' });
104164
expect(tabs.find('#second-tab').prop('aria-selected')).toBe(true);
105165
expect(tabs.find('#second').prop('aria-expanded')).toBe(true);

0 commit comments

Comments
 (0)