React Material UI Card Content expand
Posted
技术标签:
【中文标题】React Material UI Card Content expand【英文标题】: 【发布时间】:2021-06-15 23:25:39 【问题描述】:我正在尝试使用 React & Material UI 在我的网站上放置 3 个不同的可扩展卡片。但是我很难弄清楚如何让每张卡自行扩展?目前,当我单击三个展开按钮中的任何一个时,它们都会展开。任何帮助表示赞赏。谢谢,卡蒂
我的代码是:
import React from 'react';
import makeStyles from '@material-ui/core/styles';
import clsx from 'clsx';
import Card from '@material-ui/core/Card';
import CardContent from '@material-ui/core/CardContent';
import CardActions from '@material-ui/core/CardActions';
import Collapse from '@material-ui/core/Collapse';
import IconButton from '@material-ui/core/IconButton';
import Typography from '@material-ui/core/Typography';
import FavoriteIcon from '@material-ui/icons/Favorite';
import ShareIcon from '@material-ui/icons/Share';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
const useStyles = makeStyles((theme) => (
expand:
transform: 'rotate(0deg)',
marginLeft: 'auto',
transition: theme.transitions.create('transform',
duration: theme.transitions.duration.shortest,
),
,
expandOpen:
transform: 'rotate(180deg)',
));
const IconBoxes = () =>
const classes = useStyles();
const [expanded, setExpanded] = React.useState(false);
const handleExpandClick = () =>
setExpanded(!expanded);
;
return (
<Card className="section section-icons grey lighten-4 center">
<div class="container">
<div class="row">
<div className="col s12 m4">
<div className="card-panel">
<i className="material-icons large teal-text">room</i>
<h4>Pick Where</h4>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.
Corporis, nostrum!</p>
<CardActions disableSpacing>
<IconButton aria-label="add to favorites">
<FavoriteIcon />
</IconButton>
<IconButton aria-label="share">
<ShareIcon />
</IconButton>
<IconButton
className=clsx(classes.expand,
[classes.expandOpen]: expanded,
)
onClick=handleExpandClick
aria-expanded=expanded
aria-label="show more"
>
<ExpandMoreIcon />
</IconButton>
</CardActions>
<Collapse in=expanded timeout="auto" unmountOnExit>
<CardContent>
<Typography paragraph>Method:</Typography>
<Typography paragraph>
Heat 1/2 cup of the broth in a pot until simmering, add saffron and set aside for 10
minutes.
</Typography>
<Typography paragraph>
Heat oil in a (14- to 16-inch) paella pan or a large, deep skillet over medium-high
heat. Add chicken, shrimp and chorizo, and cook, stirring occasionally until lightly
browned, 6 to 8 minutes. Transfer shrimp to a large plate and set aside, leaving chicken
and chorizo in the pan. Add pimentón, bay leaves, garlic, tomatoes, onion, salt and
pepper, and cook, stirring often until thickened and fragrant, about 10 minutes. Add
saffron broth and remaining 4 1/2 cups chicken broth; bring to a boil.
</Typography>
<Typography paragraph>
Add rice and stir very gently to distribute. Top with artichokes and peppers, and cook
without stirring, until most of the liquid is absorbed, 15 to 18 minutes. Reduce heat to
medium-low, add reserved shrimp and mussels, tucking them down into the rice, and cook
again without stirring, until mussels have opened and rice is just tender, 5 to 7
minutes more. (Discard any mussels that don’t open.)
</Typography>
<Typography>
Set aside off of the heat to let rest for 10 minutes, and then serve.
</Typography>
</CardContent>
</Collapse>
</div>
</div>
<div className="col s12 m4">
<div className="card-panel">
<i className="material-icons large teal-text">store</i>
<h4>Travel Shop</h4>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.
Corporis, nostrum!</p>
<CardActions disableSpacing>
<IconButton aria-label="add to favorites">
<FavoriteIcon />
</IconButton>
<IconButton aria-label="share">
<ShareIcon />
</IconButton>
<IconButton
className=clsx(classes.expand,
[classes.expandOpen]: expanded,
)
onClick=handleExpandClick
aria-expanded=expanded
aria-label="show more"
>
<ExpandMoreIcon />
</IconButton>
</CardActions>
<Collapse in=expanded timeout="auto" unmountOnExit>
<CardContent>
<Typography paragraph>Method:</Typography>
<Typography paragraph>
Heat 1/2 cup of the broth in a pot until simmering, add saffron and set aside for 10
minutes.
</Typography>
<Typography paragraph>
Heat oil in a (14- to 16-inch) paella pan or a large, deep skillet over medium-high
heat. Add chicken, shrimp and chorizo, and cook, stirring occasionally until lightly
browned, 6 to 8 minutes. Transfer shrimp to a large plate and set aside, leaving chicken
and chorizo in the pan. Add pimentón, bay leaves, garlic, tomatoes, onion, salt and
pepper, and cook, stirring often until thickened and fragrant, about 10 minutes. Add
saffron broth and remaining 4 1/2 cups chicken broth; bring to a boil.
</Typography>
<Typography paragraph>
Add rice and stir very gently to distribute. Top with artichokes and peppers, and cook
without stirring, until most of the liquid is absorbed, 15 to 18 minutes. Reduce heat to
medium-low, add reserved shrimp and mussels, tucking them down into the rice, and cook
again without stirring, until mussels have opened and rice is just tender, 5 to 7
minutes more. (Discard any mussels that don’t open.)
</Typography>
<Typography>
Set aside off of the heat to let rest for 10 minutes, and then serve.
</Typography>
</CardContent>
</Collapse>
</div>
</div>
<div className="col s12 m4">
<div className="card-panel">
<i className="material-icons large teal-text">airplanemode_active</i>
<h4>Fly Cheap</h4>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.
Corporis, nostrum!</p>
<CardActions disableSpacing>
<IconButton aria-label="add to favorites">
<FavoriteIcon />
</IconButton>
<IconButton aria-label="share">
<ShareIcon />
</IconButton>
<IconButton
className=clsx(classes.expand,
[classes.expandOpen]: expanded,
)
onClick=handleExpandClick
aria-expanded=expanded
aria-label="show more"
>
<ExpandMoreIcon />
</IconButton>
</CardActions>
<Collapse in=expanded timeout="auto" unmountOnExit>
<CardContent>
<Typography paragraph>Method:</Typography>
<Typography paragraph>
Heat 1/2 cup of the broth in a pot until simmering, add saffron and set aside for 10
minutes.
</Typography>
<Typography paragraph>
Heat oil in a (14- to 16-inch) paella pan or a large, deep skillet over medium-high
heat. Add chicken, shrimp and chorizo, and cook, stirring occasionally until lightly
browned, 6 to 8 minutes. Transfer shrimp to a large plate and set aside, leaving chicken
and chorizo in the pan. Add pimentón, bay leaves, garlic, tomatoes, onion, salt and
pepper, and cook, stirring often until thickened and fragrant, about 10 minutes. Add
saffron broth and remaining 4 1/2 cups chicken broth; bring to a boil.
</Typography>
<Typography paragraph>
Add rice and stir very gently to distribute. Top with artichokes and peppers, and cook
without stirring, until most of the liquid is absorbed, 15 to 18 minutes. Reduce heat to
medium-low, add reserved shrimp and mussels, tucking them down into the rice, and cook
again without stirring, until mussels have opened and rice is just tender, 5 to 7
minutes more. (Discard any mussels that don’t open.)
</Typography>
<Typography>
Set aside off of the heat to let rest for 10 minutes, and then serve.
</Typography>
</CardContent>
</Collapse>
</div>
</div>
</div>
</div>
</Card>
);
export default IconBoxes;
【问题讨论】:
【参考方案1】:您正在使用相同的状态 expanded
来控制卡的所有三个 <Collapse>
部分。如果你展开一张,状态就会改变,所有三张牌都会受到影响。
要允许每个部分分别展开/折叠,您需要为每个部分设置单独的状态和点击处理函数。
即
<...>
const IconBoxes = () =>
const [expanded1, setExpanded1] = React.useState(false);
const [expanded2, setExpanded2] = React.useState(false);
const [expanded3, setExpanded3] = React.useState(false);
const handleExpandClick1 = () =>
setExpanded(!expanded1);
;
const handleExpandClick2 = () =>
setExpanded(!expanded2);
;
const handleExpandClick3 = () =>
setExpanded(!expanded3);
;
<...>
<IconButton onClick=handleExpandClick1>
<Collapse in=expanded1>
<...>
<IconButton onClick=handleExpandClick2>
<Collapse in=expanded2>
<...>
<IconButton onClick=handleExpandClick3>
<Collapse in=expanded3>
<...>
【讨论】:
非常感谢!现在效果很好!以上是关于React Material UI Card Content expand的主要内容,如果未能解决你的问题,请参考以下文章