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

IBX-2803: Fix drag and drop after React update #18

Merged
merged 3 commits into from
May 19, 2022
Merged

Conversation

tischsoic
Copy link
Contributor

@tomaszszopinski tomaszszopinski requested a review from micszo May 17, 2022 13:25
Copy link
Contributor

@micszo micszo left a comment

Choose a reason for hiding this comment

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

Could you provide a brief explanation? 🙂
Is the tmp const crucial?

@@ -31,7 +31,7 @@ public function dragAndDrop(string $from, string $hover, string $to): void
throw new RuntimeException('drag-mock library has to be added to the page in order to use this method. Refer to README in BehatBundle for more information.');
}

$movingScript = sprintf('dragMock.dragStart(%s).dragOver(%s).delay(100).drop(%s);', $from, $hover, $to);
$movingScript = sprintf('{ const dragMockTmp = dragMock.dragStart(%s).delay(50).dragOver(%s).delay(50); setTimeout(() => dragMockTmp.drop(%s), 100); }', $from, $hover, $to);
Copy link
Contributor Author

@tischsoic tischsoic May 17, 2022

Choose a reason for hiding this comment

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

After updating React from v17 to v18 it seems that React started to do renders asynchronously (what was mentioned e.g. here), which means that after calling render function component is not rendered immediately but after some short period of time, hence some modifications have been applied here to make d&d work in Page Builder and Form Builder.

  1. The first problem was that in PB placeholder is rendered with delay so we need to delay evaluation of $to statement (equal to: document.querySelector('#page-builder-preview').contentDocument.querySelector('.droppable-placeholder')). delay function does not delay drop call and evaluation of $to. Because of this we need setTimeout to both call drop and let browser engine evaluate $to with a delay. We need dragMockTmp to pass dragMock reference to setTimeout. Block statement was added so that dragMockTmp won't pollute the global namespace.

  2. In Form Builder, when drag is started the state is being updated (handleDragStartSidebarField) and we need to give React time to apply this state change thus delay(50) was added before dragOver so that when handleZoneDragOver is being called the state of the component is already updated.

  3. Finally, delay(50) was added after dragOver, because otherwise drop would not be fired by dragMock (probably there is a bug in the library).

@dew326 dew326 merged commit 09a0bfc into main May 19, 2022
@dew326 dew326 deleted the IBX-2803-update-react branch May 19, 2022 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants