Getting Started with the KendoReact ListBox

This guide provides essential information about using the KendoReact ListBox package—you will learn how to install the ListBox package and add a free React ListBox component to your project.

This is a Free React ListBoxThe KendoReact ListBox is free to use, including in production—no sign-up or license required. Check out all 120+ free and premium UI components in the enterprise-grade KendoReact library.

After completing this guide, you will have a free React ListBox up and running.

Change Theme
Theme
Loading ...

Before You Begin

sh
npm create vite@latest my-app -- --template react

This guide requires that you have basic knowledge of React and TypeScript, and that you have already created a blank React project.

You can speed up the development of your KendoReact application with the Kendo UI Template Wizard for Visual Studio Code.

Install the Component

sh
npm i @progress/kendo-react-listbox @progress/kendo/theme/default

Run these commands in the root of your React project to install the KendoReact ListBox package and its dependencies, and the Kendo UI Default theme.

Import the Component

tsx
import {
    ListBox,
    ListBoxToolbar,
    processListBoxData,
    ListBoxToolbarClickEvent,
    ListBoxItemClickEvent
} from '@progress/kendo-react-listbox';
import '@progress/kendo-theme-default/dist/all.css';

Place the import statements in the App component file (for example: src/App.tsx) for your project.

Use the Component

  1. Import the data for the ListBox.

    tsx
    import { data } from './data';
  2. Create a state variable to hold the data for the two ListBox components.

    tsx
    const [state, setState] = React.useState({
        employees: data,
        developers: []
    });
  3. Set the selectedField value. That value will determine which items are selected.

    tsx
    const SELECTED_FIELD = 'selected';
  4. Handle the onItemClick event of the ListBox to select or deselect items.

    tsx
    const handleItemClick = (event, data, connectedData) => {
        setState({
            ...state,
            [data]: state[data].map((item) => {
                if (item.name === event.dataItem.name) {
                    item[SELECTED_FIELD] = !item[SELECTED_FIELD];
                } else if (!event.nativeEvent.ctrlKey) {
                    item[SELECTED_FIELD] = false;
                }
                return item;
            }),
            [connectedData]: state[connectedData].map((item) => {
                item[SELECTED_FIELD] = false;
                return item;
            })
        });
    };
  5. Handle the onToolClick event of the ListBoxToolbar to update the lists.

    tsx
    const handleToolBarClick = (e: ListBoxToolbarClickEvent) => {
        let toolName: string = e.toolName || '';
        let result: any = processListBoxData(state.employees, state.developers, toolName, SELECTED_FIELD);
        setState({
            ...state,
            employees: result.listBoxOneData,
            developers: result.listBoxTwoData
        });
    };
  6. Add the markup of the two components to the src/App.js file in your project.

    tsx
    return (
        <div className="container">
            <div className="row justify-content-center">
                <div className="col k-pr-2">
                    <h6>Employees</h6>
                    <ListBox
                        style={{ height: 400, width: '100%' }}
                        data={state.employees}
                        textField="name"
                        selectedField={SELECTED_FIELD}
                        onItemClick={(e: ListBoxItemClickEvent) => handleItemClick(e, 'employees', 'developers')}
                        toolbar={() => {
                            return (
                                <ListBoxToolbar
                                    tools={[
                                        'moveUp',
                                        'moveDown',
                                        'transferTo',
                                        'transferFrom',
                                        'transferAllTo',
                                        'transferAllFrom',
                                        'remove'
                                    ]}
                                    data={state.employees}
                                    dataConnected={state.developers}
                                    onToolClick={handleToolBarClick}
                                />
                            );
                        }}
                    />
                </div>
                <div className="col k-pl-0">
                    <h6>Developers</h6>
                    <ListBox
                        style={{ height: 400, width: '100%' }}
                        data={state.developers}
                        textField="name"
                        selectedField={SELECTED_FIELD}
                        onItemClick={(e: ListBoxItemClickEvent) => handleItemClick(e, 'developers', 'employees')}
                    />
                </div>
            </div>
        </div>
    );
  7. Build and run the application by typing the following command in the root folder of your project:

    sh
    npm run dev
  8. Navigate to http://localhost:3000 to see the KendoReact ListBox component on the page.

Style the Components

Are you looking for guidance around how to create visually appealing and consistent user interfaces with Telerik UI components? Check out the Progress Design System.

With the import "@progress/kendo-theme-default/dist/all.css"; statement present in your code, you already have professionally designed styling applied to your app out-of-box. You can also try any of the other available Kendo UI Themes.

KendoReact ListBox APIs

ListBox API

KendoReact ListBox Dependencies

The ListBox package requires you to install the following peer dependencies in your application:

Package NameDescription
react 16.8.2*Contains the functionality necessary to define React components.
react-domContains the React renderer for the web.
@progress/kendo-licensingContains the internal infrastructure related to licensing.
@progress/kendo-react-intlContains the KendoReact Internationalization package that applies the desired cultures by providing services and pipes for the parsing and formatting of dates and numbers.
@progress/kendo-svg-iconsContains the KendoReact SVG icons.
@progress/kendo-react-buttonsContains the KendoReact Buttons components.
@progress/kendo-react-commonContains common utilities that enhance the performance and functionalities of the KendoReact UI components.