按 T​​ab 键时材质 ui 对话框会自动关闭

pan*_*909 5 javascript reactjs material-ui

我有一个 React 项目,正在使用material-ui v3。我有一个appBar,其中包含一个带有一些menuItems 的菜单,单击menuItem后,我将打开一个包含表单的对话框,现在一切看起来都很好,直到我填写第一个输入框并按Tab 键切换到另一个输入框,一旦我按 T​​ab 对话框自动关闭。以下是相关代码片段。

header.js

<header>
            <AppBar>
                <Toolbar>
                    <Typography variant="title" color="inherit" className={classes.flex} component={Link} to='/'>
            {appName}
                    </Typography>
                    <Avatar className={classes.orangeAvatar}>
                        <Button
                            color="primary"
                            aria-owns={anchorEl ? 'simple-menu' : null}
                            aria-haspopup="true"
                            onClick={this.handleClick}
                        >
                            {user && user.username[0] || "-"}
                        </Button>
                    </Avatar>
                    <Menu
                        id="simple-menu"
                        anchorEl={anchorEl}
                        open={Boolean(anchorEl)}
                        onClose={this.handleClose}
                    >
                        <ChangePassword
                            {...this.props}
            >
                            {({ onClick }) => {
                                return (
                                    <MenuItem onClick={onClick}>
                                        Change password
                                        </MenuItem>
                                );
                            }}
                        </ChangePassword>
                        <MenuItem onClick={async e => {
                            this.handleClose(e);
                            await window.localStorage.clear();
                            client.resetStore();
                            window.location.href = "/";
                        }}
                        >
                            <InputIcon className={classes.icon} /> Logout
                            </MenuItem>
                    </Menu>
                </Toolbar>
            </AppBar>
        </header>
Run Code Online (Sandbox Code Playgroud)

更改密码.js

class ChangePassword extends React.PureComponent {
state = {
    open: false,
};

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

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

render() {
    const { open } = this.state;
    const {
        classes,
        history,            
        negativeHandler = e => this.handleClose(),
        positiveHandler = e => null,
        postMutation = e => null,
        children
    } = this.props;
    const title = "Change password",
        content = "Change password of this user.";
    return (
        <Mutation mutation={UPDATE_USER_PASSWORD}>
            {(mutate, { loading, error, data }) => {
                return (
                    <React.Fragment>
                        {children({ onClick: this.handleOpen })}                            
                        {
                            open ? (
                                <Dialog
                                    fullScreen={false}
                                    open={open}
                                    onClose={negativeHandler}
                                    aria-labelledby={title}
                                >
                                    <Form
                                        onSubmit={e => {
                                            positiveHandler(mutate, e)
                                                .then((data) => {
                                                    if (postMutation) {
                                                        postMutation(data);
                                                        this.handleClose(e);
                                                    }
                                                    else {
                                                        history.goBack()
                                                    }
                                                })
                                        }}
                                    >
                                        <DialogTitle id={title}>{title}</DialogTitle>
                                        <DialogContent>
                                            <DialogContentText>
                                                {content}
                                            </DialogContentText>
                                            {
                                                getFormJSX(defaults)
                                            }
                                        </DialogContent>
                                        <DialogActions>
                                            {
                                                loading ? <CircularProgress className={classes.progress} /> : null
                                            }
                                            <Button onClick={negativeHandler} color="primary">Cancel</Button>
                                            <Button size="large" type="submit" disabled={loading}>Confirm</Button>
                                        </DialogActions>
                                    </Form>
                                </Dialog>
                            ) : null

                        }

                    </React.Fragment>
                );
            }}
        </Mutation>
    );
}}

export default withStyles(styles)(ChangePassword);
Run Code Online (Sandbox Code Playgroud)

getFormJSX (defaults)方法简单地根据defaults对象生成动态表单,返回值仅包含表单控件而不包含标签本身。除此之外,除了或其他对话框之外,我的应用程序其余部分的常规表单中的所有内容都运行良好。仅当对话框位于 menuItem 内(该 menuItem 又位于 appBar 内的菜单内)时,才会出现此问题。如果我可以提供任何其他内容来支持我的问题,请告诉我。