javascript - Material-ui 对话框 : manipulating the Escape button and mouse clicking outside of the dialog

标签 javascript reactjs dialog material-ui

我正在使用material-ui的对话框:当用户按下“退出”按钮时,会打开一个对话框,并出现"is"或“否”按钮,询问用户是否确实想要退出。

当用户单击“退出”按钮或仅单击窗口外部时,系统会将其注销 - 就像按"is"一样。我怎样才能控制这个?我希望他们激活“否”按钮。

谢谢!

最佳答案

再见,您可以使用 Mui 的确认对话框变体 Dialog 。通过这种方式,您可以强制用户单击按钮 YesNo在你的对话框中。

Here我制作了一个codesandbox示例(受到 Mui Dialog page 的启发)

基本上我做了一个简单的Button使用调用 Dialog 的组件:

return (
  <div className={classes.root}>
    <Button onClick={handleClickListItem}>sign out</Button>
    <ConfirmationDialogRaw
      classes={{
        paper: classes.paper
      }}
      keepMounted
      open={open}
      onClose={handleClose}
    />
  </div>
);

ConfirmationDialogRaw组件:

function ConfirmationDialogRaw(props) {
  const { onClose, open, ...other } = props;

  const handleCancel = () => {
    // here just close the confirmation dialog
    console.log("stay in");
    onClose();
  };

  const handleOk = () => {
    // here manage signout
    console.log("sing out");
    onClose();
  };

  return (
    <Dialog
      disableBackdropClick
      disableEscapeKeyDown
      maxWidth="xs"
      aria-labelledby="confirmation-dialog-title"
      open={open}
      {...other}
    >
      <DialogTitle id="confirmation-dialog-title">Sing Out</DialogTitle>
      <DialogContent dividers>
        <Typography>Are you sure you want to exit?</Typography>
      </DialogContent>
      <DialogActions>
        <Button autoFocus onClick={handleCancel} color="primary">
          No
        </Button>
        <Button onClick={handleOk} color="primary">
          Yes
        </Button>
      </DialogActions>
    </Dialog>
  );
}

魔法是由 Prop 完成的 disableBackdropClickdisableEscapeKeyDownDialog组件。

关于javascript - Material-ui 对话框 : manipulating the Escape button and mouse clicking outside of the dialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63535729/

相关文章:

javascript - 选中所有复选框有问题吗?

javascript - JavaScript Live错误显示?

javascript - 读取文件 Reactjs

android - 是否可以使用标准音乐应用程序来选择音频文件?

javascript - 设置秒表定时器的格式

javascript - 销毁 react Prop 中的es6默认值

javascript - 在 ReactJS 中显示来自 flask send_file 函数的图像

java - 单击某处时防止对话框关闭

javascript - 没有 jQuery 的模态对话框

javascript - div滚动的无限循环