-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrollTabs.react.js
204 lines (175 loc) · 6.42 KB
/
ScrollTabs.react.js
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
class ScrollTabs extends React.Component {
constructor (props) {
super(props);
this.state = {
selectedIndex: this.props.selectedIndex ? this.props.selectedIndex : 0,
showHeadMore: false,
showTailMore: false,
width: undefined
}
}
init () {
this.moreWidth = $(this.headMoreEl).outerWidth();
this.handleResizeThis = this.handleResize.bind(this);
this.handleResize();
$(window).on('resize', this.handleResizeThis);
}
handleResize () {
const width = $(this.container).width();
if (width !== this.state.width) {
this.setState({width: $(this.container).width()});
}
}
handleTabClick (ev) {
const idx = Number($(ev.target).closest('.tab-index').data('tabIndex'));
this.setState({
selectedIndex: idx,
showHeadMore: false,
showTailMore: false,
});
if (this.props.handleTabClick) {
this.props.handleTabClick(this.props.tabs[idx]);
}
}
handleMoreMouseEnter (ev) {
let state = {};
state[$(ev.target).closest('.scroll-tabs-more-link').data('showState')] = true;
this.setState(state);
}
handleMoreMouseLeave (ev) {
let state = {};
state[$(ev.target).closest('.scroll-tabs-more-link').data('showState')] = false;
this.setState(state);
}
componentWillReceiveProps (nextProps) {
if (nextProps.selectedIndex !== undefined &&
this.state.selectedIndex !== nextProps.selectedIndex) {
this.setState({selectedIndex: nextProps.selectedIndex});
}
this.handleResize();
}
componentDidMount () {
this.init();
}
componentWillUnmount () {
$(window).off('resize', this.handleResizeThis);
}
renderTabs (tabs, tabWidth, selectedIndex) {
return tabs.map((tab, i) =>
<div key={i}
className={classNames('scroll-tab tab-index', {
selected: i + this.headIndex === selectedIndex
})}
data-tab-index={i + this.headIndex}
style={{width: tabWidth}}
onClick={this.handleTabClick.bind(this)}
>{this.props.renderTabContent ? this.props.renderTabContent(tab) :
<span className='label'>tab</span>}</div>
);
}
renderMore (tabs, indexOffset) {
return tabs.map((tab, i) =>
<div key={i}
className='scroll-tab-more tab-index'
data-tab-index={i + indexOffset}
onClick={this.handleTabClick.bind(this)}
>{this.props.renderMoreTabContent ? this.props.renderMoreTabContent(tab) :
<span className='label'>tab</span>}</div>
);
}
divideTabs (allTabs, containerWidth, minWidth, selectedIndex) {
let tabTotal, tabWidth, headIndex;
for (tabTotal = allTabs.length; tabTotal > 0; tabTotal--) {
tabWidth = (containerWidth - 2 * this.moreWidth) / tabTotal;
if (tabWidth >= minWidth) {
break;
}
};
if (this.tabTotal === undefined) {
this.tabTotal = tabTotal;
headIndex = selectedIndex - tabTotal + 1;
// container not resized
} else if (tabTotal === this.tabTotal){
if (selectedIndex < this.headIndex) {
headIndex = selectedIndex;
} else if (selectedIndex >= this.headIndex + tabTotal) {
headIndex = selectedIndex - tabTotal + 1;
} else {
headIndex = this.headIndex;
}
// container resized
} else {
if (tabTotal > 0) {
headIndex = this.headIndex - tabTotal;
}
this.tabTotal = tabTotal;
}
if (headIndex < 0) {
headIndex = 0;
}
this.headIndex = headIndex;
return {
tabWidth: tabWidth,
headMore: allTabs.slice(0, this.headIndex),
tailMore: allTabs.slice(this.headIndex + this.tabTotal),
main: allTabs.slice(this.headIndex, this.headIndex + this.tabTotal)
};
}
render () {
let main = '',
headMore = '',
tailMore = '',
tabs;
if (this.state.width) {
tabs = this.divideTabs(this.props.tabs, this.state.width,
this.props.tabMinWidth, this.state.selectedIndex);
if (this.state.showHeadMore && tabs.headMore.length) {
headMore = (
<div className='scroll-tabs-more head'>
{this.renderMore(tabs.headMore, 0)}
</div>
);
}
if (this.state.showTailMore && tabs.tailMore.length) {
tailMore = (
<div className='scroll-tabs-more tail'>
{this.renderMore(tabs.tailMore, this.headIndex + tabs.main.length)}
</div>
);
}
main = this.renderTabs(tabs.main, tabs.tabWidth, this.state.selectedIndex);
}
return (
<div className='scroll-tabs'
ref={el => this.container = el}
>
<div ref={el => this.headMoreEl = el}
className={classNames('scroll-tabs-more-link head', {
show: tabs && tabs.headMore.length
})}
onMouseEnter={this.handleMoreMouseEnter.bind(this)}
onMouseLeave={this.handleMoreMouseLeave.bind(this)}
data-show-state='showHeadMore'
>
<span className='label' />
{headMore}
</div>
<div className='scroll-tabs-tabs'>
{main}
</div>
<div
className={classNames('scroll-tabs-more-link tail', {
show: tabs && tabs.tailMore.length
})}
onMouseEnter={this.handleMoreMouseEnter.bind(this)}
onMouseLeave={this.handleMoreMouseLeave.bind(this)}
data-show-state='showTailMore'
>
<span className='label' />
{tailMore}
</div>
</div>
);
}
}
window.ScrollTabs = ScrollTabs;