@@ -15,21 +15,80 @@ export default class ItemsSlider extends Component<ItemSliderProps> {
1515 nextDisable : false
1616 }
1717
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+
1859 // check if the next and before buttons should be disabled
1960 // only works for rtl
2061 checkForDisabled = ( divToScroll :any ) => {
62+
63+ if ( this . props . rtl ) {
64+
65+ if ( divToScroll . scrollLeft >= 0 ) {
66+ this . setState ( { beforeDisable : true , nextDisable : false } ) ;
67+ }
2168
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+
2280 if ( divToScroll . scrollLeft >= 0 ) {
2381 this . setState ( { beforeDisable : true , nextDisable : false } ) ;
2482 }
2583
26- if ( divToScroll . scrollLeft < 0 ) {
84+ if ( divToScroll . scrollLeft > 0 ) {
2785 this . setState ( { beforeDisable : false , nextDisable : false } ) ;
2886 }
2987
30- if ( ( Math . abs ( divToScroll . scrollLeft ) + divToScroll . clientWidth + 20 ) >= divToScroll . scrollWidth ) {
88+ if ( ( Math . abs ( divToScroll . scrollLeft ) + divToScroll . clientWidth ) >= divToScroll . scrollWidth ) {
3189 this . setState ( { nextDisable : true } ) ;
3290 }
91+
3392 }
3493
3594 // set interval for scroll (to add animation)
@@ -42,7 +101,7 @@ export default class ItemsSlider extends Component<ItemSliderProps> {
42101
43102 if ( count === 50 ) {
44103 clearInterval ( scrollInterval ) ;
45- if ( this . props . rtl ) this . checkForDisabled ( divToScroll ) ;
104+ this . checkForDisabled ( divToScroll ) ;
46105 }
47106 } , 10 )
48107
@@ -51,9 +110,12 @@ export default class ItemsSlider extends Component<ItemSliderProps> {
51110 scrollTo = ( e :any , direction :string ) => {
52111
53112 let divToScroll = document . querySelector ( `#${ this . props . id } ` ) ;
54-
55113 let navDirections = ( this . props . rtl ) ? [ 5 , - 5 ] : [ - 5 , 5 ] ;
56114
115+ if ( ! divToScroll ) {
116+ return ;
117+ }
118+
57119 switch ( direction ) {
58120 case 'next' :
59121 this . scrollWithAnimation ( navDirections [ 0 ] , divToScroll ) ;
0 commit comments