Skip to content

Commit

Permalink
[TreeView] Fix the parameters passed for the `canMoveItemToNewPositio…
Browse files Browse the repository at this point in the history
…n` prop (#14176)
  • Loading branch information
flaviendelangle authored Aug 12, 2024
1 parent d5f2c0f commit 01fa734
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"description": "Used to determine if a given item can move to some new position.",
"typeDescriptions": {
"params": "The params describing the item re-ordering.",
"params.itemId": "The id of the item to check.",
"params.itemId": "The id of the item that is being moved to a new position.",
"params.oldPosition": "The old position of the item.",
"params.newPosition": "The new position of the item.",
"boolean": "<code>true</code> if the item can move to the new position."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ RichTreeViewPro.propTypes = {
/**
* Used to determine if a given item can move to some new position.
* @param {object} params The params describing the item re-ordering.
* @param {string} params.itemId The id of the item to check.
* @param {string} params.itemId The id of the item that is being moved to a new position.
* @param {TreeViewItemReorderPosition} params.oldPosition The old position of the item.
* @param {TreeViewItemReorderPosition} params.newPosition The new position of the item.
* @returns {boolean} `true` if the item can move to the new position.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,23 @@ describeTreeView<
});

describe('canMoveItemToNewPosition prop', () => {
it('should call canMoveItemToNewPosition with the correct parameters', () => {
const canMoveItemToNewPosition = spy();
const response = render({
experimentalFeatures: { indentationAtItemLevel: true, itemsReordering: true },
items: [{ id: '1' }, { id: '2' }, { id: '3' }],
itemsReordering: true,
canMoveItemToNewPosition,
});

dragEvents.fullDragSequence(response.getItemRoot('1'), response.getItemContent('2'));
expect(canMoveItemToNewPosition.lastCall.firstArg).to.deep.equal({
itemId: '1',
oldPosition: { parentId: null, index: 0 },
newPosition: { parentId: null, index: 1 },
});
});

it('should not allow to drop an item when canMoveItemToNewPosition returns false', () => {
const response = render({
experimentalFeatures: { indentationAtItemLevel: true, itemsReordering: true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,19 @@ export const useTreeViewItemsReordering: TreeViewPlugin<UseTreeViewItemsReorderi

const getDroppingTargetValidActions = React.useCallback(
(itemId: string) => {
if (!state.itemsReordering) {
const itemsReordering = state.itemsReordering;
if (!itemsReordering) {
throw new Error('There is no ongoing reordering.');
}

if (itemId === state.itemsReordering.draggedItemId) {
if (itemId === itemsReordering.draggedItemId) {
return {};
}

const canMoveItemToNewPosition = params.canMoveItemToNewPosition;
const targetItemMeta = instance.getItemMeta(itemId);
const targetItemIndex = instance.getItemIndex(targetItemMeta.id);
const draggedItemMeta = instance.getItemMeta(state.itemsReordering.draggedItemId);
const draggedItemMeta = instance.getItemMeta(itemsReordering.draggedItemId);
const draggedItemIndex = instance.getItemIndex(draggedItemMeta.id);

const oldPosition: TreeViewItemReorderPosition = {
Expand All @@ -84,7 +85,7 @@ export const useTreeViewItemsReordering: TreeViewPlugin<UseTreeViewItemsReorderi
isValid = false;
} else if (canMoveItemToNewPosition) {
isValid = canMoveItemToNewPosition({
itemId,
itemId: itemsReordering.draggedItemId,
oldPosition,
newPosition: positionAfterAction,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export interface UseTreeViewItemsReorderingParameters {
/**
* Used to determine if a given item can move to some new position.
* @param {object} params The params describing the item re-ordering.
* @param {string} params.itemId The id of the item to check.
* @param {string} params.itemId The id of the item that is being moved to a new position.
* @param {TreeViewItemReorderPosition} params.oldPosition The old position of the item.
* @param {TreeViewItemReorderPosition} params.newPosition The new position of the item.
* @returns {boolean} `true` if the item can move to the new position.
Expand Down

0 comments on commit 01fa734

Please sign in to comment.