How to open a Dialog on Context Menu click in React
04 Sep 20268 minutes to read
This section explains how to open a Dialog when a React Context Menu item is clicked. This integration is commonly used for modal forms and confirmations. The functionality is achieved by handling the Dialog opening logic in the select event of the React Context Menu component.
When a React Context Menu item is selected, the select event provides access to the clicked item’s details, enabling conditional Dialog opening based on the selected menu option.
In the following sample, a Dialog opens when the user clicks the Save As… menu item. The Dialog displays the content “This file can be saved as PDF” and a Submit button that closes it.
import { ContextMenuComponent } from '@syncfusion/ej2-react-navigations';
import { DialogComponent } from '@syncfusion/ej2-react-popups';
import * as React from 'react';
import * as ReactDom from 'react-dom';
function App() {
let dialogInstance;
let position = { X: 100, Y: 100 };
let visible = false;
let buttons = [{
buttonModel: {
content: 'Submit',
cssClass: 'e-flat',
isPrimary: true
},
'click': () => {
hide();
}
}];
let menuItems = [
{
text: 'Back'
},
{
text: 'Forward'
},
{
text: 'Reload'
},
{
separator: true
},
{
text: 'Save As...'
},
{
text: 'Print'
},
{
text: 'Cast'
}
];
function hide() {
dialogInstance.hide();
}
function select(args) {
if (args.item.text === 'Save As...') {
dialogInstance.show();
}
}
return (<div className="container">
<div id='target'>Right click / Touch hold to open the ContextMenu</div>
<DialogComponent width='200px' height='110px' content='This file can be saved as PDF' buttons={buttons} visible={visible} position={position} ref={dialog => dialogInstance = dialog}/>
<ContextMenuComponent id='contextmenu' target='#target' items={menuItems} select={select}/>
</div>);
}
export default App;
ReactDom.render(<App />, document.getElementById('element'));import { ContextMenuComponent, MenuEventArgs, MenuItemModel } from '@syncfusion/ej2-react-navigations';
import { DialogComponent } from '@syncfusion/ej2-react-popups';
import * as React from 'react';
import * as ReactDom from 'react-dom';
function App() {
let dialogInstance: DialogComponent;
let position: any = { X: 100, Y: 100 };
let visible: boolean = false;
let buttons: any = [{
buttonModel: {
content: 'Submit',
cssClass: 'e-flat',
isPrimary: true
},
'click': () => {
hide();
}
}];
let menuItems: MenuItemModel[] = [
{
text: 'Back'
},
{
text: 'Forward'
},
{
text: 'Reload'
},
{
separator: true
},
{
text: 'Save As...'
},
{
text: 'Print'
},
{
text: 'Cast'
}];
function hide (): void {
dialogInstance.hide();
}
function select(args: MenuEventArgs) {
if (args.item.text === 'Save As...') {
dialogInstance.show();
}
}
return (
<div className="container">
<div id='target'>Right click / Touch hold to open the ContextMenu</div>
<DialogComponent width='200px' height='110px' content='This file can be saved as PDF' buttons={buttons} visible={visible} position={position} ref={dialog => dialogInstance = dialog as DialogComponent} />
<ContextMenuComponent id='contextmenu' target='#target' items={menuItems} select={select}/>
</div>
);
}
export default App;
ReactDom.render(<App />,document.getElementById('element'));