gpt4 book ai didi

inheritance - 如何以编程方式 'open' Material-UI 选择字段?

转载 作者:行者123 更新时间:2023-12-03 13:17:04 25 4
gpt4 key购买 nike

可以在此处找到选择字段:in the Material-UI demo

它的方法似乎是从菜单/弹出窗口类继承的,但我无法弄清楚如何在 onFocus 事件触发时触发“打开”。这将解决我与键盘相关的可用性问题!

最佳答案

看例子https://material-ui.com/components/selects/#controlled-open-select

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import InputLabel from '@material-ui/core/InputLabel';
import MenuItem from '@material-ui/core/MenuItem';
import FormControl from '@material-ui/core/FormControl';
import Select from '@material-ui/core/Select';
import Button from '@material-ui/core/Button';

const useStyles = makeStyles((theme) => ({
button: {
display: 'block',
marginTop: theme.spacing(2),
},
formControl: {
margin: theme.spacing(1),
minWidth: 120,
},
}));

export default function ControlledOpenSelect() {
const classes = useStyles();
const [age, setAge] = React.useState('');
const [open, setOpen] = React.useState(false);

const handleChange = (event) => {
setAge(event.target.value);
};

const handleClose = () => {
setOpen(false);
};

const handleOpen = () => {
setOpen(true);
};

return (
<div>
<Button className={classes.button} onClick={handleOpen}>
Open the select
</Button>
<FormControl className={classes.formControl}>
<InputLabel id="demo-controlled-open-select-label">Age</InputLabel>
<Select
labelId="demo-controlled-open-select-label"
id="demo-controlled-open-select"
open={open}
onClose={handleClose}
onOpen={handleOpen}
value={age}
onChange={handleChange}
>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
</FormControl>
</div>
);
}

关于inheritance - 如何以编程方式 'open' Material-UI 选择字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35585727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com