React ButtonGroup - Getting Started

NOTE
Before you start the tutorial, ensure DevExtreme is installed in your application.

The ButtonGroup is a set of toggle buttons that can be used as a mode switcher.

This tutorial describes how to configure basic ButtonGroup features. We create a ButtonGroup that logs the names of the selected buttons into the browser's console (open the console to see them):

Each section in this tutorial covers a single configuration step. You can also find the full code in the GitHub repository.

View on GitHub

Create the ButtonGroup

You can use the following code to create the ButtonGroup:

App.js
  • import React from 'react';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { ButtonGroup } from 'devextreme-react/button-group';
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  • }
  •  
  • render() {
  • return (
  • <ButtonGroup
  • // Configuration goes here
  • />
  • );
  • }
  • }
  •  
  • export default App;

Add Buttons to the ButtonGroup

Buttons are configured by objects that can contain custom and predefined fields. For instance, the fontStyles array in the following code has four data objects with one predefined field (icon) and one custom field (style) each.

Assign the array to the items property and use the keyExpr to specify style as the key field. We log the field's values into the console at a later step.

App.js
  • // ...
  • const fontStyles = [{
  • icon: "bold",
  • style: "bold"
  • }, {
  • icon: "italic",
  • style: "italic"
  • }, {
  • icon: "underline",
  • style: "underline"
  • }, {
  • icon: "strike",
  • style: "strike"
  • }];
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <ButtonGroup
  • items={fontStyles}
  • keyExpr="style"
  • />
  • );
  • }
  • }
  •  
  • export default App;

If you run the code, you should see a ButtonGroup that contains four buttons but nothing happens when you click any of them.

Preselect a Button

Populate the selectedItemKeys array with the keys of the buttons that should be preselected. In this tutorial, we add the "italic" button to this array.

App.js
  • // ...
  • class App extends React.Component {
  • selectedFontStyleNames = [ 'italic' ];
  •  
  • render() {
  • return (
  • <ButtonGroup ...
  • defaultSelectedItemKeys={this.selectedFontStyleNames}
  • />
  • );
  • }
  • }
  •  
  • export default App;

If you run the code now, you should see the "italic" button initially selected.

Handle the Selection Change Event

Assign a function to the onSelectionChanged property. In this tutorial, the function logs the added or removed style's name to the browser's console:

App.js
  • // ...
  • class App extends React.Component {
  • // ...
  • render() {
  • return (
  • <ButtonGroup ...
  • onSelectionChanged={this.logSelectionChanged}
  • />
  • );
  • }
  •  
  • logSelectionChanged(e) {
  • if(e.addedItems[0]) {
  • console.log("The following style is added: " + e.addedItems[0].style);
  • }
  • if(e.removedItems[0]) {
  • console.log("The following style is removed: " + e.removedItems[0].style)
  • }
  • }
  • }

Run the code, open the browser's console, and select or clear the button selection in the ButtonGroup. You should see messages like the following:

  • The following style is added: underline
  • The following style is removed: bold

Enable Multiple Selection

Set the selectionMode property to "multiple" to allow users to select multiple buttons. You can also change the onSelectionChanged handler from the previous step to log all the selected styles in the console:

App.js
  • // ...
  • class App extends React.Component {
  • render() {
  • return (
  • <ButtonGroup ...
  • selectionMode="multiple"
  • onSelectionChanged={this.logSelectionChanged}
  • />
  • );
  • }
  •  
  • logSelectionChanged(e) {
  • const selectedItemKeys = e.component.option("selectedItemKeys");
  • let message;
  • if(selectedItemKeys.length > 0) {
  • message = "The following styles are selected: " + selectedItemKeys.join(", ");
  • } else {
  • message = "There are no selected styles"
  • }
  • console.log(message);
  • }
  • }
  •  
  • export default App;

Now you should be able to select multiple buttons and see messages like the following in the console:

  • The following styles are selected: bold, underline, strike

You have configured basic ButtonGroup features. To take a more detailed look at this UI component, explore the following resources: