/
index.tsx
176 lines (160 loc) · 5.3 KB
/
index.tsx
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
import React, { useEffect, useState, useRef, useCallback, useMemo } from 'react';
import { IProps, HTMLDivProps } from '@uiw/utils';
import Pane from './Pane';
import './style/index.less';
import Popover from '@uiw/react-popover';
export * from './Pane';
Tabs.Pane = Pane;
export interface TabsProps extends IProps, HTMLDivProps {
prefixCls?: string;
activeKey?: string;
type?: 'default' | 'line' | 'card';
children?: React.ReactNode;
onTabClick?: (key: string, item: React.ReactElement, e: React.MouseEvent) => void;
}
type FlowNavType = {
content: number;
nav: Array<Record<'index' | 'width' | 'curWidth', number>>;
flowLeft: number;
displayStart: number;
displayEnd: number;
};
export default function Tabs(props: TabsProps) {
const {
prefixCls = 'w-tabs',
className,
children,
type = 'default',
activeKey: _,
onTabClick,
...elementProps
} = props;
const [activeKey, setActiveKey] = useState(props.activeKey);
const [slideStyle, setSlideStyle] = useState({ width: 0, left: 0 });
const activeItem = useRef<HTMLDivElement | undefined>();
const cls = [prefixCls, className, type ? `${prefixCls}-${type}` : null].filter(Boolean).join(' ').trim();
const [flowNav, flowNavSet] = useState<FlowNavType>({
content: 0,
nav: [],
flowLeft: -1,
displayStart: 0,
displayEnd: 0,
});
const [hiddenNav, hiddenNavSet] = useState<Array<number>>([]);
const deviation = 15;
const [nodes, nodesSet] = useState<any>();
const divContentRef = useCallback((node) => {
if (node !== null) {
nodesSet(nodes);
node.addEventListener('scroll', (e: any) => {
const { clientWidth, scrollLeft } = e.target;
flowNav.displayStart = scrollLeft;
flowNav.displayEnd = clientWidth + scrollLeft;
flowNavSet({ ...flowNav });
});
flowNav.displayEnd = node.getBoundingClientRect().width;
flowNavSet({ ...flowNav });
}
}, []);
const divNavRef = useCallback((node, key: number) => {
if (node !== null) {
node.addEventListener('click', (e: any) => {
activeItem.current = node;
});
divNavWidthChange(node.getBoundingClientRect().width, key);
}
}, []);
const divNavWidthChange = (width: number, index: number) => {
let curWidth = 0;
flowNav.nav.slice(0, index + 1).forEach((nav) => (curWidth += nav.width));
flowNav.nav[index] = { width, curWidth: Math.floor(curWidth), index };
flowNavSet(flowNav);
};
useEffect(() => {
showHideenNav();
}, [flowNav.displayEnd > flowNav.nav[flowNav.nav.length - 1]?.curWidth]);
const showHideenNav = () => {
const hiddenNav: Array<number> = [];
if (flowNav.nav.length > 0) {
flowNav.nav.forEach((item) => {
const curWidth = item.curWidth - deviation;
if (curWidth < flowNav.displayStart || curWidth > flowNav.displayEnd) {
hiddenNav.push(item.index);
}
});
hiddenNavSet(hiddenNav);
}
};
useEffect(() => setActiveKey(props.activeKey), [props.activeKey]);
useEffect(() => calcSlideStyle(), [activeKey]);
function calcSlideStyle() {
if (activeItem.current && type === 'line') {
setSlideStyle({
width: activeItem.current.clientWidth,
left: activeItem.current.offsetLeft,
});
}
}
return (
<div className={cls} {...elementProps}>
<div style={{ display: 'flex' }}>
<div style={{ overflow: 'hidden' }}>
<div className={`${prefixCls}-bar`} ref={divContentRef}>
<div className={`${prefixCls}-nav`} style={{ width: 'max-content' }}>
{renderNav(children)}
</div>
</div>
</div>
<div style={slideStyle} className={`${prefixCls}-slide`} />
{hiddenNav.length > 0 && (
<Popover
trigger="click"
placement="bottomRight"
visibleArrow={false}
content={
<div className={`${prefixCls}-nav-hidden`}>
{renderNav(hiddenNav.map((idx) => (children as Array<React.ReactElement>)[idx]))}
</div>
}
>
<div onClick={showHideenNav} className={`${prefixCls}-flow-content`}>
<span>…</span>
</div>
</Popover>
)}
</div>
{React.Children.map(children, (item: any) => {
if (!item || activeKey !== item.key) {
return null;
}
return React.cloneElement(item, Object.assign({}, item.props, {}));
})}
</div>
);
function renderNav(children: React.ReactNode): React.ReactNode {
return React.Children.map(children as React.ReactElement[], (item: React.ReactElement, key: number) => {
if (!item) {
return null;
}
const divProps: HTMLDivProps = {
className: [
`${prefixCls}-item`,
item.key === activeKey ? 'active' : null,
item.props.disabled ? 'disabled' : null,
]
.filter(Boolean)
.join(' ')
.trim(),
children: item.props.label,
};
if (!item.props.disabled) {
divProps.onClick = (e: React.MouseEvent) => {
setActiveKey(item.key as string);
onTabClick && onTabClick(item.key as string, item, e);
calcSlideStyle();
};
}
return <div key={key} ref={(ref) => divNavRef(ref, key)} {...divProps} />;
});
}
}