DevExtreme React - Component Configuration Syntax
To help you configure DevExtreme widgets in a React-style code structure, we implemented configuration components. These are elements nested in JSX code as if they represented a widget's sub-elements, but from the technical point of view, they only carry the configuration data. This means that widget options can be supplied declaratively and selectively, with default fallbacks in place.
Options of the Object Type
Use nested configuration components. In the following example, we configure the Chart widget's tooltip option:
import Chart, { Tooltip } from 'devextreme-react/chart'; class App extends React.Component { render() { return ( <Chart> <Tooltip enabled={true} format="thousands" /> </Chart> ); } }
Certain object type options are not implemented as nested configuration components because they depend on other options' values and cannot be typed (editorOptions in the DataGrid, editorOptions in the Form, widget options in the Toolbar). These options should be specified with an object. We recommend that you declare the object outside the configuration component to prevent unnecessary re-rendering.
import DataGrid, { Column } from 'devextreme-react/data-grid'; class App extends React.Component { columnEditorOptions = { width: 100 }; render() { return ( <DataGrid> <Column editorOptions={this.columnEditorOptions} /> </DataGrid> ); } }
Collections
Use nested configuration components. The following example shows how to configure the DataGrid widget's columns option:
import DataGrid, { Column } from 'devextreme-react/data-grid'; class App extends React.Component { render() { return ( <DataGrid> <Column dataField="firstName" caption="First Name" /> <Column dataField="lastName" caption="Last Name" defaultVisible={true} /> </DataGrid> ); } }
DevExtreme collection widgets also support the Item
component. It allows you to declare collection items in the widget markup. An Item
element can contain custom markup and have attributes that control parts of item appearance, such as badge
in the following code. The attributes are described in the Default Item Template section of each collection widget.
import List, { Item } from 'devextreme-react/list'; class App extends React.Component { render() { return ( <List> <Item>Orange</Item> <Item badge="New">White</Item> <Item>Black</Item> </List> ); } }
Event Handling
import Button from 'devextreme-react/button'; class App extends React.Component { constructor(props) { super(props); // Uncomment the line below to bind the handler to the React component's context, for example, to call this.setState() // this.handleButtonClick = this.handleButtonClick.bind(this); } render() { return ( <Button onClick={this.handleButtonClick} /> ); } handleButtonClick(e) { alert("The button was clicked") } }
Callback Functions
import VectorMap, { Layer } from 'devextreme-react/vector-map'; class App extends React.Component { render() { return ( <VectorMap> <Layer customize={this.customizeLayers} /> </VectorMap> ); } customizeLayers(elements) { // ... } }
Callback functions are executed outside the React component's context. If the context is important, explicitly bind the callback function to it in the constructor.
class App extends React.Component { myCountry: string = 'USA'; // we need to access this context variable in the callback function constructor(props) { super(props); this.customizeLayers = this.customizeLayers.bind(this); } customizeLayers(elements) { let country = this.myCountry; // ... } // ... }
Declare Content in the Markup
The following widgets allow you to declare their content directly in the markup:
- Drawer
- DropDownBox
- HtmlEditor
- Popover
- Popup
- Resizable
- ScrollView
- SlideOutView
- Tooltip
- ValidationGroup
The following is an example with ScrollView:
import ScrollView from 'devextreme-react/scroll-view'; class App extends React.Component { render() { return ( <ScrollView> <div>Some scrollable content</div> </ScrollView> ); } }
Markup Customization
Templates allow you to customize widget elements. In the DevExtreme API, template options end with Template
: itemTemplate
, groupTemplate
, contentTemplate
. When you specify them in React, replace Template
in the name with Render
or Component
, depending on whether the template is a rendering function or custom component. For example, instead of itemTemplate
, use itemRender
or itemComponent
.
If the option is called template
, without any prefix, (in the Button, Drawer, and other widgets), use the render
or component
attribute instead.
Using a Rendering Function
In the following code, rendering functions are used to specify the List's itemTemplate and the Button's template:
import List from 'devextreme-react/list'; import Button from 'devextreme-react/button'; const renderListItem = (itemData) => { return <p>{itemData.itemProperty}</p>; } const renderButton = (button) => { return <div style={{ padding: 20 }}><p>{button.text}</p></div>; } class App extends React.Component { render() { return ( <React.Fragment> <List itemRender={renderListItem} /> <Button render={renderButton} /> </React.Fragment> ); } }
Using a Custom Component
You can define the template markup in a separate component. We recommend using React.PureComponent
because React.Component
can be re-rendered unnecessarily. Alternatively, you can implement the shouldComponentUpdate() method.
In the following code, custom components are used to specify the List's itemTemplate and the Button's template. Template variables are passed to the components as props.
import List from 'devextreme-react/list'; import Button from 'devextreme-react/button'; class ListItemTmpl extends React.PureComponent { render() { return ( <p>{this.props.data.itemProperty}</p> ); } } class ButtonTmpl extends React.PureComponent { render() { return ( <div style={{ padding: 20 }}> <p>{this.props.data.text}</p> </div> ); } } class App extends React.Component { render() { return ( <React.Fragment> <List itemComponent={ListItemTmpl} /> <Button component={ButtonTmpl} /> </React.Fragment> ); } }
Using the Template Component
Several options are not implemented as nested configuration components (editorOptions in the DataGrid, editorOptions in the Form, widget options in the Toolbar). These options do not have the render
or component
attribute to which you would pass your rendering function or custom component. However, you can still customize the markup — using the Template
component.
The Template
component declares a named template. Its name
property should be assigned to a ...Template
option of the widget that uses the Template
. The template's markup can be specified as follows:
Rendering function
Pass the rendering function to theTemplate
'srender
property:App.jsdata.jsimport Form, { Item } from 'devextreme-react/form'; import { Template } from 'devextreme-react/core/template'; import service from './data.js'; const renderSelectBoxItem = item => { return <div>{item.toUpperCase()}</div>; } class App extends React.Component { constructor(props) { super(props); this.employee = service.getEmployee(); this.positions = service.getPositions(); this.positionEditorOptions = { items: this.positions, value: '', itemTemplate: 'selectBoxItem' }; } render() { return ( <Form formData={this.employee}> <Item dataField="Position" editorType="dxSelectBox" editorOptions={this.positionEditorOptions} /> <Template name="selectBoxItem" render={renderSelectBoxItem} /> </Form> ); } } export default App;
const employee = { ID: 1, FirstName: 'John', LastName: 'Heart', Position: 'CEO', BirthDate: '1964/03/16', HireDate: '1995/01/15', Address: '351 S Hill St., Los Angeles, CA', Phone: '360-684-1334', Email: 'jheart@dx-email.com' }; const positions = [ 'HR Manager', 'IT Manager', 'CEO', 'Controller', 'Sales Manager', 'Support Manager', 'Shipping Manager' ]; export default { getEmployee() { return employee; }, getPositions() { return positions; } }
Custom component
Assign the custom component to theTemplate
'scomponent
property:App.jsdata.jsimport Form, { Item } from 'devextreme-react/form'; import { Template } from 'devextreme-react/core/template'; import service from './data.js'; class SelectBoxItemTmpl extends React.PureComponent { render() { return ( <div>{this.props.data.toUpperCase()}</div> ); } } class App extends React.Component { constructor(props) { super(props); this.employee = service.getEmployee(); this.positions = service.getPositions(); this.positionEditorOptions = { items: this.positions, value: '', itemTemplate: 'selectBoxItem' }; } render() { return ( <Form formData={this.employee}> <Item dataField="Position" editorType="dxSelectBox" editorOptions={this.positionEditorOptions} /> <Template name="selectBoxItem" component={SelectBoxItemTmpl} /> </Form> ); } } export default App;
const employee = { ID: 1, FirstName: 'John', LastName: 'Heart', Position: 'CEO', BirthDate: '1964/03/16', HireDate: '1995/01/15', Address: '351 S Hill St., Los Angeles, CA', Phone: '360-684-1334', Email: 'jheart@dx-email.com' }; const positions = [ 'HR Manager', 'IT Manager', 'CEO', 'Controller', 'Sales Manager', 'Support Manager', 'Shipping Manager' ]; export default { getEmployee() { return employee; }, getPositions() { return positions; } }
Call Methods
To call widget methods, you need the widget instance. Create a ref and attach it to the target component via the ref
attribute. Implement a getter that returns the instance taken from the ref. In the following code, this approach is used to get a TextBox
instance:
import Button from 'devextreme-react/button'; import TextBox from 'devextreme-react/text-box'; class App extends React.Component { constructor(props) { super(props); this.textBoxRef = React.createRef(); this.focusTextBox = () => { this.textBox.focus() }; } get textBox() { return this.textBoxRef.current.instance; } render() { return ( <div> <TextBox ref={this.textBoxRef} /> <Button text="Focus TextBox" onClick={this.focusTextBox} /> </div> ); } }
Alternatively, you can save the widget instance in a component property once the widget is initialized:
import Button from 'devextreme-react/button'; import TextBox from 'devextreme-react/text-box'; class App extends React.Component { constructor(props) { super(props); this.saveTextBoxInstance = this.saveTextBoxInstance.bind(this); this.focusTextBox = this.focusTextBox.bind(this); } saveTextBoxInstance(e) { this.textBoxInstance = e.component; } focusTextBox() { this.textBoxInstance.focus(); } render() { return ( <div> <TextBox onInitialized={this.saveTextBoxInstance} /> <Button text="Focus TextBox" onClick={this.focusTextBox} /> </div> ); } }
Data Layer
DevExtreme Data Layer is a set of components for working with data. The following example shows how to use the DataSource component with the List UI component:
import DataSource from 'devextreme/data/data_source'; import List from 'devextreme-react/list'; const items = [ { text: '123' }, { text: '234' }, { text: '567' } ]; class Example extends React.Component { constructor(props) { super(props); this.dataSource = new DataSource({ store: { type: 'array', data: items }, sort: { getter: 'text', desc: true } }); } render() { return ( <List dataSource={this.dataSource} /> ); } componentWillUnmount() { // A DataSource instance created outside a widget should be disposed of manually this.dataSource.dispose(); } }
DevExtreme Validation Features
In the following example, two textboxes are placed in a validation group that is validated on a button click. Each textbox has a set of validation rules. The validation result is displayed under the textboxes in a validation summary.
import TextBox from 'devextreme-react/text-box'; import Validator, { ValidationRule } from 'devextreme-react/validator'; import ValidationGroup from 'devextreme-react/validation-group'; import Button from 'devextreme-react/button'; class App extends React.Component { constructor(props) { super(props); this.email = null; this.password = null; } render() { return ( <ValidationGroup> <TextBox value={this.email}> <Validator> <ValidationRule type="required" message="Email is required" /> <ValidationRule type="email" message="Email is invalid" /> </Validator> </TextBox> <TextBox value={this.password} mode="password"> <Validator> <ValidationRule type="required" message="Password is required" /> </Validator> </TextBox> <Button onClick={this.validate} text="Submit" /> </ValidationGroup> ); } validate(params) { let result = params.validationGroup.validate(); if (result.isValid) { // The values are valid // Submit them... // ... // ... and then reset // params.validationGroup.reset(); } } }
Refer to the Data Validation article for more information.
If you have technical questions, please create a support ticket in the DevExpress Support Center.