Eti*_*a49 5 javascript reactjs material-ui
我想从 React.js 中的 Material UI 向我的 Autocomplete TextField 组件显示默认值。从用户的配置文件中自动加载的预填充值,可以使用列表中的另一个值进行更改。
这是我的代码:
import React from 'react';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';
export const ComboBox =() => {
return (
<Autocomplete
id="combo-box-demo"
options={top100Films}
getOptionLabel={(option) => option.title}
style={{ width: 300 }}
renderInput={(params) => <TextField {...params} label="Combo box" defaultValue="The Godfather" variant="outlined" />}
/>
);
}
// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
{ title: 'The Shawshank Redemption', year: 1994 },
{ title: 'The Godfather', year: 1972 },
{ title: 'The Godfather: Part II', year: 1974 },
{ title: 'The Dark Knight', year: 2008 }
]
Run Code Online (Sandbox Code Playgroud)
我只看到带有标签的输入字段。defaultValue 被列为 TextField 和 Autocomplete 的 API,我也尝试将它直接移到 Autocomplete 下。还是行不通。
您的defaultValueof<AutoComplete />应该与您的格式相同options,getOptionLable()即使从您的defaultValue.
在title字符串的代码属性中是undefined,所以没有显示任何内容。
这应该可以正常工作:
<Autocomplete
id="combo-box-demo"
options={top100Films}
defaultValue={{ title: "The Godfather", year: 1972 }}
getOptionLabel={(option) => option.title}
style={{ width: 300 }}
renderInput={(params) => <TextField {...params} label="Combo box" defaultValue="The Godfather" variant="outlined" />}
/>
Run Code Online (Sandbox Code Playgroud)
您可以像这样使用defaultValue该选项:
<Autocomplete
id="combo-box-demo"
options={top100Films}
defaultValue={ top100Films[0] }
getOptionLabel={(option) => option.title}
style={{ width: 300 }}
renderInput={(params) => <TextField {...params} label="Combo box" defaultValue="The Godfather" variant="outlined" />}
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11030 次 |
| 最近记录: |