|
| 1 | +import React, { useState, useRef } from "react"; |
| 2 | +import Carousel from "react-elastic-carousel"; |
| 3 | +import styled from "styled-components"; |
| 4 | + |
| 5 | +const Item = styled.div` |
| 6 | + display: flex; |
| 7 | + justify-content: center; |
| 8 | + align-items: center; |
| 9 | + color: #fff; |
| 10 | + background-color: green; |
| 11 | + width: 100%; |
| 12 | + height: 150px; |
| 13 | + margin: 15px; |
| 14 | +`; |
| 15 | + |
| 16 | +const Layout = styled.div` |
| 17 | + display: flex; |
| 18 | + flex-direction: column; |
| 19 | + justify-content: center; |
| 20 | + align-items: center; |
| 21 | + height: 100vh; |
| 22 | +`; |
| 23 | + |
| 24 | +const ControlsLayout = styled.div` |
| 25 | + display: flex; |
| 26 | + flex-direction: column; |
| 27 | + margin: 25px; |
| 28 | +`; |
| 29 | + |
| 30 | +const StyledControlFields = styled.div` |
| 31 | + display: flex; |
| 32 | + margin: 5px; |
| 33 | +`; |
| 34 | + |
| 35 | +const breakPoints = [ |
| 36 | + { width: 200, itemsToShow: 1 }, |
| 37 | + { width: 600, itemsToShow: 2 }, |
| 38 | +]; |
| 39 | +const toggle = (updater) => () => updater((o) => !o); |
| 40 | + |
| 41 | +const CheckBox = ({ label, onToggle, ...rest }) => { |
| 42 | + return ( |
| 43 | + <StyledControlFields> |
| 44 | + <label htmlFor={label}>{label}</label> |
| 45 | + <input {...rest} id={label} type="checkbox" onChange={toggle(onToggle)} /> |
| 46 | + </StyledControlFields> |
| 47 | + ); |
| 48 | +}; |
| 49 | + |
| 50 | +const DemoApp = () => { |
| 51 | + const [items, setItems] = useState([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
| 52 | + const [itemsToShow, setItemsToShow] = useState(3); |
| 53 | + const [showArrows, setShowArrows] = useState(true); |
| 54 | + const [pagination, setPagination] = useState(true); |
| 55 | + const [verticalMode, setVerticalMode] = useState(false); |
| 56 | + const carouselRef = useRef(); |
| 57 | + |
| 58 | + const addItem = () => { |
| 59 | + setItems((currentItems) => [...currentItems, currentItems.length + 1]); |
| 60 | + }; |
| 61 | + |
| 62 | + const removeItem = () => { |
| 63 | + setItems((currentItems) => currentItems.slice(0, currentItems.length - 1)); |
| 64 | + }; |
| 65 | + |
| 66 | + const updateItemsToShow = ({ target }) => |
| 67 | + setItemsToShow(Number(target.value)); |
| 68 | + |
| 69 | + const goTo = ({ target }) => carouselRef.current.goTo(Number(target.value)); |
| 70 | + |
| 71 | + return ( |
| 72 | + <Layout> |
| 73 | + <ControlsLayout> |
| 74 | + <StyledControlFields> |
| 75 | + <button onClick={addItem}>Add Item</button> |
| 76 | + <button onClick={removeItem}>Remove Item</button> |
| 77 | + </StyledControlFields> |
| 78 | + <StyledControlFields> |
| 79 | + <label>goTo</label> |
| 80 | + <input type="number" onChange={goTo} /> |
| 81 | + </StyledControlFields> |
| 82 | + <StyledControlFields> |
| 83 | + <label>itemsToShow</label> |
| 84 | + <input |
| 85 | + type="number" |
| 86 | + value={itemsToShow} |
| 87 | + onChange={updateItemsToShow} |
| 88 | + /> |
| 89 | + </StyledControlFields> |
| 90 | + <CheckBox |
| 91 | + label="showArrows" |
| 92 | + checked={showArrows} |
| 93 | + onToggle={setShowArrows} |
| 94 | + /> |
| 95 | + <CheckBox |
| 96 | + label="pagination" |
| 97 | + checked={pagination} |
| 98 | + onToggle={setPagination} |
| 99 | + /> |
| 100 | + <CheckBox |
| 101 | + label="verticalMode" |
| 102 | + checked={verticalMode} |
| 103 | + onToggle={setVerticalMode} |
| 104 | + /> |
| 105 | + </ControlsLayout> |
| 106 | + <Carousel |
| 107 | + ref={carouselRef} |
| 108 | + verticalMode={verticalMode} |
| 109 | + itemsToShow={itemsToShow} |
| 110 | + showArrows={showArrows} |
| 111 | + pagination={pagination} |
| 112 | + > |
| 113 | + {items.map((item) => ( |
| 114 | + <Item key={item}>{item}</Item> |
| 115 | + ))} |
| 116 | + </Carousel> |
| 117 | + </Layout> |
| 118 | + ); |
| 119 | +}; |
| 120 | + |
| 121 | +export default DemoApp; |
0 commit comments