-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[TreeView] Add lazy loading for children of tree items #15308
Merged
+2,298
−65
Merged
Changes from all commits
Commits
Show all changes
64 commits
Select commit
Hold shift + click to select a range
0918085
add getChildrenCount
noraleonte 45779ba
wip
noraleonte a29e2d0
wip
noraleonte 765c2a5
wip
noraleonte 7a3fad1
wip
noraleonte 80bcc27
docs wip
noraleonte 3e1e022
wip docs
noraleonte ea5a955
wip
noraleonte cf18e16
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte ef26a40
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte 1aff38e
add selector
noraleonte 5b9f5f8
wip
noraleonte addcdb8
wip
noraleonte c99a9dd
fix expansion and adjust initial state
noraleonte 620578e
add loading and error for tree view
noraleonte 26ebc5a
update error demo
noraleonte 0d54f50
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte aeefe05
rerun selection if needed
noraleonte 081d8a3
check usability of dnd
noraleonte c31a456
rename treeViewDataSource -> dataSource
noraleonte ce5f0c5
fix initial state and error demos
noraleonte 70a2fb8
add react-query demos
noraleonte 04466c3
fix label editing
noraleonte 6408dcb
wip
noraleonte 0f5c1bd
fix label editing
noraleonte 88dc0e8
move to pro
noraleonte 321f004
update meta
noraleonte c552110
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte fd58f94
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte d449f19
docs:api
noraleonte 6cc9de7
fix ts
noraleonte 996a1b8
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte a8a603c
add JSDocs
noraleonte 90ff8b1
fix
noraleonte c5802a1
add tests
noraleonte 9cbbd44
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte a9bcc3d
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte 25ce29d
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte 70581b0
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte 8c96d04
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte 476dc72
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte ec1c2e7
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte 651127a
update query library
noraleonte 999a3b7
remove random line
noraleonte 8f94e35
update editing md
noraleonte e91559b
fix
noraleonte d911bb0
fix
noraleonte 38499c3
updates batch 1
noraleonte dad833b
batch 2
noraleonte 7cb2532
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte 4f78bc9
Fix TS
flaviendelangle d1b573a
add slot for item error and loading state
noraleonte 11f66ac
Merge branch 'lazy-loading-poc' of github.com:noraleonte/mui-x into l…
noraleonte e734f78
scripts
noraleonte f9fcea6
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte 016d256
fix
noraleonte b9df1b3
rename updateItemsState
noraleonte 2973a25
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte 4796b78
fix typing issue
noraleonte 1807791
fix
noraleonte 2ea1dd3
fix ts errors
noraleonte d7f8c49
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte 4cf46d5
Merge branch 'master' of github.com:noraleonte/mui-x into lazy-loadin…
noraleonte bb5d8f5
update docs
noraleonte 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
This file contains 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
This file contains 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
68 changes: 68 additions & 0 deletions
68
docs/data/tree-view/rich-tree-view/lazy-loading/BasicLazyLoading.js
This file contains 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,68 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import Typography from '@mui/material/Typography'; | ||
import Slider from '@mui/material/Slider'; | ||
import { | ||
randomInt, | ||
randomName, | ||
randomId, | ||
randomBoolean, | ||
} from '@mui/x-data-grid-generator'; | ||
import { RichTreeViewPro } from '@mui/x-tree-view-pro/RichTreeViewPro'; | ||
|
||
function getSliderAriaValueText(value) { | ||
return `${value}°C`; | ||
} | ||
|
||
export default function BasicLazyLoading() { | ||
const [latency, setLatency] = React.useState(1000); | ||
|
||
const handleSliderChange = (_event, newLatency) => { | ||
setLatency(newLatency); | ||
}; | ||
|
||
const fetchData = async () => { | ||
const length = randomInt(5, 10); | ||
const rows = Array.from({ length }, () => ({ | ||
id: randomId(), | ||
label: randomName({}, {}), | ||
...(randomBoolean() ? { childrenCount: length } : {}), | ||
})); | ||
|
||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(rows); | ||
}, latency); | ||
}); | ||
}; | ||
|
||
return ( | ||
<Box sx={{ width: '300px' }}> | ||
<Box sx={{ width: 250 }}> | ||
<Typography id="latency-slider" gutterBottom> | ||
Loading latency: {latency} (ms) | ||
</Typography> | ||
<Slider | ||
value={latency} | ||
onChange={handleSliderChange} | ||
aria-labelledby="latency-slider" | ||
min={500} | ||
max={10000} | ||
shiftStep={1000} | ||
step={500} | ||
marks | ||
getAriaValueText={getSliderAriaValueText} | ||
valueLabelDisplay="auto" | ||
/> | ||
</Box> | ||
<RichTreeViewPro | ||
items={[]} | ||
experimentalFeatures={{ lazyLoading: true }} | ||
dataSource={{ | ||
getChildrenCount: (item) => item?.childrenCount, | ||
getTreeItems: fetchData, | ||
}} | ||
/> | ||
</Box> | ||
); | ||
} |
75 changes: 75 additions & 0 deletions
75
docs/data/tree-view/rich-tree-view/lazy-loading/BasicLazyLoading.tsx
This file contains 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,75 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import Typography from '@mui/material/Typography'; | ||
import Slider from '@mui/material/Slider'; | ||
import { | ||
randomInt, | ||
randomName, | ||
randomId, | ||
randomBoolean, | ||
} from '@mui/x-data-grid-generator'; | ||
import { RichTreeViewPro } from '@mui/x-tree-view-pro/RichTreeViewPro'; | ||
import { TreeViewBaseItem } from '@mui/x-tree-view/models'; | ||
|
||
type ItemType = TreeViewBaseItem<{ | ||
id: string; | ||
label: string; | ||
childrenCount?: number; | ||
}>; | ||
|
||
function getSliderAriaValueText(value: number) { | ||
return `${value}°C`; | ||
} | ||
|
||
export default function BasicLazyLoading() { | ||
const [latency, setLatency] = React.useState(1000); | ||
|
||
const handleSliderChange = (_event: Event, newLatency: number | number[]) => { | ||
setLatency(newLatency as number); | ||
}; | ||
|
||
const fetchData = async (): Promise<ItemType[]> => { | ||
const length: number = randomInt(5, 10); | ||
const rows = Array.from({ length }, () => ({ | ||
id: randomId(), | ||
label: randomName({}, {}), | ||
...(randomBoolean() ? { childrenCount: length } : {}), | ||
})); | ||
|
||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(rows); | ||
}, latency); | ||
}); | ||
}; | ||
|
||
return ( | ||
<Box sx={{ width: '300px' }}> | ||
<Box sx={{ width: 250 }}> | ||
<Typography id="latency-slider" gutterBottom> | ||
Loading latency: {latency} (ms) | ||
</Typography> | ||
<Slider | ||
value={latency} | ||
onChange={handleSliderChange} | ||
aria-labelledby="latency-slider" | ||
min={500} | ||
max={10000} | ||
shiftStep={1000} | ||
step={500} | ||
marks | ||
getAriaValueText={getSliderAriaValueText} | ||
valueLabelDisplay="auto" | ||
/> | ||
</Box> | ||
<RichTreeViewPro | ||
items={[]} | ||
experimentalFeatures={{ lazyLoading: true }} | ||
dataSource={{ | ||
getChildrenCount: (item) => item?.childrenCount as number, | ||
getTreeItems: fetchData, | ||
}} | ||
/> | ||
</Box> | ||
); | ||
} |
48 changes: 48 additions & 0 deletions
48
docs/data/tree-view/rich-tree-view/lazy-loading/ErrorManagement.js
This file contains 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,48 @@ | ||
import * as React from 'react'; | ||
import Button from '@mui/material/Button'; | ||
import Stack from '@mui/material/Stack'; | ||
import { randomName, randomId } from '@mui/x-data-grid-generator'; | ||
import { RichTreeViewPro } from '@mui/x-tree-view-pro/RichTreeViewPro'; | ||
import { initialItems } from './items'; | ||
|
||
export default function ErrorManagement() { | ||
const [failRequests, setFailRequests] = React.useState(false); | ||
const fetchData = async () => { | ||
const rows = Array.from({ length: 10 }, () => ({ | ||
id: randomId(), | ||
label: randomName({}, {}), | ||
childrenCount: 10, | ||
})); | ||
|
||
// make the promise fail conditionally | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
if (failRequests) { | ||
reject(new Error('Error fetching data')); | ||
} else { | ||
resolve(rows); | ||
} | ||
}, 1000); | ||
}); | ||
}; | ||
|
||
return ( | ||
<Stack spacing={2} sx={{ width: '300px' }}> | ||
<Button | ||
onClick={() => setFailRequests((prev) => !prev)} | ||
variant="outlined" | ||
fullWidth | ||
> | ||
{failRequests ? 'Resolve requests' : 'Fail Requests'} | ||
</Button> | ||
<RichTreeViewPro | ||
items={initialItems} | ||
experimentalFeatures={{ lazyLoading: true }} | ||
dataSource={{ | ||
getChildrenCount: (item) => item?.childrenCount, | ||
getTreeItems: fetchData, | ||
}} | ||
/> | ||
</Stack> | ||
); | ||
} |
48 changes: 48 additions & 0 deletions
48
docs/data/tree-view/rich-tree-view/lazy-loading/ErrorManagement.tsx
This file contains 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,48 @@ | ||
import * as React from 'react'; | ||
import Button from '@mui/material/Button'; | ||
import Stack from '@mui/material/Stack'; | ||
import { randomName, randomId } from '@mui/x-data-grid-generator'; | ||
import { RichTreeViewPro } from '@mui/x-tree-view-pro/RichTreeViewPro'; | ||
import { initialItems, ItemType } from './items'; | ||
|
||
export default function ErrorManagement() { | ||
const [failRequests, setFailRequests] = React.useState(false); | ||
const fetchData = async (): Promise<ItemType[]> => { | ||
const rows = Array.from({ length: 10 }, () => ({ | ||
id: randomId(), | ||
label: randomName({}, {}), | ||
childrenCount: 10, | ||
})); | ||
|
||
// make the promise fail conditionally | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
if (failRequests) { | ||
reject(new Error('Error fetching data')); | ||
} else { | ||
resolve(rows); | ||
} | ||
}, 1000); | ||
}); | ||
}; | ||
|
||
return ( | ||
<Stack spacing={2} sx={{ width: '300px' }}> | ||
<Button | ||
onClick={() => setFailRequests((prev) => !prev)} | ||
variant="outlined" | ||
fullWidth | ||
> | ||
{failRequests ? 'Resolve requests' : 'Fail Requests'} | ||
</Button> | ||
<RichTreeViewPro | ||
items={initialItems} | ||
experimentalFeatures={{ lazyLoading: true }} | ||
dataSource={{ | ||
getChildrenCount: (item) => item?.childrenCount as number, | ||
getTreeItems: fetchData, | ||
}} | ||
/> | ||
</Stack> | ||
); | ||
} |
15 changes: 15 additions & 0 deletions
15
docs/data/tree-view/rich-tree-view/lazy-loading/ErrorManagement.tsx.preview
This file contains 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,15 @@ | ||
<Button | ||
onClick={() => setFailRequests((prev) => !prev)} | ||
variant="outlined" | ||
fullWidth | ||
> | ||
{failRequests ? 'Resolve requests' : 'Fail Requests'} | ||
</Button> | ||
<RichTreeViewPro | ||
items={initialItems} | ||
experimentalFeatures={{ lazyLoading: true }} | ||
dataSource={{ | ||
getChildrenCount: (item) => item?.childrenCount as number, | ||
getTreeItems: fetchData, | ||
}} | ||
/> |
65 changes: 65 additions & 0 deletions
65
docs/data/tree-view/rich-tree-view/lazy-loading/FetchingWithReactQuery.js
This file contains 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,65 @@ | ||
import * as React from 'react'; | ||
import { RichTreeViewPro } from '@mui/x-tree-view-pro/RichTreeViewPro'; | ||
import { | ||
randomInt, | ||
randomName, | ||
randomId, | ||
randomBoolean, | ||
} from '@mui/x-data-grid-generator'; | ||
|
||
import { | ||
QueryClient, | ||
QueryClientProvider, | ||
useQueryClient, | ||
} from '@tanstack/react-query'; | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
const items = []; | ||
|
||
const fetchData = async (_parentId) => { | ||
const length = randomInt(5, 10); | ||
const rows = Array.from({ length }, () => ({ | ||
id: randomId(), | ||
label: randomName({}, {}), | ||
...(randomBoolean() ? { childrenCount: length } : {}), | ||
})); | ||
|
||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(rows); | ||
}, 1000); | ||
}); | ||
}; | ||
|
||
export function FetchChildren() { | ||
const myQueryClient = useQueryClient(); | ||
|
||
const fetchTreeItems = async (parentId) => { | ||
const queryKey = parentId ? ['treeItems', parentId] : ['treeItems', 'root']; | ||
const data = await myQueryClient.fetchQuery({ | ||
queryKey, | ||
queryFn: () => fetchData(parentId), | ||
}); | ||
return data; | ||
}; | ||
|
||
return ( | ||
<RichTreeViewPro | ||
items={items} | ||
experimentalFeatures={{ lazyLoading: true }} | ||
dataSource={{ | ||
getChildrenCount: (item) => item?.childrenCount, | ||
getTreeItems: fetchTreeItems, | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
export default function FetchingWithReactQuery() { | ||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
<FetchChildren /> | ||
</QueryClientProvider> | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.