Skip to content

Commit 26d8f01

Browse files
kuri-sunwingkwong
andauthored
fix(docs): added ts example for infinite pagination (#2718)
* 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>
1 parent 87336c7 commit 26d8f01

File tree

8 files changed

+119
-7
lines changed

8 files changed

+119
-7
lines changed

apps/docs/components/docs/components/code-demo/code-demo.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const CodeDemo: React.FC<CodeDemoProps> = ({
5858
isPreviewCentered = false,
5959
// when false .js files will be used
6060
typescriptStrict = false,
61-
showOpenInCodeSandbox,
61+
showOpenInCodeSandbox = true,
6262
isGradientBox = false,
6363
previewHeight = "auto",
6464
overflow = "visible",
@@ -139,7 +139,7 @@ export const CodeDemo: React.FC<CodeDemoProps> = ({
139139
files={files}
140140
highlightedLines={highlightedLines}
141141
showEditor={showEditor}
142-
showOpenInCodeSandbox={showOpenInCodeSandbox || showPreview}
142+
showOpenInCodeSandbox={showOpenInCodeSandbox}
143143
showPreview={showSandpackPreview}
144144
typescriptStrict={typescriptStrict}
145145
/>

apps/docs/content/components/table/infinite-pagination.ts

+88
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,98 @@ export default function App() {
6767
);
6868
}`;
6969

70+
const AppTs = `import {Table, TableHeader, TableColumn, TableBody, TableRow, TableCell, Pagination, Spinner, getKeyValue} from "@nextui-org/react";
71+
import { useInfiniteScroll } from "@nextui-org/use-infinite-scroll";
72+
import { useAsyncList } from "@react-stately/data";
73+
74+
interface SWCharacter {
75+
name: string;
76+
height: string;
77+
mass: string;
78+
birth_year: string;
79+
}
80+
81+
export default function App() {
82+
const [isLoading, setIsLoading] = React.useState<boolean>(true);
83+
const [hasMore, setHasMore] = React.useState<boolean>(false);
84+
85+
let list = useAsyncList<SWCharacter>({
86+
async load({ signal, cursor }) {
87+
if (cursor) {
88+
setIsLoading(false);
89+
}
90+
91+
// If no cursor is available, then we're loading the first page.
92+
// Otherwise, the cursor is the next URL to load, as returned from the previous page.
93+
const res = await fetch(
94+
cursor || "https://swapi.py4e.com/api/people/?search=",
95+
{ signal }
96+
);
97+
let json = await res.json();
98+
99+
setHasMore(json.next !== null);
100+
101+
return {
102+
items: json.results,
103+
cursor: json.next,
104+
};
105+
},
106+
});
107+
108+
const [loaderRef, scrollerRef] = useInfiniteScroll({
109+
hasMore,
110+
onLoadMore: list.loadMore,
111+
});
112+
113+
return (
114+
<Table
115+
isHeaderSticky
116+
aria-label="Example table with infinite pagination"
117+
baseRef={scrollerRef}
118+
bottomContent={
119+
hasMore ? (
120+
<div className="flex w-full justify-center">
121+
<Spinner ref={loaderRef} color="white" />
122+
</div>
123+
) : null
124+
}
125+
classNames={{
126+
base: "max-h-[520px] overflow-scroll",
127+
table: "min-h-[400px]",
128+
}}
129+
>
130+
<TableHeader>
131+
<TableColumn key="name">Name</TableColumn>
132+
<TableColumn key="height">Height</TableColumn>
133+
<TableColumn key="mass">Mass</TableColumn>
134+
<TableColumn key="birth_year">Birth year</TableColumn>
135+
</TableHeader>
136+
<TableBody
137+
isLoading={isLoading}
138+
items={list.items}
139+
loadingContent={<Spinner color="white" />}
140+
>
141+
{(item: SWCharacter) => (
142+
<TableRow key={item.name}>
143+
{(columnKey) => (
144+
<TableCell>{getKeyValue(item, columnKey)}</TableCell>
145+
)}
146+
</TableRow>
147+
)}
148+
</TableBody>
149+
</Table>
150+
);
151+
}`;
152+
70153
const react = {
71154
"/App.jsx": App,
72155
};
73156

157+
const reactTs = {
158+
"/App.tsx": AppTs,
159+
};
160+
74161
export default {
75162
...react,
163+
...reactTs,
76164
};

apps/docs/content/docs/components/autocomplete.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ import {useFilter} from "@react-aria/i18n";
226226
<CodeDemo
227227
title="Fully Controlled"
228228
showPreview={false}
229+
showOpenInCodeSandbox={false}
229230
highlightedLines="63-64,67,69-71"
230231
files={autocompleteContent.fullyControlled}
231232
/>
@@ -281,6 +282,7 @@ import {useAsyncList} from "@react-stately/data";
281282
typescriptStrict={true}
282283
title="Asynchronous Filtering"
283284
showPreview={false}
285+
showOpenInCodeSandbox={false}
284286
highlightedLines="27-29,33"
285287
files={autocompleteContent.asyncFiltering}
286288
/>
@@ -307,6 +309,7 @@ import {useInfiniteScroll} from "@nextui-org/use-infinite-scroll";
307309

308310
<CodeDemo
309311
showPreview={false}
312+
showOpenInCodeSandbox={false}
310313
typescriptStrict={true}
311314
title="Asynchronous Loading"
312315
highlightedLines="21-22,25,27"

apps/docs/content/docs/components/avatar.mdx

+3-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ You can also provide a custom fallback component to be displayed when the `src`
9090

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

93-
<CodeDemo showPreview={false} title="Custom implementation" files={avatarContent.customImpl} />
93+
<CodeDemo showPreview={false} showOpenInCodeSandbox={false} title="Custom implementation" files={avatarContent.customImpl} />
94+
9495

9596
### Custom initials logic
9697

@@ -134,7 +135,7 @@ By passing the `isGrid` prop to the `AvatarGroup` component, the avatars will be
134135
In case you need to customize the avatar group even further, you can use the `useAvatarGroup` hook and the
135136
`AvatarGroupProvider` to create your own implementation.
136137

137-
<CodeDemo showPreview={false} title="Custom implementation" files={avatarContent.groupCustomImpl} />
138+
<CodeDemo showPreview={false} showOpenInCodeSandbox={false} title="Custom implementation" files={avatarContent.groupCustomImpl} />
138139

139140
## Slots
140141

apps/docs/content/docs/components/button.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ You can customize the `Button` component by passing custom Tailwind CSS classes
101101

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

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

106106
## Button Group
107107

apps/docs/content/docs/components/image.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ You can use the `fallbackSrc` prop to display a fallback image when:
7979
Next.js provides an optimized [Image](https://nextjs.org/docs/app/api-reference/components/image) component,
8080
you can use it with NextUI `Image` component as well.
8181

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

8484
> **Note**: NextUI's `Image` component is `client-side`, using hooks like `useState` for loading states
8585
> and animations. Use Next.js `Image` alone if these features aren't required.

apps/docs/content/docs/components/link.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function App() {
102102

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

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

107107
<Spacer y={4} />
108108

apps/docs/content/docs/components/table.mdx

+20
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ sure to install it before using the sorting feature.
223223
npm: "npm install @react-stately/data",
224224
yarn: "yarn add @react-stately/data",
225225
pnpm: "pnpm add @react-stately/data",
226+
bun: "bun add @react-stately/data",
226227
}}
227228
/>
228229

@@ -288,13 +289,32 @@ It is also possible to use the [Pagination](/components/pagination) component to
288289
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
289290
[@nextui-org/use-infinite-scroll](https://www.npmjs.com/package/@nextui-org/use-infinite-scroll) hook.
290291

292+
<PackageManagers
293+
commands={{
294+
npm: "npm install @react-stately/data @nextui-org/use-infinite-scroll",
295+
yarn: "yarn add @react-stately/data @nextui-org/use-infinite-scroll",
296+
pnpm: "pnpm add @react-stately/data @nextui-org/use-infinite-scroll",
297+
bun: "bun add @react-stately/data @nextui-org/use-infinite-scroll",
298+
}}
299+
/>
300+
301+
```jsx
302+
import { useInfiniteScroll } from "@nextui-org/use-infinite-scroll";
303+
304+
import { useAsyncList } from "@react-stately/data";
305+
```
306+
307+
<Spacer y={2} />
308+
291309
<CodeDemo
292310
asIframe
293311
title="Infinite Paginated Table"
294312
resizeEnabled={false}
295313
files={tableContent.infinitePagination}
296314
previewHeight="620px"
297315
displayMode="visible"
316+
showPreview={true}
317+
showOpenInCodeSandbox={false}
298318
iframeSrc="/examples/table/infinite-pagination"
299319
/>
300320

0 commit comments

Comments
 (0)