11import React , { createRef , useState } from "react" ;
2+ import { TouchHandler } from "./services/TouchHandler" ;
23
34const Item = ( { children, increaseValue, decreaseValue, setViewValue } ) => {
45 const [ x , setX ] = useState ( 0 ) ;
56 const [ viewX , setViewX ] = useState ( 0 ) ;
67
8+ const [ elementKey , setElementKey ] = useState ( "" ) ;
9+
710 const itemRef = createRef ( ) ;
811
912 const getTouches = ( evt ) => evt . touches ;
@@ -15,7 +18,9 @@ const Item = ({ children, increaseValue, decreaseValue, setViewValue }) => {
1518 } ;
1619
1720 const handleTouchStart = ( evt ) => {
18- let currentX = Math . trunc ( getTouches ( evt ) [ 0 ] . clientX ) ;
21+ let key = TouchHandler . insertElement ( ) ;
22+ setElementKey ( key ) ;
23+ let currentX = Math . trunc ( getTouches ( evt ) [ TouchHandler . getIndex ( key ) ] . clientX ) ;
1924 setX ( currentX ) ;
2025 setViewX ( currentX ) ;
2126 } ;
@@ -28,7 +33,7 @@ const Item = ({ children, increaseValue, decreaseValue, setViewValue }) => {
2833 } ;
2934
3035 const handleTouchMove = ( evt ) => {
31- let currentX = Math . trunc ( getTouches ( evt ) [ 0 ] . clientX ) ;
36+ let currentX = Math . trunc ( getTouches ( evt ) [ TouchHandler . getIndex ( elementKey ) ] . clientX ) ;
3237 if ( currentX !== viewX ) {
3338 setViewValue ( x - currentX ) ;
3439 setViewX ( currentX ) ;
@@ -41,6 +46,7 @@ const Item = ({ children, increaseValue, decreaseValue, setViewValue }) => {
4146 } ;
4247
4348 const handleTouchEnd = ( ) => {
49+ TouchHandler . removeElement ( elementKey ) ;
4450 if ( viewX < x ) increaseValue ( ) ;
4551 else if ( viewX > x ) decreaseValue ( ) ;
4652 } ;
0 commit comments