【发布时间】:2018-08-07 00:32:09
【问题描述】:
我在一个 React 项目中使用Material UI Next。我有卡片组件,里面有一个图像(卡片媒体)和文本(卡片文本)。我在文本下方也有一个按钮。我的问题是..如何使整张卡片可点击? IE。无论用户按下卡片文本、卡片图像还是按钮,它都应该触发我在按钮上调用的 onClick 事件。
【问题讨论】:
标签: javascript css reactjs material-design material-ui
我在一个 React 项目中使用Material UI Next。我有卡片组件,里面有一个图像(卡片媒体)和文本(卡片文本)。我在文本下方也有一个按钮。我的问题是..如何使整张卡片可点击? IE。无论用户按下卡片文本、卡片图像还是按钮,它都应该触发我在按钮上调用的 onClick 事件。
【问题讨论】:
标签: javascript css reactjs material-design material-ui
您可以将onClick={clickFunction} 添加到卡片的包含 div 中,该 div 链接到与按钮相同的功能。
【讨论】:
v3 更新 - 2018 年 8 月 29 日
在Material UI 3.0.0 版中添加了一个特定的CardActionArea component 来专门涵盖这种情况。
请仅在您使用 v1 时使用以下解决方案。
您可能想要实现的是卡片顶部的Card Action (see specification)。
Web 库的 Material Components 将其作为 first usage example for the Card Component。
您可以通过将 MUI Card* 组件与强大的 ButtonBase 组件组合来轻松重现该确切行为。 可以在here on CodeSandbox: https://codesandbox.io/s/q9wnzv7684找到一个运行示例。
相关代码是这样的:
import Card from '@material-ui/core/Card';
import CardActions from '@material-ui/core/CardActions';
import CardContent from '@material-ui/core/CardContent';
import CardMedia from '@material-ui/core/CardMedia';
import Typography from '@material-ui/core/Typography';
import ButtonBase from '@material-ui/core/ButtonBase';
const styles = {
cardAction: {
display: 'block',
textAlign: 'initial'
}
}
function MyCard(props) {
return (
<Card>
<ButtonBase
className={props.classes.cardAction}
onClick={event => { ... }}
>
<CardMedia ... />
<CardContent>...</CardContent>
</ButtonBase>
</Card>
);
}
export default withStyles(styles)(MyCard)
另外我强烈建议将CardActions 组件保留在ButtonBase 之外。
【讨论】:
width: 100%; height: 100% 添加到 ButtonBase 和 CardContent 中。
我们也可以使用 Link 标签来使整个 Card 组件可点击和导航
import { Link } from 'react-router-dom';
function myCard() {
return (
<Link to={'/give_your_path'}>
<Card>
<Card text="This is text"/>
</Card>
</Link>
);
}
【讨论】:
只需将整个东西包装在 Material CardActionArea 组件中。里面的所有东西都可以点击。
<CardActionArea>
<CardMedia>
.......Image Stuff
</CardMedia>
<CardContent>
.......Content
</CardContent>
</CardActionArea>
【讨论】:
感谢https://stackoverflow.com/a/50444524/192092,这是对我们有用的解决方案
import { Link as RouterLink } from 'react-router-dom'
import Link from '@material-ui/core/Link'
<Link underline='none' component={RouterLink} to='/your-target-path'>
<Card>
<CardActionArea>
...
</CardActionArea>
</Card>
</Link>
【讨论】:
使用 Material UI 4.9.10,这可以工作。
<Card>
<CardActionArea href="https://google.com">
<CardContent>
<Typography>Click me!</Typography>
</CardContent>
</CardActionArea>
</Card>
如果您使用的是 react 路由器,这也可以。
<Card>
<CardActionArea component={RouterLink} to="/questions">
<CardContent>
<Typography>Click me!</Typography>
</CardContent>
</CardActionArea>
</Card>
【讨论】:
target="_blank"
只需在您的卡片中添加 onPress 道具
<Card
onPress = {() => {console.log('onclick')}}
style={styles.item}
status="basic"
header={(headerProps) =>
this.renderItemHeader(headerProps, item)
}>
<Text>{item.description}</Text>
</Card>
【讨论】:
像这样使用 onClick 事件。
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Card from '@material-ui/core/Card';
import CardActions from '@material-ui/core/CardActions';
import CardContent from '@material-ui/core/CardContent';
import Button from '@material-ui/core/Button';
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
},
card: {
cursor: "pointer",
padding: theme.spacing(2),
textAlign: 'center',
color: theme.palette.text.secondary,
},
}));
function App() {
const classes = useStyles();
const clickMe = (event) => {
console.log(event);
}
return (
<div className={classes.root}>
<Card className={classes.card} onClick={(event) =>
{clickMe(event)}}>
<CardContent>
<h4>test</h4>
</CardContent>
<CardActions>
<Button size="small">Learn More</Button>
</CardActions>
</Card>
</div>
);
}
export default App;
【讨论】: