/
Tabs.tsx
239 lines (210 loc) · 6.96 KB
/
Tabs.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import React, { PureComponent, CSSProperties, ReactElement } from 'react';
import classnames from 'classnames';
import PropsType from './PropsType';
import TabPanel, { TabPanelProps } from './TabPanel';
import Carousel from '../carousel';
import { getTransformPropValue, getPxStyle, scrollLeftTo } from './util/index';
export interface TabsProps extends PropsType {
prefixCls?: string;
className?: string;
}
interface TabsStates {
value: number;
prevValue?: number;
}
const getSelectIndex = (children) => {
let selectIndex;
React.Children.forEach(children, (item, index) => {
if (item.props && item.props.selected) {
selectIndex = index;
}
});
return selectIndex;
};
// 这里用 94 因为如果当前的 tab 需要滚动 能方便能让用户,看出来是可以滚动的
const scrollTabTotalWidth = 94;
const defaultScrollThreshold = 3;
export default class Tabs extends PureComponent<TabsProps, TabsStates> {
static Panel: typeof TabPanel;
private carousel?: Carousel;
private layout?: HTMLUListElement;
static defaultProps = {
prefixCls: 'za-tabs',
disabled: false,
canSwipe: false,
scrollThreshold: defaultScrollThreshold,
};
constructor(props: Tabs['props']) {
super(props);
this.state = {
value: props.value || props.defaultValue || getSelectIndex(props.children) || 0,
// prevValue: null,
};
}
static getDerivedStateFromProps(nextProps: Tabs['props'], state: Tabs['state']) {
if ('value' in nextProps && nextProps.value !== state.prevValue) {
return {
value: nextProps.value,
prevValue: nextProps.value,
};
}
return null;
}
componentDidUpdate() {
this.calculateScorllLeftLocation();
}
getTabSize = (scrollThreshold: number, tabLength: number) => scrollTabTotalWidth / Math.min(scrollThreshold, tabLength);
setTablistRef = (ref: HTMLUListElement) => {
this.layout = ref;
};
setCarouselRef = (ref: Carousel) => {
this.carousel = ref;
};
onTabChange = (value: number) => {
const { onChange } = this.props;
if (!('value' in this.props)) {
this.setState({ value });
}
typeof onChange === 'function' && onChange(value);
};
onTabClick = (tab: ReactElement<TabPanel['props'], typeof TabPanel>, index: number) => {
const { disabled, canSwipe } = this.props;
if (disabled || tab.props.disabled) {
return;
}
if (canSwipe) {
this.carousel && this.carousel.onSlideTo(index);
return;
}
this.onTabChange(index);
};
renderTabs = (tab: ReactElement<TabPanelProps, typeof TabPanel>, index: number) => {
const { prefixCls, disabled } = this.props;
const { value } = this.state;
const itemCls = classnames(`${prefixCls}__tab`, tab.props.className, {
[`${prefixCls}__tab--disabled`]: disabled || tab.props.disabled,
[`${prefixCls}__tab--active`]: value === index,
});
const isScroll = this.isScroll();
const itemSize = this.calculateItemWidth();
const liStyle = isScroll ? { flex: `0 0 ${itemSize}%` } : {};
return (
<li
role="tab"
key={+index}
className={itemCls}
style={liStyle}
onClick={() => this.onTabClick(tab, index)}
>
{tab.props.title}
</li>
);
};
/**
* @description: 判断当前 child 是否大于 scrollThreshold
*/
isScroll = () => {
const { children, scrollThreshold } = this.props;
const ChildCount = React.Children.count(children);
return ChildCount > scrollThreshold;
};
/**
* @description: 计算每个item width
*/
calculateItemWidth = () => {
const { scrollThreshold } = this.props;
const itemSize = scrollTabTotalWidth / scrollThreshold;
return itemSize;
};
/**
* @description: 计算 line 大小和位置
*/
caclLineSizePos = () => {
const { value } = this.state;
const { children, scrollThreshold } = this.props;
const ChildCount = React.Children.count(children);
const isScroll = this.isScroll();
const size = isScroll ? scrollTabTotalWidth / scrollThreshold : 100 / ChildCount;
const pos = value * 100;
const transformValue = getPxStyle(pos, '%');
const styleUl = getTransformPropValue(transformValue);
return {
width: `${size}%`,
...styleUl,
};
};
/**
* @description: 计算滚动条移动位置
*/
calculateScorllLeftLocation = () => {
const screenWidth = document.body.clientWidth;
const { children, scrollThreshold } = this.props;
const { value } = this.state;
const isScroll = this.isScroll();
const ChildCount = React.Children.count(children);
// --- https://github.com/react-component/m-tabs/blob/master/src/DefaultTabBar.tsx
const size = this.getTabSize(scrollThreshold!, ChildCount);
const center = scrollThreshold! / 2;
const pos = Math.min(value, ChildCount - center - 0.5);
const skipSize = Math.min(-(pos - center + 0.5) * size, 0);
const scrollPos = (screenWidth * skipSize) / 100;
// ---
if (isScroll) {
if (this.layout) {
scrollLeftTo(this.layout, Math.abs(scrollPos), 0.3);
}
}
};
render() {
const { prefixCls, className, lineWidth, canSwipe, children, disabled, scrollThreshold } = this.props;
const { value } = this.state;
const classes = classnames(prefixCls, className);
// 渲染选项
const tabsRender = React.Children.map(children, this.renderTabs);
// 渲染内容
let contentRender;
if (canSwipe) {
contentRender = (
<Carousel
swipeable={!disabled}
direction="left"
showPagination={false}
activeIndex={value}
ref={this.setCarouselRef}
onChange={(v: number) => {
this.onTabChange(v);
}}
>
{React.Children.map(children, (item: ReactElement<TabPanel['props'], typeof TabPanel>, index: number) => <div key={+index}>{item.props.children}</div>)}
</Carousel>
);
} else {
contentRender = React.Children.map(children, (item: ReactElement<TabPanel['props'], typeof TabPanel>, index) => {
return item.props.children && <TabPanel {...item.props} selected={value === index} />;
});
}
const ChildCount = React.Children.count(children);
const lineStyle: CSSProperties = this.caclLineSizePos();
let lineInnerRender;
if (lineWidth) {
lineStyle.backgroundColor = 'transparent';
lineInnerRender = <span className={`${prefixCls}__line__inner`} style={{ width: lineWidth }} />;
}
const headerCls = classnames(`${prefixCls}__header`, {
[`${prefixCls}__scroll`]: ChildCount > scrollThreshold!,
});
return (
<div className={classes}>
<div className={headerCls}>
<ul role="tablist" ref={this.setTablistRef}>
{tabsRender}
<div className={`${prefixCls}__line`} style={lineStyle}>{lineInnerRender}</div>
</ul>
</div>
<div className={`${prefixCls}__body`}>
{contentRender}
</div>
</div>
);
}
}