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 the parameters passed for the canMoveItemToNewPosition prop #14176

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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