Daw*_*n17 44 javascript reactjs material-ui
我指的是 React Material-UI 的文档(https://material-ui.com/components/autocomplete/)。
在演示代码中,
<Autocomplete
options={top100Films}
getOptionLabel={(option: FilmOptionType) => option.title}
style={{ width: 300 }}
renderInput={params => (
<TextField {...params} label="Combo box" variant="outlined" fullWidth />
)}
/>
Run Code Online (Sandbox Code Playgroud)
我知道它是如何工作的,但我不确定我应该如何获得选定的值。
例如,我想为此使用onChange
道具,以便我可以根据选择进行一些操作。
我尝试添加 onChange={v => console.log(v)}
但v
没有显示与所选值相关的任何内容。
Daw*_*n17 110
通过使用传递(event, value)
到onChange
道具来解决。
<Autocomplete
onChange={(event, value) => console.log(value)} // prints the selected value
renderInput={params => (
<TextField {...params} label="Label" variant="outlined" fullWidth />
)}
/>
Run Code Online (Sandbox Code Playgroud)
小智 8
该onChange
属性也适用于多个自动完成值(@Steve Angello @ShwetaJ)。返回value
的是所有选定选项的列表。
const [selectedOptions, setSelectedOptions] = useState([]);
const handleChange = (event, value) => setSelectedOptions(value);
const handleSubmit = () => console.log(selectedOptions);
.
.
.
<Autocomplete
multiple
autoHighlight
id="tags-outlined"
options={top100Films}
getOptionLabel={(option) => option.title}
onChange={handleChange}
filterSelectedOptions
renderInput={(params) => (
<TextField
{...params}
variant="standard"
/>
)}
/>
.
.
.
<button onClick={handleSubmit}>Submit!</button>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
36000 次 |
最近记录: |