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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 12x 6x 6x 6x 6x 6x 6x 6x 6x 28x 28x | import React, { useRef, useEffect, useState } from "react"; import classNames from "classnames"; import { TabItem } from "./TabItem"; import { Tab } from "./TabProps"; import { Button } from "../button"; import { useConfig } from "../_util/config-context"; export interface TabBarProps { activeId: string; tabs: Tab[]; onActive: (tab: Tab, evt: React.SyntheticEvent) => void; addon: React.ReactNode; vertical: boolean; maxHeight: number; tabBarRender: (children: JSX.Element, tab: Tab) => JSX.Element; } export function TabBar({ activeId, tabs, onActive, addon, vertical, maxHeight, tabBarRender, }: TabBarProps) { const { classPrefix } = useConfig(); // 插件区域宽度 const addonsRef = useRef<HTMLDivElement>(null); const [addonWidth, setAddonWidth] = useState<number>(0); useEffect(() => { Eif (addonsRef.current) { setAddonWidth(addonsRef.current.clientWidth); } }, [addon]); // Scroll 相关 const scrollAreaRef = useRef<HTMLDivElement>(null); const tabListRef = useRef<HTMLUListElement>(null); const buttonRef = useRef<HTMLButtonElement>(null); const activeItemRef = useRef<HTMLLIElement>(null); const [offset, setOffset] = useState<number>(0); const [scrolling, setScrolling] = useState<boolean>(false); // resize 或 tabs/addon 变化时重置滚动 useEffect(handleScroll, [tabs, addon]); useEffect(() => { window.addEventListener("resize", handleScroll); return () => window.removeEventListener("resize", handleScroll); }, []); // eslint-disable-line react-hooks/exhaustive-deps useEffect(handleActiveItemIntoView, [activeId]); // 设置滚动状态 function handleScroll() { const scrolling = getMaxOffset() > 0; setScrolling(scrolling); // 无需滚动时重置位置 Eif (!scrolling) { setOffset(0); } else { handleActiveItemIntoView(); } } // 处理滚动至当前选中 TabItem function handleActiveItemIntoView() { setTimeout(() => { if (!scrollAreaRef.current || !activeItemRef.current) { return; } const scrollAreaRect = scrollAreaRef.current.getBoundingClientRect(); const activeItemRect = activeItemRef.current.getBoundingClientRect(); const [startPropertyName, endPropertyName] = vertical ? ["top", "bottom"] : ["left", "right"]; const buttonProperty = vertical ? buttonRef.current.clientHeight : buttonRef.current.clientWidth; const startDelta = scrollAreaRect[startPropertyName] - activeItemRect[startPropertyName] + buttonProperty; const endDelta = activeItemRect[endPropertyName] - scrollAreaRect[endPropertyName] + buttonProperty; if (startDelta > 0) { setOffset(Math.min(0, offset + startDelta)); } else if (endDelta > 0) { setOffset(Math.max(0 - getMaxOffset(), offset - endDelta)); } }, 0); } // 获取最大滚动偏移 function getMaxOffset() { // 垂直时不指定 maxHeight 不触发滚动 if (!scrollAreaRef.current || (vertical && !maxHeight)) { return 0; } const propertyName = vertical ? "clientHeight" : "clientWidth"; const scrollAreaProperty = vertical ? maxHeight : scrollAreaRef.current[propertyName]; const tabListProperty = tabListRef.current[propertyName]; const buttonProperty = buttonRef.current[propertyName]; Eif (scrollAreaProperty >= tabListProperty) { return 0; } return tabListProperty - (scrollAreaProperty - buttonProperty * 2); } // 获取最大单次滚动步长 function getStep() { const propertyName = vertical ? "clientHeight" : "clientWidth"; return ( scrollAreaRef.current[propertyName] - buttonRef.current[propertyName] * 2 ); } function handleBackward() { setOffset(offset => Math.min(0, offset + getStep())); } function handleForward() { setOffset(offset => Math.max(0 - getMaxOffset(), offset - getStep())); } return ( <div className={`${classPrefix}-tabs__tabbar`}> <div ref={scrollAreaRef} className={classNames(`${classPrefix}-tabs__scroll-area`, { "is-scrolling": scrolling, })} style={{ maxHeight: vertical ? maxHeight : undefined, marginRight: addonWidth, }} > <ul ref={tabListRef} className={`${classPrefix}-tabs__tablist`} style={{ transition: "transform ease-out .2s", transform: vertical ? `translate3d(0, ${offset}px, 0)` : `translate3d(${offset}px, 0, 0)`, }} > {tabs.map(tab => ( <TabItem ref={tab.id === activeId ? activeItemRef : undefined} key={tab.id} label={tab.label} actived={tab.id === activeId} disabled={tab.disabled} onClose={tab.onClose ? evt => tab.onClose(tab, evt) : null} onClick={evt => onActive(tab, evt)} render={children => tabBarRender(children, tab)} /> ))} </ul> <Button ref={buttonRef} className={`${classPrefix}-tabs__backward`} type="icon" icon={vertical ? "arrowup" : "arrowleft"} disabled={offset >= 0} onClick={handleBackward} /> <Button className={`${classPrefix}-tabs__forward`} type="icon" icon={vertical ? "arrowdown" : "arrowright"} disabled={offset <= 0 - getMaxOffset()} onClick={handleForward} /> </div> <div className={`${classPrefix}-tabs__addons`} ref={addonsRef}> {addon} </div> </div> ); } |