-
Notifications
You must be signed in to change notification settings - Fork 2
/
GroupBar.js
175 lines (171 loc) · 5.05 KB
/
GroupBar.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
/**
* 分组滑动组件
* 比如联系人按拼音分组, 侧边栏滑动快速选择
* index的值不能重复
* example:
*
var indexs = [
'A', 'B', 'C', 'Z',
// 支持自定义每项, 必须要有render方法和index,
// index 为每项的唯一标示.
{
index: '#',
render: (item) => {
return (
<Text>{item.index}</Text>
)
}
}
]
<GroupBar
indexs={indexs}
onChange={this.onChange.bind(this)}
fontStyle={{color: 'rgba(5, 169, 214, 0.5)',fontSize: 14, backgroundColor: 'rgba(0,0,0,0)'}}
style={{flex: 1, height: 300}}
>
</GroupBar>
*/
import React, { Component } from 'react';
import { View, Text, StyleSheet } from 'react-native';
export default class GroupBar extends Component {
constructor(props, context) {
super(props, context);
this.state = {
indexs: [],
}
// 当前划过的索引
this.curKey = null;
}
componentWillMount() {
let { indexs } = this.props;
indexs = indexs || [];
this.setState({
indexs
});
}
componentWillUnmount() {
// 卸载时清空
this.curKey = null;
this.state = null;
this._unmount = true;
}
componentWillReceiveProps(nextProps) {
if (nextProps.indexs) {
this.setState({
indexs: nextProps.indexs
});
}
}
renderBar() {
let {indexs} = this.state;
return indexs.map((bar, i) => {
// indexs 的每项 支持string / object类型
let type = typeof bar;
let inner = null;
// 当前项的索引值
let index = null;
if (type === 'string' || type === 'number') {
inner = <Text style={this.props.fontStyle}>{bar}</Text>;
index = bar;
}
if (type === 'object'
&& typeof bar.render === 'function'
&& bar.index !== undefined) {
// object 类型的项需要传入 render 和 index 选项
inner = bar.render(bar, i);
index = bar.index;
}
return (
<View key={i} onLayout={this._itemlayout.bind(this, index)} style={this.props.itemStyle}>
{inner}
</View>
);
})
}
_touchmove(e) {
let { locationX, locationY , pageY, pageX} = e.nativeEvent;
// 滑动方向 ,支持垂直 和水平滑动
let { direction } = this.props;
let isVertical = direction === 'vertical';
let diff = isVertical ? (pageY - this.rect.pageY) : (pageX - this.rect.pageX);
let nkey = null;
for (var key in this.barItems) {
if (!this.barItems.hasOwnProperty(key) || key === nkey) {
continue;
}
// 起始边界
let start = this.barItems[key][isVertical ? 'y' : 'x'];
// 每个元素的范围
let range = this.barItems[key][isVertical ? 'height' : 'width'];
// 元素结束边界
let end = start + range;
if (diff >= start && diff < end) {
if (key !== nkey) {
nkey = key;
break;
}
}
}
if (nkey && this.curKey !== nkey) {
this.curKey = nkey;
if (typeof this.props.onChange === 'function') {
this.props.onChange(nkey, e);
}
} else {
this.curKey = null;
}
if (this.props.onTouchMove) {
this.props.onTouchMove(e);
}
}
_innerLayout(e) {
this.refs.bar.measure((x, y, width, height, pageX, pageY) => {
if(this._unmount) return;
this.rect = {x, y, width, height, pageX, pageY};
});
if (this.props.onLayout) {
this.props.onLayout(e);
}
}
_itemlayout(key, e) {
// 保存好每个元素的 大小、坐标, 以index为 key
if (!this.barItems) {
this.barItems = {};
}
key && (this.barItems[key] = e.nativeEvent.layout);
}
render() {
return(
<View
{...this.props}
ref='bar'
onLayout={this._innerLayout.bind(this)}
onTouchMove={this._touchmove.bind(this)}
>
{this.props.children}
{this.renderBar()}
</View>
)
}
}
GroupBar.propTypes = {
...View.propTypes,
// index值不能重复
indexs: React.PropTypes.array,
// 手指滑过时的回调
onChange: React.PropTypes.func,
// 方向
direction: React.PropTypes.oneOf(['vertical', 'horizontal'])
};
GroupBar.defaultProps = {
indexs: [],
direction: 'vertical',
itemStyle: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
fontStyle: {
backgroundColor: 'rgba(0,0,0,0)'
},
}