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 1 commit
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 @@ -13,6 +13,8 @@ import {
UseTreeViewKeyboardNavigationSignature,
} from './useTreeViewKeyboardNavigation.types';
import { MuiCancellableEvent } from '../../models/MuiCancellableEvent';
import { hasPlugin } from '../../utils/plugins';
import { useTreeViewFocus } from '../useTreeViewFocus';

function isPrintableCharacter(string: string) {
return !!string && string.length === 1 && !!string.match(/\S/);
Expand Down Expand Up @@ -77,9 +79,8 @@ export const useTreeViewKeyboardNavigation: TreeViewPlugin<
const canToggleItemSelection = (itemId: string) =>
!params.disableSelection && !instance.isItemDisabled(itemId);

const canToggleItemExpansion = (itemId: string) => {
return !instance.isItemDisabled(itemId) && instance.isItemExpandable(itemId);
};
const canToggleItemExpansion = (itemId: string) =>
!instance.isItemDisabled(itemId) && instance.isItemExpandable(itemId);

// ARIA specification: https://www.w3.org/WAI/ARIA/apg/patterns/treeview/#keyboardinteraction
const handleItemKeyDown = (
Expand Down Expand Up @@ -142,7 +143,9 @@ export const useTreeViewKeyboardNavigation: TreeViewPlugin<
const nextItem = getNextNavigableItem(instance, itemId);
if (nextItem) {
event.preventDefault();
instance.focusItem(event, nextItem);
if (hasPlugin(instance, useTreeViewFocus)) {
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 used this plugin as an example but I will remove before merging 👌

instance.focusItem(event, nextItem);
}

// Multi select behavior when pressing Shift + ArrowDown
// Toggles the selection state of the next item
Expand All @@ -159,7 +162,9 @@ export const useTreeViewKeyboardNavigation: TreeViewPlugin<
const previousItem = getPreviousNavigableItem(instance, itemId);
if (previousItem) {
event.preventDefault();
instance.focusItem(event, previousItem);
if (hasPlugin(instance, useTreeViewFocus)) {
instance.focusItem(event, previousItem);
}

// Multi select behavior when pressing Shift + ArrowUp
// Toggles the selection state of the previous item
Expand All @@ -177,7 +182,9 @@ export const useTreeViewKeyboardNavigation: TreeViewPlugin<
if (instance.isItemExpanded(itemId)) {
const nextItemId = getNextNavigableItem(instance, itemId);
if (nextItemId) {
instance.focusItem(event, nextItemId);
if (hasPlugin(instance, useTreeViewFocus)) {
instance.focusItem(event, nextItemId);
}
event.preventDefault();
}
} else if (canToggleItemExpansion(itemId)) {
Expand All @@ -197,7 +204,9 @@ export const useTreeViewKeyboardNavigation: TreeViewPlugin<
} else {
const parent = instance.getItemMeta(itemId).parentId;
if (parent) {
instance.focusItem(event, parent);
if (hasPlugin(instance, useTreeViewFocus)) {
instance.focusItem(event, parent);
}
event.preventDefault();
}
}
Expand All @@ -211,7 +220,7 @@ export const useTreeViewKeyboardNavigation: TreeViewPlugin<
// Selects the focused item and all items up to the first item.
if (canToggleItemSelection(itemId) && params.multiSelect && ctrlPressed && event.shiftKey) {
instance.selectRangeFromStartToItem(event, itemId);
} else {
} else if (hasPlugin(instance, useTreeViewFocus)) {
instance.focusItem(event, getFirstNavigableItem(instance));
}

Expand All @@ -225,7 +234,7 @@ export const useTreeViewKeyboardNavigation: TreeViewPlugin<
// Selects the focused item and all the items down to the last item.
if (canToggleItemSelection(itemId) && params.multiSelect && ctrlPressed && event.shiftKey) {
instance.selectRangeFromItemToEnd(event, itemId);
} else {
} else if (hasPlugin(instance, useTreeViewFocus)) {
instance.focusItem(event, getLastNavigableItem(instance));
}

Expand All @@ -252,7 +261,7 @@ export const useTreeViewKeyboardNavigation: TreeViewPlugin<
// TODO: Support typing multiple characters
case !ctrlPressed && !event.shiftKey && isPrintableCharacter(key): {
const matchingItem = getFirstMatchingItem(itemId, key);
if (matchingItem != null) {
if (matchingItem != null && hasPlugin(instance, useTreeViewFocus)) {
instance.focusItem(event, matchingItem);
event.preventDefault();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export type UseTreeViewKeyboardNavigationSignature = TreeViewPluginSignature<{
dependencies: [
UseTreeViewItemsSignature,
UseTreeViewSelectionSignature,
UseTreeViewFocusSignature,
UseTreeViewExpansionSignature,
];
optionalDependencies: [UseTreeViewFocusSignature];
}>;

export type TreeViewFirstCharMap = { [itemId: string]: string };
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);
};
Loading