Skip to content

Commit 3eebf49

Browse files
authored
Shuffle @oxide/pagination files (#1970)
shuffle @oxide/pagination
1 parent 80f1167 commit 3eebf49

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

libs/pagination/Pagination.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*
66
* Copyright Oxide Computer Company
77
*/
8+
import { useState } from 'react'
89
import tunnel from 'tunnel-rat'
910

1011
import {
@@ -29,3 +30,28 @@ export function Pagination({ inline = false, ...props }: PaginationProps) {
2930
}
3031

3132
Pagination.Target = Tunnel.Out
33+
34+
type PageToken = string | undefined
35+
36+
export function usePagination() {
37+
const [prevPages, setPrevPages] = useState<PageToken[]>([])
38+
const [currentPage, setCurrentPage] = useState<PageToken>()
39+
40+
const goToPrevPage = () => {
41+
const prevPage = prevPages.pop()
42+
setCurrentPage(prevPage)
43+
setPrevPages(prevPages)
44+
}
45+
46+
const goToNextPage = (nextPageToken: string) => {
47+
setPrevPages([...prevPages, currentPage])
48+
setCurrentPage(nextPageToken)
49+
}
50+
51+
return {
52+
currentPage,
53+
goToNextPage,
54+
goToPrevPage,
55+
hasPrev: prevPages.length > 0,
56+
}
57+
}

libs/pagination/__tests__/use-pagination.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { act, renderHook } from '@testing-library/react'
99
import { describe, expect, it } from 'vitest'
1010

11-
import { usePagination } from '../use-pagination'
11+
import { usePagination } from '../index.tsx'
1212

1313
describe('usePagination', () => {
1414
it('starts with empty state', () => {

libs/pagination/index.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@
66
* Copyright Oxide Computer Company
77
*/
88
import { useState } from 'react'
9+
import tunnel from 'tunnel-rat'
10+
11+
import {
12+
Pagination as UIPagination,
13+
type PaginationProps as UIPaginationProps,
14+
} from '@oxide/ui'
15+
16+
const Tunnel = tunnel('pagination')
17+
18+
interface PaginationProps extends UIPaginationProps {
19+
/** If true pagination will be rendered wherever `Pagination.Target` is included */
20+
inline?: boolean
21+
}
22+
export function Pagination({ inline = false, ...props }: PaginationProps) {
23+
if (inline) return <UIPagination {...props} />
24+
25+
return (
26+
<Tunnel.In>
27+
<UIPagination className="gutter h-14 py-5" {...props} />
28+
</Tunnel.In>
29+
)
30+
}
31+
32+
Pagination.Target = Tunnel.Out
933

1034
type PageToken = string | undefined
1135

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"@oxide/ui": ["libs/ui/index.ts"],
2020
"@oxide/util": ["libs/util/index.ts"],
2121
"@oxide/table": ["libs/table/index.ts"],
22-
"@oxide/pagination": ["libs/pagination/index.ts"]
22+
"@oxide/pagination": ["libs/pagination/index.tsx"]
2323
},
2424
"resolveJsonModule": true,
2525
"rootDir": ".",

0 commit comments

Comments
 (0)