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

Fix event handling with react-beautiful-dnd #5842

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

itsdouges
Copy link

@itsdouges itsdouges commented Jan 9, 2024

This pull request removes logic to bail out from an event if someone else listening to the event (usually in a capture phase) has called preventDefault(). This fixes interop between react-select and react-beautiful-dnd.

To ensure things don't regress, and to ensure to not contradict what was solved in #5134 I've added four e2e tests covering the various flows in both:

  • an emulated react-beautiful-dnd environment, and
  • direct react-calendar interop

All tests pass, the same behaviour remains, hopefully we're good to go.

TODO

  • fix tests
  • assess the solution and seek alternatives if needed

Closes #5833

Copy link

changeset-bot bot commented Jan 9, 2024

🦋 Changeset detected

Latest commit: 16b4be7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
react-select Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@itsdouges
Copy link
Author

cc @rkulinski @flexdinesh

Copy link

codesandbox-ci bot commented Jan 9, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 16b4be7:

Sandbox Source
react-codesandboxer-example Configuration

docs/Tests.tsx Outdated Show resolved Hide resolved
@@ -1293,10 +1293,6 @@ export default class Select<
onControlMouseDown = (
event: React.MouseEvent<HTMLDivElement> | React.TouchEvent<HTMLDivElement>
) => {
// Event captured by dropdown indicator

Choose a reason for hiding this comment

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

is there a test for this particular use case - dropdown indicator preventing default event (

event.preventDefault();
)

Also, not quite clear why stopPropagation is not a case, onMenuMouseDown uses it (uses both) for example.


This code was added here for a reason, and I can see other pieces relying on this one. Basically - all .preventDefault in this file.

What if we try to change the game rules and just rely on a different information source?

const createEventTracker = () => {
  const tracker = new WeakSet();
  return {
       mark: (event) => {
        tracker.add(event.target);
        Promise.resolve().then(() => tracker.delete(event.target);
      },
      isMarked: (event) => tracker.has(event.target);
 }
} 

/// ....
const mouseDownTracker = createEventTracker();

onDropdownIndicatorMouseDown = (e) => {
  // ...
  mouseDownTracker.mark(e);
}

onControlMouseDown = (event) => {
    // Event captured by dropdown indicator
    if (mouseDownTracker.isMarked(event)) {
      return;
    }
}

Might be a little too much code to support a single line, but this code will keep the old behavior and unlock our usecase.

Copy link
Author

Choose a reason for hiding this comment

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

This prevent default was added in #5134 — I can't imagine anything in Select is leaning on it?

We can definitely change the rules like you said if the behaviour is needed. I'm just not sure it is.

Copy link
Author

Choose a reason for hiding this comment

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

The failing tests might point to it being needed I suppose ;).

Choose a reason for hiding this comment

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

Right, and you added tests for #5050 case, and they are passing. May be something else in between, including changes in DatePicker resolved the problem in the meanwhile

Copy link
Author

Choose a reason for hiding this comment

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

Yeah you're right, which is what the tests are showing now. I'll dig in soon.

mdo2 added a commit to mdo2/react-select that referenced this pull request Jan 22, 2024
This is intended to be a temporal fix, as I think a better solution must be developed.

Referenced issues: JedWatson#5833, JedWatson#5176

Another PRs abaout this: JedWatson#5842
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.

Select behaves inconsistently when receiving a mousedown event with defaultPrevented
3 participants