根据材料 ui 以前的版本 4 或当前版本 5,您可以使用 autoFocus props简单地聚焦自动完成输入元素,如果 autoFocus 设置为 true,则输入元素将聚焦在自动完成组件的每个第一次安装上。
const [query, setQuery] = useState('');
<Autocomplete
.....
renderInput={(params) => {
const { InputLabelProps, InputProps, ...rest } = params;
return <InputBase
{...params.InputProps}
{...rest}
name="query"
value={query}
onChange={handleSearch}
autoFocus
/>
}}
/>
// 这只是一个例子,您可以根据自己的意愿处理以下功能
function handleOnSearch({ currentTarget = {} }) {
const { value } = currentTarget;
setQuery(value);
}
如果您想在单击按钮后打开自动完成输入:-
//button to be clicked to open autocomplete input
const clickButton=()=>{
setOpen(true)
}
const handleClose =()=>{
setOpen(false)
}
<Dialogue
close={handleClose}
open={open}
>
<DialogActions>
<Autocomplete
.....
renderInput={(params) => {
const { InputLabelProps, InputProps, ...rest } = params;
return <InputBase
{...params.InputProps}
{...rest}
name="query"
value={query}
onChange={handleSearch}
autoFocus
/>
}}
/>
</DialogActions>
</Dialogue>
干杯!!!