|
|
@ -1,4 +1,10 @@ |
|
|
|
import React, { useState, FunctionComponent, KeyboardEvent, useEffect } from 'react'; |
|
|
|
import React, { |
|
|
|
|
|
|
|
useState, |
|
|
|
|
|
|
|
FunctionComponent, |
|
|
|
|
|
|
|
KeyboardEvent, |
|
|
|
|
|
|
|
useEffect, |
|
|
|
|
|
|
|
useRef, |
|
|
|
|
|
|
|
} from 'react'; |
|
|
|
import { Arrow } from '../arrow'; |
|
|
|
import { Arrow } from '../arrow'; |
|
|
|
import { ItemProvider } from '../item'; |
|
|
|
import { ItemProvider } from '../item'; |
|
|
|
import { |
|
|
|
import { |
|
|
@ -13,6 +19,7 @@ import { |
|
|
|
import { SlideDirection, Item, ArrowKeys } from '../../types/carousel'; |
|
|
|
import { SlideDirection, Item, ArrowKeys } from '../../types/carousel'; |
|
|
|
import { defaultProps } from './defaultProps'; |
|
|
|
import { defaultProps } from './defaultProps'; |
|
|
|
import styles from '../../styles/styles.module.css'; |
|
|
|
import styles from '../../styles/styles.module.css'; |
|
|
|
|
|
|
|
import { usePrevious } from '../../hooks'; |
|
|
|
|
|
|
|
|
|
|
|
export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselProps) => { |
|
|
|
export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselProps) => { |
|
|
|
const props: Required<CarouselProps> = { ...defaultProps, ...userProps }; |
|
|
|
const props: Required<CarouselProps> = { ...defaultProps, ...userProps }; |
|
|
@ -29,40 +36,35 @@ export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselPr |
|
|
|
const [showArrow, setShowArrow] = useState( |
|
|
|
const [showArrow, setShowArrow] = useState( |
|
|
|
getShowArrow(props.children.length, props.show, props.infinite, current), |
|
|
|
getShowArrow(props.children.length, props.show, props.infinite, current), |
|
|
|
); |
|
|
|
); |
|
|
|
const [dynamicElements, setDynamicElements] = useState<Item[]>(userProps.children); |
|
|
|
const prevChildren = usePrevious<Item[]>(userProps.children); |
|
|
|
|
|
|
|
const [page, setPage] = useState<number>(0); |
|
|
|
/*useEffect(() => { |
|
|
|
const itemsRef = useRef(initItems(props.children, props.slide, props.infinite)); |
|
|
|
if (Array.isArray(props.extraItems) && props.extraItems.length > 0) { |
|
|
|
const isPaginating = useRef(false); |
|
|
|
const elements = dynamicElements.length > 0 ? dynamicElements : props.children; |
|
|
|
|
|
|
|
setDynamicElements(elements.concat(props.extraItems)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, [props.extraItems]);*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
|
|
if (dynamicElements.length > props.children.length) { |
|
|
|
|
|
|
|
slide(SlideDirection.Right, true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, [dynamicElements]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (props.dynamic) { |
|
|
|
if (props.dynamic) { |
|
|
|
useEffect(() => { |
|
|
|
useEffect(() => { |
|
|
|
console.log({ |
|
|
|
|
|
|
|
children: userProps.children, |
|
|
|
|
|
|
|
dynamicElements, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const newItems = updateNodes( |
|
|
|
const newItems = updateNodes( |
|
|
|
items, |
|
|
|
itemsRef.current, |
|
|
|
dynamicElements, |
|
|
|
props.children, |
|
|
|
|
|
|
|
prevChildren, |
|
|
|
props.slide, |
|
|
|
props.slide, |
|
|
|
props.infinite, |
|
|
|
props.infinite, |
|
|
|
); |
|
|
|
); |
|
|
|
console.log({ newItems }); |
|
|
|
|
|
|
|
setItems(newItems); |
|
|
|
setItems(newItems); |
|
|
|
}, [userProps.children, dynamicElements]); |
|
|
|
itemsRef.current = newItems; |
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
|
|
page < props.pageCount && |
|
|
|
|
|
|
|
prevChildren && |
|
|
|
|
|
|
|
prevChildren?.length < props.children.length |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
slide(SlideDirection.Right); |
|
|
|
|
|
|
|
setPage(page + 1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, [props.children]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const slide = async (direction: SlideDirection, cb?: boolean) => { |
|
|
|
const slide = (direction: SlideDirection) => { |
|
|
|
if ( |
|
|
|
if ( |
|
|
|
animation.isSliding || |
|
|
|
animation.isSliding || |
|
|
|
(direction === SlideDirection.Right && !showArrow.right) || |
|
|
|
(direction === SlideDirection.Right && !showArrow.right) || |
|
|
@ -71,19 +73,18 @@ export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselPr |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const dynamicElementsExists = dynamicElements.length > 0; |
|
|
|
if ( |
|
|
|
|
|
|
|
props.paginationCallback && |
|
|
|
if (!cb && props.beforeChange) { |
|
|
|
direction === SlideDirection.Right && |
|
|
|
const extraItems = await props.beforeChange(direction); |
|
|
|
page < props.pageCount - 1 && |
|
|
|
if (Array.isArray(extraItems) && extraItems.length > 0) { |
|
|
|
!isPaginating.current |
|
|
|
const elements = |
|
|
|
) { |
|
|
|
dynamicElements.length > 0 ? dynamicElements : props.children; |
|
|
|
isPaginating.current = true; |
|
|
|
setDynamicElements(elements.concat(extraItems)); |
|
|
|
props.paginationCallback(direction); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const elements = dynamicElementsExists ? dynamicElements : props.children; |
|
|
|
const elements = props.children; |
|
|
|
|
|
|
|
|
|
|
|
const next = getCurrent(current, props.slide, elements.length, direction); |
|
|
|
const next = getCurrent(current, props.slide, elements.length, direction); |
|
|
|
const rotated = props.infinite |
|
|
|
const rotated = props.infinite |
|
|
@ -91,6 +92,7 @@ export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselPr |
|
|
|
: items; |
|
|
|
: items; |
|
|
|
if (props.infinite && direction === SlideDirection.Right) { |
|
|
|
if (props.infinite && direction === SlideDirection.Right) { |
|
|
|
setItems(rotated); |
|
|
|
setItems(rotated); |
|
|
|
|
|
|
|
itemsRef.current = rotated; |
|
|
|
} |
|
|
|
} |
|
|
|
setAnimation({ |
|
|
|
setAnimation({ |
|
|
|
transform: |
|
|
|
transform: |
|
|
@ -102,7 +104,13 @@ export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselPr |
|
|
|
setShowArrow(getShowArrow(elements.length, props.show, props.infinite, next)); |
|
|
|
setShowArrow(getShowArrow(elements.length, props.show, props.infinite, next)); |
|
|
|
setTimeout(() => { |
|
|
|
setTimeout(() => { |
|
|
|
if (props.infinite) { |
|
|
|
if (props.infinite) { |
|
|
|
setItems(cleanItems(rotated, props.slide, direction)); |
|
|
|
const cleanedItems = cleanItems( |
|
|
|
|
|
|
|
direction === SlideDirection.Right ? itemsRef.current : rotated, |
|
|
|
|
|
|
|
props.slide, |
|
|
|
|
|
|
|
direction, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
setItems(cleanedItems); |
|
|
|
|
|
|
|
itemsRef.current = cleanedItems; |
|
|
|
} |
|
|
|
} |
|
|
|
setAnimation({ |
|
|
|
setAnimation({ |
|
|
|
transform: props.infinite |
|
|
|
transform: props.infinite |
|
|
@ -113,6 +121,7 @@ export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselPr |
|
|
|
isSliding: false, |
|
|
|
isSliding: false, |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, props.transition * 1_0_0_0); |
|
|
|
}, props.transition * 1_0_0_0); |
|
|
|
|
|
|
|
isPaginating.current = false; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const widthCallBack = (calculatedWidth: number) => { |
|
|
|
const widthCallBack = (calculatedWidth: number) => { |
|
|
@ -164,7 +173,7 @@ export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselPr |
|
|
|
<ItemProvider |
|
|
|
<ItemProvider |
|
|
|
{...props} |
|
|
|
{...props} |
|
|
|
transition={animation.transition} |
|
|
|
transition={animation.transition} |
|
|
|
items={items} |
|
|
|
items={itemsRef.current} |
|
|
|
transform={animation.transform} |
|
|
|
transform={animation.transform} |
|
|
|
slideCallback={slideCallback} |
|
|
|
slideCallback={slideCallback} |
|
|
|
dragCallback={dragCallback} |
|
|
|
dragCallback={dragCallback} |
|
|
@ -190,9 +199,8 @@ export interface CarouselProps { |
|
|
|
useArrowKeys?: boolean; |
|
|
|
useArrowKeys?: boolean; |
|
|
|
a11y?: { [key: string]: string }; |
|
|
|
a11y?: { [key: string]: string }; |
|
|
|
dynamic?: boolean; |
|
|
|
dynamic?: boolean; |
|
|
|
beforeChange?: ((direction: SlideDirection) => Promise<any[] | null>) | null; |
|
|
|
paginationCallback?: ((direction: SlideDirection) => any) | null; |
|
|
|
afterChange?: ((direction: SlideDirection) => void) | null; |
|
|
|
pageCount?: number; |
|
|
|
extraItems?: any[]; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export interface CarouselState { |
|
|
|
export interface CarouselState { |
|
|
|