【发布时间】:2020-08-21 13:05:25
【问题描述】:
我想在 material-ui 自动完成下拉菜单上显示标题,当有人选择一个选项并提交时,我希望提交年份值(注册到 RHF):
import React from "react";
import { useForm, Controller } from "react-hook-form";
import { TextField } from "@material-ui/core";
import Autocomplete from "@material-ui/lab/Autocomplete";
function App() {
const { control, handleSubmit, register } = useForm();
const onSubmit = (data) => console.log(data);
const top100Films = [
{ title: "The Shawshank Redemption", value: 1994 },
{ title: "The Godfather", year: 1972 },
{ title: "The Godfather: Part II", year: 1974 }
];
return (
<form onSubmit={handleSubmit(onSubmit)}>
<Autocomplete
id="combo-box-demo"
options={top100Films}
getOptionLabel={(option) => option.title}
style={{ width: 300 }}
renderInput={(params) => (
<TextField
{...params}
inputRef={register}
name="formfield"
label="Combo box"
variant="outlined"
/>
)}
/>
<input type="submit" />
</form>
);
}
export default App;
【问题讨论】:
标签: reactjs forms autocomplete material-ui react-hook-form