前面可能找不着,所以接着往后找

This commit is contained in:
liaoxuezhi 2019-05-24 17:11:19 +08:00 committed by catchonme
parent b2c83e7956
commit dc15298c84
1 changed files with 9 additions and 5 deletions

View File

@ -116,21 +116,25 @@ export default class Tabs extends React.Component<TabsProps, TabsState> {
return; return;
} }
// 当前 tab 可能不可见,所以需要自动切到一个可见的 tab, 向前找,找一圈
const tabIndex = findIndex(tabs, (tab: TabProps, index) => const tabIndex = findIndex(tabs, (tab: TabProps, index) =>
tab.hash ? tab.hash === this.state.activeKey : index === this.state.activeKey tab.hash ? tab.hash === this.state.activeKey : index === this.state.activeKey
); );
if (tabs[tabIndex] && !isVisible(tabs[tabIndex], this.props.data)) { if (tabs[tabIndex] && !isVisible(tabs[tabIndex], this.props.data)) {
let i = tabIndex - 1; let len = tabs.length;
while (tabs[i]) { let i = (tabIndex - 1) + len;
if (isVisible(tabs[i], data)) { let tries = len - 1;
let activeKey = tabs[i].hash || i;
while (tries--) {
const index = (i--) % len;
if (isVisible(tabs[index], data)) {
let activeKey = tabs[index].hash || index;
this.setState({ this.setState({
activeKey activeKey
}) })
break; break;
} }
i--;
} }
} }
} }