材料UI工具提示在触发对话框关闭后保持打开状态

时间:2018-05-01 19:39:03

标签: reactjs material-design material-ui

我的假设是Dialog的状态导致问题,但我无法弄清楚这一点。工具提示按预期工作,直到单击IconButton。对话框会弹出,但退出对话框后,工具提示会弹出为活动状态。

class DeleteDocument extends React.Component {
  state = {
    open: false,
  };

  onDeleteFile() {
    try {
      ensureJobIsUnlocked();
    } catch (err) {
      return;
    }

    const confirmedByUser = true;
    if (confirmedByUser) {
      this.props.onDeleteFile(this.props.selectedDocument.id);
      this.setState({ open: false });
    }
  }

  handleClickOpen = () => {
    this.setState({ open: true });
  };

  handleClose = () => {
    this.setState({ open: false });
  };

  render() {
    return (
      <div>
        <Tooltip id="tooltip-icon" title="Delete Document">
          <div>
            <IconButton
              disabled={(this.props.selectedDocument == null)}
              onClick={this.handleClickOpen}
            >
              <DeleteIcon />
            </IconButton>
          </div>
        </Tooltip>
        <Dialog
          open={this.state.open}
          onClose={this.handleClose}
          aria-labelledby="alert-dialog-title"
          aria-describedby="alert-dialog-description"
        >
          <DialogTitle id="alert-dialog-title">{'Delete Document'}</DialogTitle>
          <DialogContent>
            <DialogContentText id="alert-dialog-description">
              This will delete the currently active PDF/Component Design. Are you sure you want to do this?
            </DialogContentText>
          </DialogContent>
          <DialogActions>
            <Button onClick={this.handleClose} color="primary">
              Cancel
            </Button>
            <Button onClick={this.onDeleteFile.bind(this)} color="primary" autoFocus>
              Delete
            </Button>
          </DialogActions>
        </Dialog>
      </div>
    );
  }
}

2 个答案:

答案 0 :(得分:10)

请参阅问题#9624

  

这是预期的行为。它已经完成了可访问性考虑。您有两个选项,要么禁用工具提示响应以聚焦事件,要么禁用对话框还原焦点行为。

<强> 1。禁用工具提示响应以关注事件(docs

<Tooltip disableTriggerFocus={true} />

Edit Material-ui - tooltip disable restore focus trigger

<强> 2。禁用对话框恢复焦点行为(docs

<Dialog disableRestoreFocus={true} />

Edit Material-ui - tooltip disable restore focus

答案 1 :(得分:5)

根据此文档https://material-ui.com/api/tooltip/

设置disableFocusListener = {true}