Skip to content

[Switch] preventDefault is ignored #23709

Closed

Description

@joshuambg
  • 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

Activity

  1. added
    status: waiting for maintainer
    on Nov 25, 2020
  2. eps1lon commented on Nov 25, 2020

    @eps1lon
    Member

    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

  3. added
    type: bug
    scope: switch
    and removed
    status: waiting for maintainer
    on Nov 25, 2020
  4. changed the title [-]No way to prevent Switch from updating on click[/-] [+][Switch] preventDefault is ignored[/+] on Nov 25, 2020
  5. oliviertassinari commented on Nov 25, 2020

    @oliviertassinari
    Member

    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);
  6. added
    good first issue
    on Nov 27, 2020
  7. satyadeeproat commented on Nov 29, 2020

    @satyadeeproat

    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?

  8. oliviertassinari commented on Nov 29, 2020

    @oliviertassinari
    Member

    @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.

  9. satyadeeproat commented on Nov 29, 2020

    @satyadeeproat

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

  10. eps1lon commented on Nov 30, 2020

    @eps1lon
    Member

    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: react/react#9023

  11. marianayap commented on Nov 30, 2020

    @marianayap
    Contributor

    Hello @joshuambg @oliviertassinari , I am looking to work on the issue as a first time with open source. Is it taken already? If not I'll like to contribute

  12. oliviertassinari commented on Nov 30, 2020

    @oliviertassinari
    Member

    @marianayap Sorry, the issue was already worked on. The problem should now have been solved :)

  13. Wakabanan commented on Dec 23, 2020

    @Wakabanan

    Hi @oliviertassinari . Seems to me that issue is still present. From
    <IconButton onClick={(event) => {event.preventDefault()}} onChange={(e) => {e.preventDefault()}}>
    wont stop rerender.

  14. oliviertassinari commented on Dec 24, 2020

    @oliviertassinari
    Member

    @laik97 do you have a reproduction with v5.0.0-alpha.20?

  15. Wakabanan commented on Dec 28, 2020

    @Wakabanan

    @oliviertassinari yes. Just updated with latest release and error still appears.
    It seems to have problem with:
    <IconButton onClick={myAccountHandle}> <AccountCircleIcon/> </IconButton>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issuescope: switchtype: bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions