Sitelet https://web.archive.org/web/20201130104506/https://github.com/mui-org/material-ui/issues/23709
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

[Switch] preventDefault is ignored #23709

Open
joshuambg opened this issue Nov 25, 2020 · 6 comments · May be fixed by #23786
Open

[Switch] preventDefault is ignored #23709

joshuambg opened this issue Nov 25, 2020 · 6 comments · May be fixed by #23786

Comments

@joshuambg
Copy link

@joshuambg joshuambg commented Nov 25, 2020 •

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

Using preventDefault does not prevent the Switch UI from updating.

          <Switch
            onClick={(event)=>{event.preventDefault();}}
          />

Expected Behavior 🤔

Using preventDefault in the onClick handler should stop the element from updating.

Steps to Reproduce 🕹

Steps:

  1. Add a Switch from latest material-ui
  2. Call preventDefault inside onClick
  3. Watch the element change state

Context 🔦

Your Environment 🌎

Tech Version
Material-UI v4.11.0
React v16.14.0
@eps1lon
Copy link
Member

@eps1lon eps1lon commented Nov 25, 2020

Below the [Switch API props table] you'll find

Any other props supplied will be provided to the root element (IconButton).

which means that you're not preventing the default behavior of the click event on <input /> but <IconButton />.

However, it doesn't seem like you can do this at the moment. Passing onClick to inputProps works only after the first state change: https://codesandbox.io/s/switch-click-prevent-default-tutkj

@oliviertassinari oliviertassinari changed the title No way to prevent Switch from updating on click [Switch] preventDefault is ignored Nov 25, 2020
@oliviertassinari
Copy link
Member

@oliviertassinari oliviertassinari commented Nov 25, 2020

Should we solve the issue like this?

diff --git a/packages/material-ui/src/internal/SwitchBase.js b/packages/material-ui/src/internal/SwitchBase.js
index 3db2a57393..009679bf56 100644
--- a/packages/material-ui/src/internal/SwitchBase.js
+++ b/packages/material-ui/src/internal/SwitchBase.js
@@ -84,6 +84,10 @@ const SwitchBase = React.forwardRef(function SwitchBase(props, ref) {
   };

   const handleInputChange = (event) => {
+    if (event.nativeEvent.defaultPrevented) {
+      return;
+    }
+
     const newChecked = event.target.checked;

     setCheckedState(newChecked);
@satyadeeproat
Copy link

@satyadeeproat satyadeeproat commented Nov 29, 2020

Looks like a small change. If no one is working on it, then I can do it. This will be my first contribution.
As far as my understanding, when you pass
onClick={(event)=>{event.preventDefault();}}
the state will change from checked to uncheck, but we don't want that right?

@oliviertassinari
Copy link
Member

@oliviertassinari oliviertassinari commented Nov 29, 2020

@satyadeeproat this sounds accurate. The objective is to reproduce the behavior of a native input checkbox. We will need to add a test case for it.

@satyadeeproat
Copy link

@satyadeeproat satyadeeproat commented Nov 29, 2020

Ok. let me do it. Will add a test case too.

@praveenkumar-kalidass praveenkumar-kalidass linked a pull request that will close this issue Nov 30, 2020
1 of 1 task complete
@eps1lon
Copy link
Member

@eps1lon eps1lon commented Nov 30, 2020 •

Now that I look at this issue without Material-UI: This looks like a React bug to me. We should still get this fix in but I'm also opening a React issue to get some clarification.

Edit: Already reported: facebook/react#9023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants
You can’t perform that action at this time.