Search results

Open a dialog on popup item click in React SplitButton component

25 Jan 2023 / 2 minutes to read

This section explains about how to open a dialog on SplitButton popup item click. This can be achieved by handling dialog open in select event of the SplitButton.

In the following example, Dialog will open while selecting Update... item.

Source
Preview
app.jsx
index.html
style.css
app.tsx
Copied to clipboard
import { enableRipple } from '@syncfusion/ej2-base';
import { DialogComponent } from '@syncfusion/ej2-react-popups';
import { SplitButtonComponent } from '@syncfusion/ej2-react-splitbuttons';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
// To render SplitButton.
function App() {
    let dialogRef;
    let items = [
        {
            text: 'Help'
        },
        {
            text: 'About'
        },
        {
            text: 'Update...'
        }
    ];
    let alertDlgButtons = [{
            buttonModel: {
                content: 'Ok',
                isPrimary: true
            },
            'click': () => {
                hide();
            }
        },
        {
            buttonModel: {
                content: 'Cancel',
                isPrimary: true
            },
            'click': () => {
                hide();
            }
        }];
    function hide() {
        dialogRef.hide();
    }
    function select(args) {
        if (args.item.text === 'Update...') {
            dialogRef.show();
        }
    }
    return (<div>
       <DialogComponent ref={dialog => dialogRef = dialog} width='250px' id='dialog' content='Are you sure want to update?' header='Software Update' buttons={alertDlgButtons} visible={false}/>
       <SplitButtonComponent items={items} select={select}>Help</SplitButtonComponent>
      </div>);
}
export default App;
ReactDom.render(<App />, document.getElementById('button'));
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
            
    <title>Syncfusion React SplitButton</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.4.38/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/20.4.38/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/20.4.38/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/20.4.38/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
	<link href="style.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='button'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>
Copied to clipboard
#loader {
  color: #008cff;
  height: 40px;
  left: 45%;
  position: absolute;
  top: 45%;
  width: 30%;
}
Copied to clipboard
import { enableRipple } from '@syncfusion/ej2-base';
import { DialogComponent } from '@syncfusion/ej2-react-popups';
import { ItemModel, MenuEventArgs, SplitButtonComponent } from '@syncfusion/ej2-react-splitbuttons';
import * as React from 'react';
import * as ReactDom from 'react-dom';

enableRipple(true);

// To render SplitButton.
function App() {
    let dialogRef: DialogComponent;
    let items: ItemModel[] = [
    {
        text: 'Help'
    },
    {
        text: 'About'
    },
    {
        text: 'Update...'
    }];

    let alertDlgButtons: any [] = [{
        buttonModel: {
            content: 'Ok',
            isPrimary: true
        },
        'click': () => {
            hide();
        }
    },
    {
        buttonModel: {
            content: 'Cancel',
            isPrimary: true
        },
        'click': () => {
            hide();
        }
    }];
    function hide() {
        dialogRef.hide();
    }
    function select (args: MenuEventArgs) {
        if (args.item.text === 'Update...') {
            dialogRef.show();
        }
    }

    return (
    <div>
       <DialogComponent ref={dialog => dialogRef = dialog as DialogComponent} width='250px' id='dialog' content='Are you sure want to update?' header='Software Update' buttons={alertDlgButtons} visible={false}/>
       <SplitButtonComponent items = {items} select={select}>Help</SplitButtonComponent>
      </div>
    );
}
export default App;
ReactDom.render(<App />,document.getElementById('button'));