|
| 1 | +import { |
| 2 | + Button, |
| 3 | + Card, |
| 4 | + CardBody, |
| 5 | + Chip, |
| 6 | + Column, |
| 7 | + Monospace, |
| 8 | + Pagination, |
| 9 | + Row, |
| 10 | + Slider, |
| 11 | +} from '@committed/components' |
| 12 | +import { Meta, Story } from '@storybook/react' |
| 13 | +import React, { useEffect, useMemo } from 'react' |
| 14 | +import useSwr from 'swr' |
| 15 | +import { usePagination } from '.' |
| 16 | + |
| 17 | +export interface UsePaginationDocsProps { |
| 18 | + /** The total number of items, if know when initializing, use setTotalItems if not */ |
| 19 | + totalItems: number |
| 20 | + /** The page to start on */ |
| 21 | + startPage: number |
| 22 | + /** The size of each page */ |
| 23 | + pageSize: number |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Utility hook for Pagination state |
| 28 | + * |
| 29 | + * returns an object containing the current page, additional useful state and functions for manipulation. |
| 30 | + * |
| 31 | + */ |
| 32 | +export const UsePaginationDocs: React.FC<UsePaginationDocsProps> = ( |
| 33 | + _props: UsePaginationDocsProps |
| 34 | +) => null |
| 35 | +UsePaginationDocs.defaultProps = { |
| 36 | + totalItems: 0, |
| 37 | + startPage: 1, |
| 38 | + pageSize: 20, |
| 39 | +} |
| 40 | + |
| 41 | +export default { |
| 42 | + title: 'Hooks/usePagination', |
| 43 | + component: UsePaginationDocs, |
| 44 | + excludeStories: ['UsePaginationDocs'], |
| 45 | +} as Meta |
| 46 | + |
| 47 | +const Template: Story<UsePaginationDocsProps> = (args) => { |
| 48 | + const { |
| 49 | + page, |
| 50 | + pageSize, |
| 51 | + totalPages, |
| 52 | + startIndex, |
| 53 | + endIndex, |
| 54 | + isNextDisabled, |
| 55 | + isPreviousDisabled, |
| 56 | + setPage, |
| 57 | + } = usePagination(args) |
| 58 | + return ( |
| 59 | + <Column css={{ gap: '$2' }}> |
| 60 | + <Card> |
| 61 | + <CardBody> |
| 62 | + <Monospace> |
| 63 | + {JSON.stringify( |
| 64 | + { |
| 65 | + page, |
| 66 | + pageSize, |
| 67 | + totalPages, |
| 68 | + startIndex, |
| 69 | + endIndex, |
| 70 | + isNextDisabled, |
| 71 | + isPreviousDisabled, |
| 72 | + }, |
| 73 | + null, |
| 74 | + 2 |
| 75 | + )} |
| 76 | + </Monospace> |
| 77 | + </CardBody> |
| 78 | + </Card> |
| 79 | + <Pagination page={page} onPageChange={setPage} count={totalPages} /> |
| 80 | + </Column> |
| 81 | + ) |
| 82 | +} |
| 83 | + |
| 84 | +export const Default = Template.bind({}) |
| 85 | +Default.args = { totalItems: 100, startPage: 1, pageSize: 20 } |
| 86 | + |
| 87 | +export const ClientSide: Story = () => { |
| 88 | + const items = Array.from({ length: 100 }, (v, i) => i) |
| 89 | + const { page, totalPages, startIndex, endIndex, setPage } = usePagination({ |
| 90 | + totalItems: 100, |
| 91 | + pageSize: 20, |
| 92 | + }) |
| 93 | + |
| 94 | + return ( |
| 95 | + <> |
| 96 | + <Row css={{ gap: '$2', mb: '$3' }}> |
| 97 | + {items.slice(startIndex, endIndex).map((item) => ( |
| 98 | + <div>{item}</div> |
| 99 | + ))} |
| 100 | + </Row> |
| 101 | + <Pagination page={page} onPageChange={setPage} count={totalPages} /> |
| 102 | + </> |
| 103 | + ) |
| 104 | +} |
| 105 | +ClientSide.parameters = { |
| 106 | + docs: { |
| 107 | + description: { |
| 108 | + story: |
| 109 | + 'The hook can use used to manage pagination for a known long list on the client side by providing the relevant start data. Then use the `startIndex` and `endIndex` to slice the data.', |
| 110 | + }, |
| 111 | + }, |
| 112 | +} |
| 113 | + |
| 114 | +type User = { |
| 115 | + id: number |
| 116 | + name: string |
| 117 | + email: string |
| 118 | + gender: string |
| 119 | + status: string |
| 120 | +} |
| 121 | + |
| 122 | +export const ServerSide: Story = () => { |
| 123 | + const { page, pageSize, totalPages, setPage, setTotalItems, setPageSize } = |
| 124 | + usePagination() |
| 125 | + |
| 126 | + const { data } = useSwr( |
| 127 | + ['https://gorest.co.in/public/v2/users', page, pageSize], |
| 128 | + async ([url, page, pageSize]) => { |
| 129 | + const res = await fetch(`${url}?page=${page - 1}&per_page=${pageSize}`) |
| 130 | + |
| 131 | + const users = (await res.json()) as User[] |
| 132 | + const totalItems = Number(res.headers.get('x-pagination-total')) |
| 133 | + |
| 134 | + return { |
| 135 | + users, |
| 136 | + totalItems, |
| 137 | + } |
| 138 | + }, |
| 139 | + { refreshInterval: 0, shouldRetryOnError: false } |
| 140 | + ) |
| 141 | + |
| 142 | + const users = useMemo(() => { |
| 143 | + return data?.users ?? [] |
| 144 | + }, [data]) |
| 145 | + |
| 146 | + useEffect(() => { |
| 147 | + if (data?.totalItems) { |
| 148 | + setTotalItems(data.totalItems) |
| 149 | + } |
| 150 | + }, [data]) |
| 151 | + |
| 152 | + return ( |
| 153 | + <Column css={{ gap: '$3' }}> |
| 154 | + <Pagination page={page} onPageChange={setPage} count={totalPages} /> |
| 155 | + <Slider |
| 156 | + labelFunction={(value) => `Page size ${value}`} |
| 157 | + value={[pageSize]} |
| 158 | + onValueChange={(value) => setPageSize(value[0])} |
| 159 | + /> |
| 160 | + <div> |
| 161 | + {users.map((item) => ( |
| 162 | + <div>{item.name}</div> |
| 163 | + ))} |
| 164 | + </div> |
| 165 | + </Column> |
| 166 | + ) |
| 167 | +} |
| 168 | +ServerSide.parameters = { |
| 169 | + docs: { |
| 170 | + description: { |
| 171 | + story: |
| 172 | + 'The hook can use used to manage pagination for remote data by setting the total items after the first call. The `startIndex` or `page` can be used in the API query.', |
| 173 | + }, |
| 174 | + }, |
| 175 | +} |
| 176 | + |
| 177 | +export const MakeYourOwn: Story = () => { |
| 178 | + const items = Array.from({ length: 100 }, (v, i) => i) |
| 179 | + const { |
| 180 | + page, |
| 181 | + startIndex, |
| 182 | + endIndex, |
| 183 | + setNextPage, |
| 184 | + setPreviousPage, |
| 185 | + isNextDisabled, |
| 186 | + isPreviousDisabled, |
| 187 | + } = usePagination({ |
| 188 | + totalItems: 100, |
| 189 | + pageSize: 20, |
| 190 | + }) |
| 191 | + |
| 192 | + return ( |
| 193 | + <> |
| 194 | + <Row css={{ gap: '$2', mb: '$3' }}> |
| 195 | + {items.slice(startIndex, endIndex).map((item) => ( |
| 196 | + <div>{item}</div> |
| 197 | + ))} |
| 198 | + </Row> |
| 199 | + <Row css={{ gap: '$2' }}> |
| 200 | + <Button disabled={isPreviousDisabled} onClick={setPreviousPage}> |
| 201 | + Previous |
| 202 | + </Button> |
| 203 | + <Chip>{`Page ${page}`}</Chip> |
| 204 | + <Button disabled={isNextDisabled} onClick={setNextPage}> |
| 205 | + Next |
| 206 | + </Button> |
| 207 | + </Row> |
| 208 | + </> |
| 209 | + ) |
| 210 | +} |
| 211 | +MakeYourOwn.parameters = { |
| 212 | + docs: { |
| 213 | + description: { |
| 214 | + story: |
| 215 | + 'The data can be used to manage a `Pagination` component or you can create your own.', |
| 216 | + }, |
| 217 | + }, |
| 218 | +} |
0 commit comments