【发布时间】:2020-07-19 11:41:41
【问题描述】:
我在 Material-UI 中有一个弹出框,我想将锚点永久设置为按钮。不仅仅是点击event.currentTarget。
我怎样才能用打字稿做到这一点?
不幸的是,Material-UI 中的当前示例使用 event.currentTarget 并且引用它不起作用。
import React,{useRef} from 'react';
import { makeStyles, createStyles, Theme } from '@material-ui/core/styles';
import Popover from '@material-ui/core/Popover';
import Typography from '@material-ui/core/Typography';
import Button from '@material-ui/core/Button';
const useStyles = makeStyles((theme: Theme) =>
createStyles({
typography: {
padding: theme.spacing(2),
},
}),
);
export default function SimplePopover() {
const classes = useStyles();
const ref = useRef(null)
return (
<div>
<Button ref={ref} variant="contained" color="primary">
Open Popover
</Button>
<Popover
open
anchorEl={ref}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'center',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'center',
}}
>
<Typography className={classes.typography}>The content of the Popover.</Typography>
</Popover>
</div>
);
}
Here 一个代码框。
【问题讨论】:
标签: reactjs material-ui popover