React SelectBox - Customize Item Appearance
For a minor customization of SelectBox items, you can define specific fields in their data objects. For example, the following code generates three items: the first is not customized, the second is disabled and the third is hidden.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import SelectBox from 'devextreme-react/select-box';
- const products = [
- { text: "HD Video Player" },
- { text: "SuperHD Video Player", disabled: true },
- { text: "SuperPlasma 50", visible: false }
- ];
- export default function App(props) {
- return (
- <SelectBox ...
- dataSource={products}
- displayExpr="text"
- valueExpr="text"
- />
- );
- }
If you need a more flexible solution, specify itemTemplate.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import SelectBox from 'devextreme-react/select-box';
- const renderItem = (data) => {
- return (
- <div>
- <img src={data.imgSrc} />
- <div style={{display: 'inline-block'}}>{data.name}</div>
- </div>
- );
- }
- const selectBoxData = [{
- id: 1,
- name: "HD Video Player",
- imgSrc: "images/products/1-small.png"
- }, {
- id: 2,
- name: "UltraHD Player",
- imgSrc: "images/products/2-small.png"
- },
- // ...
- ];
- export default function App(props) {
- return (
- <SelectBox ...
- dataSource={selectBoxData}
- displayExpr="name"
- valueExpr="id"
- itemRender={renderItem}
- />
- );
- }
You can also customize individual SelectBox items. Declare them using the dxItem component.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { SelectBox, Item } from 'devextreme-react/select-box';
- export default function App(props) {
- return (
- <SelectBox>
- <Item>
- <span>User</span>
- </Item>
- <Item>
- <span>Comment</span>
- </Item>
- </SelectBox>
- );
- }
Using similar techniques, you can customize the input field of the SelectBox. The template for it should be assigned to the fieldTemplate property. Note that the input field must contain the TextBox UI component.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { SelectBox } from 'devextreme-react/select-box';
- import { TextBox } from 'devextreme-react/text-box';
- const renderField = (data) => {
- return (
- <div>
- <img src={data.imgSrc} />
- <TextBox style={{display: 'inline-block'}}
- defaultValue={data.name} />
- </div>
- );
- }
- const selectBoxData = [{
- id: 1,
- name: "HD Video Player",
- imgSrc: "images/products/1-small.png"
- }, {
- id: 2,
- name: "UltraHD Player",
- imgSrc: "images/products/2-small.png"
- },
- // ...
- ];
- export default function App(props) {
- return (
- <SelectBox ...
- dataSource={selectBoxData}
- value={1}
- displayExpr="name"
- valueExpr="id"
- fieldRender={renderField}
- />
- );
- }
See Also
If you have technical questions, please create a support ticket in the DevExpress Support Center.