Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs): added ts example for infinite pagination #2718

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const CodeDemo: React.FC<CodeDemoProps> = ({
isPreviewCentered = false,
// when false .js files will be used
typescriptStrict = false,
showOpenInCodeSandbox,
showOpenInCodeSandbox = true,
isGradientBox = false,
defaultExpanded = false,
previewHeight = "auto",
Expand Down Expand Up @@ -142,7 +142,7 @@ export const CodeDemo: React.FC<CodeDemoProps> = ({
files={files}
highlightedLines={highlightedLines}
showEditor={showEditor}
showOpenInCodeSandbox={showOpenInCodeSandbox || showPreview}
showOpenInCodeSandbox={showOpenInCodeSandbox}
showPreview={showSandpackPreview}
typescriptStrict={typescriptStrict}
/>
Expand Down
88 changes: 88 additions & 0 deletions apps/docs/content/components/table/infinite-pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,98 @@ export default function App() {
);
}`;

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,
};
},
});
wingkwong marked this conversation as resolved.
Show resolved Hide resolved

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,
};
3 changes: 3 additions & 0 deletions apps/docs/content/docs/components/autocomplete.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ import {useFilter} from "@react-aria/i18n";
<CodeDemo
title="Fully Controlled"
showPreview={false}
showOpenInCodeSandbox={false}
highlightedLines="63-64,67,69-71"
files={autocompleteContent.fullyControlled}
/>
Expand Down Expand Up @@ -254,6 +255,7 @@ import {useAsyncList} from "@react-stately/data";
typescriptStrict={true}
title="Asynchronous Filtering"
showPreview={false}
showOpenInCodeSandbox={false}
highlightedLines="27-29,33"
files={autocompleteContent.asyncFiltering}
/>
Expand All @@ -280,6 +282,7 @@ import {useInfiniteScroll} from "@nextui-org/use-infinite-scroll";

<CodeDemo
showPreview={false}
showOpenInCodeSandbox={false}
typescriptStrict={true}
title="Asynchronous Loading"
highlightedLines="21-22,25,27"
Expand Down
5 changes: 3 additions & 2 deletions apps/docs/content/docs/components/avatar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ You can also provide a custom fallback component to be displayed when the `src`

In case you need to customize the avatar even further, you can use the `useAvatar` hook to create your own implementation.

<CodeDemo showPreview={false} title="Custom implementation" files={avatarContent.customImpl} />
<CodeDemo showPreview={false} showOpenInCodeSandbox={false} title="Custom implementation" files={avatarContent.customImpl} />


### Custom initials logic

Expand Down Expand Up @@ -120,7 +121,7 @@ By passing the `isGrid` prop to the `AvatarGroup` component, the avatars will be
In case you need to customize the avatar group even further, you can use the `useAvatarGroup` hook and the
`AvatarGroupProvider` to create your own implementation.

<CodeDemo showPreview={false} title="Custom implementation" files={avatarContent.groupCustomImpl} />
<CodeDemo showPreview={false} showOpenInCodeSandbox={false} title="Custom implementation" files={avatarContent.groupCustomImpl} />

## Slots

Expand Down
2 changes: 1 addition & 1 deletion apps/docs/content/docs/components/button.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ You can customize the `Button` component by passing custom Tailwind CSS classes

You can also use the `useButton` hook to create your own button component.

<CodeDemo showPreview={false} title="Custom Implementation" files={buttonContent.customImpl} />
<CodeDemo showPreview={false} showOpenInCodeSandbox={false} title="Custom Implementation" files={buttonContent.customImpl} />

## Button Group

Expand Down
2 changes: 1 addition & 1 deletion apps/docs/content/docs/components/image.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ You can use the `fallbackSrc` prop to display a fallback image when:
Next.js provides an optimized [Image](https://nextjs.org/docs/app/api-reference/components/image) component,
you can use it with NextUI `Image` component as well.

<CodeDemo showPreview={false} title="With Next.js Image" files={imageContent.nextjs} />
<CodeDemo showPreview={false} showOpenInCodeSandbox={false} title="With Next.js Image" files={imageContent.nextjs} />

> **Note**: NextUI's `Image` component is `client-side`, using hooks like `useState` for loading states
> and animations. Use Next.js `Image` alone if these features aren't required.
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/content/docs/components/link.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function App() {

In case you need to customize the link even further, you can use the `useLink` hook to create your own implementation.

<CodeDemo showPreview={false} title="Custom implementation" files={linkContent.customImpl} />
<CodeDemo showPreview={false} showOpenInCodeSandbox={false} title="Custom implementation" files={linkContent.customImpl} />

<Spacer y={4} />{" "}

Expand Down
18 changes: 18 additions & 0 deletions apps/docs/content/docs/components/table.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,31 @@ It is also possible to use the [Pagination](/components/pagination) component to
Table also supports infinite pagination. To do so, you can use the `useAsyncList` hook from [@react-stately/data](https://react-spectrum.adobe.com/react-stately/useAsyncList.html) and
[@nextui-org/use-infinite-scroll](https://www.npmjs.com/package/@nextui-org/use-infinite-scroll) hook.

<PackageManagers
commands={{
npm: "npm install @react-stately/data @nextui-org/use-infinite-scroll",
yarn: "yarn add @react-stately/data @nextui-org/use-infinite-scroll",
pnpm: "pnpm add @react-stately/data @nextui-org/use-infinite-scroll",
}}
/>

```jsx
import { useInfiniteScroll } from "@nextui-org/use-infinite-scroll";

import { useAsyncList } from "@react-stately/data";
```

<Spacer y={2} />

<CodeDemo
asIframe
title="Infinite Paginated Table"
resizeEnabled={false}
files={tableContent.infinitePagination}
previewHeight="620px"
displayMode="visible"
showPreview={true}
showOpenInCodeSandbox={false}
iframeSrc="/examples/table/infinite-pagination"
/>

Expand Down
Loading