1
1
import _ from 'lodash' ;
2
- import React , { PureComponent , ReactElement , ElementRef } from 'react' ;
2
+ import React , { PureComponent , ReactElement } from 'react' ;
3
3
import {
4
4
StyleSheet ,
5
5
PanResponder ,
@@ -155,9 +155,9 @@ export default class Slider extends PureComponent<SliderProps, State> {
155
155
156
156
static defaultProps = defaultProps ;
157
157
158
- private thumb : ElementRef < typeof RNView > | undefined = undefined ;
158
+ private thumb = React . createRef < RNView > ( ) ;
159
159
private _thumbStyles : ThumbStyle = { } ;
160
- private minTrack : ElementRef < typeof RNView > | undefined = undefined ;
160
+ private minTrack = React . createRef < RNView > ( ) ;
161
161
private _minTrackStyles : MinTrackStyle = { } ;
162
162
private _x = 0 ;
163
163
private _dx = 0 ;
@@ -299,27 +299,18 @@ export default class Slider extends PureComponent<SliderProps, State> {
299
299
}
300
300
301
301
updateStyles ( x : number ) {
302
- if ( this . thumb ) {
302
+ if ( this . thumb . current ) {
303
303
const { disableRTL} = this . props ;
304
304
const { trackSize} = this . state ;
305
- const _x = Constants . isRTL && disableRTL ? trackSize . width - x : x ;
306
- const position = _x - this . initialThumbSize . width / 2 ;
307
- const deviation = 3 ;
308
-
309
- if ( position + deviation < 0 ) {
310
- this . _thumbStyles . left = 0 ;
311
- } else if ( position - deviation > trackSize . width - this . initialThumbSize . width ) {
312
- this . _thumbStyles . left = trackSize . width - this . initialThumbSize . width ;
313
- } else {
314
- this . _thumbStyles . left = position ;
315
- }
316
-
317
- this . thumb . setNativeProps ( this . _thumbStyles ) ;
305
+ const nonOverlappingTrackWidth = trackSize . width - this . initialThumbSize . width ;
306
+ const _x = Constants . isRTL && disableRTL ? nonOverlappingTrackWidth - x : x ; // adjust for RTL
307
+ this . _thumbStyles . left = trackSize . width === 0 ? _x : ( _x * nonOverlappingTrackWidth ) / trackSize . width ; // do not render above prefix\suffix icon\text
308
+ this . thumb . current ?. setNativeProps ( this . _thumbStyles ) ;
318
309
}
319
310
320
- if ( this . minTrack ) {
311
+ if ( this . minTrack . current ) {
321
312
this . _minTrackStyles . width = Math . min ( this . state . trackSize . width , x ) ;
322
- this . minTrack . setNativeProps ( this . _minTrackStyles ) ;
313
+ this . minTrack . current ?. setNativeProps ( this . _minTrackStyles ) ;
323
314
}
324
315
}
325
316
@@ -329,14 +320,14 @@ export default class Slider extends PureComponent<SliderProps, State> {
329
320
}
330
321
331
322
updateThumbStyle ( start : boolean ) {
332
- if ( this . thumb && ! this . props . disableActiveStyling ) {
323
+ if ( this . thumb . current && ! this . props . disableActiveStyling ) {
333
324
const { thumbStyle, activeThumbStyle} = this . props ;
334
325
const style = thumbStyle || styles . thumb ;
335
326
const activeStyle = activeThumbStyle || styles . activeThumb ;
336
327
337
328
const activeOrInactiveStyle = ! this . props . disabled ? ( start ? activeStyle : style ) : { } ;
338
329
this . _thumbStyles . style = _ . omit ( activeOrInactiveStyle , 'height' , 'width' ) ;
339
- this . thumb . setNativeProps ( this . _thumbStyles ) ;
330
+ this . thumb . current ?. setNativeProps ( this . _thumbStyles ) ;
340
331
this . scaleThumb ( start ) ;
341
332
}
342
333
}
@@ -396,13 +387,6 @@ export default class Slider extends PureComponent<SliderProps, State> {
396
387
return range ;
397
388
}
398
389
399
- setMinTrackRef = ( ref : ElementRef < typeof RNView > ) => {
400
- this . minTrack = ref ;
401
- } ;
402
-
403
- setThumbRef = ( ref : ElementRef < typeof RNView > ) => {
404
- this . thumb = ref ;
405
- } ;
406
390
407
391
calculatedThumbActiveScale = ( ) => {
408
392
const { activeThumbStyle, thumbStyle, disabled, disableActiveStyling} = this . props ;
@@ -527,7 +511,7 @@ export default class Slider extends PureComponent<SliderProps, State> {
527
511
return (
528
512
< Animated . View
529
513
hitSlop = { thumbHitSlop }
530
- ref = { this . setThumbRef }
514
+ ref = { this . thumb }
531
515
onLayout = { this . onThumbLayout }
532
516
{ ...this . _panResponder . panHandlers }
533
517
style = { [
@@ -588,7 +572,7 @@ export default class Slider extends PureComponent<SliderProps, State> {
588
572
onLayout = { this . onTrackLayout }
589
573
/>
590
574
< View
591
- ref = { this . setMinTrackRef }
575
+ ref = { this . minTrack }
592
576
style = { [
593
577
styles . track ,
594
578
trackStyle ,
0 commit comments