Skip to content

Commit

Permalink
export publicAPI form useTreeItem2Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
noraleonte committed Sep 25, 2024
1 parent 407f3ca commit 5ba561b
Showing 1 changed file with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand All @@ -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<TSignatures, TOptionalSignatures>;
}

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<TSignatures, TOptionalSignatures> => {
const {
instance,
selection: { multiSelect },
} = useTreeViewContext<UseTreeItem2UtilsMinimalPlugins, UseTreeItem2UtilsOptionalPlugins>();
publicAPI,
} = useTreeViewContext<TSignatures, TOptionalSignatures>();

const status: UseTreeItem2Status = {
expandable: isItemExpandable(children),
Expand Down Expand Up @@ -176,5 +188,5 @@ export const useTreeItem2Utils = ({
handleCancelItemLabelEditing,
};

return { interactions, status };
return { interactions, status, publicAPI };
};

0 comments on commit 5ba561b

Please sign in to comment.