11React Pagination
22=============
33
4- This package includes pagination hooks for React.
4+ This package includes pagination hooks for React. The hooks support saving the search and pagination values in local state or in the browser URL.
55
66## Table of content
77
88- [ Usage] ( #usage )
9+ - [ useSearch] ( #usesearch )
10+ - [ usePagination] ( #usepagination )
11+ - [ useSearchAndPagination] ( #usesearchandpagination )
12+ - [ Supported Implementations] ( #supported-implementations )
913 - [ In Memory Pagination] ( #in-memory-pagination )
10- - [ Router based pagination] ( #router-based-pagination )
14+ - [ React-Router based pagination] ( #router-based-pagination )
15+ - [ NextJS Router based pagination] ( #nextjs-router-based-pagination )
1116- [ Build & Publish] ( #build--publish )
1217- [ Information] ( #information )
1318
@@ -21,55 +26,138 @@ npm install @aboutbits/react-pagination
2126
2227Second, you can make use of the different hooks.
2328
24- ### In Memory Pagination
29+ - [ useSearch] ( #usesearch )
30+ - [ usePagination] ( #usepagination )
31+ - [ useSearchAndPagination] ( #usesearchandpagination )
32+
33+ ### useSearch
2534
26- Use this pagination hook if you want to keep track of the pagination in memory. This is very handy for dialogs.
35+ This hook saves the value of your search input and lets you change it or clear it.
36+
37+ ``` tsx
38+
39+ import { useSearch } from ' @aboutbits/react-pagination/nextRouterPagination'
40+
41+ const users = [
42+ ' Alex' , ' Nadia' , ' Natan' , ' Marie' , ' Moritz'
43+ ]
44+
45+ function UserList() {
46+ const { search, searchActions } = useSearch ()
47+
48+ return (
49+ <div >
50+ <input onChange = { searchActions .search } />
51+
52+ <ul >
53+ { users
54+ .filter (user => user .startsWith (search ))
55+ .map (user => <li >{ user } </li >)}
56+ </ul >
57+ </div >
58+ )
59+ }
60+ ```
61+
62+ ### usePagination
63+
64+ This hook saves the value of your current page and lets you change it or clear it.
2765
2866``` tsx
29- import { ReactElement } from " react" ;
30- import { useSearchAndPaginationInMemory } from ' @aboutbits/react-pagination'
3167
32- function UsersDialog(): ReactElement {
68+ import { usePagination } from ' @aboutbits/react-pagination/nextRouterPagination"
69+
70+ const users = [
71+ [' Alex' , ' Nadia' ],
72+ [' Natan' , ' Marie' ],
73+ [' Moritz' , ' Franz' ]
74+ ]
75+
76+ function UserList() {
77+ const { page, size, paginationActions } = usePagination ()
78+
79+ return (
80+ <div >
81+ <select onSelect = { paginationActions .setPage } >
82+ <option value = { 0 } >First Page</option >
83+ <option value = { 1 } >Second Page</option >
84+ <option value = { 2 } >Third Page</option >
85+ </select >
86+
87+ <ul >
88+ { users [page ]
89+ .map (user => <li >{ user } </li >)}
90+ </ul >
91+ </div >
92+ )
93+ }
94+ ```
3395
34- const {
35- page,
36- size,
37- search,
38- searchActions,
39- paginationActions,
40- } = useSearchAndPaginationInMemory ()
96+ ### useSearchAndPagination
97+
98+ This hook supports the combination of a search value and pagination.
99+ The ` clear ` function clears the search value and resets the page to the first page.
100+
101+ ``` tsx
102+ import { useSearchAndPagination } from ' @aboutbits/react-pagination/nextRouterPagination'
103+
104+ const users = [
105+ ' Alex' , ' Simon' , ' Natan' , ' Nadia' , ' Moritz' , ' Marie'
106+ ]
107+
108+ function UserList() {
109+ const { page, size, search, searchActions, paginationActions } = useSearchAndPagination ()
41110
42111 return (
43- <Dialog >
44- <DialogSelectHeader
45- title = " Users"
46- search = { search }
47- searchActions = { searchActions }
48- />
49- <AsyncView
50- { ... useSearchUsers ({
51- page: page ,
52- size: size ,
53- query: search ,
54- })}
55- renderSuccess = { (data ) => (
56- <DisplayData
57- data = { data }
58- paginationActions = { paginationActions }
59- />
60- )}
61- renderLoading = { <LoadingList />}
62- renderError = { (error ) => (
63- <Error error = { error } />
64- )}
65- />
66- </Dialog >
112+ <div >
113+ <input onChange = { searchActions .search } />
114+ <button onClick = { searchActions .clear } >Clear Input</button >
115+ <select onSelect = { paginationActions .setPage } >
116+ <option value = { 0 } >First Page</option >
117+ <option value = { 1 } >Second Page</option >
118+ </select >
119+
120+ <ul >
121+ { users .filter (user => user .startsWith (search ))
122+ .slice (page , page + size )
123+ .map (user => <li >{ user } </li >)}
124+ </ul >
125+ </div >
67126 )
68-
69127}
70128```
71129
72- ### Router based pagination
130+ ## Supported implementations
131+
132+ This package includes 3 different implementations of the above hooks.
133+ - [ In Memory] ( #in-memory-pagination )
134+ - [ React Router] ( #react-router-based-pagination )
135+ - [ NextJS Router] ( #nextjs-router-based-pagination )
136+
137+ ### In Memory Pagination
138+
139+ Use this pagination hook if you want to keep track of the pagination in memory.
140+ This is very handy for dialogs.
141+
142+ ``` tsx
143+ import { useSearchAndPaginationInMemory } from ' @aboutbits/react-pagination'
144+ ```
145+
146+ ### React-Router based pagination
147+
148+ These are specific hooks for applications that use [ React Router] ( https://reactrouter.com/ ) for routing.
149+
150+ ``` tsx
151+ import { useSearchAndPagination } from ' @aboutbits/react-pagination/reactRouterPagination'
152+ ```
153+
154+ ### NextJS Router based pagination
155+
156+ These are specific hooks for applications that use [ NextJS Router] ( https://nextjs.org/docs/api-reference/next/router ) for routing.
157+
158+ ``` tsx
159+ import { useSearchAndPagination } from ' @aboutbits/react-pagination/nextRouterPagination'
160+ ```
73161
74162## Build & Publish
75163
0 commit comments