Skip to content

Commit cb7c46c

Browse files
committed
Improve TabsRemoval example
1 parent 9c24058 commit cb7c46c

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

examples/src/TabsRemoval/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class TabsRemoval extends PureComponent {
1111

1212
this.state = {
1313
items: this.getTabs(),
14+
selectedTabKey: 0,
1415
};
1516
}
1617

@@ -30,7 +31,13 @@ export class TabsRemoval extends PureComponent {
3031
// create a new array without [indexToRemove] item
3132
const newTabs = [...currentTabs.slice(0, indexToRemove), ...currentTabs.slice(indexToRemove + 1)];
3233

33-
this.setState({ items: newTabs });
34+
const nextSelectedIndex = newTabs[indexToRemove] ? indexToRemove : indexToRemove - 1;
35+
if (!newTabs[nextSelectedIndex]) {
36+
alert('You can not delete the last tab!');
37+
return;
38+
}
39+
40+
this.setState({ items: newTabs, selectedTabKey: newTabs[nextSelectedIndex].key });
3441
};
3542

3643
getTabs = () =>
@@ -46,9 +53,10 @@ export class TabsRemoval extends PureComponent {
4653
}));
4754

4855
render() {
56+
const { items, selectedTabKey } = this.state;
4957
return (
5058
<div className="itemRemoval__wrapper">
51-
<Tabs items={this.state.items} selectedTabKey={0} allowRemove removeActiveOnly onRemove={this.onRemoveTab} />
59+
<Tabs items={items} selectedTabKey={selectedTabKey} allowRemove removeActiveOnly onRemove={this.onRemoveTab} />
5260
</div>
5361
);
5462
}

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export default class Tabs extends Component {
3434
}
3535

3636
componentWillReceiveProps(nextProps) {
37-
const { items, selectedTabKey } = this.props;
37+
const { items } = this.props;
38+
const { selectedTabKey } = this.state;
3839
const newState = {};
3940
if (items !== nextProps.items) {
4041
newState.blockWidth = 0;

0 commit comments

Comments
 (0)