-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e2422e5
commit 05fc47c
Showing
18 changed files
with
26,418 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { FC } from 'react' | ||
import { components, ClassName, renderClassName, events } from 'fastui' | ||
|
||
interface Link { | ||
Display: FC | ||
ariaLabel?: string | ||
locked?: boolean | ||
active?: boolean | ||
page?: number | ||
} | ||
|
||
export const Pagination: FC<components.PaginationProps> = (props) => { | ||
const { page, pageCount } = props | ||
|
||
if (pageCount === 1) return null | ||
|
||
const links: Link[] = [ | ||
{ | ||
Display: () => <span aria-hidden="true">«</span>, | ||
ariaLabel: 'Previous', | ||
locked: page === 1, | ||
page: page - 1, | ||
}, | ||
{ | ||
Display: () => <>1</>, | ||
locked: page === 1, | ||
active: page === 1, | ||
page: 1, | ||
}, | ||
] | ||
|
||
if (page > 4) { | ||
links.push({ Display: () => <>...</> }) | ||
} | ||
|
||
for (let p = page - 2; p <= page + 2; p++) { | ||
if (p <= 1 || p >= pageCount) continue | ||
links.push({ | ||
Display: () => <>{p}</>, | ||
locked: page === p, | ||
active: page === p, | ||
page: p, | ||
}) | ||
} | ||
|
||
if (page < pageCount - 3) { | ||
links.push({ Display: () => <>...</> }) | ||
} | ||
|
||
links.push({ | ||
Display: () => <>{pageCount}</>, | ||
locked: page === pageCount, | ||
page: pageCount, | ||
}) | ||
|
||
links.push({ | ||
Display: () => <span aria-hidden="true">»</span>, | ||
ariaLabel: 'Next', | ||
locked: page === pageCount, | ||
page: page + 1, | ||
}) | ||
|
||
return ( | ||
<nav aria-label="Pagination"> | ||
<ul className="pagination justify-content-center"> | ||
{links.map((link, i) => ( | ||
<PaginationLink key={i} {...link} /> | ||
))} | ||
</ul> | ||
</nav> | ||
) | ||
} | ||
|
||
const PaginationLink: FC<Link> = ({ Display, ariaLabel, locked, active, page }) => { | ||
if (!page) { | ||
return ( | ||
<li className="page-item"> | ||
<span className="page-link px-2 text-muted"> | ||
<Display /> | ||
</span> | ||
</li> | ||
) | ||
} | ||
const className = renderClassName({ 'page-link': true, disabled: locked && !active, active } as ClassName) | ||
let onClick: events.GoToEvent | ||
if (page === 1) { | ||
onClick = { type: 'go-to', query: { page: null } } | ||
} else { | ||
onClick = { type: 'go-to', query: { page } } | ||
} | ||
return ( | ||
<li className="page-item"> | ||
<components.LinkRender onClick={onClick} className={className} locked={locked || active} ariaLabel={ariaLabel}> | ||
<Display /> | ||
</components.LinkRender> | ||
</li> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { FC } from 'react' | ||
|
||
import { ClassName, useClassName } from '../hooks/className' | ||
|
||
import { LinkProps } from './link' | ||
import { LinkListComp } from './LinkList' | ||
|
||
export interface PaginationProps { | ||
type: 'Pagination' | ||
page: number | ||
pageSize: number | ||
total: number | ||
pageCount: number | ||
className?: ClassName | ||
} | ||
|
||
export const PaginationComp: FC<PaginationProps> = (props) => { | ||
const { page, pageCount } = props | ||
const className = useClassName(props) | ||
|
||
if (pageCount === 1) return null | ||
|
||
const links: LinkProps[] = [ | ||
{ | ||
type: 'Link', | ||
components: [{ type: 'Text', text: 'Previous' }], | ||
locked: page !== 1, | ||
onClick: { type: 'go-to', query: { page: page - 1 } }, | ||
}, | ||
{ | ||
type: 'Link', | ||
components: [{ type: 'Text', text: 'Next' }], | ||
locked: page !== pageCount, | ||
onClick: { type: 'go-to', query: { page: page + 1 } }, | ||
}, | ||
] | ||
|
||
return ( | ||
<div className={className}> | ||
<LinkListComp type="LinkList" links={links} mode="pagination" /> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.