Skip to content

Commit

Permalink
[TreeView] Move all mandatory plugins to the core plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Jan 25, 2024
1 parent 58c5fe8 commit 9e8c681
Show file tree
Hide file tree
Showing 24 changed files with 46 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
TreeViewItemPluginOptions,
TreeViewItemPluginResponse,
} from '../models';
import { TreeViewCorePluginsSignature } from '../corePlugins';

export type TreeViewContextValue<TPlugins extends readonly TreeViewAnyPluginSignature[]> =
MergePluginsProperty<TPlugins, 'contextValue'> & {
instance: TreeViewInstance<TPlugins>;
runItemPlugins: (options: TreeViewItemPluginOptions) => Required<TreeViewItemPluginResponse>;
};
MergePluginsProperty<TPlugins, 'contextValue'> &
TreeViewCorePluginsSignature['contextValue'] & {
instance: TreeViewInstance<TPlugins>;
runItemPlugins: (options: TreeViewItemPluginOptions) => Required<TreeViewItemPluginResponse>;
};

export interface TreeViewProviderProps<TPlugins extends readonly TreeViewAnyPluginSignature[]> {
value: TreeViewContextValue<TPlugins>;
Expand Down
12 changes: 11 additions & 1 deletion packages/x-tree-view/src/internals/corePlugins/corePlugins.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { useTreeViewInstanceEvents } from './useTreeViewInstanceEvents';
import { useTreeViewId, UseTreeViewIdParameters } from './useTreeViewId';
import { useTreeViewNodes, UseTreeViewNodesParameters } from './useTreeViewNodes';
import { ConvertPluginsIntoSignatures, MergePlugins } 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] as const;
export const TREE_VIEW_CORE_PLUGINS = [
useTreeViewInstanceEvents,
useTreeViewId,
useTreeViewNodes,
] as const;

export type TreeViewCorePluginsSignature = MergePlugins<
ConvertPluginsIntoSignatures<typeof TREE_VIEW_CORE_PLUGINS>
>;

export interface TreeViewCorePluginsParameters<R extends {}>
extends UseTreeViewIdParameters,
UseTreeViewNodesParameters<R> {}
2 changes: 1 addition & 1 deletion packages/x-tree-view/src/internals/corePlugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { TREE_VIEW_CORE_PLUGINS } from './corePlugins';
export type { TreeViewCorePluginsSignature } from './corePlugins';
export type { TreeViewCorePluginsSignature, TreeViewCorePluginsParameters } from './corePlugins';
3 changes: 1 addition & 2 deletions packages/x-tree-view/src/internals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ export type {
DefaultTreeViewPluginSlots,
DefaultTreeViewPluginSlotProps,
} from './plugins/defaultPlugins';

export type { DefaultTreeViewPluginParameters } from './plugins/defaultPlugins';
export type { UseTreeViewExpansionSignature } from './plugins/useTreeViewExpansion';
export type { UseTreeViewSelectionSignature } from './plugins/useTreeViewSelection';
export type { UseTreeViewFocusSignature } from './plugins/useTreeViewFocus';
export type { UseTreeViewKeyboardNavigationSignature } from './plugins/useTreeViewKeyboardNavigation';
export type { UseTreeViewIdSignature } from './plugins/useTreeViewId';
export type { UseTreeViewIconsSignature } from './plugins/useTreeViewIcons';
export type { UseTreeViewNodesSignature } from './plugins/useTreeViewNodes';
export type { UseTreeViewJSXNodesSignature } from './plugins/useTreeViewJSXNodes';

export { extractPluginParamsFromProps } from './utils/extractPluginParamsFromProps';
3 changes: 2 additions & 1 deletion packages/x-tree-view/src/internals/models/treeView.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TreeViewAnyPluginSignature } from './plugin';
import type { MergePluginsProperty } from './helpers';
import type { TreeViewCorePluginsSignature } from '../corePlugins';

export interface TreeViewNode {
id: string;
Expand Down Expand Up @@ -28,4 +29,4 @@ export interface TreeViewModel<TValue> {
}

export type TreeViewInstance<TSignatures extends readonly TreeViewAnyPluginSignature[]> =
MergePluginsProperty<TSignatures, 'instance'>;
TreeViewCorePluginsSignature['instance'] & MergePluginsProperty<TSignatures, 'instance'>;
8 changes: 2 additions & 6 deletions packages/x-tree-view/src/internals/plugins/defaultPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { useTreeViewId, UseTreeViewIdParameters } from './useTreeViewId';
import { useTreeViewNodes, UseTreeViewNodesParameters } from './useTreeViewNodes';
import { useTreeViewExpansion, UseTreeViewExpansionParameters } from './useTreeViewExpansion';
import { useTreeViewSelection, UseTreeViewSelectionParameters } from './useTreeViewSelection';
import { useTreeViewFocus, UseTreeViewFocusParameters } from './useTreeViewFocus';
import { useTreeViewKeyboardNavigation } from './useTreeViewKeyboardNavigation';
import { useTreeViewIcons, UseTreeViewIconsParameters } from './useTreeViewIcons';
import { ConvertPluginsIntoSignatures, MergePluginsProperty } from '../models';
import { TreeViewCorePluginsParameters } from '../corePlugins';

export const DEFAULT_TREE_VIEW_PLUGINS = [
useTreeViewId,
useTreeViewNodes,
useTreeViewExpansion,
useTreeViewSelection,
useTreeViewFocus,
Expand All @@ -28,8 +25,7 @@ export type DefaultTreeViewPluginSlotProps = MergePluginsProperty<

// We can't infer this type from the plugin, otherwise we would lose the generics.
export interface DefaultTreeViewPluginParameters<R extends {}, Multiple extends boolean | undefined>
extends UseTreeViewIdParameters,
UseTreeViewNodesParameters<R>,
extends TreeViewCorePluginsParameters<R>,
UseTreeViewExpansionParameters,
UseTreeViewFocusParameters,
UseTreeViewSelectionParameters<Multiple>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import { DefaultizedProps, TreeViewPluginSignature } from '../../models';
import { UseTreeViewNodesSignature } from '../useTreeViewNodes';

export interface UseTreeViewExpansionInstance {
isNodeExpanded: (nodeId: string) => boolean;
Expand Down Expand Up @@ -50,5 +49,4 @@ export type UseTreeViewExpansionSignature = TreeViewPluginSignature<{
defaultizedParams: UseTreeViewExpansionDefaultizedParameters;
instance: UseTreeViewExpansionInstance;
modelNames: 'expandedNodes';
dependantPlugins: [UseTreeViewNodesSignature];
}>;
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as React from 'react';
import { TreeViewPluginSignature } from '../../models';
import { UseTreeViewIdSignature } from '../useTreeViewId/useTreeViewId.types';
import type { UseTreeViewNodesSignature } from '../useTreeViewNodes';
import type { UseTreeViewSelectionSignature } from '../useTreeViewSelection';
import { UseTreeViewExpansionSignature } from '../useTreeViewExpansion';

Expand Down Expand Up @@ -32,10 +30,5 @@ export type UseTreeViewFocusSignature = TreeViewPluginSignature<{
defaultizedParams: UseTreeViewFocusDefaultizedParameters;
instance: UseTreeViewFocusInstance;
state: UseTreeViewFocusState;
dependantPlugins: [
UseTreeViewIdSignature,
UseTreeViewNodesSignature,
UseTreeViewSelectionSignature,
UseTreeViewExpansionSignature,
];
dependantPlugins: [UseTreeViewSelectionSignature, UseTreeViewExpansionSignature];
}>;
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { SlotComponentProps } from '@mui/base/utils';
import { TreeViewPluginSignature } from '../../models';
import { UseTreeViewNodesSignature } from '../useTreeViewNodes';
import { UseTreeViewSelectionSignature } from '../useTreeViewSelection';

export interface UseTreeViewIconsParameters {}
Expand Down Expand Up @@ -43,5 +42,5 @@ export type UseTreeViewIconsSignature = TreeViewPluginSignature<{
contextValue: UseTreeViewIconsContextValue;
slots: UseTreeViewIconsSlots;
slotProps: UseTreeViewIconsSlotProps;
dependantPlugins: [UseTreeViewNodesSignature, UseTreeViewSelectionSignature];
dependantPlugins: [UseTreeViewSelectionSignature];
}>;
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { TreeViewNode, TreeViewPluginSignature } from '../../models';
import { UseTreeViewNodesSignature } from '../useTreeViewNodes';
import { UseTreeViewKeyboardNavigationSignature } from '../useTreeViewKeyboardNavigation';

export interface UseTreeViewNodesInstance {
Expand All @@ -16,5 +15,5 @@ export type UseTreeViewJSXNodesSignature = TreeViewPluginSignature<{
params: UseTreeViewNodesParameters;
defaultizedParams: UseTreeViewNodesDefaultizedParameters;
instance: UseTreeViewNodesInstance;
dependantPlugins: [UseTreeViewNodesSignature, UseTreeViewKeyboardNavigationSignature];
dependantPlugins: [UseTreeViewKeyboardNavigationSignature];
}>;
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { TreeViewPluginSignature } from '../../models';
import { UseTreeViewNodesSignature } from '../useTreeViewNodes';
import { UseTreeViewSelectionSignature } from '../useTreeViewSelection';
import { UseTreeViewFocusSignature } from '../useTreeViewFocus';
import { UseTreeViewExpansionSignature } from '../useTreeViewExpansion';
Expand All @@ -11,7 +10,6 @@ export interface UseTreeViewKeyboardNavigationInstance {
export type UseTreeViewKeyboardNavigationSignature = TreeViewPluginSignature<{
instance: UseTreeViewKeyboardNavigationInstance;
dependantPlugins: [
UseTreeViewNodesSignature,
UseTreeViewSelectionSignature,
UseTreeViewFocusSignature,
UseTreeViewExpansionSignature,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import type { DefaultizedProps, TreeViewItemRange, TreeViewPluginSignature } from '../../models';
import { UseTreeViewNodesSignature } from '../useTreeViewNodes';
import { UseTreeViewExpansionSignature } from '../useTreeViewExpansion';

export interface UseTreeViewSelectionInstance {
Expand Down Expand Up @@ -75,9 +74,5 @@ export type UseTreeViewSelectionSignature = TreeViewPluginSignature<{
instance: UseTreeViewSelectionInstance;
contextValue: UseTreeViewSelectionContextValue;
modelNames: 'selectedNodes';
dependantPlugins: [
UseTreeViewNodesSignature,
UseTreeViewExpansionSignature,
UseTreeViewNodesSignature,
];
dependantPlugins: [UseTreeViewExpansionSignature];
}>;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TreeViewInstance } from '../../models';
import { UseTreeViewNodesSignature } from '../useTreeViewNodes';
import { UseTreeViewNodesSignature } from '../../corePlugins/useTreeViewNodes';

/**
* This is used to determine the start and end of a selection range so
Expand Down
19 changes: 12 additions & 7 deletions packages/x-tree-view/src/internals/useTreeView/useTreeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ import {
} from './useTreeView.types';
import { useTreeViewModels } from './useTreeViewModels';
import { TreeViewContextValue } from '../TreeViewProvider';
import { TREE_VIEW_CORE_PLUGINS } from '../corePlugins';

export const useTreeView = <Plugins extends readonly TreeViewPlugin<TreeViewAnyPluginSignature>[]>(
inParams: UseTreeViewParameters<Plugins>,
): UseTreeViewReturnValue<ConvertPluginsIntoSignatures<Plugins>> => {
const plugins = [...TREE_VIEW_CORE_PLUGINS, ...inParams.plugins];
import { TREE_VIEW_CORE_PLUGINS, TreeViewCorePluginsSignature } from '../corePlugins';

export const useTreeView = <
InPlugins extends readonly TreeViewPlugin<TreeViewAnyPluginSignature>[],
>(
inParams: UseTreeViewParameters<InPlugins>,
): UseTreeViewReturnValue<
[TreeViewCorePluginsSignature, ...ConvertPluginsIntoSignatures<InPlugins>]
> => {
const plugins = [...TREE_VIEW_CORE_PLUGINS, ...inParams.plugins] as const;
type Plugins = typeof plugins;
type Signatures = ConvertPluginsIntoSignatures<typeof plugins>;

const params = plugins.reduce((acc, plugin) => {
Expand Down Expand Up @@ -49,7 +54,7 @@ export const useTreeView = <Plugins extends readonly TreeViewPlugin<TreeViewAnyP
if (plugin.getInitialState) {
Object.assign(
temp,
plugin.getInitialState(params as UseTreeViewDefaultizedParameters<any>),
plugin.getInitialState(params as UseTreeViewDefaultizedParameters<Plugins>),
);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
MergePluginsProperty,
TreeViewInstance,
} from '../models';
import { TreeViewCorePluginsSignature } from '../corePlugins';

export type UseTreeViewParameters<
TPlugins extends readonly TreeViewPlugin<TreeViewAnyPluginSignature>[],
> = UseTreeViewBaseParameters<TPlugins> &
TreeViewCorePluginsSignature['params'] &
MergePluginsProperty<ConvertPluginsIntoSignatures<TPlugins>, 'params'>;

export interface UseTreeViewBaseParameters<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TreeViewAnyPluginSignature, TreeViewInstance, TreeViewUsedInstance } from '../models';
import type { UseTreeViewExpansionSignature } from '../plugins/useTreeViewExpansion';
import type { UseTreeViewNodesSignature } from '../plugins/useTreeViewNodes';
import type { UseTreeViewNodesSignature } from '../corePlugins/useTreeViewNodes';

export const getPreviousNode = (
instance: TreeViewInstance<[UseTreeViewNodesSignature, UseTreeViewExpansionSignature]>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { ConvertPluginsIntoSignatures, MergePluginsProperty, TreeViewPlugin } from '../models';
import { UseTreeViewBaseParameters } from '../useTreeView/useTreeView.types';
import { TreeViewCorePluginsSignature } from '@mui/x-tree-view/internals/corePlugins';

export const extractPluginParamsFromProps = <
TPlugins extends readonly TreeViewPlugin<any>[],
Expand All @@ -16,7 +17,8 @@ export const extractPluginParamsFromProps = <
plugins: TPlugins;
rootRef?: React.Ref<HTMLUListElement>;
}) => {
type PluginParams = MergePluginsProperty<ConvertPluginsIntoSignatures<TPlugins>, 'params'>;
type PluginParams = TreeViewCorePluginsSignature['params'] &
MergePluginsProperty<ConvertPluginsIntoSignatures<TPlugins>, 'params'>;

const paramsLookup = {} as Record<keyof PluginParams, true>;
plugins.forEach((plugin) => {
Expand Down

0 comments on commit 9e8c681

Please sign in to comment.