Drag and drop in React Tab component
29 Aug 202324 minutes to read
The Tab component allows you to drag and drop any item by setting allowDragAndDrop to true. Items can be reordered to any place by dragging and dropping them onto the desired location.
-
If you need to prevent dragging action for a particular item, the
onDragStart
event can be used which will trigger when the item drag is started. If you need to prevent dropping action for a particular item, thedragged
event can be used which will trigger when the drag action is stopped. -
The
dragArea
defines the area in which the draggable element movement will be occurring. Outside that area will be restricted for the draggable element movement. -
The
onDragStart
event will be triggered before dragging the item from Tab. -
The
dragging
event will be triggered when the Tab item is being dragged. -
The
dragged
event will be triggered when the Tab item is dropped on the target element successfully.
In the following sample, the allowDragAndDrop property is enabled.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { TabComponent, TabItemDirective, TabItemsDirective } from '@syncfusion/ej2-react-navigations';
const ReactApp = () => {
let headerText = [{ text: "India" }, { text: "Australia" }, { text: "USA" }, { text: "France" }];
const indiaContent = () => {
return <div>
India officially the Republic of India, is a country in South Asia. It is the seventh-largest country by area, the second-most populous country with over 1.2 billion people, and the most populous democracy in the world. Bounded by the Indian Ocean on the south, the Arabian Sea on the south-west, and the Bay of Bengal on the south-east, it shares land borders with Pakistan to the west;China, Nepal, and Bhutan to the north-east; and Burma and Bangladesh to the east. In the Indian Ocean, India is in the vicinity of Sri Lanka and the Maldives; in addition, India Andaman and Nicobar Islands share a maritime border with Thailand and Indonesia.
</div>;
}
const australiaContent = () => {
return <div>
Australia, officially the Commonwealth of Australia, is a country comprising the mainland of the Australian continent, the island of Tasmania and numerous smaller islands. It is the world sixth-largest country by total area. Neighboring countries include Indonesia, East Timor and Papua New Guinea to the north; the Solomon Islands, Vanuatu and New Caledonia to the north-east; and New Zealand to the south-east.
</div>;
}
const unitedContent = () => {
return <div>
The United States of America (USA or U.S.A.), commonly called the United States (US or U.S.) and America, is a federal republic consisting of fifty states and a federal district. The 48 contiguous states and the federal district of Washington, D.C. are in central North America between Canada and Mexico. The state of Alaska is west of Canada and east of Russia across the Bering Strait, and the state of Hawaii is in the mid-North Pacific. The country also has five populated and nine unpopulated territories in the Pacific and the Caribbean.
</div>;
}
const franceContent = () => {
return <div>
France, officially the French Republic is a sovereign state comprising territory in western Europe and several overseas regions and territories. The European part of France, called Metropolitan France, extends from the Mediterranean Sea to the English Channel and the North Sea, and from the Rhine to the Atlantic Ocean; France covers 640,679 square kilo metres and as of August 2015 has a population of 67 million, counting all the overseas departments and territories.
</div>;
}
return (
<TabComponent id='draggableTab' heightAdjustMode='Auto' allowDragAndDrop={true} dragArea="#container">
<TabItemsDirective>
<TabItemDirective header={headerText[0]} content={indiaContent} />
<TabItemDirective header={headerText[1]} content={australiaContent} />
<TabItemDirective header={headerText[2]} content={unitedContent} />
<TabItemDirective header={headerText[3]} content={franceContent} />
</TabItemsDirective>
</TabComponent>
);
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { TabComponent, TabItemDirective, TabItemsDirective } from '@syncfusion/ej2-react-navigations';
const ReactApp = () => {
let headerText: any = [{ text: "India" }, { text: "Australia" }, { text: "USA" }, { text: "France" }];
const indiaContent = () => {
return <div>
India officially the Republic of India, is a country in South Asia. It is the seventh-largest country by area, the second-most populous country with over 1.2 billion people, and the most populous democracy in the world. Bounded by the Indian Ocean on the south, the Arabian Sea on the south-west, and the Bay of Bengal on the south-east, it shares land borders with Pakistan to the west;China, Nepal, and Bhutan to the north-east; and Burma and Bangladesh to the east. In the Indian Ocean, India is in the vicinity of Sri Lanka and the Maldives; in addition, India Andaman and Nicobar Islands share a maritime border with Thailand and Indonesia.
</div>;
}
const australiaContent = () => {
return <div>
Australia, officially the Commonwealth of Australia, is a country comprising the mainland of the Australian continent, the island of Tasmania and numerous smaller islands. It is the world sixth-largest country by total area. Neighboring countries include Indonesia, East Timor and Papua New Guinea to the north; the Solomon Islands, Vanuatu and New Caledonia to the north-east; and New Zealand to the south-east.
</div>;
}
const unitedContent = () => {
return <div>
The United States of America (USA or U.S.A.), commonly called the United States (US or U.S.) and America, is a federal republic consisting of fifty states and a federal district. The 48 contiguous states and the federal district of Washington, D.C. are in central North America between Canada and Mexico. The state of Alaska is west of Canada and east of Russia across the Bering Strait, and the state of Hawaii is in the mid-North Pacific. The country also has five populated and nine unpopulated territories in the Pacific and the Caribbean.
</div>;
}
const franceContent = () => {
return <div>
France, officially the French Republic is a sovereign state comprising territory in western Europe and several overseas regions and territories. The European part of France, called Metropolitan France, extends from the Mediterranean Sea to the English Channel and the North Sea, and from the Rhine to the Atlantic Ocean; France covers 640,679 square kilo metres and as of August 2015 has a population of 67 million, counting all the overseas departments and territories.
</div>;
}
return (
<TabComponent id='draggableTab' heightAdjustMode='Auto' allowDragAndDrop={true} dragArea="#container">
<TabItemsDirective>
<TabItemDirective header={headerText[0]} content={indiaContent} />
<TabItemDirective header={headerText[1]} content={australiaContent} />
<TabItemDirective header={headerText[2]} content={unitedContent} />
<TabItemDirective header={headerText[3]} content={franceContent} />
</TabItemsDirective>
</TabComponent>
);
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);
Drag and drop item between tabs
It is possible to drag and drop the tab items between two tabs, by manually saving those dropped items as new tab item data through the addTab
method of Tab and removing the dragged item through the removeTab
method of Tab.
In this example, we have used the tab control as an external source, and the item from the tab component is dragged and dropped onto another Tab. Therefore, it is necessary to use the onDragStart
and dragged
event of the Tab component, where we can form an event object and save it using the addTab
method of the Tab and remove the dragged item through removeTab
method of Tab using the dragged item index.
import { useRef } from 'react';
import * as React from "react";
import * as ReactDOM from 'react-dom';
import { TabComponent, TabItemDirective, TabItemsDirective, DragEventArgs } from '@syncfusion/ej2-react-navigations';
import { isNullOrUndefined } from "@syncfusion/ej2-base";
const ReactApp = () => {
const firstTabObj = useRef(null);
const secondTabObj = useRef(null);
let headerText = [{ text: "India" }, { text: "Australia" }, { text: "USA" }, { text: "France" }, { text: "HTML" }, { text: "C Sharp(C#)" }, { text: "Java" }, { text: "VB.Net" }];
let allowDragAndDrop = true;
let firstTabitem = [];
let secondTabitem = [];
let dragItemIndex;
let dragItemContainer;
const firstTabdragStart = (args) => {
firstTabitem = [firstTabObj.current.items[args.index]];
args.draggedItem.style.visibility = 'hidden';
dragItemContainer = args.draggedItem.closest('.e-tab');
}
const firstTabDragStop = (args) => {
if (!isNullOrUndefined(args.target.closest('.e-tab')) && !dragItemContainer.isSameNode(args.target.closest('.e-tab'))) {
args.cancel = true;
let TabElement = args.target.closest('.e-tab');
let dropItem = args.target.closest('.e-toolbar-item');
if (TabElement != null && dropItem != null) {
dragItemIndex = Array.prototype.indexOf.call(firstTabObj.current.element.querySelectorAll('.e-toolbar-item'), args.draggedItem);
let dropItemContainer = args.target.closest('.e-toolbar-items');
let dropItemIndex = (dropItemContainer != null) ? (Array.prototype.slice.call(dropItemContainer.querySelectorAll('.e-toolbar-item'))).indexOf(dropItem) : -1;
secondTabObj.current.addTab(firstTabitem, dropItemIndex);
firstTabObj.current.removeTab(dragItemIndex);
}
}
}
const secondTabDragStart = (args) => {
secondTabitem = [secondTabObj.current.items[args.index]];
args.draggedItem.style.visibility = 'hidden';
dragItemContainer = args.draggedItem.closest('.e-tab');
}
const secondTabDragStop = (args) => {
if (!isNullOrUndefined(args.target.closest('.e-tab')) && !dragItemContainer.isSameNode(args.target.closest('.e-tab'))) {
args.cancel = true;
let TabElement = args.target.closest('.e-tab');
let dropItem = args.target.closest('.e-toolbar-item');
if (TabElement != null && dropItem != null) {
dragItemIndex = Array.prototype.indexOf.call(secondTabObj.current.element.querySelectorAll('.e-toolbar-item'), args.draggedItem);
let dropItemContainer = args.target.closest('.e-toolbar-items');
let dropItemIndex = (dropItemContainer != null) ? (Array.prototype.slice.call(dropItemContainer.querySelectorAll('.e-toolbar-item'))).indexOf(dropItem) : -1;
firstTabObj.current.addTab(secondTabitem, dropItemIndex);
secondTabObj.current.removeTab(dragItemIndex);
}
}
}
const indiaContent = () => {
return <div>
India officially the Republic of India, is a country in South Asia. It is the seventh-largest country by area, the second-most populous country with over 1.2 billion people, and the most populous democracy in the world. Bounded by the Indian Ocean on the south, the Arabian Sea on the south-west, and the Bay of Bengal on the south-east, it shares land borders with Pakistan to the west;China, Nepal, and Bhutan to the north-east; and Burma and Bangladesh to the east. In the Indian Ocean, India is in the vicinity of Sri Lanka and the Maldives; in addition, India Andaman and Nicobar Islands share a maritime border with Thailand and Indonesia.
</div>;
}
const ausContent = () => {
return <div>
Australia, officially the Commonwealth of Australia, is a country comprising the mainland of the Australian continent, the island of Tasmania and numerous smaller islands. It is the world sixth-largest country by total area. Neighboring countries include Indonesia, East Timor and Papua New Guinea to the north; the Solomon Islands, Vanuatu and New Caledonia to the north-east; and New Zealand to the south-east.
</div>;
}
const usaContent = () => {
return <div>
The United States of America (USA or U.S.A.), commonly called the United States (US or U.S.) and America, is a federal republic consisting of fifty states and a federal district. The 48 contiguous states and the federal district of Washington, D.C. are in central North America between Canada and Mexico. The state of Alaska is west of Canada and east of Russia across the Bering Strait, and the state of Hawaii is in the mid-North Pacific. The country also has five populated and nine unpopulated territories in the Pacific and the Caribbean.
</div>;
}
const franceContent = () => {
return <div>
France, officially the French Republic is a sovereign state comprising territory in western Europe and several overseas regions and territories. The European part of France, called Metropolitan France, extends from the Mediterranean Sea to the English Channel and the North Sea, and from the Rhine to the Atlantic Ocean; France covers 640,679 square kilo metres and as of August 2015 has a population of 67 million, counting all the overseas departments and territories.
</div>;
}
const htmlContent = () => {
return <div>
HyperText Markup Language, commonly referred to as HTML, is the standard markup language used to create web pages. Along with CSS, and JavaScript, HTML is a cornerstone technology, used by most websites to create visually engaging web pages, user interfaces for web applications, and user interfaces for many mobile applications.[1] Web browsers can read HTML files and render them into visible or audible web pages. HTML describes the structure of a website semantically along with cues for presentation, making it a markup language, rather than a programming language.
</div>;
}
const csharpContent = () => {
return <div>
C# is intended to be a simple, modern, general-purpose, object-oriented programming language. Its development team is led by Anders Hejlsberg. The most recent version is C# 5.0, which was released on August 15, 2012.
</div>;
}
const javaContent = () => {
return <div>
Java is a set of computer software and specifications developed by Sun Microsystems, later acquired by Oracle Corporation, that provides a system for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones to enterprise servers and supercomputers. While less common, Java applets run in secure, sandboxed environments to provide many features of native applications and can be embedded in HTML pages.
</div>;
}
const vbNetContent = () => {
return <div>
The command-line compiler, VBC.EXE, is installed as part of the freeware .NET Framework SDK. Mono also includes a command-line VB.NET compiler. The most recent version is VB 2012, which was released on August 15, 2012.
</div>;
}
return (
<div>
<TabComponent ref={firstTabObj} id='firstTab' heightAdjustMode='Auto' allowDragAndDrop={allowDragAndDrop} dragArea="#container" onDragStart={firstTabdragStart} dragged={firstTabDragStop}>
<TabItemsDirective>
<TabItemDirective header={headerText[0]} content={indiaContent} />
<TabItemDirective header={headerText[1]} content={ausContent} />
<TabItemDirective header={headerText[2]} content={usaContent} />
<TabItemDirective header={headerText[3]} content={franceContent} />
</TabItemsDirective>
</TabComponent>
<br></br>
<TabComponent ref={secondTabObj} id='secondTab' heightAdjustMode='Auto' allowDragAndDrop={allowDragAndDrop} dragArea="#container" onDragStart={secondTabDragStart} dragged={secondTabDragStop}>
<TabItemsDirective>
<TabItemDirective header={headerText[4]} content={htmlContent} />
<TabItemDirective header={headerText[5]} content={csharpContent} />
<TabItemDirective header={headerText[6]} content={javaContent} />
<TabItemDirective header={headerText[7]} content={vbNetContent} />
</TabItemsDirective>
</TabComponent>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);
import { useRef } from 'react';
import * as React from "react";
import * as ReactDOM from 'react-dom';
import { TabComponent, TabItemDirective, TabItemsDirective, DragEventArgs } from '@syncfusion/ej2-react-navigations';
import { isNullOrUndefined } from "@syncfusion/ej2-base";
const ReactApp = () => {
const firstTabObj = useRef<TabComponent>(null);
const secondTabObj = useRef<TabComponent>(null);
let headerText: any = [{ text: "India" }, { text: "Australia" }, { text: "USA" }, { text: "France" }, { text: "HTML" }, { text: "C Sharp(C#)" }, { text: "Java" }, { text: "VB.Net" }];
let allowDragAndDrop: boolean = true;
let firstTabitem: Object[] = [];
let secondTabitem: Object[] = [];
let dragItemIndex: number;
let dragItemContainer: Element;
const firstTabdragStart = (args: DragEventArgs) => {
firstTabitem = [firstTabObj.current.items[args.index]];
args.draggedItem.style.visibility = 'hidden';
dragItemContainer = args.draggedItem.closest('.e-tab') as Element;
}
const firstTabDragStop = (args: DragEventArgs) => {
if (!isNullOrUndefined(args.target.closest('.e-tab') as HTMLElement) && !dragItemContainer.isSameNode(args.target.closest('.e-tab'))) {
args.cancel = true;
let TabElement: Element = args.target.closest('.e-tab') as Element;
let dropItem: Element = args.target.closest('.e-toolbar-item') as Element;
if (TabElement != null && dropItem != null) {
dragItemIndex = Array.prototype.indexOf.call(firstTabObj.current.element.querySelectorAll('.e-toolbar-item'), args.draggedItem);
let dropItemContainer: Element = args.target.closest('.e-toolbar-items') as Element;
let dropItemIndex: number = (dropItemContainer != null) ? (Array.prototype.slice.call(dropItemContainer.querySelectorAll('.e-toolbar-item'))).indexOf(dropItem) : -1;
secondTabObj.current.addTab(firstTabitem, dropItemIndex);
firstTabObj.current.removeTab(dragItemIndex);
}
}
}
const secondTabDragStart = (args: DragEventArgs) => {
secondTabitem = [secondTabObj.current.items[args.index]];
args.draggedItem.style.visibility = 'hidden';
dragItemContainer = args.draggedItem.closest('.e-tab') as Element;
}
const secondTabDragStop = (args: DragEventArgs) => {
if (!isNullOrUndefined(args.target.closest('.e-tab') as HTMLElement) && !dragItemContainer.isSameNode(args.target.closest('.e-tab'))) {
args.cancel = true;
let TabElement: Element = args.target.closest('.e-tab') as Element;
let dropItem: Element = args.target.closest('.e-toolbar-item') as Element;
if (TabElement != null && dropItem != null) {
dragItemIndex = Array.prototype.indexOf.call(secondTabObj.current.element.querySelectorAll('.e-toolbar-item'), args.draggedItem);
let dropItemContainer: Element = args.target.closest('.e-toolbar-items') as Element;
let dropItemIndex: number = (dropItemContainer != null) ? (Array.prototype.slice.call(dropItemContainer.querySelectorAll('.e-toolbar-item'))).indexOf(dropItem) : -1;
firstTabObj.current.addTab(secondTabitem, dropItemIndex);
secondTabObj.current.removeTab(dragItemIndex);
}
}
}
const indiaContent = () => {
return <div>
India officially the Republic of India, is a country in South Asia. It is the seventh-largest country by area, the second-most populous country with over 1.2 billion people, and the most populous democracy in the world. Bounded by the Indian Ocean on the south, the Arabian Sea on the south-west, and the Bay of Bengal on the south-east, it shares land borders with Pakistan to the west;China, Nepal, and Bhutan to the north-east; and Burma and Bangladesh to the east. In the Indian Ocean, India is in the vicinity of Sri Lanka and the Maldives; in addition, India Andaman and Nicobar Islands share a maritime border with Thailand and Indonesia.
</div>;
}
const ausContent = () => {
return <div>
Australia, officially the Commonwealth of Australia, is a country comprising the mainland of the Australian continent, the island of Tasmania and numerous smaller islands. It is the world sixth-largest country by total area. Neighboring countries include Indonesia, East Timor and Papua New Guinea to the north; the Solomon Islands, Vanuatu and New Caledonia to the north-east; and New Zealand to the south-east.
</div>;
}
const usaContent = () => {
return <div>
The United States of America (USA or U.S.A.), commonly called the United States (US or U.S.) and America, is a federal republic consisting of fifty states and a federal district. The 48 contiguous states and the federal district of Washington, D.C. are in central North America between Canada and Mexico. The state of Alaska is west of Canada and east of Russia across the Bering Strait, and the state of Hawaii is in the mid-North Pacific. The country also has five populated and nine unpopulated territories in the Pacific and the Caribbean.
</div>;
}
const franceContent = () => {
return <div>
France, officially the French Republic is a sovereign state comprising territory in western Europe and several overseas regions and territories. The European part of France, called Metropolitan France, extends from the Mediterranean Sea to the English Channel and the North Sea, and from the Rhine to the Atlantic Ocean; France covers 640,679 square kilo metres and as of August 2015 has a population of 67 million, counting all the overseas departments and territories.
</div>;
}
const htmlContent = () => {
return <div>
HyperText Markup Language, commonly referred to as HTML, is the standard markup language used to create web pages. Along with CSS, and JavaScript, HTML is a cornerstone technology, used by most websites to create visually engaging web pages, user interfaces for web applications, and user interfaces for many mobile applications.[1] Web browsers can read HTML files and render them into visible or audible web pages. HTML describes the structure of a website semantically along with cues for presentation, making it a markup language, rather than a programming language.
</div>;
}
const csharpContent = () => {
return <div>
C# is intended to be a simple, modern, general-purpose, object-oriented programming language. Its development team is led by Anders Hejlsberg. The most recent version is C# 5.0, which was released on August 15, 2012.
</div>;
}
const javaContent = () => {
return <div>
Java is a set of computer software and specifications developed by Sun Microsystems, later acquired by Oracle Corporation, that provides a system for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones to enterprise servers and supercomputers. While less common, Java applets run in secure, sandboxed environments to provide many features of native applications and can be embedded in HTML pages.
</div>;
}
const vbNetContent = () => {
return <div>
The command-line compiler, VBC.EXE, is installed as part of the freeware .NET Framework SDK. Mono also includes a command-line VB.NET compiler. The most recent version is VB 2012, which was released on August 15, 2012.
</div>;
}
return (
<div>
<TabComponent ref={firstTabObj} id='firstTab' heightAdjustMode='Auto' allowDragAndDrop={allowDragAndDrop} dragArea="#container" onDragStart={firstTabdragStart} dragged={firstTabDragStop}>
<TabItemsDirective>
<TabItemDirective header={headerText[0]} content={indiaContent} />
<TabItemDirective header={headerText[1]} content={ausContent} />
<TabItemDirective header={headerText[2]} content={usaContent} />
<TabItemDirective header={headerText[3]} content={franceContent} />
</TabItemsDirective>
</TabComponent>
<br></br>
<TabComponent ref={secondTabObj} id='secondTab' heightAdjustMode='Auto' allowDragAndDrop={allowDragAndDrop} dragArea="#container" onDragStart={secondTabDragStart} dragged={secondTabDragStop}>
<TabItemsDirective>
<TabItemDirective header={headerText[4]} content={htmlContent} />
<TabItemDirective header={headerText[5]} content={csharpContent} />
<TabItemDirective header={headerText[6]} content={javaContent} />
<TabItemDirective header={headerText[7]} content={vbNetContent} />
</TabItemsDirective>
</TabComponent>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);
Drag and drop items to external source
It is possible to drag and drop the items to any of the external sources from the Tab, by manually saving those dropped items as new node data through the addNodes
method of Treeview and removing the dragged item through the removeTab
method of Tab.
In this example, we have used the tree view control as an external source, and the item from the tab component is dragged and dropped onto the child nodes of the tree view component. Therefore, it is necessary to use the dragged
event of the Tab component, where we can form an event object and save it using the addNodes
method of the Treeview and remove the dragged item through the removeTab
method of Tab using the dragged item index.
import { useRef, useEffect } from 'react';
import * as React from "react";
import * as ReactDOM from 'react-dom';
import { TabComponent, TabItemDirective, TabItemsDirective, TreeViewComponent } from '@syncfusion/ej2-react-navigations';
import { isNullOrUndefined } from "@syncfusion/ej2-base";
const ReactApp = () => {
const tabObj = useRef(null);
const treeObj = useRef(null);
let headerText = [{ text: "India" }, { text: "Australia" }, { text: "USA" }, { text: "France" }];
let allowDragAndDrop = true;
let data = [
{ text: 'Hennessey Venom', id: 'list-01' },
{ text: 'Bugatti Chiron', id: 'list-02' },
{ text: 'Bugatti Veyron Super Sport', id: 'list-03' },
{ text: 'SSC Ultimate Aero', id: 'list-04' },
{ text: 'Koenigsegg CCR', id: 'list-05' },
{ text: 'McLaren F1', id: 'list-06' },
{ text: 'Aston Martin One- 77', id: 'list-07' },
{ text: 'Jaguar XJ220', id: 'list-08' },
{ text: 'McLaren P1', id: 'list-09' },
{ text: 'Ferrari LaFerrari', id: 'list-10' },
];
let field = { dataSource: data, id: 'id', text: 'text' };
let i = 0;
useEffect(() => {
const tabElement = tabObj.current.element;
if (!isNullOrUndefined(tabElement)) {
tabElement.querySelector('.e-tab-header').classList.add('e-droppable');
tabElement.querySelector('.e-content').classList.add('tab-content');
}
}, []);
const tabDragStop = (args) => {
const dragTabIndex = Array.prototype.indexOf.call(tabObj.current.element.querySelectorAll('.e-toolbar-item'), args.draggedItem);
const dragItem = tabObj.current.items[dragTabIndex];
const dropNode = args.target.closest('#draggableTreeview .e-list-item');
if (dropNode != null && !args.target.closest('#draggableTab .e-toolbar-item')) {
args.cancel = true;
const dropContainer = document.querySelector('.treeview-external-drop-tab').querySelectorAll('.e-list-item');
const dropIndex = Array.prototype.indexOf.call(dropContainer, dropNode);
const newNode = [{ id: 'list' + i, text: dragItem.header ? dragItem.header.text : '' }];
tabObj.current.removeTab(dragTabIndex);
treeObj.current.addNodes(newNode, 'Treeview', dropIndex);
}
}
const indiaContent = () => {
return <div>
India officially the Republic of India, is a country in South Asia. It is the seventh-largest country by area, the second-most populous country with over 1.2 billion people, and the most populous democracy in the world. Bounded by the Indian Ocean on the south, the Arabian Sea on the south-west, and the Bay of Bengal on the south-east, it shares land borders with Pakistan to the west;China, Nepal, and Bhutan to the north-east; and Burma and Bangladesh to the east. In the Indian Ocean, India is in the vicinity of Sri Lanka and the Maldives; in addition, India Andaman and Nicobar Islands share a maritime border with Thailand and Indonesia.
</div>;
}
const ausContent = () => {
return <div>
Australia, officially the Commonwealth of Australia, is a country comprising the mainland of the Australian continent, the island of Tasmania and numerous smaller islands. It is the world sixth-largest country by total area. Neighboring countries include Indonesia, East Timor and Papua New Guinea to the north; the Solomon Islands, Vanuatu and New Caledonia to the north-east; and New Zealand to the south-east.
</div>;
}
const usaContent = () => {
return <div>
The United States of America (USA or U.S.A.), commonly called the United States (US or U.S.) and America, is a federal republic consisting of fifty states and a federal district. The 48 contiguous states and the federal district of Washington, D.C. are in central North America between Canada and Mexico. The state of Alaska is west of Canada and east of Russia across the Bering Strait, and the state of Hawaii is in the mid-North Pacific. The country also has five populated and nine unpopulated territories in the Pacific and the Caribbean.
</div>;
}
const franceContent = () => {
return <div>
France, officially the French Republic is a sovereign state comprising territory in western Europe and several overseas regions and territories. The European part of France, called Metropolitan France, extends from the Mediterranean Sea to the English Channel and the North Sea, and from the Rhine to the Atlantic Ocean; France covers 640,679 square kilo metres and as of August 2015 has a population of 67 million, counting all the overseas departments and territories.
</div>;
}
return (
<div>
<TabComponent ref={tabObj} heightAdjustMode='Auto' allowDragAndDrop={allowDragAndDrop} dragArea="#container" dragged={tabDragStop}>
<TabItemsDirective>
<TabItemDirective header={headerText[0]} content={indiaContent} />
<TabItemDirective header={headerText[1]} content={ausContent} />
<TabItemDirective header={headerText[2]} content={usaContent} />
<TabItemDirective header={headerText[3]} content={franceContent} />
</TabItemsDirective>
</TabComponent>
<br></br>
<TreeViewComponent id="draggableTreeview" ref={treeObj} cssClass="treeview-external-drop-tab" fields={field} />
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);
import { useRef, useEffect } from 'react';
import * as React from "react";
import * as ReactDOM from 'react-dom';
import { TabComponent, TabItemDirective, TabItemsDirective, TreeViewComponent } from '@syncfusion/ej2-react-navigations';
import { isNullOrUndefined } from "@syncfusion/ej2-base";
const ReactApp = () => {
const tabObj = useRef<TabComponent>(null);
const treeObj = useRef<TreeViewComponent>(null);
let headerText: any = [{ text: "India" }, { text: "Australia" }, { text: "USA" }, { text: "France" }];
let allowDragAndDrop: boolean = true;
let data: { [key: string]: Object }[] = [
{ text: 'Hennessey Venom', id: 'list-01' },
{ text: 'Bugatti Chiron', id: 'list-02' },
{ text: 'Bugatti Veyron Super Sport', id: 'list-03' },
{ text: 'SSC Ultimate Aero', id: 'list-04' },
{ text: 'Koenigsegg CCR', id: 'list-05' },
{ text: 'McLaren F1', id: 'list-06' },
{ text: 'Aston Martin One- 77', id: 'list-07' },
{ text: 'Jaguar XJ220', id: 'list-08' },
{ text: 'McLaren P1', id: 'list-09' },
{ text: 'Ferrari LaFerrari', id: 'list-10' },
];
let field: Object = { dataSource: data, id: 'id', text: 'text' };
let i: number = 0;
useEffect(() => {
const tabElement = tabObj.current.element;
if (!isNullOrUndefined(tabElement)) {
tabElement.querySelector('.e-tab-header').classList.add('e-droppable');
tabElement.querySelector('.e-content').classList.add('tab-content');
}
}, []);
const tabDragStop = (args) => {
const dragTabIndex = Array.prototype.indexOf.call(tabObj.current.element.querySelectorAll('.e-toolbar-item'), args.draggedItem);
const dragItem = tabObj.current.items[dragTabIndex];
const dropNode = args.target.closest('#draggableTreeview .e-list-item');
if (dropNode != null && !args.target.closest('#draggableTab .e-toolbar-item')) {
args.cancel = true;
const dropContainer = document.querySelector('.treeview-external-drop-tab').querySelectorAll('.e-list-item');
const dropIndex = Array.prototype.indexOf.call(dropContainer, dropNode);
const newNode = [{ id: 'list' + i, text: dragItem.header ? dragItem.header.text : '' }];
tabObj.current.removeTab(dragTabIndex);
treeObj.current.addNodes(newNode, 'Treeview', dropIndex);
}
}
const indiaContent = () => {
return <div>
India officially the Republic of India, is a country in South Asia. It is the seventh-largest country by area, the second-most populous country with over 1.2 billion people, and the most populous democracy in the world. Bounded by the Indian Ocean on the south, the Arabian Sea on the south-west, and the Bay of Bengal on the south-east, it shares land borders with Pakistan to the west;China, Nepal, and Bhutan to the north-east; and Burma and Bangladesh to the east. In the Indian Ocean, India is in the vicinity of Sri Lanka and the Maldives; in addition, India Andaman and Nicobar Islands share a maritime border with Thailand and Indonesia.
</div>;
}
const ausContent = () => {
return <div>
Australia, officially the Commonwealth of Australia, is a country comprising the mainland of the Australian continent, the island of Tasmania and numerous smaller islands. It is the world sixth-largest country by total area. Neighboring countries include Indonesia, East Timor and Papua New Guinea to the north; the Solomon Islands, Vanuatu and New Caledonia to the north-east; and New Zealand to the south-east.
</div>;
}
const usaContent = () => {
return <div>
The United States of America (USA or U.S.A.), commonly called the United States (US or U.S.) and America, is a federal republic consisting of fifty states and a federal district. The 48 contiguous states and the federal district of Washington, D.C. are in central North America between Canada and Mexico. The state of Alaska is west of Canada and east of Russia across the Bering Strait, and the state of Hawaii is in the mid-North Pacific. The country also has five populated and nine unpopulated territories in the Pacific and the Caribbean.
</div>;
}
const franceContent = () => {
return <div>
France, officially the French Republic is a sovereign state comprising territory in western Europe and several overseas regions and territories. The European part of France, called Metropolitan France, extends from the Mediterranean Sea to the English Channel and the North Sea, and from the Rhine to the Atlantic Ocean; France covers 640,679 square kilo metres and as of August 2015 has a population of 67 million, counting all the overseas departments and territories.
</div>;
}
return (
<div>
<TabComponent ref={tabObj} heightAdjustMode='Auto' allowDragAndDrop={allowDragAndDrop} dragArea="#container" dragged={tabDragStop}>
<TabItemsDirective>
<TabItemDirective header={headerText[0]} content={indiaContent} />
<TabItemDirective header={headerText[1]} content={ausContent} />
<TabItemDirective header={headerText[2]} content={usaContent} />
<TabItemDirective header={headerText[3]} content={franceContent} />
</TabItemsDirective>
</TabComponent>
<br></br>
<TreeViewComponent id="draggableTreeview" ref={treeObj} cssClass="treeview-external-drop-tab" fields={field} />
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);
Drag and drop items from external source
It is possible to drag and drop the items from any of the external sources into the Tab, by manually saving those dropped items as new item data through the addTab
method of Tab and removing the dragged node through the removeNodes
method of Treeview.
In this example, we have used the tree view control as an external source, and the child nodes from the tree view component are dragged and dropped onto the Tab. Therefore, it is necessary to use the nodeDragStop
event of the Treeview component, where we can form an event object and save it using the addTab
method of the Tab and remove the dragged node through the removeNodes
method of Treeview.
import { useRef } from 'react';
import * as React from "react";
import * as ReactDOM from 'react-dom';
import { TabComponent, TabItemDirective, TabItemsDirective, TreeViewComponent } from '@syncfusion/ej2-react-navigations';
import { isNullOrUndefined } from "@syncfusion/ej2-base";
const ReactApp = () => {
const tabObj = useRef(null);
const treeObj = useRef(null);
let headerText = [{ text: "India" }, { text: "Australia" }, { text: "USA" }, { text: "France" }];
let allowDragAndDrop = true;
let data = [
{ text: 'Hennessey Venom', id: 'list-01' },
{ text: 'Bugatti Chiron', id: 'list-02' },
{ text: 'Bugatti Veyron Super Sport', id: 'list-03' },
{ text: 'SSC Ultimate Aero', id: 'list-04' },
{ text: 'Koenigsegg CCR', id: 'list-05' },
{ text: 'McLaren F1', id: 'list-06' },
{ text: 'Aston Martin One- 77', id: 'list-07' },
{ text: 'Jaguar XJ220', id: 'list-08' },
{ text: 'McLaren P1', id: 'list-09' },
{ text: 'Ferrari LaFerrari', id: 'list-10' },
];
let field = { dataSource: data, id: 'id', text: 'text' };
const onNodeDragStop = (args) => {
let dropElement = args.target.closest('#draggableTab .e-toolbar-item');
if (dropElement != null) {
let tabElement = tabObj.current.element;
let dropItemIndex = [].slice.call(tabElement.querySelectorAll('.e-toolbar-item')).indexOf(dropElement);
let newTabItem = [{
header: { 'text': args.draggedNodeData.text.toString() },
content: args.draggedNodeData.text.toString() + ' Content'
}];
tabObj.current.addTab(newTabItem, dropItemIndex);
treeObj.current.removeNodes([args.draggedNode]);
args.cancel = true;
} else {
let dropNode = args.target.closest('#draggableTreeview .e-list-item ');
if (!isNullOrUndefined(dropNode) && args.dropIndicator === 'e-drop-in') {
args.cancel = true;
}
}
}
const onNodeDrag = (args) => {
if (!isNullOrUndefined(args.target.closest('.tab-content'))) {
args.dropIndicator = 'e-no-drop';
} else if (!isNullOrUndefined(args.target.closest('#draggableTab .e-tab-header'))) {
args.dropIndicator = 'e-drop-in';
}
}
const indiaContent = () => {
return <div>
India officially the Republic of India, is a country in South Asia. It is the seventh-largest country by area, the second-most populous country with over 1.2 billion people, and the most populous democracy in the world. Bounded by the Indian Ocean on the south, the Arabian Sea on the south-west, and the Bay of Bengal on the south-east, it shares land borders with Pakistan to the west;China, Nepal, and Bhutan to the north-east; and Burma and Bangladesh to the east. In the Indian Ocean, India is in the vicinity of Sri Lanka and the Maldives; in addition, India Andaman and Nicobar Islands share a maritime border with Thailand and Indonesia.
</div>;
}
const ausContent = () => {
return <div>
Australia, officially the Commonwealth of Australia, is a country comprising the mainland of the Australian continent, the island of Tasmania and numerous smaller islands. It is the world sixth-largest country by total area. Neighboring countries include Indonesia, East Timor and Papua New Guinea to the north; the Solomon Islands, Vanuatu and New Caledonia to the north-east; and New Zealand to the south-east.
</div>;
}
const usaContent = () => {
return <div>
The United States of America (USA or U.S.A.), commonly called the United States (US or U.S.) and America, is a federal republic consisting of fifty states and a federal district. The 48 contiguous states and the federal district of Washington, D.C. are in central North America between Canada and Mexico. The state of Alaska is west of Canada and east of Russia across the Bering Strait, and the state of Hawaii is in the mid-North Pacific. The country also has five populated and nine unpopulated territories in the Pacific and the Caribbean.
</div>;
}
const franceContent = () => {
return <div>
France, officially the French Republic is a sovereign state comprising territory in western Europe and several overseas regions and territories. The European part of France, called Metropolitan France, extends from the Mediterranean Sea to the English Channel and the North Sea, and from the Rhine to the Atlantic Ocean; France covers 640,679 square kilo metres and as of August 2015 has a population of 67 million, counting all the overseas departments and territories.
</div>;
}
return (
<div>
<TabComponent ref={tabObj} id='draggableTab' heightAdjustMode='Auto' dragArea="#container">
<TabItemsDirective>
<TabItemDirective header={headerText[0]} content={indiaContent} />
<TabItemDirective header={headerText[1]} content={ausContent} />
<TabItemDirective header={headerText[2]} content={usaContent} />
<TabItemDirective header={headerText[3]} content={franceContent} />
</TabItemsDirective>
</TabComponent>
<br></br>
<TreeViewComponent id="draggableTreeview" ref={treeObj} dragArea="#container" cssClass="treeview-external-drop-tab" fields={field} nodeDragStop={onNodeDragStop} nodeDragging={onNodeDrag} allowDragAndDrop={allowDragAndDrop} />
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);
import { useRef } from 'react';
import * as React from "react";
import * as ReactDOM from 'react-dom';
import { TabComponent, TabItemDirective, TabItemsDirective, TabItemModel, TreeViewComponent, DragAndDropEventArgs } from '@syncfusion/ej2-react-navigations';
import { isNullOrUndefined } from "@syncfusion/ej2-base";
const ReactApp = () => {
const tabObj = useRef<TabComponent>(null);
const treeObj = useRef<TreeViewComponent>(null);
let headerText: any = [{ text: "India" }, { text: "Australia" }, { text: "USA" }, { text: "France" }];
let allowDragAndDrop: boolean = true;
let data: { [key: string]: Object }[] = [
{ text: 'Hennessey Venom', id: 'list-01' },
{ text: 'Bugatti Chiron', id: 'list-02' },
{ text: 'Bugatti Veyron Super Sport', id: 'list-03' },
{ text: 'SSC Ultimate Aero', id: 'list-04' },
{ text: 'Koenigsegg CCR', id: 'list-05' },
{ text: 'McLaren F1', id: 'list-06' },
{ text: 'Aston Martin One- 77', id: 'list-07' },
{ text: 'Jaguar XJ220', id: 'list-08' },
{ text: 'McLaren P1', id: 'list-09' },
{ text: 'Ferrari LaFerrari', id: 'list-10' },
];
let field: Object = { dataSource: data, id: 'id', text: 'text' };
const onNodeDragStop = (args: DragAndDropEventArgs) => {
let dropElement: Element = args.target.closest('#draggableTab .e-toolbar-item') as Element;
if (dropElement != null) {
let tabElement: HTMLElement = tabObj.current.element;
let dropItemIndex: number = [].slice.call(tabElement.querySelectorAll('.e-toolbar-item')).indexOf(dropElement as never);
let newTabItem: TabItemModel[] = [{
header: { 'text': args.draggedNodeData.text.toString() },
content: args.draggedNodeData.text.toString() + ' Content'
}];
tabObj.current.addTab(newTabItem, dropItemIndex);
treeObj.current.removeNodes([args.draggedNode]);
args.cancel = true;
} else {
let dropNode: Element = args.target.closest('#draggableTreeview .e-list-item ') as Element;
if (!isNullOrUndefined(dropNode) && args.dropIndicator === 'e-drop-in') {
args.cancel = true;
}
}
}
const onNodeDrag = (args: DragAndDropEventArgs) => {
if (!isNullOrUndefined(args.target.closest('.tab-content') as HTMLElement)) {
args.dropIndicator = 'e-no-drop';
} else if (!isNullOrUndefined(args.target.closest('#draggableTab .e-tab-header') as HTMLElement)) {
args.dropIndicator = 'e-drop-in';
}
}
const indiaContent = () => {
return <div>
India officially the Republic of India, is a country in South Asia. It is the seventh-largest country by area, the second-most populous country with over 1.2 billion people, and the most populous democracy in the world. Bounded by the Indian Ocean on the south, the Arabian Sea on the south-west, and the Bay of Bengal on the south-east, it shares land borders with Pakistan to the west;China, Nepal, and Bhutan to the north-east; and Burma and Bangladesh to the east. In the Indian Ocean, India is in the vicinity of Sri Lanka and the Maldives; in addition, India Andaman and Nicobar Islands share a maritime border with Thailand and Indonesia.
</div>;
}
const ausContent = () => {
return <div>
Australia, officially the Commonwealth of Australia, is a country comprising the mainland of the Australian continent, the island of Tasmania and numerous smaller islands. It is the world sixth-largest country by total area. Neighboring countries include Indonesia, East Timor and Papua New Guinea to the north; the Solomon Islands, Vanuatu and New Caledonia to the north-east; and New Zealand to the south-east.
</div>;
}
const usaContent = () => {
return <div>
The United States of America (USA or U.S.A.), commonly called the United States (US or U.S.) and America, is a federal republic consisting of fifty states and a federal district. The 48 contiguous states and the federal district of Washington, D.C. are in central North America between Canada and Mexico. The state of Alaska is west of Canada and east of Russia across the Bering Strait, and the state of Hawaii is in the mid-North Pacific. The country also has five populated and nine unpopulated territories in the Pacific and the Caribbean.
</div>;
}
const franceContent = () => {
return <div>
France, officially the French Republic is a sovereign state comprising territory in western Europe and several overseas regions and territories. The European part of France, called Metropolitan France, extends from the Mediterranean Sea to the English Channel and the North Sea, and from the Rhine to the Atlantic Ocean; France covers 640,679 square kilo metres and as of August 2015 has a population of 67 million, counting all the overseas departments and territories.
</div>;
}
return (
<div>
<TabComponent ref={tabObj} id='draggableTab' heightAdjustMode='Auto' dragArea="#container">
<TabItemsDirective>
<TabItemDirective header={headerText[0]} content={indiaContent} />
<TabItemDirective header={headerText[1]} content={ausContent} />
<TabItemDirective header={headerText[2]} content={usaContent} />
<TabItemDirective header={headerText[3]} content={franceContent} />
</TabItemsDirective>
</TabComponent>
<br></br>
<TreeViewComponent id="draggableTreeview" ref={treeObj} dragArea="#container" cssClass="treeview-external-drop-tab" fields={field} nodeDragStop={onNodeDragStop} nodeDragging={onNodeDrag} allowDragAndDrop={allowDragAndDrop} />
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);