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-react-navigations
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-react-base
    |-- @syncfusion/ej2-navigations
        |-- @syncfusion/ej2-buttons
        |-- @syncfusion/ej2-popups

Setup for Local Development

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
  • To setup basic React sample use following commands.
create-react-app quickstart --scripts-version=react-scripts-ts

cd quickstart
create-react-app quickstart

cd quickstart

Adding Syncfusion packages

All the available Essential JS 2 packages are published in npmjs.com public registry. To install Accordion component, use the following command

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

Adding CSS reference

Add components style as given below in src/App.css.

@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-react-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-react-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-react-navigations/styles/material.css';

To refer App.css in the application then import it in the src/App.tsx file.

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.jsx
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 {
    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>;
    }
    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>;
    }
    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>;
    }
    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"));
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.jsx
index.tsx
index.html
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 {
    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"));
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"));
<!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>

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

See Also