Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | 19x 5x 23x 6x 6x 5x 5x 6x 6x 5x 5x 6x 6x 6x 6x 26x 25x 25x 25x 25x 6x 25x 19x 25x 13x 25x 6x 19x 1x 1x 6x 6x 6x | import React, { useState, useRef } from "react"; import classNames from "classnames"; import { isChildOfType } from "../_util/is-child-of-type"; import { TabBar } from "./TabBar"; import { TabPanel } from "./TabPanel"; import { TabsProps, Tab } from "./TabProps"; import { useConfig } from "../_util/config-context"; const getHiddenPanelStyle: ( vertical: boolean ) => React.CSSProperties = vertical => ({ height: 0, width: 0, overflow: "hidden", opacity: 0, padding: 0, margin: 0, pointerEvents: "none", display: "block", transform: vertical ? "translate3d(10px, 0, 0)" : "translate3d(0, 10px, 0)", }); const animatedPanelStyle: React.CSSProperties = { transition: "opacity .45s ease, transform .45s ease", transformOrigin: "center", }; const defaultTabBarRender = c => <a>{c}</a>; export function Tabs({ tabs = [], destroyInactiveTabPanel = true, activeId, onActive, defaultActiveId, children, animated = true, placement, ceiling, addon, maxHeight, className, style, tabBarRender = defaultTabBarRender, }: TabsProps) { const { classPrefix } = useConfig(); // 如果都没有定义,获取第一个没被禁用的选项卡 id if ( typeof activeId === "undefined" && typeof defaultActiveId === "undefined" ) { const firstAvaliable = tabs.find(x => !x.disabled); // eslint-disable-next-line no-param-reassign defaultActiveId = firstAvaliable ? firstAvaliable.id : null; } // 注意 useState 是个 Hooks,不能写在 if 里面 const [internalActiveId, setInternalActiveId] = useState(defaultActiveId); // 非受控模式下,使用内部状态 if (typeof activeId === "undefined") { // eslint-disable-next-line no-param-reassign activeId = internalActiveId; // eslint-disable-next-line no-param-reassign onActive = (onActive => (tab: Tab, evt: React.SyntheticEvent) => { setInternalActiveId(tab.id); if (typeof onActive === "function") { onActive(tab, evt); } })(onActive); } const panelRenderedSetRef = useRef<Set<string>>(new Set()); function getTabPanels() { const panelRenderedSet = panelRenderedSetRef.current; const panelChildren = []; React.Children.forEach(children, (child, index) => { if (isChildOfType(child, TabPanel)) { const { props, key } = child; const { style = {}, id } = props; const active = activeId === props.id; if (active && !panelRenderedSet.has(activeId)) { panelRenderedSet.add(activeId); } if (!active) { Object.assign(style, getHiddenPanelStyle(placement === "left")); } if (animated) { Object.assign(style, animatedPanelStyle); } if ( child.props.forceRender || (destroyInactiveTabPanel && active) || (!destroyInactiveTabPanel && panelRenderedSet.has(props.id)) ) { panelChildren.push( React.cloneElement(child, { key: key || id, style }) ); } else { panelChildren.push( <TabPanel id={id} key={key || id} style={style} /> ); } } else Eif (React.isValidElement(child)) { panelChildren.push(React.cloneElement(child, { key: index })); } else { panelChildren.push(<div key={index}>{child}</div>); } }); return panelChildren; } const containerClassName = classNames(`${classPrefix}-tabs`, { // 垂直布局 [`${classPrefix}-tabs--vertical`]: placement === "left", [`${classPrefix}-tabs--ceiling`]: ceiling, [className]: className, }); return ( <div className={containerClassName} style={style}> <TabBar tabs={tabs} activeId={activeId} onActive={onActive} addon={addon} vertical={placement === "left"} maxHeight={maxHeight} tabBarRender={tabBarRender} /> {getTabPanels()} </div> ); } |