Skip to content
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] New instance and publicAPI method: getItem #12251

Merged
merged 4 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ export interface UseTreeViewFocusInstance {
focusDefaultNode: (event: React.SyntheticEvent) => void;
focusRoot: () => void;
}
export interface UseTreeViewFocusPublicAPI {
focusNode: (event: React.SyntheticEvent, nodeId: string | null) => void;
}

export interface UseTreeViewFocusPublicAPI extends Pick<UseTreeViewFocusInstance, 'focusNode'> {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ensure the public API is always a subset of the private one


export interface UseTreeViewFocusParameters {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import * as React from 'react';
import useEventCallback from '@mui/utils/useEventCallback';
import { TreeViewPlugin } from '../../models';
import { populateInstance } from '../../useTreeView/useTreeView.utils';
import { populateInstance, populatePublicAPI } from '../../useTreeView/useTreeView.utils';
import {
UseTreeViewNodesSignature,
UseTreeViewNodesDefaultizedParameters,
TreeViewNodeMap,
TreeViewNodeIdAndChildren,
UseTreeViewNodesState,
TreeViewItemMap,
} from './useTreeViewNodes.types';
import { publishTreeViewEvent } from '../../utils/publishTreeViewEvent';
import { TreeViewBaseItem } from '../../../models';

const updateState = ({
const updateNodesState = ({
items,
isItemDisabled,
getItemLabel,
getItemId,
}: Pick<
UseTreeViewNodesDefaultizedParameters<TreeViewBaseItem>,
'items' | 'isItemDisabled' | 'getItemLabel' | 'getItemId'
>): UseTreeViewNodesState => {
>): UseTreeViewNodesState<any>['nodes'] => {
const nodeMap: TreeViewNodeMap = {};
const itemMap: TreeViewItemMap<any> = {};

const processItem = (
item: TreeViewBaseItem,
Expand Down Expand Up @@ -73,6 +75,8 @@ const updateState = ({
disabled: isItemDisabled ? isItemDisabled(item) : false,
};

itemMap[id] = item;

return {
id,
children: item.children?.map((child, childIndex) => processItem(child, childIndex, id)),
Expand All @@ -84,6 +88,7 @@ const updateState = ({
return {
nodeMap,
nodeTree,
itemMap,
};
};

Expand All @@ -93,7 +98,15 @@ export const useTreeViewNodes: TreeViewPlugin<UseTreeViewNodesSignature> = ({
state,
setState,
}) => {
const getNode = React.useCallback((nodeId: string) => state.nodeMap[nodeId], [state.nodeMap]);
const getNode = React.useCallback(
(nodeId: string) => state.nodes.nodeMap[nodeId],
[state.nodes.nodeMap],
);

const getItem = React.useCallback(
(nodeId: string) => state.nodes.itemMap[nodeId],
[state.nodes.itemMap],
);

const isNodeDisabled = React.useCallback(
(nodeId: string | null): nodeId is string => {
Expand Down Expand Up @@ -125,7 +138,7 @@ export const useTreeViewNodes: TreeViewPlugin<UseTreeViewNodesSignature> = ({
);

const getChildrenIds = useEventCallback((nodeId: string | null) =>
Object.values(state.nodeMap)
Object.values(state.nodes.nodeMap)
.filter((node) => node.parentId === nodeId)
.sort((a, b) => a.index - b.index)
.map((child) => child.id),
Expand All @@ -142,14 +155,14 @@ export const useTreeViewNodes: TreeViewPlugin<UseTreeViewNodesSignature> = ({

React.useEffect(() => {
setState((prevState) => {
const newState = updateState({
const newState = updateNodesState({
items: params.items,
isItemDisabled: params.isItemDisabled,
getItemId: params.getItemId,
getItemLabel: params.getItemLabel,
});

Object.values(prevState.nodeMap).forEach((node) => {
Object.values(prevState.nodes.nodeMap).forEach((node) => {
if (!newState.nodeMap[node.id]) {
publishTreeViewEvent(instance, 'removeNode', { id: node.id });
}
Expand All @@ -171,7 +184,7 @@ export const useTreeViewNodes: TreeViewPlugin<UseTreeViewNodesSignature> = ({
id,
children,
}: TreeViewNodeIdAndChildren): ReturnType<typeof instance.getNodesToRender>[number] => {
const node = state.nodeMap[id];
const node = state.nodes.nodeMap[id];
return {
label: node.label!,
nodeId: node.id,
Expand All @@ -180,29 +193,35 @@ export const useTreeViewNodes: TreeViewPlugin<UseTreeViewNodesSignature> = ({
};
};

return state.nodeTree.map(getPropsFromNodeId);
return state.nodes.nodeTree.map(getPropsFromNodeId);
});

populateInstance<UseTreeViewNodesSignature>(instance, {
getNode,
getItem,
getNodesToRender,
getChildrenIds,
getNavigableChildrenIds,
isNodeDisabled,
});

populatePublicAPI<UseTreeViewNodesSignature>(instance, {
getItem,
});

return {
contextValue: { disabledItemsFocusable: params.disabledItemsFocusable },
};
};

useTreeViewNodes.getInitialState = (params) =>
updateState({
useTreeViewNodes.getInitialState = (params) => ({
nodes: updateNodesState({
items: params.items,
isItemDisabled: params.isItemDisabled,
getItemId: params.getItemId,
getItemLabel: params.getItemLabel,
});
}),
});

useTreeViewNodes.getDefaultizedParams = (params) => ({
...params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ interface TreeViewNodeProps {
children?: TreeViewNodeProps[];
}

export interface UseTreeViewNodesInstance {
export interface UseTreeViewNodesInstance<R extends {}> {
getNode: (nodeId: string) => TreeViewNode;
getItem: (nodeId: string) => R;
getNodesToRender: () => TreeViewNodeProps[];
getChildrenIds: (nodeId: string | null) => string[];
getNavigableChildrenIds: (nodeId: string | null) => string[];
isNodeDisabled: (nodeId: string | null) => nodeId is string;
}

export interface UseTreeViewNodesPublicAPI<R extends {}>
extends Pick<UseTreeViewNodesInstance<R>, 'getItem'> {}

export interface UseTreeViewNodesParameters<R extends {}> {
/**
* If `true`, will allow focus on disabled items.
Expand Down Expand Up @@ -66,9 +70,12 @@ export interface TreeViewNodeIdAndChildren {
children?: TreeViewNodeIdAndChildren[];
}

export interface UseTreeViewNodesState {
nodeTree: TreeViewNodeIdAndChildren[];
nodeMap: TreeViewNodeMap;
export interface UseTreeViewNodesState<R extends {}> {
nodes: {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I scoped the node state because having 3 elements at the root was starting to be messy

nodeTree: TreeViewNodeIdAndChildren[];
nodeMap: TreeViewNodeMap;
itemMap: TreeViewItemMap<R>;
};
}

interface UseTreeViewNodesContextValue
Expand All @@ -77,10 +84,12 @@ interface UseTreeViewNodesContextValue
export type UseTreeViewNodesSignature = TreeViewPluginSignature<{
params: UseTreeViewNodesParameters<any>;
defaultizedParams: UseTreeViewNodesDefaultizedParameters<any>;
instance: UseTreeViewNodesInstance;
instance: UseTreeViewNodesInstance<any>;
events: UseTreeViewNodesEventLookup;
state: UseTreeViewNodesState;
state: UseTreeViewNodesState<any>;
contextValue: UseTreeViewNodesContextValue;
}>;

export type TreeViewNodeMap = { [nodeId: string]: TreeViewNode };

export type TreeViewItemMap<R extends {}> = { [nodeId: string]: R };
Loading