【发布时间】:2021-03-23 23:30:24
【问题描述】:
我正在尝试为我的 react js 应用程序创建一个弹出对话框,当用户单击按钮时,对话框打开。我在该对话框中有输入字段的表单,在用户填写所有输入后,他可以通过单击弹出对话框底部的“提交”按钮提交信息。问题是我不知道如何将提交按钮粘贴到页脚,因此即使有 15 个以上的输入,用户也不必一直向下滚动即可看到“提交”按钮。我知道材料 ui 为此目的具有 DialogActions,但由于对话框位于父组件中,我无法从子组件访问 DialogActions。我的代码:
App.js (parent)
import React, { useState } from "react";
import Info from "./Info";
import Dialog from "@material-ui/core/Dialog";
import { DialogTitle } from "@material-ui/core";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import { DialogActions } from "@material-ui/core";
export default function App() {
const [open, setOpen] = useState(false);
const handleClickOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
};
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<button onClick={handleClickOpen}>Click me to open dialog</button>
<Dialog
open={open}
aria-labelledby="responsive-dialog-title"
maxWidth="md"
setMaxWidth="md"
fullWidth={true}
>
<dialogContent>
<dialogTitle>
{" "}
<div>
<h4>Fill out the form</h4>
</div>
</dialogTitle>
<DialogContentText>
<Info />
</DialogContentText>
</dialogContent>
{/* <DialogActions>
<button id="responsive-dialog-title" onClick={handleClose}>
{" "}
Submit{" "}
</button>
</DialogActions> */}
</Dialog>{" "}
</div>
);
}
和 Info.js(子):
import React, { useState } from "react";
export default function Info() {
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const handleClickOpen = () => {
setOpen(true);
};
const handleSubmit = () => {
console.log(username);
console.log(password);
console.log(address);
};
return (
<form onSubmit={handleSubmit}>
<div
style={{
display: "flex",
flexDirection: "column",
width: "350px",
padding: "20px"
}}
>
<label> Username</label>
<input
value={username}
onChange={(e) => setUsername(e.target.value)}
type="text"
/>
</div>
<div
style={{
display: "flex",
flexDirection: "column",
width: "350px",
padding: "20px"
}}
>
<label> Password</label>
<input
value={password}
onChange={(e) => setPassword(e.target.value)}
type="password"
/>
</div>
<button> Submit</button>
</form>
);
}
有没有办法让 Info.js 中的“提交”按钮显示为 DialogActions/ 固定在底部?非常感谢任何帮助和建议。
【问题讨论】:
标签: javascript css reactjs material-ui dialog