|
119 | 119 | return _this; |
120 | 120 | } |
121 | 121 |
|
| 122 | + // UNSAFE_componentWillReceiveProps(nextProps) { |
| 123 | + // this.setState({ selected: nextProps.selected }); |
| 124 | + // } |
| 125 | + |
122 | 126 | _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 | + } |
126 | 132 | } |
127 | 133 | }, { |
128 | 134 | key: 'render', |
|
164 | 170 | var selectedIndex = void 0; |
165 | 171 | if (typeof selected === 'number') { |
166 | 172 | 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 | | - } |
179 | 173 | } else { |
180 | 174 | // selected is a string - should be the tab label so find the index of that tab |
181 | 175 | var selectedLabel = this.state.selected; |
182 | 176 | selectedIndex = onlyOneChild ? 0 : this.props.children.findIndex(function (child) { |
183 | 177 | return selectedLabel === child.props.label; |
184 | 178 | }); |
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 }); |
195 | 179 | } |
196 | 180 |
|
197 | 181 | var contentTab = onlyOneChild ? this.props.children : this.props.children[selectedIndex]; |
|
213 | 197 | this.props.onSelect(index, this.props.children[index].props.label); |
214 | 198 | } |
215 | 199 | } |
| 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 | + } |
216 | 242 | }]); |
217 | 243 |
|
218 | 244 | return TabsComponent; |
|
0 commit comments