(props)
| 29 | const getInitialState = (numItems: number): CarouselState => ({ pos: numItems - 1, sliding: false, dir: NEXT }); |
| 30 | |
| 31 | const Carousel: FunctionComponent<{children: ReactNode}> = (props) => { |
| 32 | const numItems = React.Children.count(props.children); |
| 33 | const [state, dispatch] = React.useReducer(reducer, getInitialState(numItems)); |
| 34 | |
| 35 | const slide = (dir: Direction) => { |
| 36 | dispatch({ type: dir, numItems }); |
| 37 | setTimeout(() => { |
| 38 | dispatch({ type: 'stopSliding' }); |
| 39 | }, 50); |
| 40 | }; |
| 41 | |
| 42 | const handlers = useSwipeable({ |
| 43 | onSwipedLeft: () => slide(NEXT), |
| 44 | onSwipedRight: () => slide(PREV), |
| 45 | swipeDuration: 500, |
| 46 | preventScrollOnSwipe: true, |
| 47 | trackMouse: true |
| 48 | }); |
| 49 | |
| 50 | return ( |
| 51 | <div {...handlers}> |
| 52 | <Wrapper> |
| 53 | <CarouselContainer dir={state.dir} sliding={state.sliding}> |
| 54 | {React.Children.map(props.children, (child, index) => ( |
| 55 | <CarouselSlot |
| 56 | order={getOrder(index, state.pos, numItems)} |
| 57 | > |
| 58 | {child} |
| 59 | </CarouselSlot> |
| 60 | ))} |
| 61 | </CarouselContainer> |
| 62 | </Wrapper> |
| 63 | <SlideButtonContainer> |
| 64 | <SlideButton onClick={() => slide(PREV)} float="left"> |
| 65 | Prev |
| 66 | </SlideButton> |
| 67 | <SlideButton onClick={() => slide(NEXT)} float="right"> |
| 68 | Next |
| 69 | </SlideButton> |
| 70 | </SlideButtonContainer> |
| 71 | </div> |
| 72 | ); |
| 73 | }; |
| 74 | |
| 75 | function reducer(state: CarouselState, action: CarouselAction): CarouselState { |
| 76 | switch (action.type) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…