@mui/x-tree-view-pro 8.0.0-beta.2 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/CHANGELOG.md +423 -96
  2. package/RichTreeViewPro/RichTreeViewPro.js +12 -8
  3. package/RichTreeViewPro/RichTreeViewPro.plugins.d.ts +3 -5
  4. package/RichTreeViewPro/RichTreeViewPro.plugins.js +1 -1
  5. package/RichTreeViewPro/RichTreeViewPro.types.d.ts +4 -4
  6. package/esm/RichTreeViewPro/RichTreeViewPro.js +11 -7
  7. package/esm/RichTreeViewPro/RichTreeViewPro.plugins.d.ts +3 -5
  8. package/esm/RichTreeViewPro/RichTreeViewPro.plugins.js +2 -2
  9. package/esm/RichTreeViewPro/RichTreeViewPro.types.d.ts +4 -4
  10. package/esm/index.js +1 -1
  11. package/esm/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.itemPlugin.js +1 -1
  12. package/esm/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.js +17 -11
  13. package/esm/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.selectors.d.ts +30 -2
  14. package/esm/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.selectors.js +2 -2
  15. package/index.js +1 -1
  16. package/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.itemPlugin.js +1 -1
  17. package/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.js +16 -10
  18. package/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.selectors.d.ts +30 -2
  19. package/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.selectors.js +1 -1
  20. package/package.json +9 -17
  21. package/esm/internals/utils/releaseInfo.d.ts +0 -1
  22. package/esm/internals/utils/releaseInfo.js +0 -13
  23. package/internals/utils/releaseInfo.d.ts +0 -1
  24. package/internals/utils/releaseInfo.js +0 -21
  25. package/modern/RichTreeViewPro/RichTreeViewPro.d.ts +0 -20
  26. package/modern/RichTreeViewPro/RichTreeViewPro.js +0 -351
  27. package/modern/RichTreeViewPro/RichTreeViewPro.plugins.d.ts +0 -7
  28. package/modern/RichTreeViewPro/RichTreeViewPro.plugins.js +0 -6
  29. package/modern/RichTreeViewPro/RichTreeViewPro.types.d.ts +0 -51
  30. package/modern/RichTreeViewPro/RichTreeViewPro.types.js +0 -1
  31. package/modern/RichTreeViewPro/index.d.ts +0 -3
  32. package/modern/RichTreeViewPro/index.js +0 -3
  33. package/modern/RichTreeViewPro/richTreeViewProClasses.d.ts +0 -5
  34. package/modern/RichTreeViewPro/richTreeViewProClasses.js +0 -6
  35. package/modern/index.d.ts +0 -12
  36. package/modern/index.js +0 -22
  37. package/modern/internals/index.d.ts +0 -1
  38. package/modern/internals/index.js +0 -1
  39. package/modern/internals/plugins/useTreeViewItemsReordering/index.d.ts +0 -2
  40. package/modern/internals/plugins/useTreeViewItemsReordering/index.js +0 -1
  41. package/modern/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.d.ts +0 -3
  42. package/modern/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.itemPlugin.d.ts +0 -3
  43. package/modern/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.itemPlugin.js +0 -126
  44. package/modern/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.js +0 -203
  45. package/modern/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.selectors.d.ts +0 -417
  46. package/modern/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.selectors.js +0 -50
  47. package/modern/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.types.d.ts +0 -127
  48. package/modern/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.types.js +0 -1
  49. package/modern/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.utils.d.ts +0 -37
  50. package/modern/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.utils.js +0 -158
  51. package/modern/internals/plugins/useTreeViewLazyLoading/index.d.ts +0 -1
  52. package/modern/internals/plugins/useTreeViewLazyLoading/index.js +0 -1
  53. package/modern/internals/plugins/useTreeViewLazyLoading/useTreeViewLazyLoading.d.ts +0 -3
  54. package/modern/internals/plugins/useTreeViewLazyLoading/useTreeViewLazyLoading.js +0 -269
  55. package/modern/internals/plugins/useTreeViewLazyLoading/utils.d.ts +0 -32
  56. package/modern/internals/plugins/useTreeViewLazyLoading/utils.js +0 -85
  57. package/modern/internals/utils/releaseInfo.d.ts +0 -1
  58. package/modern/internals/utils/releaseInfo.js +0 -13
  59. package/modern/internals/zero-styled/index.d.ts +0 -3
  60. package/modern/internals/zero-styled/index.js +0 -7
  61. package/modern/package.json +0 -1
  62. package/modern/themeAugmentation/components.d.ts +0 -11
  63. package/modern/themeAugmentation/index.d.ts +0 -3
  64. package/modern/themeAugmentation/index.js +0 -0
  65. package/modern/themeAugmentation/overrides.d.ts +0 -12
  66. package/modern/themeAugmentation/props.d.ts +0 -10
  67. package/tsconfig.build.tsbuildinfo +0 -1
@@ -1,127 +0,0 @@
1
- import * as React from 'react';
2
- import { DefaultizedProps } from '@mui/x-internals/types';
3
- import { TreeViewPluginSignature, UseTreeViewItemsSignature } from '@mui/x-tree-view/internals';
4
- import { TreeViewItemId, TreeViewItemsReorderingAction, TreeViewCancellableEventHandler } from '@mui/x-tree-view/models';
5
- import { TreeItemDragAndDropOverlayProps } from '@mui/x-tree-view/TreeItemDragAndDropOverlay';
6
- export interface UseTreeViewItemsReorderingInstance {
7
- /**
8
- * Check if a given item can be dragged.
9
- * @param {TreeViewItemId} itemId The id of the item to check.
10
- * @returns {boolean} `true` if the item can be dragged, `false` otherwise.
11
- */
12
- canItemBeDragged: (itemId: TreeViewItemId) => boolean;
13
- /**
14
- * Get the valid reordering action if a given item is the target of the ongoing reordering.
15
- * @param {TreeViewItemId} itemId The id of the item to get the action of.
16
- * @returns {TreeViewItemItemReorderingValidActions} The valid actions for the item.
17
- */
18
- getDroppingTargetValidActions: (itemId: TreeViewItemId) => TreeViewItemItemReorderingValidActions;
19
- /**
20
- * Start a reordering for the given item.
21
- * @param {TreeViewItemId} itemId The id of the item to start the reordering for.
22
- */
23
- startDraggingItem: (itemId: TreeViewItemId) => void;
24
- /**
25
- * Stop the reordering of a given item.
26
- * @param {TreeViewItemId} itemId The id of the item to stop the reordering for.
27
- */
28
- stopDraggingItem: (itemId: TreeViewItemId) => void;
29
- /**
30
- * Set the new target item for the ongoing reordering.
31
- * The action will be determined based on the position of the cursor inside the target and the valid actions for this target.
32
- * @param {object} params The params describing the new target item.
33
- * @param {TreeViewItemId} params.itemId The id of the new target item.
34
- * @param {TreeViewItemItemReorderingValidActions} params.validActions The valid actions for the new target item.
35
- * @param {number} params.targetHeight The height of the target item.
36
- * @param {number} params.cursorY The Y coordinate of the mouse cursor.
37
- * @param {number} params.cursorX The X coordinate of the mouse cursor.
38
- * @param {HTMLDivElement} params.contentElement The DOM element rendered for the content slot.
39
- */
40
- setDragTargetItem: (params: {
41
- itemId: TreeViewItemId;
42
- validActions: TreeViewItemItemReorderingValidActions;
43
- targetHeight: number;
44
- cursorY: number;
45
- cursorX: number;
46
- contentElement: HTMLDivElement;
47
- }) => void;
48
- }
49
- export interface TreeViewItemReorderPosition {
50
- parentId: string | null;
51
- index: number;
52
- }
53
- export type TreeViewItemItemReorderingValidActions = { [key in TreeViewItemsReorderingAction]?: TreeViewItemReorderPosition };
54
- export interface UseTreeViewItemsReorderingParameters {
55
- /**
56
- * If `true`, the reordering of items is enabled.
57
- * @default false
58
- */
59
- itemsReordering?: boolean;
60
- /**
61
- * Determine if a given item can be reordered.
62
- * @param {string} itemId The id of the item to check.
63
- * @returns {boolean} `true` if the item can be reordered.
64
- * @default () => true
65
- */
66
- isItemReorderable?: (itemId: string) => boolean;
67
- /**
68
- * Used to determine if a given item can move to some new position.
69
- * @param {object} params The params describing the item re-ordering.
70
- * @param {string} params.itemId The id of the item that is being moved to a new position.
71
- * @param {TreeViewItemReorderPosition} params.oldPosition The old position of the item.
72
- * @param {TreeViewItemReorderPosition} params.newPosition The new position of the item.
73
- * @returns {boolean} `true` if the item can move to the new position.
74
- */
75
- canMoveItemToNewPosition?: (params: {
76
- itemId: string;
77
- oldPosition: TreeViewItemReorderPosition;
78
- newPosition: TreeViewItemReorderPosition;
79
- }) => boolean;
80
- /**
81
- * Callback fired when a Tree Item is moved in the tree.
82
- * @param {object} params The params describing the item re-ordering.
83
- * @param {string} params.itemId The id of the item moved.
84
- * @param {TreeViewItemReorderPosition} params.oldPosition The old position of the item.
85
- * @param {TreeViewItemReorderPosition} params.newPosition The new position of the item.
86
- */
87
- onItemPositionChange?: (params: {
88
- itemId: string;
89
- oldPosition: TreeViewItemReorderPosition;
90
- newPosition: TreeViewItemReorderPosition;
91
- }) => void;
92
- }
93
- export type UseTreeViewItemsReorderingDefaultizedParameters = DefaultizedProps<UseTreeViewItemsReorderingParameters, 'itemsReordering'>;
94
- export interface UseTreeViewItemsReorderingState {
95
- itemsReordering: {
96
- isItemReorderable: (itemId: string) => boolean;
97
- currentReorder: {
98
- draggedItemId: string;
99
- targetItemId: string;
100
- newPosition: TreeViewItemReorderPosition | null;
101
- action: TreeViewItemsReorderingAction | null;
102
- } | null;
103
- };
104
- }
105
- export type UseTreeViewItemsReorderingSignature = TreeViewPluginSignature<{
106
- params: UseTreeViewItemsReorderingParameters;
107
- defaultizedParams: UseTreeViewItemsReorderingDefaultizedParameters;
108
- instance: UseTreeViewItemsReorderingInstance;
109
- state: UseTreeViewItemsReorderingState;
110
- dependencies: [UseTreeViewItemsSignature];
111
- }>;
112
- export interface UseTreeItemRootSlotPropsFromItemsReordering {
113
- draggable?: true;
114
- onDragStart?: TreeViewCancellableEventHandler<React.DragEvent>;
115
- onDragOver?: TreeViewCancellableEventHandler<React.DragEvent>;
116
- onDragEnd?: TreeViewCancellableEventHandler<React.DragEvent>;
117
- }
118
- export interface UseTreeItemContentSlotPropsFromItemsReordering {
119
- onDragEnter?: TreeViewCancellableEventHandler<React.DragEvent>;
120
- onDragOver?: TreeViewCancellableEventHandler<React.DragEvent>;
121
- }
122
- export interface UseTreeItemDragAndDropOverlaySlotPropsFromItemsReordering extends TreeItemDragAndDropOverlayProps {}
123
- declare module '@mui/x-tree-view/useTreeItem' {
124
- interface UseTreeItemRootSlotOwnProps extends UseTreeItemRootSlotPropsFromItemsReordering {}
125
- interface UseTreeItemContentSlotOwnProps extends UseTreeItemContentSlotPropsFromItemsReordering {}
126
- interface UseTreeItemDragAndDropOverlaySlotOwnProps extends UseTreeItemDragAndDropOverlaySlotPropsFromItemsReordering {}
127
- }
@@ -1,37 +0,0 @@
1
- import { TreeViewUsedStore, UseTreeViewItemsState } from '@mui/x-tree-view/internals';
2
- import { TreeViewItemId, TreeViewItemsReorderingAction } from '@mui/x-tree-view/models';
3
- import { TreeViewItemItemReorderingValidActions, TreeViewItemReorderPosition, UseTreeViewItemsReorderingSignature } from "./useTreeViewItemsReordering.types.js";
4
- /**
5
- * Checks if the item with the id itemIdB is an ancestor of the item with the id itemIdA.
6
- */
7
- export declare const isAncestor: (store: TreeViewUsedStore<UseTreeViewItemsReorderingSignature>, itemIdA: string, itemIdB: string) => boolean;
8
- interface GetNewPositionParams {
9
- itemChildrenIndentation: string | number;
10
- validActions: TreeViewItemItemReorderingValidActions;
11
- targetHeight: number;
12
- targetDepth: number;
13
- cursorY: number;
14
- cursorX: number;
15
- contentElement: HTMLDivElement;
16
- }
17
- export declare const chooseActionToApply: ({
18
- itemChildrenIndentation,
19
- validActions,
20
- targetHeight,
21
- targetDepth,
22
- cursorX,
23
- cursorY,
24
- contentElement
25
- }: GetNewPositionParams) => TreeViewItemsReorderingAction | null;
26
- export declare const moveItemInTree: <R extends {}>({
27
- itemToMoveId,
28
- oldPosition,
29
- newPosition,
30
- prevState
31
- }: {
32
- itemToMoveId: TreeViewItemId;
33
- oldPosition: TreeViewItemReorderPosition;
34
- newPosition: TreeViewItemReorderPosition;
35
- prevState: UseTreeViewItemsState<R>["items"];
36
- }) => UseTreeViewItemsState<R>["items"];
37
- export {};
@@ -1,158 +0,0 @@
1
- import _extends from "@babel/runtime/helpers/esm/extends";
2
- import { buildSiblingIndexes, TREE_VIEW_ROOT_PARENT_ID, selectorItemMeta } from '@mui/x-tree-view/internals';
3
- /**
4
- * Checks if the item with the id itemIdB is an ancestor of the item with the id itemIdA.
5
- */
6
- export const isAncestor = (store, itemIdA, itemIdB) => {
7
- const itemMetaA = selectorItemMeta(store.value, itemIdA);
8
- if (itemMetaA.parentId === itemIdB) {
9
- return true;
10
- }
11
- if (itemMetaA.parentId == null) {
12
- return false;
13
- }
14
- return isAncestor(store, itemMetaA.parentId, itemIdB);
15
- };
16
-
17
- /**
18
- * Transforms a CSS string `itemChildrenIndentation` into a number representing the indentation in number.
19
- * @param {string | null} itemChildrenIndentation The indentation as passed to the `itemChildrenIndentation` prop.
20
- * @param {HTMLElement} contentElement The DOM element to which the indentation will be applied.
21
- */
22
- const parseItemChildrenIndentation = (itemChildrenIndentation, contentElement) => {
23
- if (typeof itemChildrenIndentation === 'number') {
24
- return itemChildrenIndentation;
25
- }
26
- const pixelExec = /^(\d.+)(px)$/.exec(itemChildrenIndentation);
27
- if (pixelExec) {
28
- return parseFloat(pixelExec[1]);
29
- }
30
-
31
- // If the format is neither `px` nor a number, we need to measure the indentation using an actual DOM element.
32
- const tempElement = document.createElement('div');
33
- tempElement.style.width = itemChildrenIndentation;
34
- tempElement.style.position = 'absolute';
35
- contentElement.appendChild(tempElement);
36
- const value = tempElement.offsetWidth;
37
- contentElement.removeChild(tempElement);
38
- return value;
39
- };
40
- export const chooseActionToApply = ({
41
- itemChildrenIndentation,
42
- validActions,
43
- targetHeight,
44
- targetDepth,
45
- cursorX,
46
- cursorY,
47
- contentElement
48
- }) => {
49
- let action;
50
- const itemChildrenIndentationPx = parseItemChildrenIndentation(itemChildrenIndentation, contentElement);
51
- // If we can move the item to the parent of the target, then we allocate the left offset to this action
52
- // Support moving to other ancestors
53
- if (validActions['move-to-parent'] && cursorX < itemChildrenIndentationPx * targetDepth) {
54
- action = 'move-to-parent';
55
- }
56
-
57
- // If we can move the item inside the target, then we have the following split:
58
- // - the upper quarter of the target moves it above
59
- // - the lower quarter of the target moves it below
60
- // - the inner half makes it a child
61
- else if (validActions['make-child']) {
62
- if (validActions['reorder-above'] && cursorY < 1 / 4 * targetHeight) {
63
- action = 'reorder-above';
64
- } else if (validActions['reorder-below'] && cursorY > 3 / 4 * targetHeight) {
65
- action = 'reorder-below';
66
- } else {
67
- action = 'make-child';
68
- }
69
- }
70
- // If we can't move the item inside the target, then we have the following split:
71
- // - the upper half of the target moves it above
72
- // - the lower half of the target moves it below
73
- else {
74
- // eslint-disable-next-line no-lonely-if
75
- if (validActions['reorder-above'] && cursorY < 1 / 2 * targetHeight) {
76
- action = 'reorder-above';
77
- } else if (validActions['reorder-below'] && cursorY >= 1 / 2 * targetHeight) {
78
- action = 'reorder-below';
79
- } else {
80
- action = null;
81
- }
82
- }
83
- return action;
84
- };
85
- export const moveItemInTree = ({
86
- itemToMoveId,
87
- oldPosition,
88
- newPosition,
89
- prevState
90
- }) => {
91
- const itemToMoveMeta = prevState.itemMetaLookup[itemToMoveId];
92
- const oldParentId = oldPosition.parentId ?? TREE_VIEW_ROOT_PARENT_ID;
93
- const newParentId = newPosition.parentId ?? TREE_VIEW_ROOT_PARENT_ID;
94
-
95
- // 1. Update the `itemOrderedChildrenIds`.
96
- const itemOrderedChildrenIds = _extends({}, prevState.itemOrderedChildrenIdsLookup);
97
- if (oldParentId === newParentId) {
98
- const updatedChildren = [...itemOrderedChildrenIds[oldParentId]];
99
- updatedChildren.splice(oldPosition.index, 1);
100
- updatedChildren.splice(newPosition.index, 0, itemToMoveId);
101
- itemOrderedChildrenIds[itemToMoveMeta.parentId ?? TREE_VIEW_ROOT_PARENT_ID] = updatedChildren;
102
- } else {
103
- const updatedOldParentChildren = [...itemOrderedChildrenIds[oldParentId]];
104
- updatedOldParentChildren.splice(oldPosition.index, 1);
105
- itemOrderedChildrenIds[oldParentId] = updatedOldParentChildren;
106
- const updatedNewParentChildren = [...(itemOrderedChildrenIds[newParentId] ?? [])];
107
- updatedNewParentChildren.splice(newPosition.index, 0, itemToMoveId);
108
- itemOrderedChildrenIds[newParentId] = updatedNewParentChildren;
109
- }
110
-
111
- // 2. Update the `itemChildrenIndexes`
112
- const itemChildrenIndexes = _extends({}, prevState.itemChildrenIndexesLookup);
113
- itemChildrenIndexes[oldParentId] = buildSiblingIndexes(itemOrderedChildrenIds[oldParentId]);
114
- if (newParentId !== oldParentId) {
115
- itemChildrenIndexes[newParentId] = buildSiblingIndexes(itemOrderedChildrenIds[newParentId]);
116
- }
117
-
118
- // 3. Update the `itemMetaLookup`
119
- const itemMetaLookup = _extends({}, prevState.itemMetaLookup);
120
-
121
- // 3.1 Update the `expandable` property of the old and the new parent
122
- function updateExpandable(itemId) {
123
- const isExpandable = itemOrderedChildrenIds[itemId].length > 0;
124
- if (itemMetaLookup[itemId].expandable !== isExpandable) {
125
- itemMetaLookup[itemId] = _extends({}, itemMetaLookup[itemId], {
126
- expandable: isExpandable
127
- });
128
- }
129
- }
130
- if (oldParentId !== TREE_VIEW_ROOT_PARENT_ID && oldParentId !== newParentId) {
131
- updateExpandable(oldParentId);
132
- }
133
- if (newParentId !== TREE_VIEW_ROOT_PARENT_ID && newParentId !== oldParentId) {
134
- updateExpandable(newParentId);
135
- }
136
-
137
- // 3.2 Update the `parentId` and `depth` properties of the item to move
138
- // The depth is always defined because drag&drop is only usable with Rich Tree View components.
139
- const itemToMoveDepth = newPosition.parentId == null ? 0 : itemMetaLookup[newParentId].depth + 1;
140
- itemMetaLookup[itemToMoveId] = _extends({}, itemToMoveMeta, {
141
- parentId: newPosition.parentId,
142
- depth: itemToMoveDepth
143
- });
144
-
145
- // 3.3 Update the depth of all the children of the item to move
146
- const updateItemDepth = (itemId, depth) => {
147
- itemMetaLookup[itemId] = _extends({}, itemMetaLookup[itemId], {
148
- depth
149
- });
150
- itemOrderedChildrenIds[itemId]?.forEach(childId => updateItemDepth(childId, depth + 1));
151
- };
152
- itemOrderedChildrenIds[itemToMoveId]?.forEach(childId => updateItemDepth(childId, itemToMoveDepth + 1));
153
- return _extends({}, prevState, {
154
- itemOrderedChildrenIdsLookup: itemOrderedChildrenIds,
155
- itemChildrenIndexesLookup: itemChildrenIndexes,
156
- itemMetaLookup
157
- });
158
- };
@@ -1 +0,0 @@
1
- export { useTreeViewLazyLoading } from "./useTreeViewLazyLoading.js";
@@ -1 +0,0 @@
1
- export { useTreeViewLazyLoading } from "./useTreeViewLazyLoading.js";
@@ -1,3 +0,0 @@
1
- import { TreeViewPlugin } from '@mui/x-tree-view/internals';
2
- import type { UseTreeViewLazyLoadingSignature } from '@mui/x-tree-view/internals';
3
- export declare const useTreeViewLazyLoading: TreeViewPlugin<UseTreeViewLazyLoadingSignature>;
@@ -1,269 +0,0 @@
1
- import _extends from "@babel/runtime/helpers/esm/extends";
2
- import * as React from 'react';
3
- import useLazyRef from '@mui/utils/useLazyRef';
4
- import useEventCallback from '@mui/utils/useEventCallback';
5
- import { selectorItemMeta, selectorIsItemSelected, useInstanceEventHandler, selectorDataSourceState, selectorGetTreeItemError } from '@mui/x-tree-view/internals';
6
- import { DataSourceCacheDefault } from '@mui/x-tree-view/utils';
7
- import { NestedDataManager } from "./utils.js";
8
- const INITIAL_STATE = {
9
- loading: {},
10
- errors: {}
11
- };
12
- const noopCache = {
13
- clear: () => {},
14
- get: () => undefined,
15
- set: () => {}
16
- };
17
- function getCache(cacheProp) {
18
- if (cacheProp === null) {
19
- return noopCache;
20
- }
21
- return cacheProp ?? new DataSourceCacheDefault({});
22
- }
23
- export const useTreeViewLazyLoading = ({
24
- instance,
25
- params,
26
- store
27
- }) => {
28
- const isLazyLoadingEnabled = !!params.dataSource;
29
- const firstRenderRef = React.useRef(true);
30
- const nestedDataManager = useLazyRef(() => new NestedDataManager(instance)).current;
31
- const cacheRef = useLazyRef(() => getCache(params.dataSourceCache));
32
- const setDataSourceLoading = useEventCallback((itemId, isLoading) => {
33
- if (!isLazyLoadingEnabled) {
34
- return;
35
- }
36
- store.update(prevState => {
37
- if (!prevState.lazyLoading.dataSource.loading[itemId] && !isLoading) {
38
- return prevState;
39
- }
40
- const loading = _extends({}, prevState.lazyLoading.dataSource.loading);
41
- if (isLoading === false) {
42
- delete loading[itemId];
43
- } else {
44
- loading[itemId] = isLoading;
45
- }
46
- return _extends({}, prevState, {
47
- lazyLoading: _extends({}, prevState.lazyLoading, {
48
- dataSource: _extends({}, prevState.lazyLoading.dataSource, {
49
- loading
50
- })
51
- })
52
- });
53
- });
54
- });
55
- const setDataSourceError = (itemId, error) => {
56
- if (!isLazyLoadingEnabled) {
57
- return;
58
- }
59
- store.update(prevState => {
60
- const errors = _extends({}, prevState.lazyLoading.dataSource.errors);
61
- if (error === null && errors[itemId] !== undefined) {
62
- delete errors[itemId];
63
- } else {
64
- errors[itemId] = error;
65
- }
66
- errors[itemId] = error;
67
- return _extends({}, prevState, {
68
- lazyLoading: _extends({}, prevState.lazyLoading, {
69
- dataSource: _extends({}, prevState.lazyLoading.dataSource, {
70
- errors
71
- })
72
- })
73
- });
74
- });
75
- };
76
- const resetDataSourceState = useEventCallback(() => {
77
- if (!isLazyLoadingEnabled) {
78
- return;
79
- }
80
- store.update(prevState => _extends({}, prevState, {
81
- lazyLoading: _extends({}, prevState.lazyLoading, {
82
- dataSource: INITIAL_STATE
83
- })
84
- }));
85
- });
86
- const fetchItems = useEventCallback(async parentIds => {
87
- if (!isLazyLoadingEnabled) {
88
- return;
89
- }
90
- const getChildrenCount = params.dataSource?.getChildrenCount || (() => 0);
91
- const getTreeItems = params.dataSource?.getTreeItems;
92
- if (!getTreeItems) {
93
- return;
94
- }
95
- if (parentIds) {
96
- await nestedDataManager.queue(parentIds);
97
- return;
98
- }
99
- nestedDataManager.clear();
100
-
101
- // reset the state if we are refetching the first visible items
102
- if (selectorDataSourceState(store.value) !== INITIAL_STATE) {
103
- resetDataSourceState();
104
- }
105
- // handle caching here
106
- const cachedData = cacheRef.current.get('root');
107
- if (cachedData !== undefined) {
108
- return;
109
- }
110
-
111
- // handle loading here
112
- instance.setTreeViewLoading(true);
113
- try {
114
- const getTreeItemsResponse = await getTreeItems();
115
-
116
- // set caching
117
- cacheRef.current.set('root', getTreeItemsResponse);
118
-
119
- // update the items in the state
120
- instance.addItems({
121
- items: getTreeItemsResponse,
122
- depth: 0,
123
- getChildrenCount
124
- });
125
- } catch (error) {
126
- // set the items to empty
127
- instance.addItems({
128
- items: [],
129
- depth: 0,
130
- getChildrenCount
131
- });
132
- // set error state
133
- instance.setTreeViewError(error);
134
- } finally {
135
- // set loading state
136
- instance.setTreeViewLoading(false);
137
- }
138
- });
139
- const fetchItemChildren = useEventCallback(async id => {
140
- if (!isLazyLoadingEnabled) {
141
- return;
142
- }
143
- const getChildrenCount = params.dataSource?.getChildrenCount || (() => 0);
144
- const getTreeItems = params.dataSource?.getTreeItems;
145
- if (!getTreeItems) {
146
- nestedDataManager.clearPendingRequest(id);
147
- return;
148
- }
149
- const parent = selectorItemMeta(store.value, id);
150
- if (!parent) {
151
- nestedDataManager.clearPendingRequest(id);
152
- return;
153
- }
154
- const depth = parent.depth ? parent.depth + 1 : 1;
155
-
156
- // handle loading here
157
- instance.setDataSourceLoading(id, true);
158
-
159
- // handle caching here
160
- const cachedData = cacheRef.current.get(id);
161
- if (cachedData !== undefined && cachedData !== -1) {
162
- nestedDataManager.setRequestSettled(id);
163
- instance.addItems({
164
- items: cachedData,
165
- depth,
166
- parentId: id,
167
- getChildrenCount
168
- });
169
- instance.setDataSourceLoading(id, false);
170
- return;
171
- }
172
- if (cachedData === -1) {
173
- instance.removeChildren(id);
174
- }
175
- const existingError = selectorGetTreeItemError(store.value, id) ?? null;
176
- if (existingError) {
177
- instance.setDataSourceError(id, null);
178
- }
179
- try {
180
- const getTreeItemsResponse = await getTreeItems(id);
181
- nestedDataManager.setRequestSettled(id);
182
-
183
- // set caching
184
- cacheRef.current.set(id, getTreeItemsResponse);
185
- // update the items in the state
186
- instance.addItems({
187
- items: getTreeItemsResponse,
188
- depth,
189
- parentId: id,
190
- getChildrenCount
191
- });
192
- } catch (error) {
193
- const childrenFetchError = error;
194
- // handle errors here
195
- instance.setDataSourceError(id, childrenFetchError);
196
- instance.removeChildren(id);
197
- } finally {
198
- // unset loading
199
- instance.setDataSourceLoading(id, false);
200
- nestedDataManager.setRequestSettled(id);
201
- }
202
- });
203
- useInstanceEventHandler(instance, 'beforeItemToggleExpansion', async eventParameters => {
204
- if (!isLazyLoadingEnabled || !eventParameters.shouldBeExpanded) {
205
- return;
206
- }
207
- eventParameters.isExpansionPrevented = true;
208
- await instance.fetchItems([eventParameters.itemId]);
209
- const fetchErrors = Boolean(selectorGetTreeItemError(store.value, eventParameters.itemId));
210
- if (!fetchErrors) {
211
- instance.applyItemExpansion({
212
- itemId: eventParameters.itemId,
213
- shouldBeExpanded: true,
214
- event: eventParameters.event
215
- });
216
- if (selectorIsItemSelected(store.value, eventParameters.itemId)) {
217
- // make sure selection propagation works correctly
218
- instance.setItemSelection({
219
- event: eventParameters.event,
220
- itemId: eventParameters.itemId,
221
- keepExistingSelection: true,
222
- shouldBeSelected: true
223
- });
224
- }
225
- }
226
- });
227
- React.useEffect(() => {
228
- if (isLazyLoadingEnabled && firstRenderRef.current) {
229
- store.update(prevState => _extends({}, prevState, {
230
- lazyLoading: _extends({}, prevState.lazyLoading, {
231
- enabled: true
232
- })
233
- }));
234
- if (params.items.length) {
235
- const getChildrenCount = params.dataSource?.getChildrenCount || (() => 0);
236
- instance.addItems({
237
- items: params.items,
238
- depth: 0,
239
- getChildrenCount
240
- });
241
- } else {
242
- instance.fetchItems();
243
- }
244
- firstRenderRef.current = false;
245
- }
246
- }, [instance, params.items, params.dataSource, isLazyLoadingEnabled, store]);
247
- if (isLazyLoadingEnabled) {
248
- instance.preventItemUpdates();
249
- }
250
- return {
251
- instance: {
252
- fetchItemChildren,
253
- fetchItems,
254
- setDataSourceLoading,
255
- setDataSourceError
256
- },
257
- publicAPI: {}
258
- };
259
- };
260
- useTreeViewLazyLoading.getInitialState = () => ({
261
- lazyLoading: {
262
- enabled: false,
263
- dataSource: INITIAL_STATE
264
- }
265
- });
266
- useTreeViewLazyLoading.params = {
267
- dataSource: true,
268
- dataSourceCache: true
269
- };
@@ -1,32 +0,0 @@
1
- import { TreeViewInstance, UseTreeViewLazyLoadingSignature } from '@mui/x-tree-view/internals';
2
- import { TreeViewItemId } from '@mui/x-tree-view/models';
3
- export declare enum RequestStatus {
4
- QUEUED = 0,
5
- PENDING = 1,
6
- SETTLED = 2,
7
- UNKNOWN = 3,
8
- }
9
- /**
10
- * Plugins that the `NestedDataManager` class can use if they are present, but are not required.
11
- */
12
- export type NestedDataManagerOptionalPlugins = readonly [];
13
- /**
14
- * Fetches row children from the server with option to limit the number of concurrent requests
15
- * Determines the status of a request based on the enum `RequestStatus`
16
- * Uses `ParentId` to uniquely identify a request
17
- */
18
- export declare class NestedDataManager {
19
- private pendingRequests;
20
- private queuedRequests;
21
- private settledRequests;
22
- private instance;
23
- private maxConcurrentRequests;
24
- constructor(instance: TreeViewInstance<[UseTreeViewLazyLoadingSignature]>, maxConcurrentRequests?: number);
25
- private processQueue;
26
- queue: (ids: TreeViewItemId[]) => Promise<void>;
27
- setRequestSettled: (id: TreeViewItemId) => Promise<void>;
28
- clear: () => void;
29
- clearPendingRequest: (id: TreeViewItemId) => Promise<void>;
30
- getRequestStatus: (id: TreeViewItemId) => RequestStatus;
31
- getActiveRequestsCount: () => number;
32
- }