Search code examples
reactjsmaterial-uiconfirmation

ReactJS Modal with Material UI


I'm trying to make a reusable confirmation modal with Material UI but when I press CANCEL or OK the modal does not close.

The code is here:

https://codesandbox.io/s/lucid-hoover-sput6?file=/src/App.js

I can't figure it out why the pop don't dissapear.

LE: added the code here so it remains

ConfirmModal.js

import React from "react";
import { Button } from "@material-ui/core";
import Dialog from "@material-ui/core/Dialog";
import DialogActions from "@material-ui/core/DialogActions";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";

const ConfirmModal = (props) => {
  const { content, open, setOpen, onConfirm } = props;

  return (
    <Dialog
      open={open}
      onClose={() => setOpen(false)}
      aria-labelledby="dialog-title"
    >
      <DialogContent>
        <DialogContentText>{content}</DialogContentText>
      </DialogContent>
      <DialogActions>
        <Button autoFocus onClick={() => setOpen(false)} color="primary">
          Cancel
        </Button>
        <Button
          onClick={() => {
            setOpen(false);
            onConfirm();
          }}
          color="primary"
        >
          OK
        </Button>
      </DialogActions>
    </Dialog>
    // </div>
  );
};

export default ConfirmModal;

App.js

import React, { useState } from "react";
import { IconButton } from "@material-ui/core";
import { Input as InputIcon } from "@material-ui/icons";

import ConfirmModal from "./ConfirmModal";

export default function App() {
  const [confirmOpen, setConfirmOpen] = useState(false);

  const handleLogout = () => {
    console.log("this hould logout the user");
  };

  return (
    <div className="App">
      <h2>Press the button below so the confirmation modal appears </h2>
      <IconButton color="inherit" onClick={() => setConfirmOpen(true)}>
        <InputIcon />
        <ConfirmModal
          content="Are you sure you want to leeeave us ?"
          open={confirmOpen}
          setOpen={setConfirmOpen}
          onConfirm={handleLogout}
        />
      </IconButton>
    </div>
  );
}

Solution

  • Move the modal out of the button. The Modal's cancel/confirm/backdrop click events are propagating (bubbling) up to the open button (IconButton) and its onClick handler is just reopening the modal by setting confirmOpen state true.

    export default function App() {
      const [confirmOpen, setConfirmOpen] = useState(false);
    
      const handleLogout = () => {
        console.log("this hould logout the user");
      };
    
      return (
        <div className="App">
          <h2>Press the button below so the confirmation modal appears </h2>
          <IconButton color="inherit" onClick={() => setConfirmOpen(true)}>
            <InputIcon />
          </IconButton>
          <ConfirmModal
            content="Are you sure you want to leeeave us ?"
            open={confirmOpen}
            setOpen={setConfirmOpen}
            onConfirm={handleLogout}
          />
        </div>
      );
    }