-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSnapSlider.js
More file actions
155 lines (148 loc) · 4.89 KB
/
SnapSlider.js
File metadata and controls
155 lines (148 loc) · 4.89 KB
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
'use strict';
var React = require('react');
var ReactNative = require('react-native');
var PropTypes = require('prop-types');
var createReactClass = require('create-react-class');
var {
StyleSheet,
Slider,
Text,
View,
ViewPropTypes
} = ReactNative;
var SnapSlider = createReactClass({
propTypes: {
onSlidingComplete: PropTypes.func,
style: ViewPropTypes.style,
containerStyle: ViewPropTypes.style,
itemWrapperStyle: ViewPropTypes.style,
itemStyle: Text.propTypes.style,
items: PropTypes.array.isRequired,
defaultItem: PropTypes.number,
labelPosition: PropTypes.string
},
getInitialState() {
var sliderRatio = this.props.maximumValue / (this.props.items.length - 1);
var value = sliderRatio * this.props.defaultItem;
var item = this.props.defaultItem;
return {
sliderRatio: sliderRatio,
value: value,
item: item,
adjustSign: 1,
itemWidth: [],
sliderWidth: 0,
sliderLeft: 0,
};
},
getDefaultProps() {
return {
minimumValue: 0,
maximumValue: 1,
};
},
_sliderStyle() {
return [defaultStyles.slider, {width: this.state.sliderWidth, left: this.state.sliderLeft}, this.props.style];
},
_onSlidingCompleteCallback: function (v) {
//pad the value to the snap position
var halfRatio = this.state.sliderRatio / 2;
var i = 0;
for (;;) {
if ((v < this.state.sliderRatio) || (v <= 0)) {
if (v >= halfRatio) {
i++;
}
break;
}
v = v - this.state.sliderRatio;
i++;
}
var value = this.state.sliderRatio * i;
//Move the slider
value = value + (this.state.adjustSign * 0.000001);//enforce UI update
if (this.state.adjustSign > 0) {
this.setState({adjustSign: -1});
} else {
this.setState({adjustSign: 1});
}
this.setState({value: value, item: i}, () =>
//callback
this.props.onSlidingComplete(i)
);
},
/*
componentWillUpdate() {
//get the width for all items
var iw = [];
for (var i = 0; i < this.props.items.length; i++) {
var node = eval('this.refs.t' + i);
node.measure(function (ox, oy, width, height, px, py) {
iw.push(width);
});
}
},
*/
_getItemWidth: function (x) {
var width = x.nativeEvent.layout.width;
var itemWidth = this.state.itemWidth;
itemWidth.push(width);
this.setState({itemWidth: itemWidth});
//we have all itemWidth determined, let's update the silder width
if (this.state.itemWidth.length == this.props.items.length) {
var max = Math.max.apply(null, this.state.itemWidth);
if (this.refs.slider && this.state.sliderWidth > 0) {
var that = this;
var w, l;
var buffer = 30;//add buffer for the slider 'ball' control
if(buffer > w){
buffer = 0;
}
w = that.state.sliderWidth - max;
w = w + buffer;
l = max / 2;
l = l - buffer / 2;
that.setState({sliderWidth: w});
that.setState({sliderLeft: l});
}
}
},
_getSliderWidth: function (e) {
var {x, y, width, height} = e.nativeEvent.layout;
this.setState({sliderWidth: width});
},
_labelView() {
var itemStyle = [defaultStyles.item, this.props.itemStyle];
let labels = this.props.items.map((i, j) => <Text key={i.value} ref={"t"+j} style={itemStyle} onLayout={this._getItemWidth}>{i.label}</Text>);
return (
<View style={[defaultStyles.itemWrapper, this.props.itemWrapperStyle]}>
{ labels }
</View>
);
},
render() {
var that = this;
return (
<View onLayout={that._getSliderWidth} style={[defaultStyles.container, this.props.containerStyle]}>
{this.props.labelPosition == 'top' ? this._labelView() : null}
<Slider ref="slider" {...this.props} style={this._sliderStyle()} onSlidingComplete={(value) => this._onSlidingCompleteCallback(value)} value={this.state.value} />
{this.props.labelPosition === undefined || this.props.labelPosition == 'bottom' ? this._labelView() : null}
</View>
);
}
});
var defaultStyles = StyleSheet.create({
container: {
alignSelf: 'stretch',
},
slider: {
},
itemWrapper: {
justifyContent: 'space-between',
alignSelf: 'stretch',
flexDirection: 'row',
},
item: {
},
});
module.exports = SnapSlider;