@@ -15,21 +15,80 @@ export default class ItemsSlider extends Component<ItemSliderProps> {
15
15
nextDisable : false
16
16
}
17
17
18
+ componentDidMount ( ) {
19
+ let divToScroll :any = document . querySelector ( `#${ this . props . id } ` ) ;
20
+ let isDown :boolean = false ;
21
+ let start :number ;
22
+ let scrollLeft :number ;
23
+
24
+ // add code to add event listeners to div only when we have id
25
+ // and only add them once
26
+ if ( divToScroll ) {
27
+ this . checkForDisabled ( divToScroll ) ;
28
+
29
+ const setIsDownFalse = ( ) => {
30
+ isDown = false ;
31
+ }
32
+
33
+ const mouseDownHandler = ( e :MouseEvent ) => {
34
+ isDown = true ;
35
+ start = e . pageX ;
36
+ scrollLeft = divToScroll . scrollLeft ;
37
+ }
38
+
39
+ const mouseMoveHandler = ( e :MouseEvent ) => {
40
+ if ( isDown ) {
41
+ e . preventDefault ( ) ;
42
+ const x = e . pageX ;
43
+ const walk = x - start ;
44
+ divToScroll . scrollLeft = scrollLeft - walk ;
45
+ }
46
+ }
47
+
48
+ divToScroll . addEventListener ( 'mousedown' , mouseDownHandler ) ;
49
+
50
+ divToScroll . addEventListener ( 'mouseup' , setIsDownFalse ) ;
51
+
52
+ divToScroll . addEventListener ( 'mouseleave' , setIsDownFalse ) ;
53
+
54
+ divToScroll . addEventListener ( 'mousemove' , mouseMoveHandler ) ;
55
+ }
56
+
57
+ }
58
+
18
59
// check if the next and before buttons should be disabled
19
60
// only works for rtl
20
61
checkForDisabled = ( divToScroll :any ) => {
62
+
63
+ if ( this . props . rtl ) {
64
+
65
+ if ( divToScroll . scrollLeft >= 0 ) {
66
+ this . setState ( { beforeDisable : true , nextDisable : false } ) ;
67
+ }
21
68
69
+ if ( divToScroll . scrollLeft < 0 ) {
70
+ this . setState ( { beforeDisable : false , nextDisable : false } ) ;
71
+ }
72
+
73
+ if ( ( Math . abs ( divToScroll . scrollLeft ) + divToScroll . clientWidth + 20 ) >= divToScroll . scrollWidth ) {
74
+ this . setState ( { nextDisable : true } ) ;
75
+ }
76
+
77
+ return ;
78
+ }
79
+
22
80
if ( divToScroll . scrollLeft >= 0 ) {
23
81
this . setState ( { beforeDisable : true , nextDisable : false } ) ;
24
82
}
25
83
26
- if ( divToScroll . scrollLeft < 0 ) {
84
+ if ( divToScroll . scrollLeft > 0 ) {
27
85
this . setState ( { beforeDisable : false , nextDisable : false } ) ;
28
86
}
29
87
30
- if ( ( Math . abs ( divToScroll . scrollLeft ) + divToScroll . clientWidth + 20 ) >= divToScroll . scrollWidth ) {
88
+ if ( ( Math . abs ( divToScroll . scrollLeft ) + divToScroll . clientWidth ) >= divToScroll . scrollWidth ) {
31
89
this . setState ( { nextDisable : true } ) ;
32
90
}
91
+
33
92
}
34
93
35
94
// set interval for scroll (to add animation)
@@ -42,7 +101,7 @@ export default class ItemsSlider extends Component<ItemSliderProps> {
42
101
43
102
if ( count === 50 ) {
44
103
clearInterval ( scrollInterval ) ;
45
- if ( this . props . rtl ) this . checkForDisabled ( divToScroll ) ;
104
+ this . checkForDisabled ( divToScroll ) ;
46
105
}
47
106
} , 10 )
48
107
@@ -51,9 +110,12 @@ export default class ItemsSlider extends Component<ItemSliderProps> {
51
110
scrollTo = ( e :any , direction :string ) => {
52
111
53
112
let divToScroll = document . querySelector ( `#${ this . props . id } ` ) ;
54
-
55
113
let navDirections = ( this . props . rtl ) ? [ 5 , - 5 ] : [ - 5 , 5 ] ;
56
114
115
+ if ( ! divToScroll ) {
116
+ return ;
117
+ }
118
+
57
119
switch ( direction ) {
58
120
case 'next' :
59
121
this . scrollWithAnimation ( navDirections [ 0 ] , divToScroll ) ;
0 commit comments