Skip to content

Commit 28a74d4

Browse files
authored
Merge pull request #23 from freeranger/v2.0.0
Version 2.0.0 - Updated to React 16.13.1 and deprecated code removed
2 parents a425978 + 14a9b57 commit 28a74d4

File tree

8 files changed

+3194
-1542
lines changed

8 files changed

+3194
-1542
lines changed

.eslintrc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
"node": true,
66
"es6": true
77
},
8-
"ecmaFeatures": {
9-
"modules": true
10-
},
118
"rules": {
129
"no-bitwise": 2,
1310
"no-else-return": 2,

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
## 1.0.1 19 Mar 2016
1212
- Migrated to use PropTypes from React.PropTypes
1313

14-
## 1.0.3 04 jul 2020
14+
## 1.0.3 04 Jul 2020
1515
- componentWillReceiveProps renamed to UNSAFE_componentWillReceiveProps
1616
- Failing test (as a result) disabled - needs a load of package updates to fix
17+
18+
## 2.0.0 05 Jul 2020
19+
- Updated to React 16.13.1
20+
- UNSAFE_componentWillReceiveProps replaced with getDerivedStateFromProps
21+
- All packages updated
22+
- All tests passing

dist/index.js

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,16 @@
119119
return _this;
120120
}
121121

122+
// UNSAFE_componentWillReceiveProps(nextProps) {
123+
// this.setState({ selected: nextProps.selected });
124+
// }
125+
122126
_createClass(TabsComponent, [{
123-
key: 'UNSAFE_componentWillReceiveProps',
124-
value: function UNSAFE_componentWillReceiveProps(nextProps) {
125-
this.setState({ selected: nextProps.selected });
127+
key: 'componentDidUpdate',
128+
value: function componentDidUpdate(prevProps, prevState) {
129+
if (prevState.selected !== this.state.selected) {
130+
this.setState({ selected: this.state.selected });
131+
}
126132
}
127133
}, {
128134
key: 'render',
@@ -164,34 +170,12 @@
164170
var selectedIndex = void 0;
165171
if (typeof selected === 'number') {
166172
selectedIndex = selected;
167-
if (selectedIndex < 0) {
168-
console.warn('tab index \'' + this.state.selected + '\' < 0, defaulting to first tab');
169-
selectedIndex = 0;
170-
selected = selectedIndex;
171-
} else {
172-
var tabCount = this.props.children.length || 1;
173-
if (selectedIndex > tabCount - 1) {
174-
console.warn('tab index \'' + this.state.selected + '\' > number of tabs (' + tabCount + ', defaulting to last tab');
175-
selectedIndex = tabCount - 1;
176-
selected = selectedIndex;
177-
}
178-
}
179173
} else {
180174
// selected is a string - should be the tab label so find the index of that tab
181175
var selectedLabel = this.state.selected;
182176
selectedIndex = onlyOneChild ? 0 : this.props.children.findIndex(function (child) {
183177
return selectedLabel === child.props.label;
184178
});
185-
if (selectedIndex < 0) {
186-
console.warn('tab \'' + this.state.selected + '\' not found, defaulting to first tab');
187-
selectedIndex = 0;
188-
selected = onlyOneChild ? this.props.children.props.label : this.props.children[selectedIndex].props.label;
189-
}
190-
}
191-
192-
// If the selected tab has changed then we need to update the state
193-
if (selected !== this.state.selected) {
194-
this.setState({ selected: selected });
195179
}
196180

197181
var contentTab = onlyOneChild ? this.props.children : this.props.children[selectedIndex];
@@ -213,6 +197,48 @@
213197
this.props.onSelect(index, this.props.children[index].props.label);
214198
}
215199
}
200+
}], [{
201+
key: 'getDerivedStateFromProps',
202+
value: function getDerivedStateFromProps(nextProps, prevState) {
203+
204+
var onlyOneChild = !Array.isArray(nextProps.children);
205+
206+
var selected = nextProps.selected;
207+
208+
// Find the tab index - selected could be the index or could be the tab label
209+
var selectedIndex = void 0;
210+
if (typeof nextProps.selected === 'number') {
211+
selectedIndex = nextProps.selected;
212+
if (selectedIndex < 0) {
213+
console.warn('tab index \'' + nextProps.selected + '\' < 0, defaulting to first tab');
214+
selectedIndex = 0;
215+
selected = selectedIndex;
216+
} else {
217+
var tabCount = nextProps.children && nextProps.children.length || 1;
218+
if (selectedIndex > tabCount - 1) {
219+
console.warn('tab index \'' + nextProps.selected + '\' > number of tabs (' + tabCount + ', defaulting to last tab');
220+
selectedIndex = tabCount - 1;
221+
selected = selectedIndex;
222+
}
223+
}
224+
} else {
225+
// selected is a string - should be the tab label so find the index of that tab
226+
var selectedLabel = nextProps.selected;
227+
selectedIndex = onlyOneChild ? 0 : nextProps.children.findIndex(function (child) {
228+
return selectedLabel === child.props.label;
229+
});
230+
if (selectedIndex < 0) {
231+
console.warn('tab \'' + nextProps.selected + '\' not found, defaulting to first tab');
232+
selectedIndex = 0;
233+
selected = onlyOneChild ? nextProps.children.props.label : nextProps.children[selectedIndex].props.label;
234+
}
235+
}
236+
237+
if (selected !== nextProps.selected) {
238+
return { selected: selected };
239+
}
240+
return null;
241+
}
216242
}]);
217243

218244
return TabsComponent;

0 commit comments

Comments
 (0)