1- import React , { useState } from 'react' ;
1+ import React from 'react' ;
22import {
33 StyleSheet ,
44 Text ,
@@ -12,6 +12,11 @@ import {
1212 ColorValue ,
1313} from 'react-native' ;
1414
15+ type HasReachedMaxMin = {
16+ hasReachedMax : boolean ;
17+ hasReachedMin : boolean ;
18+ } ;
19+
1520export type SimpleStepperProps = {
1621 initialValue ?: number ;
1722 minimumValue ?: number ;
@@ -47,9 +52,15 @@ export type SimpleStepperProps = {
4752 useColor ?: boolean ;
4853 color ?: ColorValue ;
4954 textDecimalPlaces ?: number ;
55+ containerTestID ?: string ;
56+ separatorTestID ?: string ;
57+ incrementImageTestID ?: string ;
58+ decrementImageTestID ?: string ;
59+ incrementButtonTestID ?: string ;
60+ decrementButtonTestID ?: string ;
5061} ;
5162
52- const SimpleStepper : React . FC < SimpleStepperProps > = ( {
63+ const SimpleStepper : React . FunctionComponent < SimpleStepperProps > = ( {
5364 initialValue = 0 ,
5465 minimumValue = 0 ,
5566 maximumValue = 10 ,
@@ -107,26 +118,32 @@ const SimpleStepper: React.FC<SimpleStepperProps> = ({
107118 useColor = false ,
108119 color = 'blue' ,
109120 textDecimalPlaces = 2 ,
121+ containerTestID = 'container' ,
122+ separatorTestID = 'separator' ,
123+ incrementImageTestID = 'incrementImage' ,
124+ decrementImageTestID = 'decrementImage' ,
125+ incrementButtonTestID = 'incrementButton' ,
126+ decrementButtonTestID = 'decrementButton' ,
110127} ) => {
111- const [ value , setValue ] = useState ( initialValue ) ;
128+ const [ value , setValue ] = React . useState ( initialValue ) ;
112129
113- const _decrementAction = ( ) => {
130+ function _decrementAction ( ) : void {
114131 const nextValue = value - stepValue ;
115132 const actualValue = _processValue ( nextValue ) ;
116133 onDecrement ( actualValue ) ;
117134 setValue ( actualValue ) ;
118135 valueChanged ( actualValue ) ;
119- } ;
136+ }
120137
121- const _incrementAction = ( ) => {
138+ function _incrementAction ( ) : void {
122139 const nextValue = value + stepValue ;
123140 const actualValue = _processValue ( nextValue ) ;
124141 onIncrement ( actualValue ) ;
125142 setValue ( actualValue ) ;
126143 valueChanged ( actualValue ) ;
127- } ;
144+ }
128145
129- const _processValue = ( actualValue : number ) => {
146+ function _processValue ( actualValue : number ) : number {
130147 if ( actualValue > maximumValue ) {
131148 return wraps ? minimumValue : maximumValue ;
132149 } else if ( actualValue === maximumValue ) {
@@ -137,9 +154,9 @@ const SimpleStepper: React.FC<SimpleStepperProps> = ({
137154 return minimumValue ;
138155 }
139156 return actualValue ;
140- } ;
157+ }
141158
142- const _getHasMinMax = ( ) => {
159+ function _getHasMinMax ( ) : HasReachedMaxMin {
143160 let hasReachedMax = true ;
144161 let hasReachedMin = true ;
145162 switch ( true ) {
@@ -160,9 +177,9 @@ const SimpleStepper: React.FC<SimpleStepperProps> = ({
160177 hasReachedMax,
161178 hasReachedMin,
162179 } ;
163- } ;
180+ }
164181
165- const _renderText = ( ) => {
182+ function _renderText ( ) : React . JSX . Element {
166183 if ( renderText ) {
167184 return renderText ( value ) ;
168185 }
@@ -175,9 +192,9 @@ const SimpleStepper: React.FC<SimpleStepperProps> = ({
175192 displayValue = Number ( value . toFixed ( textDecimalPlaces ) ) ;
176193 }
177194 return < Text style = { textStyle } > { displayValue } </ Text > ;
178- } ;
195+ }
179196
180- const _renderIncrementImage = ( opacity : number ) => {
197+ function _renderIncrementImage ( opacity : number ) : React . JSX . Element {
181198 if ( renderIncrementImage ) {
182199 return renderIncrementImage ( opacity ) ;
183200 }
@@ -190,14 +207,14 @@ const SimpleStepper: React.FC<SimpleStepperProps> = ({
190207 }
191208 return (
192209 < Image
193- testID = { 'incrementImage' }
210+ testID = { incrementImageTestID }
194211 style = { [ _incrementImageStyle , { opacity} ] }
195212 source = { incrementImage }
196213 />
197214 ) ;
198- } ;
215+ }
199216
200- const _renderDecrementImage = ( opacity : number ) => {
217+ function _renderDecrementImage ( opacity : number ) : React . JSX . Element {
201218 if ( renderDecrementImage ) {
202219 return renderDecrementImage ( opacity ) ;
203220 }
@@ -210,12 +227,12 @@ const SimpleStepper: React.FC<SimpleStepperProps> = ({
210227 }
211228 return (
212229 < Image
213- testID = { 'decrementImage' }
230+ testID = { decrementImageTestID }
214231 style = { [ _decrementImageStyle , { opacity} ] }
215232 source = { decrementImage }
216233 />
217234 ) ;
218- } ;
235+ }
219236
220237 const { hasReachedMin, hasReachedMax} = _getHasMinMax ( ) ;
221238 const decrementOpacity = hasReachedMin || disabled ? disabledOpacity : 1 ;
@@ -240,37 +257,37 @@ const SimpleStepper: React.FC<SimpleStepperProps> = ({
240257
241258 return (
242259 < View >
243- < View testID = { 'container' } style = { _containerStyle } >
260+ < View testID = { containerTestID } style = { _containerStyle } >
244261 { isLeft && _renderText ( ) }
245- { isLeft && < View testID = { 'separator' } style = { _separatorStyle } /> }
262+ { isLeft && < View testID = { separatorTestID } style = { _separatorStyle } /> }
246263 { renderDecrementStep ? (
247264 renderDecrementStep ( value , _decrementAction )
248265 ) : (
249266 < TouchableOpacity
250- testID = { 'decrementButton' }
267+ testID = { decrementButtonTestID }
251268 style = { decrementStepStyle }
252269 activeOpacity = { activeOpacity }
253270 onPress = { _decrementAction }
254271 disabled = { hasReachedMin || disabled } >
255272 { _renderDecrementImage ( decrementOpacity ) }
256273 </ TouchableOpacity >
257274 ) }
258- { isCenter && < View testID = { 'separator' } style = { _separatorStyle } /> }
275+ { isCenter && < View testID = { separatorTestID } style = { _separatorStyle } /> }
259276 { isCenter && _renderText ( ) }
260277 < View style = { _separatorStyle } />
261278 { renderIncrementStep ? (
262279 renderIncrementStep ( value , _incrementAction )
263280 ) : (
264281 < TouchableOpacity
265- testID = { 'incrementButton' }
282+ testID = { incrementButtonTestID }
266283 style = { incrementStepStyle }
267284 activeOpacity = { activeOpacity }
268285 onPress = { _incrementAction }
269286 disabled = { hasReachedMax || disabled } >
270287 { _renderIncrementImage ( incrementOpacity ) }
271288 </ TouchableOpacity >
272289 ) }
273- { isRight && < View testID = { 'separator' } style = { _separatorStyle } /> }
290+ { isRight && < View testID = { separatorTestID } style = { _separatorStyle } /> }
274291 { isRight && _renderText ( ) }
275292 </ View >
276293 </ View >
0 commit comments