我使用循环为此选项卡创建了卡片,数据来自数据库。 当我单击“显示更多”按钮时,整个可见列都会随之展开。 它不显示它刚刚扩展的内容;当我单击“显示更多”时,内容可见。 但如果该行中只有一张卡,它只会扩展该卡。
import React, { useState } from "react";
import {
Box,
Card,
CardActions,
CardContent,
Collapse,
Button,
Typography,
Rating,
useTheme,
useMediaQuery,
} from "@mui/material";
import Header from "../../components/Header";
import { useGetProductsQuery } from "../../state/api";
import LinearProgress from "@mui/material/LinearProgress";
const Product = ({
_id,
name,
description,
price,
rating,
category,
supply,
stat,
}) => {
const theme = useTheme();
const [isExpanded, setIsExpanded] = useState(false);
return (
<Card
sx={{
backgroundImage: "none",
backgroundColor: theme.palette.background.alt,
borderRadius: "0.55rem",
}}
>
<CardContent>
<Typography
sx={{ fontSize: 14 }}
color={theme.palette.secondary[700]}
gutterBottom
>
{category}
</Typography>
<Typography variant="h5" component="div">
{name}
</Typography>
<Typography sx={{ mb: "1.5rem" }} color={theme.palette.secondary[400]}>
${Number(price).toFixed(2)}
</Typography>
<Rating value={rating} readOnly />
<Typography variant="body2">{description}</Typography>
</CardContent>
<CardActions>
<Button
variant="primary"
size="small"
onClick={() => setIsExpanded(!isExpanded)}
sx={{ backgroundColor: theme.palette.secondary[600] }}
>
See More
</Button>
</CardActions>
<Collapse
in={isExpanded}
timeout="auto"
unmountOnExit
sx={{
color: theme.palette.neutral[300],
}}
>
<CardContent>
<Typography>id: {_id}</Typography>
<Typography>Supply Left: {supply}</Typography>
<Typography>
Yearly Sales This Year: {stat[0].yearlySalesTotal}
</Typography>
<Typography>
Yearly Units Sold This Year: {stat[0].yearlyTotalSoldUnits}
</Typography>
</CardContent>
</Collapse>
</Card>
);
};
const Products = () => {
const { data, isLoading } = useGetProductsQuery();
const isNonMobile = useMediaQuery("(min-width: 1000px)");
return (
<Box m="1.5rem 2.5rem">
<Header title="PRODUCTS" subtitle="See your list of products." />
{data || !isLoading ? (
<Box
mt="20px"
display="grid"
gridTemplateColumns="repeat(4, minmax(0, 1fr))"
justifyContent="space-between"
rowGap="20px"
columnGap="1.33%"
sx={{
"& > div": { gridColumn: isNonMobile ? undefined : "span 4" },
}}
>
{data.map(
({
_id,
name,
description,
price,
rating,
category,
supply,
stat,
}) => (
<Product
key={_id}
_id={_id}
name={name}
description={description}
price={price}
rating={rating}
category={category}
supply={supply}
stat={stat}
/>
)
)}
</Box>
) : (
<>
<LinearProgress
color="primary"
fourcolor="false"
variant="indeterminate"
/>
</>
)}
</Box>
);
};
export default Products;
我希望它一次只能扩展一张卡
在
isExpanded
组件内创建 Product
,这应该为每个 Product
提供其自己的独立状态。
const Product = ({
_id,
name,
description,
price,
rating,
category,
supply,
stat,
}) => {
const theme = useTheme();
const [isExpanded, setIsExpanded] = useState(false);
return (
<Card
sx={{
backgroundImage: 'none',
backgroundColor: theme.palette.background.paper,
borderRadius: '0.55rem',
}}
>
<CardContent>
<Typography sx={{ fontSize: 14 }} color="text.secondary" gutterBottom>
{category}
</Typography>
<Typography variant="h5" component="div">
{name}
</Typography>
<Typography sx={{ mb: '1.5rem' }} color="text.secondary">
${Number(price).toFixed(2)}
</Typography>
<Rating value={rating} readOnly />
<Typography variant="body2">{description}</Typography>
</CardContent>
<CardActions>
<Button
variant="contained"
size="small"
onClick={() => setIsExpanded((prev) => !prev)}
sx={{ backgroundColor: theme.palette.secondary.main }}
>
See More
</Button>
</CardActions>
<Collapse
in={isExpanded}
timeout="auto"
unmountOnExit
sx={{
color: theme.palette.text.primary,
}}
>
<CardContent>
<Typography>id: {_id}</Typography>
<Typography>Supply Left: {supply}</Typography>
<Typography>
Yearly Sales This Year: {stat[0].yearlySalesTotal}
</Typography>
<Typography>
Yearly Units Sold This Year: {stat[0].yearlyTotalSoldUnits}
</Typography>
</CardContent>
</Collapse>
</Card>
);
};
这是实时代码示例:Stackblitz