-
Notifications
You must be signed in to change notification settings - Fork 24
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
Showing
8 changed files
with
227 additions
and
49 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
apps/frontend/web/app/components/Resources/pagination.tsx
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,102 @@ | ||
import clsx from 'clsx'; | ||
|
||
import { formatChinaTime } from './utils'; | ||
import { Link } from '@remix-run/react'; | ||
|
||
export interface PaginationProps { | ||
page: number; | ||
|
||
complete: boolean; | ||
|
||
timestamp: Date; | ||
|
||
link?: (page: number) => string; | ||
|
||
navigate?: (page: number) => void | Promise<void>; | ||
} | ||
|
||
export const Pagination = (props: PaginationProps) => { | ||
const { page, complete, timestamp } = props; | ||
const isPrev = page > 1; | ||
const isNext = !complete; | ||
const pages = isPrev ? [page - 1, page, page + 1, page + 2, page + 3] : [page, page + 1, page + 2, page + 3, page + 4]; | ||
|
||
if (page === 1 && complete) { | ||
// Only one page data, no pagination | ||
return; | ||
} else { | ||
return ( | ||
<div className="mt-4 flex lt-md:flex-col font-sm"> | ||
<div className="text-base-400 py-1 pl3 lt-sm:pl1"> | ||
<span className="mr1 text-sm i-carbon-update-now op-80"></span> | ||
<span className="select-none">数据更新于 </span> | ||
<span>{formatChinaTime(timestamp)}</span> | ||
</div> | ||
<div className="flex-auto"></div> | ||
<div className="flex lt-md:(mt-4 justify-center) items-center gap-2 text-base-500"> | ||
<PageItem | ||
page={page - 1} | ||
link={props.link} | ||
navigate={props.navigate} | ||
className={clsx( | ||
isPrev || 'hidden', | ||
'block text-link-active underline decoration-dotted underline-offset-4' | ||
)} | ||
> | ||
<span>上一页</span> | ||
</PageItem> | ||
{page > 2 && <span className="select-none i-ant-design:ellipsis-outlined"></span>} | ||
{pages.map((p) => ( | ||
<PageItem | ||
key={p} | ||
page={p} | ||
link={props.link} | ||
navigate={props.navigate} | ||
className={clsx( | ||
'block', | ||
p === page && 'text-pink-600' | ||
)} | ||
> | ||
<span>{p}</span> | ||
</PageItem> | ||
))} | ||
{isNext && <span className="select-none i-ant-design:ellipsis-outlined"></span>} | ||
<PageItem | ||
page={page + 1} | ||
link={props.link} | ||
navigate={props.navigate} | ||
className={clsx( | ||
isNext || 'hidden', | ||
'block text-link-active underline decoration-dotted underline-offset-4' | ||
)} | ||
> | ||
<span>下一页</span> | ||
</PageItem> | ||
</div> | ||
</div> | ||
); | ||
} | ||
}; | ||
|
||
const PageItem = ( | ||
props: Pick<PaginationProps, 'page' | 'link' | 'navigate'> & React.HTMLAttributes<any> | ||
) => { | ||
const { page, navigate, link } = props; | ||
const className = 'px-2 py-1 rounded-md hover:bg-gray-100 select-none cursor-pointer'; | ||
|
||
if (link) { | ||
return ( | ||
<Link to={link(page)} className={clsx(className, props.className)}> | ||
{props.children} | ||
</Link> | ||
); | ||
} else if (navigate) { | ||
return ( | ||
<div className={clsx(className, props.className)} onClick={() => navigate(page)}> | ||
{props.children} | ||
</div> | ||
); | ||
} else { | ||
return <div className={clsx(className, props.className)}>{props.children}</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { formatInTimeZone } from 'date-fns-tz'; | ||
|
||
export function formatChinaTime(date: Date) { | ||
return formatInTimeZone(date, 'Asia/Shanghai', 'yyyy-MM-dd HH:mm'); | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.