mirror of
https://github.com/nextui-org/nextui.git
synced 2025-12-08 19:26:11 +00:00
* fix(docs): added ts example for infinite pagination * fix(docs): changed the condition of showOpenInCodeSandbox in CodeDemo * chore(docs): add bun command * chore(docs): add bun command --------- Co-authored-by: WK Wong <wingkwong.code@gmail.com>
165 lines
4.4 KiB
TypeScript
165 lines
4.4 KiB
TypeScript
const App = `import {Table, TableHeader, TableColumn, TableBody, TableRow, TableCell, Pagination, Spinner, getKeyValue} from "@nextui-org/react";
|
|
import {useInfiniteScroll} from "@nextui-org/use-infinite-scroll";
|
|
import {useAsyncList} from "@react-stately/data";
|
|
|
|
export default function App() {
|
|
const [isLoading, setIsLoading] = React.useState(true);
|
|
const [hasMore, setHasMore] = React.useState(false);
|
|
|
|
let list = useAsyncList({
|
|
async load({signal, cursor}) {
|
|
|
|
if (cursor) {
|
|
setIsLoading(false);
|
|
}
|
|
|
|
// If no cursor is available, then we're loading the first page.
|
|
// Otherwise, the cursor is the next URL to load, as returned from the previous page.
|
|
const res = await fetch(cursor || "https://swapi.py4e.com/api/people/?search=", {signal});
|
|
let json = await res.json();
|
|
|
|
setHasMore(json.next !== null);
|
|
|
|
return {
|
|
items: json.results,
|
|
cursor: json.next,
|
|
};
|
|
},
|
|
});
|
|
|
|
const [loaderRef, scrollerRef] = useInfiniteScroll({hasMore, onLoadMore: list.loadMore});
|
|
|
|
return (
|
|
<Table
|
|
isHeaderSticky
|
|
aria-label="Example table with infinite pagination"
|
|
baseRef={scrollerRef}
|
|
bottomContent={
|
|
hasMore ? (
|
|
<div className="flex w-full justify-center">
|
|
<Spinner ref={loaderRef} color="white" />
|
|
</div>
|
|
) : null
|
|
}
|
|
classNames={{
|
|
base: "max-h-[520px] overflow-scroll",
|
|
table: "min-h-[400px]",
|
|
}}
|
|
>
|
|
<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
|
|
isLoading={isLoading}
|
|
items={list.items}
|
|
loadingContent={<Spinner color="white" />}
|
|
>
|
|
{(item) => (
|
|
<TableRow key={item.name}>
|
|
{(columnKey) => <TableCell>{getKeyValue(item, columnKey)}</TableCell>}
|
|
</TableRow>
|
|
)}
|
|
</TableBody>
|
|
</Table>
|
|
);
|
|
}`;
|
|
|
|
const AppTs = `import {Table, TableHeader, TableColumn, TableBody, TableRow, TableCell, Pagination, Spinner, getKeyValue} from "@nextui-org/react";
|
|
import { useInfiniteScroll } from "@nextui-org/use-infinite-scroll";
|
|
import { useAsyncList } from "@react-stately/data";
|
|
|
|
interface SWCharacter {
|
|
name: string;
|
|
height: string;
|
|
mass: string;
|
|
birth_year: string;
|
|
}
|
|
|
|
export default function App() {
|
|
const [isLoading, setIsLoading] = React.useState<boolean>(true);
|
|
const [hasMore, setHasMore] = React.useState<boolean>(false);
|
|
|
|
let list = useAsyncList<SWCharacter>({
|
|
async load({ signal, cursor }) {
|
|
if (cursor) {
|
|
setIsLoading(false);
|
|
}
|
|
|
|
// If no cursor is available, then we're loading the first page.
|
|
// Otherwise, the cursor is the next URL to load, as returned from the previous page.
|
|
const res = await fetch(
|
|
cursor || "https://swapi.py4e.com/api/people/?search=",
|
|
{ signal }
|
|
);
|
|
let json = await res.json();
|
|
|
|
setHasMore(json.next !== null);
|
|
|
|
return {
|
|
items: json.results,
|
|
cursor: json.next,
|
|
};
|
|
},
|
|
});
|
|
|
|
const [loaderRef, scrollerRef] = useInfiniteScroll({
|
|
hasMore,
|
|
onLoadMore: list.loadMore,
|
|
});
|
|
|
|
return (
|
|
<Table
|
|
isHeaderSticky
|
|
aria-label="Example table with infinite pagination"
|
|
baseRef={scrollerRef}
|
|
bottomContent={
|
|
hasMore ? (
|
|
<div className="flex w-full justify-center">
|
|
<Spinner ref={loaderRef} color="white" />
|
|
</div>
|
|
) : null
|
|
}
|
|
classNames={{
|
|
base: "max-h-[520px] overflow-scroll",
|
|
table: "min-h-[400px]",
|
|
}}
|
|
>
|
|
<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
|
|
isLoading={isLoading}
|
|
items={list.items}
|
|
loadingContent={<Spinner color="white" />}
|
|
>
|
|
{(item: SWCharacter) => (
|
|
<TableRow key={item.name}>
|
|
{(columnKey) => (
|
|
<TableCell>{getKeyValue(item, columnKey)}</TableCell>
|
|
)}
|
|
</TableRow>
|
|
)}
|
|
</TableBody>
|
|
</Table>
|
|
);
|
|
}`;
|
|
|
|
const react = {
|
|
"/App.jsx": App,
|
|
};
|
|
|
|
const reactTs = {
|
|
"/App.tsx": AppTs,
|
|
};
|
|
|
|
export default {
|
|
...react,
|
|
...reactTs,
|
|
};
|