@mui/x-tree-view 6.0.0-alpha.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 (76) hide show
  1. package/CHANGELOG.md +3682 -0
  2. package/LICENSE +21 -0
  3. package/README.md +34 -0
  4. package/TreeItem/TreeItem.d.ts +13 -0
  5. package/TreeItem/TreeItem.js +392 -0
  6. package/TreeItem/TreeItem.types.d.ts +84 -0
  7. package/TreeItem/TreeItem.types.js +1 -0
  8. package/TreeItem/TreeItemContent.d.ts +52 -0
  9. package/TreeItem/TreeItemContent.js +101 -0
  10. package/TreeItem/index.d.ts +5 -0
  11. package/TreeItem/index.js +4 -0
  12. package/TreeItem/package.json +6 -0
  13. package/TreeItem/treeItemClasses.d.ts +23 -0
  14. package/TreeItem/treeItemClasses.js +6 -0
  15. package/TreeItem/useTreeItem.d.ts +10 -0
  16. package/TreeItem/useTreeItem.js +68 -0
  17. package/TreeView/TreeView.d.ts +14 -0
  18. package/TreeView/TreeView.js +854 -0
  19. package/TreeView/TreeView.types.d.ts +170 -0
  20. package/TreeView/TreeView.types.js +1 -0
  21. package/TreeView/TreeViewContext.d.ts +6 -0
  22. package/TreeView/TreeViewContext.js +31 -0
  23. package/TreeView/descendants.d.ts +42 -0
  24. package/TreeView/descendants.js +186 -0
  25. package/TreeView/index.d.ts +3 -0
  26. package/TreeView/index.js +3 -0
  27. package/TreeView/package.json +6 -0
  28. package/TreeView/treeViewClasses.d.ts +7 -0
  29. package/TreeView/treeViewClasses.js +6 -0
  30. package/index.d.ts +2 -0
  31. package/index.js +9 -0
  32. package/internals/models.d.ts +1 -0
  33. package/internals/models.js +1 -0
  34. package/legacy/TreeItem/TreeItem.js +391 -0
  35. package/legacy/TreeItem/TreeItem.types.js +1 -0
  36. package/legacy/TreeItem/TreeItemContent.js +98 -0
  37. package/legacy/TreeItem/index.js +4 -0
  38. package/legacy/TreeItem/treeItemClasses.js +6 -0
  39. package/legacy/TreeItem/useTreeItem.js +67 -0
  40. package/legacy/TreeView/TreeView.js +907 -0
  41. package/legacy/TreeView/TreeView.types.js +1 -0
  42. package/legacy/TreeView/TreeViewContext.js +41 -0
  43. package/legacy/TreeView/descendants.js +199 -0
  44. package/legacy/TreeView/index.js +3 -0
  45. package/legacy/TreeView/treeViewClasses.js +6 -0
  46. package/legacy/index.js +9 -0
  47. package/legacy/internals/models.js +1 -0
  48. package/modern/TreeItem/TreeItem.js +391 -0
  49. package/modern/TreeItem/TreeItem.types.js +1 -0
  50. package/modern/TreeItem/TreeItemContent.js +101 -0
  51. package/modern/TreeItem/index.js +4 -0
  52. package/modern/TreeItem/treeItemClasses.js +6 -0
  53. package/modern/TreeItem/useTreeItem.js +68 -0
  54. package/modern/TreeView/TreeView.js +853 -0
  55. package/modern/TreeView/TreeView.types.js +1 -0
  56. package/modern/TreeView/TreeViewContext.js +31 -0
  57. package/modern/TreeView/descendants.js +186 -0
  58. package/modern/TreeView/index.js +3 -0
  59. package/modern/TreeView/treeViewClasses.js +6 -0
  60. package/modern/index.js +9 -0
  61. package/modern/internals/models.js +1 -0
  62. package/node/TreeItem/TreeItem.js +400 -0
  63. package/node/TreeItem/TreeItem.types.js +5 -0
  64. package/node/TreeItem/TreeItemContent.js +109 -0
  65. package/node/TreeItem/index.js +38 -0
  66. package/node/TreeItem/treeItemClasses.js +15 -0
  67. package/node/TreeItem/useTreeItem.js +76 -0
  68. package/node/TreeView/TreeView.js +862 -0
  69. package/node/TreeView/TreeView.types.js +5 -0
  70. package/node/TreeView/TreeViewContext.js +40 -0
  71. package/node/TreeView/descendants.js +194 -0
  72. package/node/TreeView/index.js +27 -0
  73. package/node/TreeView/treeViewClasses.js +15 -0
  74. package/node/index.js +34 -0
  75. package/node/internals/models.js +5 -0
  76. package/package.json +59 -0
@@ -0,0 +1,170 @@
1
+ import * as React from 'react';
2
+ import { Theme } from '@mui/material/styles';
3
+ import { SxProps } from '@mui/system';
4
+ import { DefaultizedProps } from '../internals/models';
5
+ import { TreeViewClasses } from './treeViewClasses';
6
+ export interface TreeViewPropsBase extends React.HTMLAttributes<HTMLUListElement> {
7
+ /**
8
+ * The content of the component.
9
+ */
10
+ children?: React.ReactNode;
11
+ /**
12
+ * className applied to the root element.
13
+ */
14
+ className?: string;
15
+ /**
16
+ * Override or extend the styles applied to the component.
17
+ */
18
+ classes?: Partial<TreeViewClasses>;
19
+ /**
20
+ * The default icon used to collapse the node.
21
+ */
22
+ defaultCollapseIcon?: React.ReactNode;
23
+ /**
24
+ * The default icon displayed next to a end node. This is applied to all
25
+ * tree nodes and can be overridden by the TreeItem `icon` prop.
26
+ */
27
+ defaultEndIcon?: React.ReactNode;
28
+ /**
29
+ * Expanded node ids.
30
+ * Used when the item's expansion are not controlled.
31
+ * @default []
32
+ */
33
+ defaultExpanded?: string[];
34
+ /**
35
+ * The default icon used to expand the node.
36
+ */
37
+ defaultExpandIcon?: React.ReactNode;
38
+ /**
39
+ * The default icon displayed next to a parent node. This is applied to all
40
+ * parent nodes and can be overridden by the TreeItem `icon` prop.
41
+ */
42
+ defaultParentIcon?: React.ReactNode;
43
+ /**
44
+ * If `true`, will allow focus on disabled items.
45
+ * @default false
46
+ */
47
+ disabledItemsFocusable?: boolean;
48
+ /**
49
+ * If `true` selection is disabled.
50
+ * @default false
51
+ */
52
+ disableSelection?: boolean;
53
+ /**
54
+ * Expanded node ids.
55
+ * Used when the item's expansion are controlled.
56
+ */
57
+ expanded?: string[];
58
+ /**
59
+ * This prop is used to help implement the accessibility logic.
60
+ * If you don't provide this prop. It falls back to a randomly generated id.
61
+ */
62
+ id?: string;
63
+ /**
64
+ * Callback fired when tree items are focused.
65
+ * @param {React.SyntheticEvent} event The event source of the callback **Warning**: This is a generic event not a focus event.
66
+ * @param {string} nodeId The id of the node focused.
67
+ * @param {string} value of the focused node.
68
+ */
69
+ onNodeFocus?: (event: React.SyntheticEvent, nodeId: string) => void;
70
+ /**
71
+ * Callback fired when tree items are expanded/collapsed.
72
+ * @param {React.SyntheticEvent} event The event source of the callback.
73
+ * @param {array} nodeIds The ids of the expanded nodes.
74
+ */
75
+ onNodeToggle?: (event: React.SyntheticEvent, nodeIds: string[]) => void;
76
+ /**
77
+ * The system prop that allows defining system overrides as well as additional CSS styles.
78
+ */
79
+ sx?: SxProps<Theme>;
80
+ }
81
+ export interface MultiSelectTreeViewProps extends TreeViewPropsBase {
82
+ /**
83
+ * Selected node ids. (Uncontrolled)
84
+ * When `multiSelect` is true this takes an array of strings; when false (default) a string.
85
+ * @default []
86
+ */
87
+ defaultSelected?: string[];
88
+ /**
89
+ * Selected node ids. (Controlled)
90
+ * When `multiSelect` is true this takes an array of strings; when false (default) a string.
91
+ */
92
+ selected?: string[];
93
+ /**
94
+ * If true `ctrl` and `shift` will trigger multiselect.
95
+ * @default false
96
+ */
97
+ multiSelect?: true;
98
+ /**
99
+ * Callback fired when tree items are selected/unselected.
100
+ * @param {React.SyntheticEvent} event The event source of the callback
101
+ * @param {string[] | string} nodeIds Ids of the selected nodes. When `multiSelect` is true
102
+ * this is an array of strings; when false (default) a string.
103
+ */
104
+ onNodeSelect?: (event: React.SyntheticEvent, nodeIds: string[]) => void;
105
+ }
106
+ export interface SingleSelectTreeViewProps extends TreeViewPropsBase {
107
+ /**
108
+ * Selected node ids. (Uncontrolled)
109
+ * When `multiSelect` is true this takes an array of strings; when false (default) a string.
110
+ * @default []
111
+ */
112
+ defaultSelected?: string | null;
113
+ /**
114
+ * Selected node ids. (Controlled)
115
+ * When `multiSelect` is true this takes an array of strings; when false (default) a string.
116
+ */
117
+ selected?: string | null;
118
+ /**
119
+ * If true `ctrl` and `shift` will trigger multiselect.
120
+ * @default false
121
+ */
122
+ multiSelect?: false;
123
+ /**
124
+ * Callback fired when tree items are selected/unselected.
125
+ * @param {React.SyntheticEvent} event The event source of the callback
126
+ * @param {string[] | string} nodeIds Ids of the selected nodes. When `multiSelect` is true
127
+ * this is an array of strings; when false (default) a string.
128
+ */
129
+ onNodeSelect?: (event: React.SyntheticEvent, nodeIds: string) => void;
130
+ }
131
+ export type TreeViewProps = SingleSelectTreeViewProps | MultiSelectTreeViewProps;
132
+ export type TreeViewDefaultizedProps = DefaultizedProps<TreeViewProps, 'defaultExpanded' | 'defaultSelected' | 'disabledItemsFocusable' | 'disableSelection' | 'multiSelect'>;
133
+ export interface TreeViewNode {
134
+ id: string;
135
+ idAttribute: string | undefined;
136
+ index: number;
137
+ parentId: string | null;
138
+ expandable: boolean;
139
+ disabled: boolean | undefined;
140
+ }
141
+ export interface TreeViewItemRange {
142
+ start?: string | null;
143
+ end?: string | null;
144
+ next?: string | null;
145
+ current?: string;
146
+ }
147
+ export interface TreeViewContextValue {
148
+ registerNode: (node: TreeViewNode) => void;
149
+ unregisterNode: (nodeId: string) => void;
150
+ isFocused: (nodeId: string) => boolean;
151
+ isSelected: (nodeId: string) => boolean;
152
+ isExpanded: (nodeId: string) => boolean;
153
+ isExpandable: (nodeId: string) => boolean;
154
+ isDisabled: (nodeId: string) => boolean;
155
+ mapFirstChar: (nodeId: string, firstChar: string) => void;
156
+ unMapFirstChar: (nodeId: string) => void;
157
+ focus: (event: React.SyntheticEvent, nodeId: string) => void;
158
+ toggleExpansion: (event: React.SyntheticEvent, value?: string) => void;
159
+ selectNode: (event: React.SyntheticEvent, nodeId: string, multiple?: boolean) => void;
160
+ selectRange: (event: React.SyntheticEvent, nodes: TreeViewItemRange, stacked?: boolean) => void;
161
+ multiSelect: boolean;
162
+ disabledItemsFocusable: boolean;
163
+ treeId: string | undefined;
164
+ icons: {
165
+ defaultCollapseIcon: React.ReactNode;
166
+ defaultExpandIcon: React.ReactNode;
167
+ defaultParentIcon: React.ReactNode;
168
+ defaultEndIcon: React.ReactNode;
169
+ };
170
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import * as React from 'react';
2
+ import { TreeViewContextValue } from './TreeView.types';
3
+ /**
4
+ * @ignore - internal component.
5
+ */
6
+ export declare const TreeViewContext: React.Context<TreeViewContextValue>;
@@ -0,0 +1,31 @@
1
+ import * as React from 'react';
2
+ /**
3
+ * @ignore - internal component.
4
+ */
5
+ export const TreeViewContext = /*#__PURE__*/React.createContext({
6
+ registerNode: () => {},
7
+ unregisterNode: () => {},
8
+ isFocused: () => false,
9
+ isSelected: () => false,
10
+ isExpanded: () => false,
11
+ isExpandable: () => false,
12
+ isDisabled: () => false,
13
+ mapFirstChar: () => {},
14
+ unMapFirstChar: () => {},
15
+ focus: () => {},
16
+ toggleExpansion: () => {},
17
+ selectNode: () => {},
18
+ selectRange: () => {},
19
+ multiSelect: false,
20
+ disabledItemsFocusable: false,
21
+ treeId: undefined,
22
+ icons: {
23
+ defaultCollapseIcon: null,
24
+ defaultExpandIcon: null,
25
+ defaultParentIcon: null,
26
+ defaultEndIcon: null
27
+ }
28
+ });
29
+ if (process.env.NODE_ENV !== 'production') {
30
+ TreeViewContext.displayName = 'TreeViewContext';
31
+ }
@@ -0,0 +1,42 @@
1
+ import * as React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ /**
4
+ * This hook registers our descendant by passing it into an array. We can then
5
+ * search that array by to find its index when registering it in the component.
6
+ * We use this for focus management, keyboard navigation, and typeahead
7
+ * functionality for some components.
8
+ *
9
+ * The hook accepts the element node
10
+ *
11
+ * Our main goals with this are:
12
+ * 1) maximum composability,
13
+ * 2) minimal API friction
14
+ * 3) SSR compatibility*
15
+ * 4) concurrent safe
16
+ * 5) index always up-to-date with the tree despite changes
17
+ * 6) works with memoization of any component in the tree (hopefully)
18
+ *
19
+ * * As for SSR, the good news is that we don't actually need the index on the
20
+ * server for most use-cases, as we are only using it to determine the order of
21
+ * composed descendants for keyboard navigation.
22
+ */
23
+ export declare function useDescendant(descendant: TreeItemDescendant): {
24
+ parentId: string | null;
25
+ index: number;
26
+ };
27
+ interface DescendantProviderProps {
28
+ id?: string;
29
+ children: React.ReactNode;
30
+ }
31
+ export declare function DescendantProvider(props: DescendantProviderProps): React.JSX.Element;
32
+ export declare namespace DescendantProvider {
33
+ var propTypes: {
34
+ children: PropTypes.Requireable<PropTypes.ReactNodeLike>;
35
+ id: PropTypes.Requireable<string>;
36
+ };
37
+ }
38
+ export interface TreeItemDescendant {
39
+ element: HTMLLIElement;
40
+ id: string;
41
+ }
42
+ export {};
@@ -0,0 +1,186 @@
1
+ import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
2
+ import _extends from "@babel/runtime/helpers/esm/extends";
3
+ const _excluded = ["element"];
4
+ import * as React from 'react';
5
+ import PropTypes from 'prop-types';
6
+ import useEnhancedEffect from '@mui/utils/useEnhancedEffect';
7
+
8
+ /** Credit: https://github.com/reach/reach-ui/blob/86a046f54d53b6420e392b3fa56dd991d9d4e458/packages/descendants/README.md
9
+ * Modified slightly to suit our purposes.
10
+ */
11
+
12
+ // To replace with .findIndex() once we stop IE11 support.
13
+ import { jsx as _jsx } from "react/jsx-runtime";
14
+ function findIndex(array, comp) {
15
+ for (let i = 0; i < array.length; i += 1) {
16
+ if (comp(array[i])) {
17
+ return i;
18
+ }
19
+ }
20
+ return -1;
21
+ }
22
+ function binaryFindElement(array, element) {
23
+ let start = 0;
24
+ let end = array.length - 1;
25
+ while (start <= end) {
26
+ const middle = Math.floor((start + end) / 2);
27
+ if (array[middle].element === element) {
28
+ return middle;
29
+ }
30
+
31
+ // eslint-disable-next-line no-bitwise
32
+ if (array[middle].element.compareDocumentPosition(element) & Node.DOCUMENT_POSITION_PRECEDING) {
33
+ end = middle - 1;
34
+ } else {
35
+ start = middle + 1;
36
+ }
37
+ }
38
+ return start;
39
+ }
40
+ const DescendantContext = /*#__PURE__*/React.createContext({});
41
+ if (process.env.NODE_ENV !== 'production') {
42
+ DescendantContext.displayName = 'DescendantContext';
43
+ }
44
+ function usePrevious(value) {
45
+ const ref = React.useRef(null);
46
+ React.useEffect(() => {
47
+ ref.current = value;
48
+ }, [value]);
49
+ return ref.current;
50
+ }
51
+ const noop = () => {};
52
+
53
+ /**
54
+ * This hook registers our descendant by passing it into an array. We can then
55
+ * search that array by to find its index when registering it in the component.
56
+ * We use this for focus management, keyboard navigation, and typeahead
57
+ * functionality for some components.
58
+ *
59
+ * The hook accepts the element node
60
+ *
61
+ * Our main goals with this are:
62
+ * 1) maximum composability,
63
+ * 2) minimal API friction
64
+ * 3) SSR compatibility*
65
+ * 4) concurrent safe
66
+ * 5) index always up-to-date with the tree despite changes
67
+ * 6) works with memoization of any component in the tree (hopefully)
68
+ *
69
+ * * As for SSR, the good news is that we don't actually need the index on the
70
+ * server for most use-cases, as we are only using it to determine the order of
71
+ * composed descendants for keyboard navigation.
72
+ */
73
+ export function useDescendant(descendant) {
74
+ const [, forceUpdate] = React.useState();
75
+ const {
76
+ registerDescendant = noop,
77
+ unregisterDescendant = noop,
78
+ descendants = [],
79
+ parentId = null
80
+ } = React.useContext(DescendantContext);
81
+
82
+ // This will initially return -1 because we haven't registered the descendant
83
+ // on the first render. After we register, this will then return the correct
84
+ // index on the following render, and we will re-register descendants
85
+ // so that everything is up-to-date before the user interacts with a
86
+ // collection.
87
+ const index = findIndex(descendants, item => item.element === descendant.element);
88
+ const previousDescendants = usePrevious(descendants);
89
+
90
+ // We also need to re-register descendants any time ANY of the other
91
+ // descendants have changed. My brain was melting when I wrote this and it
92
+ // feels a little off, but checking in render and using the result in the
93
+ // effect's dependency array works well enough.
94
+ const someDescendantsHaveChanged = descendants.some((newDescendant, position) => {
95
+ return previousDescendants && previousDescendants[position] && previousDescendants[position].element !== newDescendant.element;
96
+ });
97
+
98
+ // Prevent any flashing
99
+ useEnhancedEffect(() => {
100
+ if (descendant.element) {
101
+ registerDescendant(_extends({}, descendant, {
102
+ index
103
+ }));
104
+ return () => {
105
+ unregisterDescendant(descendant.element);
106
+ };
107
+ }
108
+ forceUpdate({});
109
+ return undefined;
110
+ }, [registerDescendant, unregisterDescendant, index, someDescendantsHaveChanged, descendant]);
111
+ return {
112
+ parentId,
113
+ index
114
+ };
115
+ }
116
+ export function DescendantProvider(props) {
117
+ const {
118
+ children,
119
+ id
120
+ } = props;
121
+ const [items, set] = React.useState([]);
122
+ const registerDescendant = React.useCallback(_ref => {
123
+ let {
124
+ element
125
+ } = _ref,
126
+ other = _objectWithoutPropertiesLoose(_ref, _excluded);
127
+ set(oldItems => {
128
+ if (oldItems.length === 0) {
129
+ // If there are no items, register at index 0 and bail.
130
+ return [_extends({}, other, {
131
+ element,
132
+ index: 0
133
+ })];
134
+ }
135
+ const index = binaryFindElement(oldItems, element);
136
+ let newItems;
137
+ if (oldItems[index] && oldItems[index].element === element) {
138
+ // If the element is already registered, just use the same array
139
+ newItems = oldItems;
140
+ } else {
141
+ // When registering a descendant, we need to make sure we insert in
142
+ // into the array in the same order that it appears in the DOM. So as
143
+ // new descendants are added or maybe some are removed, we always know
144
+ // that the array is up-to-date and correct.
145
+ //
146
+ // So here we look at our registered descendants and see if the new
147
+ // element we are adding appears earlier than an existing descendant's
148
+ // DOM node via `node.compareDocumentPosition`. If it does, we insert
149
+ // the new element at this index. Because `registerDescendant` will be
150
+ // called in an effect every time the descendants state value changes,
151
+ // we should be sure that this index is accurate when descendent
152
+ // elements come or go from our component.
153
+
154
+ const newItem = _extends({}, other, {
155
+ element,
156
+ index
157
+ });
158
+
159
+ // If an index is not found we will push the element to the end.
160
+ newItems = oldItems.slice();
161
+ newItems.splice(index, 0, newItem);
162
+ }
163
+ newItems.forEach((item, position) => {
164
+ item.index = position;
165
+ });
166
+ return newItems;
167
+ });
168
+ }, []);
169
+ const unregisterDescendant = React.useCallback(element => {
170
+ set(oldItems => oldItems.filter(item => element !== item.element));
171
+ }, []);
172
+ const value = React.useMemo(() => ({
173
+ descendants: items,
174
+ registerDescendant,
175
+ unregisterDescendant,
176
+ parentId: id
177
+ }), [items, registerDescendant, unregisterDescendant, id]);
178
+ return /*#__PURE__*/_jsx(DescendantContext.Provider, {
179
+ value: value,
180
+ children: children
181
+ });
182
+ }
183
+ process.env.NODE_ENV !== "production" ? DescendantProvider.propTypes = {
184
+ children: PropTypes.node,
185
+ id: PropTypes.string
186
+ } : void 0;
@@ -0,0 +1,3 @@
1
+ export * from './TreeView';
2
+ export * from './treeViewClasses';
3
+ export type { TreeViewProps, SingleSelectTreeViewProps, MultiSelectTreeViewProps, TreeViewPropsBase, } from './TreeView.types';
@@ -0,0 +1,3 @@
1
+ export * from './TreeView';
2
+ export * from './treeViewClasses';
3
+ export {};
@@ -0,0 +1,6 @@
1
+ {
2
+ "sideEffects": false,
3
+ "module": "./index.js",
4
+ "main": "../node/TreeView/index.js",
5
+ "types": "./index.d.ts"
6
+ }
@@ -0,0 +1,7 @@
1
+ export interface TreeViewClasses {
2
+ /** Styles applied to the root element. */
3
+ root: string;
4
+ }
5
+ export type TreeViewClassKey = keyof TreeViewClasses;
6
+ export declare function getTreeViewUtilityClass(slot: string): string;
7
+ export declare const treeViewClasses: TreeViewClasses;
@@ -0,0 +1,6 @@
1
+ import generateUtilityClass from '@mui/utils/generateUtilityClass';
2
+ import generateUtilityClasses from '@mui/utils/generateUtilityClasses';
3
+ export function getTreeViewUtilityClass(slot) {
4
+ return generateUtilityClass('MuiTreeView', slot);
5
+ }
6
+ export const treeViewClasses = generateUtilityClasses('MuiTreeView', ['root']);
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './TreeItem';
2
+ export * from './TreeView';
package/index.js ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @mui/x-tree-view v6.0.0-alpha.0
3
+ *
4
+ * @license MIT
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ export * from './TreeItem';
9
+ export * from './TreeView';
@@ -0,0 +1 @@
1
+ export type DefaultizedProps<P extends {}, RequiredProps extends keyof P, AdditionalProps extends {} = {}> = Omit<P, RequiredProps | keyof AdditionalProps> & Required<Pick<P, RequiredProps>> & AdditionalProps;
@@ -0,0 +1 @@
1
+ export {};