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] Add utility function to check if an optional plugin is present #13788

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -1,12 +1,17 @@
import { useTreeViewInstanceEvents } from './useTreeViewInstanceEvents';
import { useTreeViewOptionalPlugins } from './useTreeViewOptionalPlugins';
import { useTreeViewId, UseTreeViewIdParameters } from './useTreeViewId';
import { ConvertPluginsIntoSignatures } from '../models';

/**
* Internal plugins that create the tools used by the other plugins.
* These plugins are used by the tree view components.
*/
export const TREE_VIEW_CORE_PLUGINS = [useTreeViewInstanceEvents, useTreeViewId] as const;
export const TREE_VIEW_CORE_PLUGINS = [
useTreeViewInstanceEvents,
useTreeViewOptionalPlugins,
useTreeViewId,
] as const;

export type TreeViewCorePluginSignatures = ConvertPluginsIntoSignatures<
typeof TREE_VIEW_CORE_PLUGINS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { useTreeViewOptionalPlugins } from './useTreeViewOptionalPlugins';
export type { UseTreeViewOptionalPluginsSignature } from './useTreeViewOptionalPlugins.types';
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { TreeViewPlugin } from '../../models';
import { UseTreeViewOptionalPluginsSignature } from './useTreeViewOptionalPlugins.types';

export const useTreeViewOptionalPlugins: TreeViewPlugin<UseTreeViewOptionalPluginsSignature> = ({
plugins,
}) => {
const pluginSet = new Set(plugins);
const getAvailablePlugins = () => pluginSet;

return {
instance: {
getAvailablePlugins,
},
};
};

useTreeViewOptionalPlugins.params = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { TreeViewPlugin, TreeViewAnyPluginSignature, TreeViewPluginSignature } from '../../models';

interface UseTreeViewOptionalPluginsInstance {
getAvailablePlugins: () => Set<TreeViewPlugin<TreeViewAnyPluginSignature>>;
}

export type UseTreeViewOptionalPluginsSignature = TreeViewPluginSignature<{
instance: UseTreeViewOptionalPluginsInstance;
}>;
1 change: 1 addition & 0 deletions packages/x-tree-view/src/internals/models/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface TreeViewPluginOptions<TSignature extends TreeViewAnyPluginSigna
models: TreeViewUsedModels<TSignature>;
setState: React.Dispatch<React.SetStateAction<TreeViewUsedState<TSignature>>>;
rootRef: React.RefObject<HTMLUListElement>;
plugins: TreeViewPlugin<TreeViewAnyPluginSignature>[];
}

type TreeViewModelsInitializer<TSignature extends TreeViewAnyPluginSignature> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export const useTreeView = <
setState,
rootRef: innerRootRef,
models,
plugins,
});

if (pluginResponse.getRootProps) {
Expand Down
12 changes: 12 additions & 0 deletions packages/x-tree-view/src/internals/utils/plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TreeViewAnyPluginSignature, TreeViewInstance, TreeViewPlugin } from '../models';

export const hasPlugin = <
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 can't have this inside useTreeViewOptionalPlugins because instance needs to be a param in order to infer its shape.

Otherwise we would need to do instance.hasPlugin(instance, plugin) which is worse IMHO.

TSignature extends TreeViewAnyPluginSignature,
TInstance extends TreeViewInstance<[], [TSignature]>,
>(
instance: TInstance,
plugin: TreeViewPlugin<TSignature>,
): instance is Omit<TInstance, keyof TSignature['instance']> & TSignature['instance'] => {
const plugins = instance.getAvailablePlugins();
return plugins.has(plugin as any);
};