({
items,
className = {},
preSelectedIndex = 0,
hasCustomIndicator = false,
onScreenIndexChange,
onClose,
onEnd,
children,
}: CarouselProps)
| 30 | } |
| 31 | |
| 32 | function Carousel({ |
| 33 | items, |
| 34 | className = {}, |
| 35 | preSelectedIndex = 0, |
| 36 | hasCustomIndicator = false, |
| 37 | onScreenIndexChange, |
| 38 | onClose, |
| 39 | onEnd, |
| 40 | children, |
| 41 | }: CarouselProps): ReactElement { |
| 42 | const [index, setIndex] = useState(preSelectedIndex); |
| 43 | |
| 44 | const onUpdateIndex = (increment: number) => { |
| 45 | onScreenIndexChange?.(index + increment); |
| 46 | return setIndex((state) => state + increment); |
| 47 | }; |
| 48 | |
| 49 | const onSwipedRight: SwipeCallback = (e) => { |
| 50 | e?.event?.stopPropagation?.(); |
| 51 | |
| 52 | if (index === 0) { |
| 53 | return isNullOrUndefined(e?.dir) && onClose?.(e); |
| 54 | } |
| 55 | |
| 56 | return onUpdateIndex(-1); |
| 57 | }; |
| 58 | |
| 59 | const onSwipedLeft: SwipeCallback = (e) => { |
| 60 | e?.event?.stopPropagation?.(); |
| 61 | |
| 62 | const max = items.length - 1; |
| 63 | if (index === max) { |
| 64 | return isNullOrUndefined(e?.dir) && onEnd?.(e); |
| 65 | } |
| 66 | |
| 67 | return onUpdateIndex(1); |
| 68 | }; |
| 69 | |
| 70 | const handlers = useSwipeable({ onSwipedLeft, onSwipedRight }); |
| 71 | const indicator = ( |
| 72 | <CarouselIndicator |
| 73 | onItemClick={onUpdateIndex} |
| 74 | max={items.length} |
| 75 | active={index} |
| 76 | className={{ |
| 77 | container: |
| 78 | (!children && 'absolute bottom-4 left-1/2 -translate-x-1/2') || |
| 79 | undefined, |
| 80 | }} |
| 81 | /> |
| 82 | ); |
| 83 | |
| 84 | const content = ( |
| 85 | <span |
| 86 | {...handlers} |
| 87 | className={classNames( |
| 88 | 'relative flex flex-row overflow-x-hidden', |
| 89 | className?.container, |
nothing calls this directly
no outgoing calls
no test coverage detected