This section briefly explains you the steps required to create a simple Accordion and demonstrate the basic usage of the Accordion control.
The following list of dependencies are required to use the React 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
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
React
sample use following commands.create-react-app quickstart --scripts-version=react-scripts-ts
cd quickstart
create-react-app quickstart
cd quickstart
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
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 thesrc/App.tsx
file.
The React Accordion can be rendered by defining an array of items
.
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"));
npm start
Output will be as follows:
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"));
<!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/20.1.58/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>
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"));
In the above sample code,
#element
is theid
of the HTML element in a page to which the Accordion is initialized.
The React 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>
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"));
<!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/20.1.58/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>
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.