nextui/apps/docs/content/components/table/async-pagination.ts
Troye d8d2b87cb8
fix(docs): table async pagination (#1491)
* fix(docs): change to swr

* chore(table): change to swr

* fix(table): keep the pagination when switching pages

* chore(repo): pnpm lockfile updated
2023-09-02 12:56:39 +00:00

69 lines
1.8 KiB
TypeScript

const App = `import {Table, TableHeader, TableColumn, TableBody, TableRow, TableCell, Pagination, Spinner, getKeyValue} from "@nextui-org/react";
import useSWR from "swr";
const fetcher = (...args) => fetch(...args).then((res) => res.json());
export default function App() {
const [page, setPage] = React.useState(1);
const {data, isLoading} = useSWR(\`https://swapi.py4e.com/api/people?page=\$\{page\}\`, fetcher, {
keepPreviousData: true,
});
const rowsPerPage = 10;
const pages = useMemo(() => {
return data?.count ? Math.ceil(data.count / rowsPerPage) : 0;
}, [data?.count, rowsPerPage]);
const loadingState = isLoading || data?.results.length === 0 ? "loading" : "idle";
return (
<Table
aria-label="Example table with client async pagination"
bottomContent={
pages > 0 ? (
<div className="flex w-full justify-center">
<Pagination
isCompact
showControls
showShadow
color="primary"
page={page}
total={pages}
onChange={(page) => setPage(page)}
/>
</div>
) : null
}
{...args}
>
<TableHeader>
<TableColumn key="name">Name</TableColumn>
<TableColumn key="height">Height</TableColumn>
<TableColumn key="mass">Mass</TableColumn>
<TableColumn key="birth_year">Birth year</TableColumn>
</TableHeader>
<TableBody
items={data?.results ?? []}
loadingContent={<Spinner />}
loadingState={loadingState}
>
{(item) => (
<TableRow key={item?.name}>
{(columnKey) => <TableCell>{getKeyValue(item, columnKey)}</TableCell>}
</TableRow>
)}
</TableBody>
</Table>
);
}`;
const react = {
"/App.jsx": App,
};
export default {
...react,
};