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] Fix drag and drop issue with label editing #15636

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -6,6 +6,7 @@ import {
UseTreeViewItemsSignature,
isTargetInDescendants,
useSelector,
selectorIsItemBeingEdited,
} from '@mui/x-tree-view/internals';
import {
UseTreeItemDragAndDropOverlaySlotPropsFromItemsReordering,
Expand All @@ -15,6 +16,7 @@ import {
UseTreeItemContentSlotPropsFromItemsReordering,
} from './useTreeViewItemsReordering.types';
import {
selectorDraggedItem,
selectorItemsReorderingDraggedItemProperties,
selectorItemsReorderingIsValidTarget,
} from './useTreeViewItemsReordering.selectors';
Expand All @@ -34,6 +36,9 @@ export const useTreeViewItemsReorderingItemPlugin: TreeViewItemPlugin = ({ props
itemId,
);
const isValidTarget = useSelector(store, selectorItemsReorderingIsValidTarget, itemId);
const draggedItemId = useSelector(store, selectorDraggedItem);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause every item to re-render everytime draggedItemId changes.

You could create a selectorIsDraggedItemBeingEdited, but same you will still re-render every item everytime you start dragging your edited item.

Could you instead prevent putting in the state an item that is being edited in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem to solve the problem. The best thing I could think of is to disable dragging when editing is enabled on any item. WDYT?


const isBeingEdited = useSelector(store, selectorIsItemBeingEdited, draggedItemId);

return {
propsEnhancers: {
Expand All @@ -44,7 +49,8 @@ export const useTreeViewItemsReorderingItemPlugin: TreeViewItemPlugin = ({ props
}): UseTreeItemRootSlotPropsFromItemsReordering => {
if (
!itemsReordering.enabled ||
(itemsReordering.isItemReorderable && !itemsReordering.isItemReorderable(itemId))
(itemsReordering.isItemReorderable && !itemsReordering.isItemReorderable(itemId)) ||
isBeingEdited
) {
return {};
}
Expand Down Expand Up @@ -79,6 +85,7 @@ export const useTreeViewItemsReorderingItemPlugin: TreeViewItemPlugin = ({ props

const handleRootDragOver = (event: React.DragEvent & TreeViewCancellableEvent) => {
externalEventHandlers.onDragOver?.(event);

if (event.defaultMuiPrevented) {
return;
}
Expand All @@ -88,6 +95,7 @@ export const useTreeViewItemsReorderingItemPlugin: TreeViewItemPlugin = ({ props

const handleRootDragEnd = (event: React.DragEvent & TreeViewCancellableEvent) => {
externalEventHandlers.onDragEnd?.(event);

if (event.defaultMuiPrevented) {
return;
}
Expand All @@ -106,12 +114,13 @@ export const useTreeViewItemsReorderingItemPlugin: TreeViewItemPlugin = ({ props
externalEventHandlers,
contentRefObject,
}): UseTreeItemContentSlotPropsFromItemsReordering => {
if (!isValidTarget) {
if (!isValidTarget || isBeingEdited) {
return {};
}

const handleDragOver = (event: React.DragEvent & TreeViewCancellableEvent) => {
externalEventHandlers.onDragOver?.(event);

if (event.defaultMuiPrevented || validActionsRef.current == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ export const selectorItemsReorderingDraggedItemProperties = createSelector(
},
);

/**
* Get the id of the item that is currently being dragged.
* @param {TreeViewState<[UseTreeViewItemsReorderingSignature]>} state The state of the tree view.
* @returns {string | null} The id of the currently dragged item.
*/
export const selectorDraggedItem = createSelector(
[selectorItemsReordering],
(itemsReordering) => itemsReordering?.draggedItemId,
);
/**
* Check if the current item is a valid target for the dragged item.
* @param {TreeViewState<[UseTreeViewItemsReorderingSignature]>} state The state of the tree view.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const useTreeItemUtils = <
return;
}

if (!status.focused) {
if (!status.focused && !status.editing) {
instance.focusItem(event, itemId);
}

Expand Down
1 change: 1 addition & 0 deletions packages/x-tree-view/src/internals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export type {
UseTreeViewItemsState,
} from './plugins/useTreeViewItems';
export { useTreeViewLabel } from './plugins/useTreeViewLabel';
export { selectorIsItemBeingEdited } from './plugins/useTreeViewLabel/useTreeViewLabel.selectors';
export type {
UseTreeViewLabelSignature,
UseTreeViewLabelParameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export const selectorIsItemEditable = createSelector(
* @returns {boolean} `true` if the item is being edited, `false` otherwise.
*/
export const selectorIsItemBeingEdited = createSelector(
[selectorTreeViewLabelState, (_, itemId: string) => itemId],
(labelState, itemId) => labelState.editedItemId === itemId,
[selectorTreeViewLabelState, (_, itemId: string | null) => itemId],
(labelState, itemId) => (itemId ? labelState.editedItemId === itemId : false),
);
Loading