Search results

Getting Started

This section briefly explains you the steps required to create a simple Accordion and demonstrate the basic usage of the Accordion control.

Dependencies

The following list of dependencies are required to use the Accordion component in your application.

|-- @syncfusion/ej2-navigations
    |-- @syncfusion/ej2-base

Installation and configuration

You can use Create-react-app to setup the applications. To install create-react-app run the following command.

npm install -g create-react-app

Start a new project using create-react-app command as follows

create-react-app quickstart --scripts-version=react-scripts-ts

cd quickstart

Install the below required dependency package in order to use the Accordion component in your application.

npm install @syncfusion/ej2-react-navigations –save

The above package installs Accordion dependencies which are required to render the Accordion component in React environment.

  • Accordion CSS files are available in the ej2-react-navigations package folder. Import the Accordion component’s required CSS references as follows in src/App.css.
@import '../../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-react-navigations/styles/material.css';

Initialize the Accordion using Items

The Accordion can be rendered by defining an array of items.

  • Import the Accordion component to your src/App.tsx file using following code.
import { AccordionComponent, AccordionItemDirective, AccordionItemsDirective  } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class ReactApp extends React.Component<{}, {}> {
  public content0() {
    return <div>
        Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web services.
      </div>;
  }
  public content1() {
    return <div>
        The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller.
      </div>;
  }
  public content2() {
    return <div>
        JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter the document content that was displayed.
      </div>;
  }
  public render() {
    return (
        <AccordionComponent>
            <AccordionItemsDirective>
              <AccordionItemDirective expanded='true' header='ASP.NET' content={this.content0} />
              <AccordionItemDirective header='ASP.NET MVC' content={this.content1} />
              <AccordionItemDirective header='JavaScript' content={this.content2} />
            </AccordionItemsDirective>
        </AccordionComponent>
    );
  }
}
ReactDOM.render(<ReactApp/>, document.getElementById("element"));
  • Run the application in the browser using the following command.
npm start

Output will be as follows:

Source
Preview
index.tsx
index.html
import { AccordionComponent, AccordionItemDirective, AccordionItemsDirective  } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class ReactApp extends React.Component<{}, {}> {
  public content0() {
    return <div>
        Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web services.
      </div>;
  }
  public content1() {
    return <div>
        The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller.
      </div>;
  }
  public content2() {
    return <div>
        JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter the document content that was displayed.
      </div>;
  }
  public render() {
    return (
        <AccordionComponent>
            <AccordionItemsDirective>
              <AccordionItemDirective expanded='true' header='ASP.NET' content={this.content0} />
              <AccordionItemDirective header='ASP.NET MVC' content={this.content1} />
              <AccordionItemDirective header='JavaScript' content={this.content2} />
            </AccordionItemsDirective>
        </AccordionComponent>
    );
  }
}
ReactDOM.render(<ReactApp/>, document.getElementById("element"));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 React Accordion Sample</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="//cdn.syncfusion.com/ej2/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <style>
        #loader {
            color: #008cff;
            height: 40px;
            left: 45%;
            position: absolute;
            top: 45%;
            width: 30%;
        }
    </style>
</head>

<body>
    <div id='element'>
        <div id='loader'>Loading....</div></div>
</body>
</html>

In the above sample code, #element is the id of the HTML element in a page to which the Accordion is initialized.

Initialize the Accordion using HTML elements

The Accordion component can be rendered based on the given HTML element using id as target property. You need to follow the below structure of HTML elements to render the Accordion.

  <div id='accordion_html_markup'>   --> Root Accordion Element
       <div>      --> Accordion Item Container
            <div>   --> Accordion Header Container
                <div> </div> --> Accordion Header
            </div>
            <div>  --> Accordion Panel Container
                <div> </div> --> Accordion Content
             </div>
        </div>
  </div>
Source
Preview
index.tsx
import { AccordionComponent } from '@syncfusion/ej2-react-navigations';
import * as React from "react";
import * as ReactDOM from "react-dom";

export default class ReactApp extends React.Component<{}, {}> {
  public render() {
    return (
      <AccordionComponent>
        <div>
          <div>
            <div> ASP.NET </div>
          </div>
          <div>
            <div>
              Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web services.
            </div>
          </div>
        </div>
        <div>
          <div>
            <div> ASP.NET MVC </div>
          </div>
          <div>
            <div>
              The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller.
            </div>
          </div>
        </div>
        <div>
          <div>
            <div> JavaScript </div>
          </div>
          <div>
            <div>
              JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser.
            </div>
          </div>
        </div>
      </AccordionComponent>
    );
  }
}
ReactDOM.render(<ReactApp />, document.getElementById("element"));

You can add the custom class into Accordion component using cssClass property which is used to customize the Accordion component.

See Also