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, add a ListBox component to your project, style the component, and activate your license.
After completing this guide, you will be able to reproduce the following example.
Setting Up Your React Project
Before you install the KendoReact ListBox, make sure that you have a running React project. The easiest way to set up a React project is to use the Create React App approach that is described in the Get Started with KendoReact article.
Installing the ListBox Package
All KendoReact packages are distributed through npm and offer a similar installation experience. To use the ListBox component, start with the installation of the ListBox npm package and its dependencies. Use Node.js v5.0.0 or later.
npm install --save @progress/kendo-react-listbox @progress/kendo-react-intl @progress/kendo-licensing @progress/kendo-svg-icons
Importing the Component
After installing the package, import the ListBox component in the React App. To provide a flowless data transfer between multiple ListBox components, import the processListBoxData
method.
In the src/App.js
file of your React project, add the imports.
// ES2015 module syntax
import { ListBox, processListBoxData } from "@progress/kendo-react-conversational-ui";
// CommonJS format
const { ListBox, processListBoxData } = require('@progress/kendo-react-conversational-ui');
Using the Component
-
Import the data for the ListBox.
import { data } from './data';
-
Create a state variable to hold the data for the two ListBox components.
const [state, setState] = React.useState({ employees: data, developers: [] });
-
Set the selectField value. That value will determine which items are selected.
const SELECTED_FIELD = 'selected';
-
Handle the onItemClick event of the ListBox to select or deselect items.
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; }) }); }
-
Handle the onToolClick event of the ListBoxToolbar to update the lists.
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 }); }
-
Add the markup of the two components to the
src/App.js
file in your project.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> );
-
To style the ListBox, install and import the Default theme, which is one of the three beautiful themes for KendoReact.
7.1. Install the Default theme package.
npm install --save @progress/kendo-theme-default
7.2. Import the CSS file from the package in
src/App.js
. Add this import before your existingApp.css
import.import '@progress/kendo-theme-default/dist/all.css';
-
Build and run the application by typing the following command in the root folder of your project:
npm start
-
Navigate to http://localhost:3000 to see the KendoReact ListBox component on the page.
Activating Your License Key
Using any of the UI components in the KendoReact library requires either a commercial license key or an active trial license key.
Follow the instructions on the KendoReact My License page to activate your trial or commercial license. You can skip this step if your application already contains a KendoReact license file.
Dependencies
The ListBox package requires you to install the following peer dependencies in your application:
Package Name | Description |
---|---|
react 16.8.2* | Contains the functionality necessary to define React components. |
react-dom | Contains the React renderer for the web. |
@progress/kendo-licensing | Contains the internal infrastructure related to licensing. |
@progress/kendo-react-intl | Contains 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-icons | Contains the KendoReact SVG icons. |