Skip to content

Commit

Permalink
[TreeView] Support defaultMuiPrevented on the onFocus prop of the…
Browse files Browse the repository at this point in the history
… root slot (#12813)
  • Loading branch information
flaviendelangle committed Apr 18, 2024
1 parent 5899fbb commit de60920
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { UseTreeViewFocusSignature } from './useTreeViewFocus.types';
import { useInstanceEventHandler } from '../../hooks/useInstanceEventHandler';
import { getActiveElement } from '../../utils/utils';
import { getFirstNavigableItem } from '../../utils/tree';
import { MuiCancellableEvent } from '../../models/MuiCancellableEvent';

const useTabbableItemId = (
instance: TreeViewUsedInstance<UseTreeViewFocusSignature>,
Expand Down Expand Up @@ -128,9 +129,14 @@ export const useTreeViewFocus: TreeViewPlugin<UseTreeViewFocusSignature> = ({
}
});

const createHandleFocus =
(otherHandlers: EventHandlers) => (event: React.FocusEvent<HTMLUListElement>) => {
const createRootHandleFocus =
(otherHandlers: EventHandlers) =>
(event: React.FocusEvent<HTMLUListElement> & MuiCancellableEvent) => {
otherHandlers.onFocus?.(event);
if (event.defaultMuiPrevented) {
return;
}

// if the event bubbled (which is React specific) we don't want to steal focus
if (event.target === event.currentTarget) {
instance.focusDefaultItem(event);
Expand All @@ -144,7 +150,7 @@ export const useTreeViewFocus: TreeViewPlugin<UseTreeViewFocusSignature> = ({

return {
getRootProps: (otherHandlers) => ({
onFocus: createHandleFocus(otherHandlers),
onFocus: createRootHandleFocus(otherHandlers),
'aria-activedescendant': activeDescendant ?? undefined,
}),
publicAPI: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ export const useTreeViewJSXItems: TreeViewPlugin<UseTreeViewJSXItemsSignature> =
};
};

const isItemExpandable = (reactChildren: React.ReactNode) => {
if (Array.isArray(reactChildren)) {
return reactChildren.length > 0 && reactChildren.some(isItemExpandable);
}
return Boolean(reactChildren);
};

const useTreeViewJSXItemsItemPlugin: TreeViewItemPlugin<TreeItemProps | TreeItem2Props> = ({
props,
rootRef,
Expand All @@ -130,15 +137,7 @@ const useTreeViewJSXItemsItemPlugin: TreeViewItemPlugin<TreeItemProps | TreeItem
}
const { registerChild, unregisterChild, parentId } = parentContext;

const isExpandable = (reactChildren: React.ReactNode) => {
if (Array.isArray(reactChildren)) {
return reactChildren.length > 0 && reactChildren.some(isExpandable);
}
return Boolean(reactChildren);
};

const expandable = isExpandable(children);

const expandable = isItemExpandable(children);
const pluginContentRef = React.useRef<HTMLDivElement>(null);
const handleContentRef = useForkRef(pluginContentRef, contentRef);

Expand Down
5 changes: 0 additions & 5 deletions packages/x-tree-view/src/useTreeItem2/useTreeItem2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const useTreeItem2 = <TPlugins extends DefaultTreeViewPlugins = DefaultTr
(otherHandlers: EventHandlers) =>
(event: React.FocusEvent<HTMLElement> & MuiCancellableEvent) => {
otherHandlers.onFocus?.(event);

if (event.defaultMuiPrevented) {
return;
}
Expand All @@ -52,7 +51,6 @@ export const useTreeItem2 = <TPlugins extends DefaultTreeViewPlugins = DefaultTr
(otherHandlers: EventHandlers) =>
(event: React.FocusEvent<HTMLElement> & MuiCancellableEvent) => {
otherHandlers.onBlur?.(event);

if (event.defaultMuiPrevented) {
return;
}
Expand All @@ -64,7 +62,6 @@ export const useTreeItem2 = <TPlugins extends DefaultTreeViewPlugins = DefaultTr
(otherHandlers: EventHandlers) =>
(event: React.KeyboardEvent<HTMLElement> & MuiCancellableEvent) => {
otherHandlers.onKeyDown?.(event);

if (event.defaultMuiPrevented) {
return;
}
Expand All @@ -75,7 +72,6 @@ export const useTreeItem2 = <TPlugins extends DefaultTreeViewPlugins = DefaultTr
const createContentHandleClick =
(otherHandlers: EventHandlers) => (event: React.MouseEvent & MuiCancellableEvent) => {
otherHandlers.onClick?.(event);

if (event.defaultMuiPrevented) {
return;
}
Expand All @@ -87,7 +83,6 @@ export const useTreeItem2 = <TPlugins extends DefaultTreeViewPlugins = DefaultTr
const createContentHandleMouseDown =
(otherHandlers: EventHandlers) => (event: React.MouseEvent & MuiCancellableEvent) => {
otherHandlers.onMouseDown?.(event);

if (event.defaultMuiPrevented) {
return;
}
Expand Down

0 comments on commit de60920

Please sign in to comment.