1
- import React , { Component } from " react" ;
2
- import { View , StyleSheet , TouchableOpacity , Image , Text } from " react-native" ;
3
- import PropTypes from " prop-types" ;
1
+ import React , { Component } from ' react' ;
2
+ import { View , StyleSheet , TouchableOpacity , Image , Text } from ' react-native' ;
3
+ import PropTypes from ' prop-types' ;
4
4
5
5
const styles = StyleSheet . create ( {
6
6
container : {
7
- flexDirection : " row"
7
+ flexDirection : ' row' ,
8
8
} ,
9
9
button : {
10
10
flex : 1 ,
11
- justifyContent : " center" ,
12
- alignItems : " center"
11
+ justifyContent : ' center' ,
12
+ alignItems : ' center' ,
13
13
} ,
14
14
valueContainer : {
15
15
flex : 1 ,
16
- justifyContent : " center" ,
17
- alignItems : " center"
18
- }
16
+ justifyContent : ' center' ,
17
+ alignItems : ' center' ,
18
+ } ,
19
19
} ) ;
20
20
21
21
class UIStepper extends Component {
@@ -53,15 +53,15 @@ class UIStepper extends Component {
53
53
minimumValue : 0 ,
54
54
maximumValue : 100 ,
55
55
steps : 1 ,
56
- tintColor : " #0076FF" ,
57
- backgroundColor : " transparent" ,
58
- decrementImage : require ( " ./assets/decrement.png" ) ,
59
- incrementImage : require ( " ./assets/increment.png" ) ,
60
- imageWidth : 45 ,
61
- imageHeight : 27 ,
56
+ tintColor : ' #0076FF' ,
57
+ backgroundColor : ' transparent' ,
58
+ decrementImage : require ( ' ./assets/decrement.png' ) ,
59
+ incrementImage : require ( ' ./assets/increment.png' ) ,
60
+ imageWidth : 20 ,
61
+ imageHeight : 15 ,
62
62
width : 94 ,
63
63
height : 29 ,
64
- borderColor : " #0076FF" ,
64
+ borderColor : ' #0076FF' ,
65
65
borderWidth : 1 ,
66
66
borderRadius : 4 ,
67
67
onValueChange : null ,
@@ -71,7 +71,7 @@ class UIStepper extends Component {
71
71
onMaximumReached : null ,
72
72
wraps : false ,
73
73
displayValue : false ,
74
- textColor : " #0076FF" ,
74
+ textColor : ' #0076FF' ,
75
75
fontSize : 15 ,
76
76
overrideTintColor : false ,
77
77
vertical : false ,
@@ -80,8 +80,9 @@ class UIStepper extends Component {
80
80
constructor ( props ) {
81
81
super ( props ) ;
82
82
this . state = {
83
- value : this . props . initialValue
83
+ value : this . props . initialValue ,
84
84
} ;
85
+ console . log ( this . props ) ;
85
86
}
86
87
decrement = ( ) => {
87
88
const { steps, onDecrement } = this . props ;
@@ -95,37 +96,70 @@ class UIStepper extends Component {
95
96
value += steps ;
96
97
this . validate ( value , onIncrement ) ;
97
98
} ;
98
- isExternalImage = image => typeof image === " string" ;
99
+ isExternalImage = image => typeof image === ' string' ;
99
100
resolveImage = image => {
100
101
if ( this . isExternalImage ( image ) ) {
101
102
return { uri : image } ;
102
103
}
103
104
return image ;
104
105
} ;
105
106
resolveStyles = image => {
106
- const { tintColor, height, width, overrideTintColor } = this . props ;
107
+ const {
108
+ tintColor,
109
+ height,
110
+ width,
111
+ overrideTintColor,
112
+ imageHeight,
113
+ imageWidth,
114
+ buttonPadding,
115
+ } = this . props ;
116
+ const containerHeight = height / 3 ;
117
+ const containerWidth = width / 3 ;
118
+
107
119
if ( this . isExternalImage ( image ) ) {
108
120
const styles = {
109
121
flex : 1 ,
110
- alignSelf : "stretch"
111
- }
112
- if ( overrideTintColor ) {
122
+ alignSelf : 'stretch' ,
123
+ width : this . getImageWidth ( ) ,
124
+ height : this . getImageHeight ( ) ,
125
+ } ;
126
+ if ( overrideTintColor ) {
113
127
styles . tintColor = tintColor ;
114
128
}
115
129
return styles ;
116
130
}
117
131
return {
118
- tintColor
132
+ tintColor,
133
+ width : this . getImageWidth ( ) ,
134
+ height : this . getImageHeight ( ) ,
119
135
} ;
120
136
} ;
137
+ getImageHeight = ( ) => {
138
+ const { imageHeight, height } = this . props ;
139
+ const containerHeight = height ;
140
+
141
+ if ( imageHeight > containerHeight ) {
142
+ return height ;
143
+ }
144
+ return imageHeight ;
145
+ } ;
146
+ getImageWidth = ( ) => {
147
+ const { imageWidth, width } = this . props ;
148
+ const containerWidth = width / 3 ;
149
+
150
+ if ( imageWidth > containerWidth ) {
151
+ return containerWidth ;
152
+ }
153
+ return imageWidth ;
154
+ } ;
121
155
validate = ( value , callback ) => {
122
156
const {
123
157
minimumValue : min ,
124
158
maximumValue : max ,
125
159
onValueChange,
126
160
onMinimumReached,
127
161
onMaximumReached,
128
- wraps
162
+ wraps,
129
163
} = this . props ;
130
164
if ( min <= value && max >= value ) {
131
165
this . setState ( {
@@ -142,7 +176,7 @@ class UIStepper extends Component {
142
176
if ( value < min ) {
143
177
if ( wraps ) {
144
178
this . setState ( {
145
- value : max
179
+ value : max ,
146
180
} ) ;
147
181
if ( onValueChange ) {
148
182
onValueChange ( max ) ;
@@ -157,7 +191,7 @@ class UIStepper extends Component {
157
191
if ( value > max ) {
158
192
if ( wraps ) {
159
193
this . setState ( {
160
- value : min
194
+ value : min ,
161
195
} ) ;
162
196
if ( onValueChange ) {
163
197
onValueChange ( min ) ;
@@ -173,7 +207,7 @@ class UIStepper extends Component {
173
207
setValue = ( value , callback ) => {
174
208
const { onValueChange } = this . props ;
175
209
this . setState ( {
176
- value : value
210
+ value : value ,
177
211
} ) ;
178
212
if ( onValueChange ) {
179
213
onValueChange ( value ) ;
@@ -200,7 +234,7 @@ class UIStepper extends Component {
200
234
textColor,
201
235
fontSize,
202
236
vertical,
203
- displayDecrementFirst
237
+ displayDecrementFirst,
204
238
} = this . props ;
205
239
return (
206
240
< View
@@ -213,49 +247,51 @@ class UIStepper extends Component {
213
247
borderColor,
214
248
borderWidth,
215
249
borderRadius,
216
- flex : 1 ,
217
- flexDirection : vertical ? displayDecrementFirst ? 'column' : 'column-reverse' : 'row' ,
218
- }
250
+ flexDirection : vertical
251
+ ? displayDecrementFirst ? 'column' : 'column-reverse'
252
+ : 'row' ,
253
+ } ,
219
254
] }
220
255
>
221
256
< TouchableOpacity
222
257
onPress = { this . decrement }
223
258
style = { [
224
259
styles . button ,
225
260
{
226
- borderRightWidth : vertical ? 0 : borderWidth ,
261
+ borderRightWidth : vertical ? 0 : borderWidth ,
227
262
borderRightColor : borderColor ,
228
- height : vertical ? 30 : 'auto'
229
- }
263
+ height : vertical ? 30 : 'auto' ,
264
+ } ,
230
265
] }
231
266
>
232
267
< Image
233
268
source = { this . resolveImage ( decrementImage ) }
234
- style = { [ this . resolveStyles ( decrementImage ) ] }
235
- resizeMode = { " contain" }
269
+ style = { this . resolveStyles ( decrementImage ) }
270
+ resizeMode = { ' contain' }
236
271
/>
237
272
</ TouchableOpacity >
238
- { displayValue &&
273
+ { displayValue && (
239
274
< View style = { styles . valueContainer } >
240
275
< Text style = { { color : textColor , fontSize } } >
241
276
{ this . state . value }
242
277
</ Text >
243
- </ View > }
278
+ </ View >
279
+ ) }
244
280
< TouchableOpacity
245
281
onPress = { this . increment }
246
282
style = { [
247
283
styles . button ,
248
284
{
249
285
borderLeftWidth : vertical ? 0 : displayValue ? 1 : 0 ,
250
286
borderColor,
251
- height : vertical ? 30 : 'auto'
252
- }
287
+ height : vertical ? 30 : 'auto' ,
288
+ } ,
253
289
] }
254
290
>
255
291
< Image
256
292
source = { this . resolveImage ( incrementImage ) }
257
- style = { [ this . resolveStyles ( incrementImage ) ] }
258
- resizeMode = { " contain" }
293
+ style = { this . resolveStyles ( incrementImage ) }
294
+ resizeMode = { ' contain' }
259
295
/>
260
296
</ TouchableOpacity >
261
297
</ View >
0 commit comments