-
Notifications
You must be signed in to change notification settings - Fork 0
/
spinner.js
109 lines (99 loc) · 2.85 KB
/
spinner.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
'use strict'
var React = require('react-native')
var {StyleSheet, Text, View, TouchableOpacity,} = React
var Spinner = React.createClass({
getInitialState: function () {
return {
value: this.props.value ? this.props.value : 0,
activeOpacity: 1,
}
},
getDefaultProps: function () {
return {
step: 1,
}
},
add: function () {
opearte.call(this, 1)
},
sub: function () {
opearte.call(this, -1)
},
render: function () {
var props = this.props,
value = this.state.value = this.props.value ? this.props.value : this.state.value
return (
<View style={styles.container}>
<TouchableOpacity
style={[styles.btn, styles.btnSub]}
onPress={this.sub}
activeOpacity={props.disabled ? 1 : 0.5}>
<Text style={[styles.btnText, props.disabled ? styles.btnTextDisabled : {}]}>-</Text>
</TouchableOpacity>
<Text style={styles.content}>
{value}
</Text>
<TouchableOpacity
style={[styles.btn, styles.btnAdd]}
onPress={this.add}
activeOpacity={props.disabled ? 1 : 0.5}>
<Text style={[styles.btnText, props.disabled ? styles.btnTextDisabled : {}]}>+</Text>
</TouchableOpacity>
</View>
)
}
})
function opearte(direction) {
this.setState(function (previousState, currentProps) {
if (!currentProps.disabled) {
var step = parseFloat(currentProps.step),
value = parseFloat(previousState.value),
maxStepDigit = 10,
result
// calculate
step = isNaN(step) ? 1 : step
result = parseFloat((value + step * direction).toPrecision(maxStepDigit))
// trigger onChange
this.props.onChange ? this.props.onChange(result) : null
return {value: result}
}
})
}
var styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
height: 50,
paddingTop: 8,
paddingBottom: 8,
borderBottomWidth: 1,
borderColor: '#ddd',
borderRadius: 4,
backgroundColor: '#fff',
},
content: {
flex: 1,
color: "#00afc7",
textAlign: 'center',
},
btn: {
width: 50,
alignSelf: 'stretch',
justifyContent: 'center',
borderColor: '#ddd',
},
btnText: {
color: "#00afc7",
textAlign: 'center',
},
btnTextDisabled: {
color: "#aaa",
},
btnAdd: {
borderLeftWidth: 1,
},
btnSub: {
borderRightWidth: 1,
},
})
module.exports = Spinner