【问题标题】:Expanding individual Cards with React + Material UI Card Expander使用 React + Material UI Card Expander 扩展单个卡片
【发布时间】:2020-05-13 06:00:05
【问题描述】:

我正在使用 React + Material UI 创建 3 张带有图像的可扩展卡片。一切正常,我可以渲染卡片并且它们(几乎)按照我想要的方式扩展。问题是:所有卡片都在同时展开我希望只有一张卡片在用户点击时展开

我知道我应该以某种方式使用状态来处理这个问题,但我不知道该怎么做。有什么见解吗?请随时分享代码或为我指明正确的方向。

CodeSandBox 以防万一有人想试一试(请在编码之前分叉!): https://codesandbox.io/s/mutable-dust-lwljo

这是我的卡片组件:

/* eslint-disable linebreak-style */
/* eslint-disable react/prop-types */
/* eslint-disable semi */
/* eslint-disable linebreak-style */
import React from 'react'
import CardHeader from '@material-ui/core/CardHeader'
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 ExpandMoreIcon from '@material-ui/icons/ExpandMore'
import { CardLayout, CardImg } from './SiteCardStyled'


export class SiteCard extends React.Component {
    constructor(props) {
        super(props)
        this.state ={
            expanded: false,
        }
    }

    handleExpandClick = () => {
        this.setState({expanded: !this.state.expanded})
    };

    render() {

        let expanded = this.state.expanded

        return <CardLayout>
            <CardHeader
                title={this.props.cardtitle}
                subheader={this.props.cardsubheader}
            />
            <CardImg
                image={this.props.media}
                title={this.props.mediatitle}
            />
            <CardContent>
                <Typography variant="body2" color="textSecondary" component="p">
                    {this.props.carddescription}
                </Typography>
            </CardContent>
            <CardActions disableSpacing>
                <IconButton aria-label="add to favorites">
                    <FavoriteIcon />
                </IconButton>
                <IconButton
                    onClick={this.handleExpandClick}
                    //AQUI
                    aria-expanded={expanded}
                    aria-label="Ver mais"
                >
                    <ExpandMoreIcon />
                </IconButton>
            </CardActions>
            {/* aqui */}
            <Collapse in={expanded} timeout="auto" unmountOnExit>
                <CardContent>
                    <Typography paragraph>{this.props.cardexpandedtitle}</Typography>
                    <Typography paragraph>
                        {this.props.cardexpandedtext}
                    </Typography>
                </CardContent>
            </Collapse>
        </CardLayout>
    }
}

这里是我调用组件 + 道具的地方:

/* eslint-disable linebreak-style */
import React from 'react'
import { MainPage, MainPageWrapper, SiteCardArea } from './AppContainerStyled'
import { MenuTop } from './MenuTop/MenuTop'
import { MainText } from './MainText/MainText'
import { SiteCard } from './SiteCard/SiteCard'

export class AppContainer extends React.Component {
    constructor(props) {
        super(props)
    }


    render() {

        return <MainPage>

            <MainPageWrapper>

                <MenuTop></MenuTop>
                <MainText></MainText>
                <SiteCardArea>
                    <SiteCard
                        cardtitle={'title'}
                        cardsubheader={'subheader'}
                        mediatitle={'mediatitle'}
                        media={'https://i.picsum.photos/id/682/300/300.jpg'}
                        carddescription={'sitedescription'}
                        cardexpandedtitle={'cardexpandedtitle'}
                        cardexpandedtext={'cardexpandedtext'}
                    ></SiteCard>
                    <SiteCard
                        cardtitle={'title2'}
                        cardsubheader={'subheader2'}
                        mediatitle={'mediatitle2'}
                        media={'https://i.picsum.photos/id/680/300/300.jpg'}
                        carddescription={'sitedescription2'}
                        cardexpandedtitle={'cardexpandedtitle2'}
                        cardexpandedtext={'cardexpandedtext2'}
                    ></SiteCard>
                    <SiteCard
                        cardtitle={'title3'}
                        cardsubheader={'subheader3'}
                        mediatitle={'mediatitle3'}
                        media={'https://i.picsum.photos/id/672/300/300.jpg'}
                        carddescription={'sitedescription3'}
                        cardexpandedtitle={'cardexpandedtitle3'}
                        cardexpandedtext={'cardexpandedtext3'}
                    ></SiteCard>
                </SiteCardArea>

            </MainPageWrapper>

        </MainPage>

    }
}

也许每张卡片都应该有一个状态?但如果是这样的话,我应该创建一个不同的函数来处理每张卡吗?我知道我可能越来越接近答案,但我被困住了。任何人都可以帮忙吗?非常感谢!

【问题讨论】:

    标签: reactjs material-ui expand


    【解决方案1】:

    将卡片的标题保存在状态中,因为它在您的情况下看起来是独一无二的,然后根据该标题打开特定的卡片。也许将其命名为activeTitle

    【讨论】:

    • 解决了!毕竟问题不在州。它在每张卡片的父容器的大小中 - 随着卡片的大小在扩展时增加,每张卡片也会增加它的大小,看起来它也被“展开”了。我只需将height: 100% 添加到卡片组件中即可。还是谢谢!
    猜你喜欢
    • 2016-09-14
    • 2020-03-16
    • 2021-06-15
    • 2021-02-13
    • 2020-04-15
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2021-03-18
    相关资源
    最近更新 更多