-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stepper.js
244 lines (210 loc) · 6.33 KB
/
Stepper.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
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
240
241
242
243
244
import React, { Component, PropTypes } from 'react'
import { Alert, StyleSheet, Text, TouchableOpacity, View } from 'react-native'
class Stepper extends Component {
constructor(props) {
super(props);
this.state = {
value: props.initValue, //this.props.initValue
hasReachedMinValue: false,
hasReachedMaxValue: false,
};
this.timer = null;
this.validateInitValueAgainstMaxValue(props.initValue, props.maxValue);
this.validateInitValueAgainstMinValue(props.initValue, props.minValue);
if (!props.ignoreMaxValidation) {
this.validateRangeMaxValue(props.initValue, props.stepValue, props.maxValue);
}
if (!props.ignoreMinValidation) {
this.validateRangeMinValue(props.initValue, props.stepValue, props.minValue);
}
}
// Checks if initValue are lower than maxValue
validateInitValueAgainstMaxValue(initValue, maxValue) {
if (maxValue !== null && initValue > maxValue) {
throw 'The initital value can\'t be higher than max value';
}
return true;
}
// Checks if minValue are lower than maxValue
validateInitValueAgainstMinValue(initValue, minValue) {
if (minValue !== null && initValue < minValue) {
throw 'The initital value can\'t be lower than min value';
}
return true;
}
// Checks if range between initalValue and MaxValue are valids
validateRangeMaxValue(initValue, stepValue, maxValue) {
if (maxValue !== null && (((maxValue - initValue) % stepValue) !== 0)) {
throw 'The initital value or step value are not valids with this max value';
}
return true;
}
// Checks if range between initalValue and MinValue are valids
validateRangeMinValue(initValue, stepValue, minValue) {
if (minValue !== null && (((initValue - minValue) % stepValue) !== 0)) {
throw 'The initital value or step value are not valids with this min value';
}
return true;
}
// Check if reached max value
hasReachedMaxValue(value) {
if (this.props.maxValue === null) {
return false;
}
return (value === this.props.maxValue);
}
// Check if reached min value
hasReachedMinValue(value) {
if (this.props.minValue === null) {
return false;
}
return (value === this.props.minValue);
}
// Increase value
increase(stepValue) {
const { state } = this;
if (this.hasReachedMaxValue(state.value)) {
this.stopTimeInterval();
return this.handleMessage(this.props.maxMessage);
}
const currentValue = state.value + stepValue;
this.setState({ ...state, value: currentValue });
this.valueChanged(currentValue);
}
// Handle max message
handleMessage(message) {
if (typeof message === 'string') {
return Alert.alert(message);
}
if (typeof message === 'function') {
return message();
}
return false;
}
// Decrease value
decrease(stepValue) {
const { state } = this;
if (this.hasReachedMinValue(state.value)) {
this.stopTimeInterval();
return this.handleMessage(this.props.minMessage);
}
const currentValue = state.value - stepValue;
this.setState({ ...state, value: currentValue });
this.valueChanged(currentValue);
}
// On press increase button
onPressIncreaseButton() {
this.increase(this.props.stepValue);
}
// When users press and hold the button
// Todo: speed up when is a long press
onPressInIncreaseButton() {
this.timer = setInterval(
() => {
if (!this.hasReachedMaxValue(this.state.value)) {
this.increase(this.props.stepValue);
}
}, 50);
}
// On press decrease button
onPressDecreaseButton() {
this.decrease(this.props.stepValue);
}
// On press decrease button and hold
onPressInDecreaseButton() {
this.timer = setInterval(
() => {
if (!this.hasReachedMinValue(this.state.value)) {
this.decrease(this.props.stepValue);
}
}, 50);
}
// Stops time interval
stopTimeInterval() {
clearInterval(this.timer);
}
onPressOutButton() {
this.stopTimeInterval();
}
// Value changed: use this function to get value
valueChanged(value) {
if (this.props.valueChanged) {
this.props.valueChanged(value);
}
}
render() {
const { containerStyle, decreaseButtonStyle, increaseButtonStyle } = this.props.style;
return (
<View style={containerStyle}>
<TouchableOpacity
style={decreaseButtonStyle}
onPress={this.onPressDecreaseButton.bind(this)}
// onPressIn={this.onPressInDecreaseButton.bind(this)}
// onPressOut={this.onPressOutButton.bind(this)}
>{this.props.decreaseComponent}</TouchableOpacity>
{/* <Text>Oi: {this.state.value}</Text> */}
<TouchableOpacity
style={increaseButtonStyle}
onPress={this.onPressIncreaseButton.bind(this)}
// onPressIn={this.onPressInIncreaseButton.bind(this)}
// onPressOut={this.onPressOutButton.bind(this)}
>{this.props.increaseComponent}</TouchableOpacity>
</View>
);
}
}
// Default props values
Stepper.defaultProps = {
// Scheleton of style
style: StyleSheet.create({
containerStyle: {
},
decreaseButtonStyle: {
},
increaseButtonStyle: {
}
}),
initValue: 0,
minValue: null,
maxValue: null,
stepValue: 1,
ignoreMinValidation: false,
ignoreMaxValidation: false,
valueChanged: null,
decreaseComponent: (<Text>-</Text>),
increaseComponent: (<Text>+</Text>),
minMessage: null,
maxMessage: null
};
// PropTypes definitions
Stepper.propTypes = {
// Initial value
initValue: PropTypes.number,
// Min value, if null is unlimited
minValue: PropTypes.number,
// Max value, if null is unlimited
maxValue: PropTypes.number,
// Step value
stepValue: PropTypes.number,
ignoreMinValidation: PropTypes.bool,
ignoreMaxValidation: PropTypes.bool,
valueChanged: PropTypes.func.isRequired,
decreaseComponent: PropTypes.oneOfType([
PropTypes.instanceOf(Component),
PropTypes.instanceOf(Object),
]),
increaseComponent: PropTypes.oneOfType([
PropTypes.instanceOf(Component),
PropTypes.instanceOf(Object),
]),
style: PropTypes.instanceOf(Object).isRequired,
minMessage: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
]),
maxMessage: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
])
};
export default Stepper;