From 5ba561b51560d0774dc63b246392e56eed12e60d Mon Sep 17 00:00:00 2001 From: noraleonte Date: Wed, 25 Sep 2024 15:16:13 +0300 Subject: [PATCH] export publicAPI form useTreeItem2Utils --- .../useTreeItem2Utils/useTreeItem2Utils.tsx | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/packages/x-tree-view/src/hooks/useTreeItem2Utils/useTreeItem2Utils.tsx b/packages/x-tree-view/src/hooks/useTreeItem2Utils/useTreeItem2Utils.tsx index 245633ecf95eb..cac94733ea711 100644 --- a/packages/x-tree-view/src/hooks/useTreeItem2Utils/useTreeItem2Utils.tsx +++ b/packages/x-tree-view/src/hooks/useTreeItem2Utils/useTreeItem2Utils.tsx @@ -11,6 +11,7 @@ import { } from '../../internals/plugins/useTreeViewLabel'; import type { UseTreeItem2Status } from '../../useTreeItem2'; import { hasPlugin } from '../../internals/utils/plugins'; +import { TreeViewPublicAPI } from '../../internals/models'; export interface UseTreeItem2Interactions { handleExpansion: (event: React.MouseEvent) => void; @@ -21,18 +22,6 @@ export interface UseTreeItem2Interactions { handleCancelItemLabelEditing: (event: React.SyntheticEvent) => void; } -interface UseTreeItem2UtilsReturnValue { - interactions: UseTreeItem2Interactions; - status: UseTreeItem2Status; -} - -const isItemExpandable = (reactChildren: React.ReactNode) => { - if (Array.isArray(reactChildren)) { - return reactChildren.length > 0 && reactChildren.some(isItemExpandable); - } - return Boolean(reactChildren); -}; - /** * Plugins that need to be present in the Tree View in order for `useTreeItem2Utils` to work correctly. */ @@ -49,17 +38,40 @@ type UseTreeItem2UtilsMinimalPlugins = readonly [ export type UseTreeItem2UtilsOptionalPlugins = readonly [UseTreeViewLabelSignature]; -export const useTreeItem2Utils = ({ +interface UseTreeItem2UtilsReturnValue< + TSignatures extends UseTreeItem2UtilsMinimalPlugins, + TOptionalSignatures extends UseTreeItem2UtilsOptionalPlugins, +> { + interactions: UseTreeItem2Interactions; + status: UseTreeItem2Status; + /** + * The object the allows Tree View manipulation. + */ + publicAPI: TreeViewPublicAPI; +} + +const isItemExpandable = (reactChildren: React.ReactNode) => { + if (Array.isArray(reactChildren)) { + return reactChildren.length > 0 && reactChildren.some(isItemExpandable); + } + return Boolean(reactChildren); +}; + +export const useTreeItem2Utils = < + TSignatures extends UseTreeItem2UtilsMinimalPlugins = UseTreeItem2UtilsMinimalPlugins, + TOptionalSignatures extends UseTreeItem2UtilsOptionalPlugins = UseTreeItem2UtilsOptionalPlugins, +>({ itemId, children, }: { itemId: string; children: React.ReactNode; -}): UseTreeItem2UtilsReturnValue => { +}): UseTreeItem2UtilsReturnValue => { const { instance, selection: { multiSelect }, - } = useTreeViewContext(); + publicAPI, + } = useTreeViewContext(); const status: UseTreeItem2Status = { expandable: isItemExpandable(children), @@ -176,5 +188,5 @@ export const useTreeItem2Utils = ({ handleCancelItemLabelEditing, }; - return { interactions, status }; + return { interactions, status, publicAPI }; };