diff --git a/components/Tabs.js b/components/Tabs.js index 6f08aada..eb1e2e69 100644 --- a/components/Tabs.js +++ b/components/Tabs.js @@ -1,4 +1,4 @@ -import React, { useState } from 'react' +import { useState } from 'react'; /** * Tabs切换标签 @@ -6,59 +6,37 @@ import React, { useState } from 'react' * @returns */ const Tabs = ({ className, children }) => { - const [currentTab, setCurrentTab] = useState(0) + const [currentTab, setCurrentTab] = useState(0); - if (!children) { - return <> + const validChildren = children.filter(c => c); + + if (validChildren.length === 0) { + return <>; } - children = children.filter(c => c && c !== '') - - let count = 0 - children.forEach(e => { - if (e) { - count++ - } - }) - - if (count === 0) { - return <> - } - - if (count === 1) { - return
- {children} -
- } - - function tabClickHandle(i) { - setCurrentTab(i) - } - - return
- -
- {children.map((item, index) => { - return
- {currentTab === index && item} -
- })} -
+ return ( +
+ + {/* 标签切换的时候不销毁 DOM 元素,使用 CSS 样式进行隐藏 */} +
+ {validChildren.map((item, index) => ( +
+ {item} +
+ ))} +
-} + ); +}; -export default Tabs +export default Tabs;