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

Conversation

flaviendelangle
Copy link
Member

@flaviendelangle flaviendelangle commented Feb 28, 2024

Closes #10957

Not sure why but I can't use the PR package on Codesandbox 🤔
Here is a basic usage of the method if you want to test it:

import * as React from 'react';
import Box from '@mui/material/Box';
import Stack from "@mui/material/Stack";
import Typography from "@mui/material/Typography";
import { TreeViewBaseItem } from '@mui/x-tree-view/models';
import { RichTreeView } from '@mui/x-tree-view/RichTreeView';
import {useTreeViewApiRef} from "@mui/x-tree-view";


const MUI_X_PRODUCTS: TreeViewBaseItem[] = [
    {
        id: 'grid',
        label: 'Data Grid',
        children: [
            { id: 'grid-community', label: '@mui/x-data-grid' },
            { id: 'grid-pro', label: '@mui/x-data-grid-pro' },
            { id: 'grid-premium', label: '@mui/x-data-grid-premium' },
        ],
    },
    {
        id: 'pickers',
        label: 'Date and Time Pickers',
        children: [
            { id: 'pickers-community', label: '@mui/x-date-pickers', },
            { id: 'pickers-pro', label: '@mui/x-date-pickers-pro' },
        ],
    },
];

export default function BasicRichTreeView() {
    const [lastExpandedItem, setLastExpandedItem] = React.useState<string | null>(null)
    const apiRef = useTreeViewApiRef()

    const handleNodeExpansionToggle = React.useCallback((    event: React.SyntheticEvent,
                                                             nodeId: string,
                                                             isExpanded: boolean,) => {
        if (isExpanded) {
            const item = apiRef.current!.getItem(nodeId);
            setLastExpandedItem(item.label)
        }
    }, [apiRef])

    return (
        <Stack spacing={2}>
            <Typography>{lastExpandedItem == null ? 'No item expanded recently' : `Last expanded item: ${lastExpandedItem}`}</Typography>
            <Box sx={{ height: 220, flexGrow: 1, maxWidth: 400 }}>
                <RichTreeView items={MUI_X_PRODUCTS} apiRef={apiRef} onNodeExpansionToggle={handleNodeExpansionToggle} />
            </Box>
        </Stack>
    );
}

@flaviendelangle flaviendelangle changed the title [TreeView] New instance and publicAPI method: getItem [TreeView] New instance and publicAPI method: getItem Feb 28, 2024
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

@mui-bot
Copy link

mui-bot commented Feb 28, 2024

Deploy preview: https://deploy-preview-12251--material-ui-x.netlify.app/

Generated by 🚫 dangerJS against ec368ce

T extends TreeViewAnyPluginSignature,
Api extends TreeViewUsedPublicAPI<T>,
>() => React.useRef(undefined) as React.MutableRefObject<Api>;
TPlugins extends readonly TreeViewAnyPluginSignature[] = DefaultTreeViewPlugins,
Copy link
Member Author

Choose a reason for hiding this comment

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

The typing was weird, it was only taking a single plugin
Now by default the type is the one of the default plugins (which are used by both the rich and the simple tree view)
but you can pass a generic to support more plugins (not useful for now, will become useful with RichTreeView-only plugins if they end up having public methods)

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

@flaviendelangle flaviendelangle self-assigned this Feb 28, 2024
@flaviendelangle flaviendelangle marked this pull request as draft February 28, 2024 19:09
@flaviendelangle flaviendelangle marked this pull request as ready for review February 29, 2024 11:52
@zannager zannager added the component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! label Feb 29, 2024
Copy link
Contributor

@noraleonte noraleonte left a comment

Choose a reason for hiding this comment

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

LGTM 🎉 Thanks for doing this 💯

@flaviendelangle flaviendelangle merged commit c44d71a into mui:next Mar 4, 2024
18 checks passed
@flaviendelangle flaviendelangle deleted the method-getItem branch March 4, 2024 07:52
thomasmoon pushed a commit to thomasmoon/mui-x that referenced this pull request Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[tree view] Allow the ability to access extra data in onNodeSelect / onNodeToggle callbacks
4 participants