-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Doc: Tree Infinite Scrolling #28197
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
Merged
Merged
Doc: Tree Infinite Scrolling #28197
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
packages/react-components/react-tree/stories/D_flatTree/TreeInfiniteScrolling.md
This file contains hidden or 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 @@ | ||
| The `InfiniteScrolling` example of the `Tree` component provides a dynamic and efficient Tree structure that loads more data as the user scrolls down the list. It uses the `useFlatTree` hook to manage a flat array of tree items, converting them into a hierarchical tree structure as needed. | ||
|
|
||
| When the `Tree` is first rendered, a set number of `TreeItem` components are displayed, ensuring fast load times and efficient handling of potentially large amounts of data. As the user scrolls down the list, the `onScroll` event triggers the loading of more `TreeItem` components. | ||
|
|
||
| This approach not only enhances the scalability of your application, but also improves the user experience by loading data as and when it is needed. The user is not overwhelmed with all the data at once and does not have to wait for large amounts of data to load initially. |
128 changes: 128 additions & 0 deletions
128
packages/react-components/react-tree/stories/D_flatTree/TreeInfiniteScrolling.stories.tsx
This file contains hidden or 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,128 @@ | ||
| import * as React from 'react'; | ||
| import { Tree, TreeItem, TreeItemLayout, useFlatTree_unstable, FlatTreeItemProps } from '@fluentui/react-tree'; | ||
| import { makeStyles, shorthands } from '@fluentui/react-components'; | ||
| import story from './TreeInfiniteScrolling.md'; | ||
| import { Spinner } from '../../../react-spinner/src/Spinner'; | ||
|
|
||
| interface Result { | ||
| results: { name: string }[]; | ||
| } | ||
|
|
||
| type Item = FlatTreeItemProps & { name: string | React.ReactNode }; | ||
|
|
||
| const MAX_PAGES = 5; | ||
|
|
||
| const pinnedItems = [ | ||
| { value: 'pinned', name: 'Pinned', id: 'pinned' }, | ||
| { value: 'pinned-item-1', parentValue: 'pinned', name: 'Pinned item 1' }, | ||
| { value: 'pinned-item-2', parentValue: 'pinned', name: 'Pinned item 2' }, | ||
| { value: 'pinned-item-3', parentValue: 'pinned', name: 'Pinned item 3' }, | ||
| ]; | ||
|
|
||
| const useStyles = makeStyles({ | ||
| container: { | ||
| height: '400px', | ||
| paddingBottom: '10px', | ||
| ...shorthands.overflow('auto'), | ||
| }, | ||
| }); | ||
|
|
||
| export const InfiniteScrolling = () => { | ||
| const [page, setPage] = React.useState(1); | ||
| const [isLoading, setIsLoading] = React.useState(false); | ||
| const peopleItems = useQuery<Item[]>([ | ||
| { value: 'people', name: 'People' }, | ||
| ...Array.from({ length: 40 }, (_, index) => ({ | ||
| value: `person-${index + 1}`, | ||
| parentValue: 'people', | ||
| name: `Person ${index + 1}`, | ||
| })), | ||
| ]); | ||
|
|
||
| const items = React.useMemo<Item[]>( | ||
| () => [ | ||
| ...pinnedItems, | ||
| ...peopleItems.value, | ||
| ...(isLoading | ||
| ? [ | ||
| { | ||
| value: 'loading-people', | ||
| parentValue: 'people', | ||
| name: <Spinner aria-label="Loading more people" size="tiny" />, | ||
| }, | ||
| ] | ||
| : []), | ||
| ], | ||
| [isLoading, peopleItems], | ||
| ); | ||
|
|
||
| const styles = useStyles(); | ||
|
|
||
| const flatTree = useFlatTree_unstable(items, { defaultOpenItems: ['pinned', 'people'] }); | ||
| const listRef = React.useRef<HTMLDivElement>(null); | ||
|
petdud marked this conversation as resolved.
Outdated
|
||
|
|
||
| const fetchMoreItems = () => { | ||
| setIsLoading(true); | ||
|
|
||
| fetch(`https://swapi.dev/api/people?page=${page}`) | ||
| .then(res => res.json()) | ||
| .then((json: Result) => { | ||
| const fetchedItems = json.results.map<Item>(person => ({ | ||
| value: `person-${person.name}`, | ||
| parentValue: 'people', | ||
| name: person.name, | ||
| })); | ||
|
|
||
| setIsLoading(false); | ||
| setPage(page + 1); | ||
| peopleItems.query(() => [...peopleItems.value, ...fetchedItems]); | ||
| }); | ||
| }; | ||
|
|
||
| const handleScroll = (event: React.UIEvent<HTMLDivElement>) => { | ||
| const { scrollTop, scrollHeight, clientHeight } = event.currentTarget; | ||
| const hasReachedEnd = scrollHeight - scrollTop === clientHeight; | ||
|
|
||
| if (!isLoading && hasReachedEnd && page < MAX_PAGES) { | ||
| fetchMoreItems(); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div ref={listRef} onScroll={handleScroll} className={styles.container}> | ||
|
petdud marked this conversation as resolved.
Outdated
|
||
| <Tree {...flatTree.getTreeProps()} aria-label="Tree"> | ||
| {Array.from(flatTree.items(), flatTreeItem => { | ||
| const { name, ...treeItemProps } = flatTreeItem.getTreeItemProps(); | ||
| return ( | ||
| <TreeItem {...treeItemProps} key={flatTreeItem.value}> | ||
| <TreeItemLayout>{name}</TreeItemLayout> | ||
| </TreeItem> | ||
| ); | ||
| })} | ||
| </Tree> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| /** | ||
| * This function is just for the sake of the example, | ||
| * a library for fetching data (like react-query) might be a better option | ||
| */ | ||
| function useQuery<Value>(initialValue: Value) { | ||
| const [queryResult, setQueryResult] = React.useState({ value: initialValue, isLoading: false, isLoaded: false }); | ||
| const query = (fn: () => Promise<Value> | Value) => { | ||
| setQueryResult(curr => ({ ...curr, isLoading: true })); | ||
| Promise.resolve(fn()).then(nextValue => { | ||
| setQueryResult({ value: nextValue, isLoaded: true, isLoading: false }); | ||
| }); | ||
| }; | ||
| return { ...queryResult, query } as const; | ||
| } | ||
|
|
||
| InfiniteScrolling.parameters = { | ||
| docs: { | ||
| description: { | ||
| story, | ||
| }, | ||
| }, | ||
| }; | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.