Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accordion - Open first tab from a mapped array - React TS

{accordion.map(accordionItem => (
                <AccordionItem
                    key={accordionItem.title}
                    text={accordionItem.text}
                    title={accordionItem.title}
                ></AccordionItem>
            ))}

I have an Accordion component that maps over an array of data. I am trying to open just the first tab. There are properties you can add to default expand all or none, but wondering how to do this on the first tab? Material UI also has customised Accordions but they are focused when all data is in one file and not mapped through an array.

<Accordion className={classes.accordion} defaultExpanded={false}>
        <AccordionSummary
            expandIcon={<ExpandMoreIcon />}
            aria-controls='panel1a-content'
            id='panel1a-header'
            style={{
                borderBottom: '2px solid #EBEDF7'
            }}
        >
            <Typography className={classes.heading}>{title}</Typography>
        </AccordionSummary>
        <AccordionDetails>
            <Text label={text} className={classes.body} html>
                {text}
            </Text>
        </AccordionDetails>
    </Accordion>
like image 276
conye9980 Avatar asked Dec 05 '25 18:12

conye9980


1 Answers

This seem to work for me with MUI v5 to open first item (summary) of the accordion

...

          summaries.map((summary, index) => (
            <MuiAccordion
              defaultExpanded={index === 0 ? true : false}                  
              key={summary.id}
            >

...

like image 96
atazmin Avatar answered Dec 08 '25 08:12

atazmin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!