我目前正在react項目中實現Material-UI庫的Accordion組件:
我想做的是有手風琴摘要(標題與& quot手風琴1 & quot,& quot手風琴2 & quot)居中,展開圖標(箭頭)在右下方,也居中。
在Material-UI文檔(https://Material-UI . com/components/accordion/)中,顯示的布局如下所示:
使用以下代碼(如文檔中所示):
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Accordion from '@material-ui/core/Accordion';
import AccordionSummary from '@material-ui/core/AccordionSummary';
import AccordionDetails from '@material-ui/core/AccordionDetails';
import Typography from '@material-ui/core/Typography';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
const useStyles = makeStyles((theme) => ({
root: {
width: '100%',
},
heading: {
fontSize: theme.typography.pxToRem(15),
fontWeight: theme.typography.fontWeightRegular,
},
}));
export default function SimpleAccordion() {
const classes = useStyles();
return (
<div className={classes.root}>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
aria-controls="panel1a-content"
id="panel1a-header"
>
<Typography className={classes.heading}>Accordion 1</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex,
sit amet blandit leo lobortis eget.
</Typography>
</AccordionDetails>
</Accordion>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
aria-controls="panel2a-content"
id="panel2a-header"
>
<Typography className={classes.heading}>Accordion 2</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex,
sit amet blandit leo lobortis eget.
</Typography>
</AccordionDetails>
</Accordion>
<Accordion disabled>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
aria-controls="panel3a-content"
id="panel3a-header"
>
<Typography className={classes.heading}>Disabled Accordion</Typography>
</AccordionSummary>
</Accordion>
</div>
);
}
謝謝大家!
將摘要內容的flex-grow從1更改為0似乎就足夠了:
import { withStyles } from "@material-ui/core/styles";
import MuiAccordionSummary from "@material-ui/core/AccordionSummary";
const AccordionSummary = withStyles({
content: {
flexGrow: 0
}
})(MuiAccordionSummary);
如果您希望箭頭在摘要文本的下方居中(而不是像我的第一個例子中那樣在它旁邊),那么您可以使用下面的代碼:
const AccordionSummary = withStyles({
root: {
flexDirection: "column"
},
content: {
marginBottom: 0
},
expandIcon: {
marginRight: 0,
paddingTop: 0
}
})(MuiAccordionSummary);
第二個示例還演示了如何使用SimpleAccordion的類(如注釋中所請求的那樣),使用withStyles而不是makeStyles/useStyles來實現示例中的附加樣式。