Search results

Load Tab items dynamically in React Tabs component

Tabs can be added dynamically by passing array of items and index value to the addTab method.

    // New tab title and content inputs are fetched and stored in local variable
    let title: string = document.getElementById('tab-title').value;
    let content: string = document.getElementById('tab-content').value;

    // Required tab item object formed by using textbox inputs
    let item: Object =  { header: { text: title }, content: createElement('pre', { innerHTML: content.replace(/\n/g, '<br>\n') }).outerHTML };

    // Item object and the index argument passed into the addTab method to add a new tab
    this.addTab([item], this.totalItems-1);

In the following demo, you can add the tab content by clicking the +. This + icon is added on the tab header using iconCss property. Enter the new Tab heading and content details in the available text boxes, click ‘Add Tab’ button to pass the details as an array and here last index is calculated to append the new tab at the end.

Source
Preview
index.tsx
index.html
index.css.jsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { TabComponent, TabItemDirective, TabItemsDirective, SelectEventArgs } from '@syncfusion/ej2-react-navigations';
import { Tooltip } from '@syncfusion/ej2-popups';
import { enableRipple, createElement } from '@syncfusion/ej2-base';

enableRipple(true);

export default class App extends React.Component<{}, {}> {
  public tabInstance: TabComponent | any;
  public totalItems: number | any;

  public tabCreated(): void {
    const tooltip: Tooltip = new Tooltip({
      content: 'Add Tab'
    });
    tooltip.appendTo('.e-ileft.e-icon');

    (document.getElementById('btn-add') as any).onclick = (e : Event) => {
      const title: string = (document.getElementById('tab-title') as any).value;
      const content: string = (document.getElementById('tab-content') as any).value;
// tslint:disable-next-line: ban-types
      const item: Object =  { header: { text: title }, content: React.createElement('pre', { innerHTML: content.replace(/\n/g, '<br>\n') }).outerHTML };
      const totalItems = document.querySelectorAll('#element .e-toolbar-item').length;
      (document.getElementById('tabelement') as any).ej2_instances[0].addTab([item], totalItems-1);
    };
  }

  public tabSelected(args: SelectEventArgs): any {
    if (args.selectedIndex === document.querySelectorAll('#element .e-toolbar-item').length -1) {
      (document.getElementById('tab-title') as any).value = '';
      (document.getElementById('tab-content') as any).value = '';
    }
  }

  public render() {
    let headertext: any;
    headertext = [{ text: "Twitter"}, { 'iconCss': 'e-add-icon' } ];

    return (
      <TabComponent id='tabelement' ref={tab => this.tabInstance = tab} created={this.tabCreated}  selected={this.tabSelected.bind(this)}>
        <TabItemsDirective>
          <TabItemDirective header= { headertext[0] } content= { '#tab1_content' } />
          <TabItemDirective header= { headertext[1] } content= { '#form-container' } />
        </TabItemsDirective>
      </TabComponent>
    );
  }
}
ReactDOM.render(<App />, document.getElementById('element'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Tab</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" />
    <link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
    <div id='container'>
            <div id="tab1_content" style="display: none">
                    <ul>
                        <li>Click on the "+" header to add dynamic tab items. </li>
                        <li>It displays input elements to get the new tab information. </li>
                        <li>Add details and click the "Add Tab" button to open the newly added tab.</li>
                    </ul>
                </div>
                <div id="form-container" style="display: none">
                    <div class="e-float-input">
                        <input type="text" id="tab-title" required="" />
                        <span class="e-float-line"></span>
                        <label class="e-float-text">Enter header title</label>
                    </div>
                    <br />
                    <div class="e-float-input">
                        <textarea rows="5" type="text" id="tab-content" required=""></textarea>
                        <span class="e-float-line"></span>
                        <label class="e-float-text">Enter content</label>
                    </div>
                    <br />
                    <div class="btn-section">
                        <button id="btn-add" class="e-btn">Add Tab</button>
                        <br />
                        <br />
                        <span class="info"> * Title is mandatory to add a new Tab</span>
                    </div>
                </div>
        <div id='element'>
            <div id='loader'>Loading</div>
        </div>
    </div>
</body>

</html>
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { TabComponent, TabItemDirective, TabItemsDirective } from '@syncfusion/ej2-react-navigations';
import { Tooltip } from '@syncfusion/ej2-popups';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);
export default class App extends React.Component {
    tabCreated() {
        const tooltip = new Tooltip({
            content: 'Add Tab'
        });
        tooltip.appendTo('.e-ileft.e-icon');
        document.getElementById('btn-add').onclick = (e) => {
            const title = document.getElementById('tab-title').value;
            const content = document.getElementById('tab-content').value;
            // tslint:disable-next-line: ban-types
            const item = { header: { text: title }, content: React.createElement('pre', { innerHTML: content.replace(/\n/g, '<br>\n') }).outerHTML };
            const totalItems = document.querySelectorAll('#element .e-toolbar-item').length;
            document.getElementById('tabelement').ej2_instances[0].addTab([item], totalItems - 1);
        };
    }
    tabSelected(args) {
        if (args.selectedIndex === document.querySelectorAll('#element .e-toolbar-item').length - 1) {
            document.getElementById('tab-title').value = '';
            document.getElementById('tab-content').value = '';
        }
    }
    render() {
        let headertext;
        headertext = [{ text: "Twitter" }, { 'iconCss': 'e-add-icon' }];
        return (<TabComponent id='tabelement' ref={tab => this.tabInstance = tab} created={this.tabCreated} selected={this.tabSelected.bind(this)}>
        <TabItemsDirective>
          <TabItemDirective header={headertext[0]} content={'#tab1_content'}/>
          <TabItemDirective header={headertext[1]} content={'#form-container'}/>
        </TabItemsDirective>
      </TabComponent>);
    }
}
ReactDOM.render(<App />, document.getElementById('element'));