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: checkbox with preventDefault on click should not trigger SyntheticEvent #22735

Closed
wants to merge 13 commits into from

Conversation

zombieJ
Copy link
Contributor

@zombieJ zombieJ commented Nov 10, 2021

Summary

React use SyntheticEvent to trigger change event on checkbox & radio by click event. But when called preventDefault on click event, it does not trigger native change event but will trigger on React. It's caused by the checked state will tmp set to opposite but reset when finished by browser:

https://html.spec.whatwg.org/multipage/input.html#the-input-element

Add check to ignore change event when prevent is marked

resolve #9023

How did you test this change?

Update ChangeEventPlugin-test.js for preventDefault case. (Since this is prevent wrong event trigger, seems can do nothing for video record)

related issue:

@sizebot
Copy link

sizebot commented Nov 10, 2021

Comparing: e0aa5e2...461333c

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js +0.05% 129.68 kB 129.75 kB +0.05% 41.46 kB 41.48 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js +0.05% 134.66 kB 134.72 kB +0.07% 42.94 kB 42.97 kB
facebook-www/ReactDOM-prod.classic.js +0.04% 424.11 kB 424.28 kB +0.02% 78.18 kB 78.19 kB
facebook-www/ReactDOM-prod.modern.js +0.04% 412.66 kB 412.83 kB +0.03% 76.43 kB 76.45 kB
facebook-www/ReactDOMForked-prod.classic.js +0.04% 424.11 kB 424.28 kB +0.02% 78.18 kB 78.20 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by 🚫 dangerJS against 461333c

@zombieJ zombieJ changed the title fix: checkbox with preventDefault on click should not trigger SyntheticEvent [WIP] fix: checkbox with preventDefault on click should not trigger SyntheticEvent Nov 10, 2021
@zombieJ zombieJ changed the title [WIP] fix: checkbox with preventDefault on click should not trigger SyntheticEvent fix: checkbox with preventDefault on click should not trigger SyntheticEvent Nov 10, 2021
@zombieJ
Copy link
Contributor Author

zombieJ commented Nov 15, 2021

hi @rickhanlonii,
I'm not sure if I missing something. Could you help to confirm this PR?

Thanks~

@zombieJ
Copy link
Contributor Author

zombieJ commented Nov 26, 2021

Anyone can take a look? Half month passed ...

@gaearon @sebmarkbage

@eps1lon
Copy link
Collaborator

eps1lon commented Nov 26, 2021

This doesn't seem to work in a Chrome Version 96.0.4664.45 (Official Build) (64-bit) when we call event.preventDefault in a React onClick handler: https://codesandbox.io/s/react-18-checkbox-click-preventdefault-change-quirk-4mwl8

@eps1lon
Copy link
Collaborator

eps1lon commented Nov 26, 2021

Here is a test that still fails in this PR which is based on https://codesandbox.io/s/react-18-checkbox-click-preventdefault-change-quirk-4mwl8?file=/src/index.js. Notice that handleNativeChange is not called as expected but handleReactChange is. If we don't call event.preventDefault then both handlers are called as expected.

it('should not fire change for checkbox input when preventDefault is called in onClick', () => {
  const handleReactChange = jest.fn();
  const handleNativeChange = jest.fn();

  const node = ReactDOM.render(
    <input
      type="checkbox"
      defaultChecked
      onClick={event => {
        event.preventDefault();
      }}
      onChange={handleReactChange}
    />,
    container,
  );
  node.addEventListener('change', handleNativeChange);

  node.click();

  expect(handleNativeChange).toHaveBeenCalledTimes(0);
  expect(handleReactChange).toHaveBeenCalledTimes(0);
});

@zombieJ
Copy link
Contributor Author

zombieJ commented Nov 29, 2021

@eps1lon,
Thx for your feedback. Let me check out~

@zombieJ zombieJ changed the title fix: checkbox with preventDefault on click should not trigger SyntheticEvent [WIP] fix: checkbox with preventDefault on click should not trigger SyntheticEvent Nov 29, 2021
@zombieJ
Copy link
Contributor Author

zombieJ commented Nov 29, 2021

Hmmm...
dispatchQueue seems push the change event before real onClick call. It makes additional change event trigger.

@zombieJ zombieJ changed the title [WIP] fix: checkbox with preventDefault on click should not trigger SyntheticEvent fix: checkbox with preventDefault on click should not trigger SyntheticEvent Nov 29, 2021
@zombieJ
Copy link
Contributor Author

zombieJ commented Nov 29, 2021

@eps1lon,
Fixed. Please check: https://codesandbox.io/s/react-18-checkbox-click-preventdefault-change-quirk-forked-ptol7?file=/src/index.js

@eps1lon
Copy link
Collaborator

eps1lon commented Nov 29, 2021

@eps1lon, Fixed. Please check: codesandbox.io/s/react-18-checkbox-click-preventdefault-change-quirk-forked-ptol7?file=/src/index.js

Fixed in Chrome 96 👍🏻 For safe measure, could you check various other browsers as well (e.g. Safari and IE 11 are notorous for inconsistent behavior).

@zombieJ
Copy link
Contributor Author

zombieJ commented Nov 29, 2021

Update:
Tested in Chrome, Firefox, Safari worked as expect.

Since CodeSandbox do not support IE11, I need additional work for test it.

@zombieJ
Copy link
Contributor Author

zombieJ commented Nov 30, 2021

@eps1lon,
I've tested follow browser with latest version. All work as expect : )

  • Chrome
  • Safari
  • IE11 (With polyfill to make React work)
  • Firefox

@zombieJ
Copy link
Contributor Author

zombieJ commented Dec 3, 2021

@eps1lon, wait your feed back : )

@zombieJ
Copy link
Contributor Author

zombieJ commented Dec 8, 2021

@eps1lon , anything more I can do? : )

@zombieJ
Copy link
Contributor Author

zombieJ commented Dec 13, 2021

@sebmarkbage could you pls help to take a look?

@zombieJ
Copy link
Contributor Author

zombieJ commented Dec 16, 2021

Anyone can help on this?

@zombieJ
Copy link
Contributor Author

zombieJ commented Dec 27, 2021

@eps1lon, could you help to take a look?~ Thanks

@zombieJ
Copy link
Contributor Author

zombieJ commented Jan 7, 2022

@bvaughn, could you help to merge this? thx

@n0099
Copy link

n0099 commented Jan 10, 2022

LGTM

@zombieJ
Copy link
Contributor Author

zombieJ commented Jan 17, 2022

Hi @bvaughn, is any chance that this PR will be merged or something else that I can help?~

@zombieJ
Copy link
Contributor Author

zombieJ commented Jan 29, 2022

Hi @bvaughn, is there any chance to merge this PR?

@zombieJ
Copy link
Contributor Author

zombieJ commented Feb 10, 2022

@sebmarkbage, could you help to take a look?

@zombieJ
Copy link
Contributor Author

zombieJ commented Mar 11, 2022

@Huxpro Could you help to find someone can handle this? thx

@n0099
Copy link

n0099 commented Mar 11, 2022

@gnoff plz take a look on this

@zombieJ
Copy link
Contributor Author

zombieJ commented Jan 6, 2023

hi @kassens,
Could you help to check this? This PR has been wait for over one year : )

@zombieJ
Copy link
Contributor Author

zombieJ commented Jan 12, 2023

@eps1lon, safe to merge? : )

@zombieJ
Copy link
Contributor Author

zombieJ commented Jan 19, 2023

Why PR is such hard. I don't want to disturb but none care about this : (

@eps1lon @gnoff @kassens @alunyov @sebmarkbage @gaearon @poteto

@zombieJ
Copy link
Contributor Author

zombieJ commented Feb 2, 2023

Copy link

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Apr 10, 2024
Copy link

Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you!

@github-actions github-actions bot closed this Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Component: DOM Resolution: Stale Automatically closed due to inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

event.preventDefault in click handler does not prevent onChange from being called
6 participants