Search code examples
javascriptreactjssemantic-uisemantic-ui-react

How to make nested Accordion?


In semantic_ui-react library exists component Accordion. There are some examples, but I have to make nested accordion, which has structure:

-accordion:
    -sub_accordion1
    -sub_accordion2
    -sub_accordion3

I saw there is example of nested accordion with panels, but I don't want to put accordions into the panels. I tried so:

export default class AccordionExampleStandard extends Component {
  state = { activeIndex: 0 }

  handleClick = (e, titleProps) => {
    const { index } = titleProps
    const { activeIndex } = this.state
    const newIndex = activeIndex === index ? -1 : index

    this.setState({ activeIndex: newIndex })
  }

  render() {
    const { activeIndex } = this.state

    return (
      <Accordion>
        <Accordion.Title active={activeIndex === 0} index={0} onClick={this.handleClick}>
          <Icon name='dropdown' />
          What is a dog?
        </Accordion.Title>
        <Accordion.Content active={activeIndex === 0}>
          <Accordion.Title active={activeIndex === 1} index={1} onClick={this.handleClick}>
          <Icon name='dropdown' />
          What kinds of dogs are there?
        </Accordion.Title>
        <Accordion.Content active={activeIndex === 1}>
          Content1
        </Accordion.Content>

        <Accordion.Title active={activeIndex === 2} index={2} onClick={this.handleClick}>
          <Icon name='dropdown' />
          How do you acquire a dog?
        </Accordion.Title>
        <Accordion.Content active={activeIndex === 2}>
          Content2
        </Accordion.Content>
<Accordion.Title active={activeIndex === 3} index={3} onClick={this.handleClick}>
          <Icon name='dropdown' />
          What kinds of dogs are there?
        </Accordion.Title>
        <Accordion.Content active={activeIndex === 3}>
         Content3
        </Accordion.Content>
        </Accordion.Content>
      </Accordion>
    )
  }
}

It renders component, but I can collapse only main accordion and can't collapse it's children. How can I perform this feature?


Solution

  • Since you mentioned that you need a simple solution, it is better to use panels because they seem to automatically manipulate the activeIndex which is the main attribute that decides the expansion and collapse of the accordion. If you were to do it yourself like above you need to maintain all the hierarchy and manipulate yourself by adjusting state in the onClick handler. So, if your use case is not custom or complex you can try something like below.

    Here is a working demo NESTED ACCORDION SEMANTIC UI

    import { Accordion } from 'semantic-ui-react';
    
    const AccordionContent = (content) => (
      <div className="indent">
        {content}
      </div>
    )
    
    const SubAccordion1Panels = [
      {
        title: 'Sub Accordion 11',
        content: { content: AccordionContent('11 Content'), key: '11-content'} ,
        key: 'sub-accordion-11'
      }, {
        title: 'Sub Accordion 12',
        content: { content: AccordionContent('12 Contents'), key: '12-content' },
        key: 'sub-accordion-12'
      }, {
        title: 'Sub Accordion 13',
        content: { content: AccordionContent('13 Contents'), key: '13-content' },
        key: 'sub-accordion-13'
      },
    ]
    
    const SubAccordion1Content = (
      <div className="indent">
        <Accordion.Accordion
          style={{marginLeft: "20px"}}
          className="no-padding"
          panels={SubAccordion1Panels}
        />
      </div>
    )
    
    const SubAccordionPanels = [
      {
        title: 'Sub Accordion 1',
        content: { content: SubAccordion1Content, key: 'sa1-content' },
        key: 'sub-accordion-1'
      }, {
        title: 'Sub Accordion 2',
        content: { content: AccordionContent('SA2 Content'), key: 'sa2-content' },
        key: 'sub-accordion-2'
      }, {
        title: 'Sub Accordion 3',
        content: { content: AccordionContent('SA3 Content'), key: 'sa3-content' },
        key: 'sub-accordion-3'
      }
    ]
    
    const SubAccordions = (
      <div className="indent">
        <Accordion.Accordion
          className="no-padding"
          panels={SubAccordionPanels}
        />
      </div>
    )
    
    const AccordionPanels = [
      { title: 'Accordion', content: { content: SubAccordions, key: 'sub-accordions' } },
    ]
    
    const AccordionExampleNested = () => (
      <Accordion
        defaultActiveIndex={0}
        panels={AccordionPanels}
      />
    )
    
    class App extends React.Component {
      render() {
        return (
          <AccordionExampleNested />
        )
      }
    }
    

    Also for the indentation, you need to override the default css

    .indent {
      margin-left: 1em;
    }
    
    .no-padding {
      padding: 0px !important;
      margin: 0px !important;
    }