Articles in this section
Category / Section

How to dynamically add icons to the menu items in the React Syncfusion Menu

3 mins read

When working with the Syncfusion Menu component in a React application, you may want to dynamically add icons to the leaf items based on certain conditions or user interactions. Below is a guide on how to achieve this functionality.

Prerequisites

Ensure you have the following installed:

  • Node.js and npm
  • React
  • Syncfusion React UI components

Step-by-Step Implementation

  1. Install Syncfusion React UI Components

    If you haven’t already, install the Syncfusion React UI components package:

    npm install @syncfusion/ej2-react-navigations --save
    
  2. Import the MenuComponent

    In your React component file, import the MenuComponent from the Syncfusion package:

    import { MenuComponent } from '@syncfusion/ej2-react-navigations';
    
  3. Define the Menu Structure

    Define the menu items with a structure that includes an iconCss property for each leaf item:

    this.state = {
      list: [
        {
          id: 'align',
          text: 'Align',
          items: [
            {
              id: 'alignCenter',
              text: 'Center',
              iconCss: 'e-icons e-tick',
            },
            {
              id: 'alignLeft',
              text: 'Left',
              iconCss: '',
            },
          ],
        },
        // ... other menu items
      ],
    };
    
  4. Implement the select handler

    Implement the select handler to update the iconCss property based on the selected item:

    onSelect = (args) => {
      // Update the iconCss for the selected item
      if (args.item.text === "Left" || args.item.text === "Center") {
        this.updateIconCss(args.item.id);
      }
    };
    
  5. Implement the updateIconCss Function

    Implement a function to update the iconCss property of the selected item:

    updateIconCss = (selectedItemId) => {
      const updatedList = this.state.list.map((item) => {
        if (item.items) {
          item.items = item.items.map((subItem) => {
            if (subItem.id === selectedItemId) {
              subItem.iconCss = 'e-icons e-tick'; // Add your desired icon class here
            } else {
              subItem.iconCss = ''; // Remove icon class
            }
            return subItem;
          });
        }
        return item;
      });
    
      this.setState({ list: updatedList });
    };
    
  6. Implement beforeClose handler

    Implement the beforeClose handler to maintain the iconCss property based on the selected item:

    beforeClose = (args) => {
       for (let i = 0; i < args.items.length; i++) {
         if (args.items[i].iconCss) {
             args.items[i].iconCss = "";
         }
         if (this.selectedItem == args.items[i].text) {
             args.items[i].iconCss = "e-icons e-tick";
         }
       }
    }
    
  7. Render the Menu Component

    Use the below code snippet to render Menu component.

    render() {
      let menuFields = [
       {
         text: 'File',
       },
       {
         text: 'Edit',
         items: this.state.list,
       },
       {
         text: 'Selection',
       },
       {
         text: 'View',
       },
       {
         text: 'Go',
       },
       {
         text: 'Run',
       }
     ];
    
      return (
        <div className="container">
          <MenuComponent
            items={menuFields}
            select={this.onSelect}
            beforeClose = {this.beforeClose}
            // ... other props
          />
        </div>
      );
    }
    
  8. Styling the Icons

    Ensure you have the appropriate CSS styles for the icons. You can use the default Syncfusion icons or your own custom icons.

Demo

To see a live demonstration of the above implementation, please visit the following sample link: Add icons dynamically to the menu item

Conclusion

By following these steps, you can dynamically add icons to the leaf items in the Syncfusion Menu Component based on user selection or other conditions.

Additional References

Please note that the code snippets provided above are for guidance and may require adjustments to fit the specific requirements of your application.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied