|
@@ -1,6 +1,6 @@
|
|
|
import React, {useRef, useEffect, useState} from 'react'
|
|
|
|
|
|
-export default ({images = [], className, onChange, onClick, ref, ...props}) => {
|
|
|
+export default ({images = [], className, onChange, onClick, ref, siblingOnly, current:propCurrent, ...props}) => {
|
|
|
const divRef = useRef()
|
|
|
const containerRef = useRef()
|
|
|
const autoscroll = useRef(false)
|
|
@@ -12,15 +12,29 @@ export default ({images = [], className, onChange, onClick, ref, ...props}) => {
|
|
|
const currentScrollPosition = useRef(0)
|
|
|
|
|
|
|
|
|
+ const widthSum = (total, img) => total + (img?.getBoundingClientRect().width || 0)
|
|
|
+
|
|
|
useEffect(() => {
|
|
|
if (divRef.current){
|
|
|
- const div = divRef.current
|
|
|
+ const div = divRef.current
|
|
|
|
|
|
- const totalWidth = imgs.reduce((total, img) => total + (img?.getBoundingClientRect().width || 0),0)
|
|
|
- div.style.minWidth = totalWidth + 'px'
|
|
|
+ const totalWidth = imgs.reduce(widthSum, 0)
|
|
|
+ div.style.minWidth = totalWidth + 'px'
|
|
|
}
|
|
|
}, [divRef, imgs])
|
|
|
|
|
|
+ useEffect(() => {
|
|
|
+ setCurrent(propCurrent)
|
|
|
+ if (containerRef.current){
|
|
|
+ const scrollTo = imgs.slice(0,propCurrent).reduce(widthSum, 0)
|
|
|
+ console.log('SCROLL TO ON PROP CURRRENT', scrollTo)
|
|
|
+ containerRef.current.scrollTo(scrollTo, 0)
|
|
|
+ currentScrollPosition.current = scrollTo
|
|
|
+ }
|
|
|
+ },[propCurrent, imgs])
|
|
|
+
|
|
|
+ console.log('CURRENT', current)
|
|
|
+
|
|
|
const scrollToCurrent = () => {
|
|
|
|
|
|
const el = containerRef.current
|
|
@@ -41,7 +55,6 @@ export default ({images = [], className, onChange, onClick, ref, ...props}) => {
|
|
|
}
|
|
|
}
|
|
|
step()
|
|
|
- //if (milaCount !== current +1) set(current +1)
|
|
|
}
|
|
|
|
|
|
const onTouchEnd = () => {
|
|
@@ -77,7 +90,7 @@ export default ({images = [], className, onChange, onClick, ref, ...props}) => {
|
|
|
}
|
|
|
const offset = imgsWidth - scrollLeft
|
|
|
newCurrent = i
|
|
|
- currentScrollPosition.current = imgsWidth - (imgs?.[i].getBoundingClientRect().width || 0)
|
|
|
+ currentScrollPosition.current = imgsWidth - (imgs?.[i]?.getBoundingClientRect().width || 0)
|
|
|
if (offset < viewPortWidth/2){
|
|
|
currentScrollPosition.current = imgsWidth
|
|
|
newCurrent++
|
|
@@ -89,12 +102,12 @@ export default ({images = [], className, onChange, onClick, ref, ...props}) => {
|
|
|
}
|
|
|
|
|
|
const onScroll = e => {
|
|
|
- //autoscroll.current = false
|
|
|
if (autoscroll.current) return;
|
|
|
|
|
|
if (timeout.current) clearInterval(timeout.current)
|
|
|
|
|
|
timeout.current = setTimeout(onTouchEnd, 200)
|
|
|
+ e.preventDefault();
|
|
|
}
|
|
|
|
|
|
useEffect(scrollToCurrent, [current])
|