|
@@ -1,6 +1,6 @@
|
|
import React, {useRef, useEffect, useState} from 'react'
|
|
import React, {useRef, useEffect, useState} from 'react'
|
|
|
|
|
|
-export const SliderImage = ({images = [], className, ...props}) => {
|
|
|
|
|
|
+export const SliderImage = ({images = [], className, onChange, onClick, ...props}) => {
|
|
const divRef = useRef()
|
|
const divRef = useRef()
|
|
const containerRef = useRef()
|
|
const containerRef = useRef()
|
|
const autoscroll = useRef(false)
|
|
const autoscroll = useRef(false)
|
|
@@ -10,6 +10,7 @@ export const SliderImage = ({images = [], className, ...props}) => {
|
|
const [current, setCurrent] = useState(0)
|
|
const [current, setCurrent] = useState(0)
|
|
const currentScrollPosition = useRef(0)
|
|
const currentScrollPosition = useRef(0)
|
|
|
|
|
|
|
|
+
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
if (divRef.current){
|
|
if (divRef.current){
|
|
const div = divRef.current
|
|
const div = divRef.current
|
|
@@ -81,12 +82,43 @@ export const SliderImage = ({images = [], className, ...props}) => {
|
|
}
|
|
}
|
|
setCurrent(newCurrent)
|
|
setCurrent(newCurrent)
|
|
if (newCurrent === current) scrollToCurrent()
|
|
if (newCurrent === current) scrollToCurrent()
|
|
|
|
+ else if (typeof onChange === 'function') onChange(newCurrent)
|
|
}, 200)
|
|
}, 200)
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
useEffect(scrollToCurrent, [current])
|
|
useEffect(scrollToCurrent, [current])
|
|
|
|
|
|
|
|
+ const onImgClick = (e, i) => {
|
|
|
|
+ const touchDevice = (navigator.maxTouchPoints || 'ontouchstart' in document.documentElement);
|
|
|
|
+ if (touchDevice && typeof onClick === 'function'){
|
|
|
|
+ onClick(i)
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ const viewPortWidth = containerRef.current.getBoundingClientRect().width
|
|
|
|
+ const clickX = e.clientX
|
|
|
|
+ const {scrollLeft} = containerRef.current;
|
|
|
|
+ if (clickX > viewPortWidth * 0.75) {
|
|
|
|
+ if (i < images.length -1){
|
|
|
|
+ currentScrollPosition.current = scrollLeft + (imgs?.[i].getBoundingClientRect().width || 0)
|
|
|
|
+ setCurrent(i +1)
|
|
|
|
+ scrollToCurrent()
|
|
|
|
+ if (typeof onChange === 'function') onChange(i +1)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else if (clickX < viewPortWidth * 0.25) {
|
|
|
|
+ if (i > 0){
|
|
|
|
+ currentScrollPosition.current = scrollLeft - (imgs?.[i-1].getBoundingClientRect().width || 0)
|
|
|
|
+ setCurrent(i -1)
|
|
|
|
+ scrollToCurrent()
|
|
|
|
+ if (typeof onChange === 'function') onChange(i -1)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else if (typeof onClick === 'function'){
|
|
|
|
+ onClick(i)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
return (
|
|
return (
|
|
<div className={className}
|
|
<div className={className}
|
|
style={{cssText: `overflow: auto; -ms-overflow-style: none; scrollbar-width: none`}}
|
|
style={{cssText: `overflow: auto; -ms-overflow-style: none; scrollbar-width: none`}}
|
|
@@ -97,6 +129,7 @@ export const SliderImage = ({images = [], className, ...props}) => {
|
|
key={`image_${i}`}
|
|
key={`image_${i}`}
|
|
alt={`image ${i}`}
|
|
alt={`image ${i}`}
|
|
onLoad={e => {imgs[i] = e.target; setImgs([...imgs]) }}
|
|
onLoad={e => {imgs[i] = e.target; setImgs([...imgs]) }}
|
|
|
|
+ onClick={e => onImgClick(e, i)}
|
|
/>)}
|
|
/>)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|