React DropDownButton - Add Tooltips to Items

The useItemTextAsTitle property adds a title attribute to each item element with the item's display text. The browser displays this text as a native tooltip when a user hovers over the item.

App.tsx
  • import React from 'react';
  • import 'devextreme/dist/css/dx.fluent.blue.light.css';
  • import { DropDownButton } from 'devextreme-react/drop-down-button';
  •  
  • const items = [
  • { id: 1, name: 'Item 1' },
  • { id: 2, name: 'Item 2' }
  • ];
  •  
  • export default function App() {
  • return (
  • <DropDownButton
  • useItemTextAsTitle={true}
  • items={items}
  • displayExpr="name"
  • valueExpr="id"
  • />
  • );
  • }

If you use a custom itemTemplate, add a title attribute to the root element of the template to display the item text as a native browser tooltip:

App.tsx
  • import React from 'react';
  • import 'devextreme/dist/css/dx.fluent.blue.light.css';
  • import { DropDownButton } from 'devextreme-react/drop-down-button';
  •  
  • const items = [
  • { id: 1, name: 'John Doe', email: 'john@example.com' },
  • { id: 2, name: 'Jane Smith', email: 'jane@example.com' }
  • ];
  •  
  • function ItemTemplate({ data }: { data: { name: string; email: string } }) {
  • return (
  • <div title={data.name}>
  • <b>{data.name}</b><br/>
  • <small>{data.email}</small>
  • </div>
  • );
  • }
  •  
  • export default function App() {
  • return (
  • <DropDownButton
  • items={items}
  • displayExpr="name"
  • valueExpr="id"
  • itemRender={(data) => <ItemTemplate data={data} />}
  • />
  • );
  • }

To display a styled Tooltip with custom content, initialize the Tooltip component inside itemTemplate:

App.tsx
  • import React from 'react';
  • import 'devextreme/dist/css/dx.fluent.blue.light.css';
  • import { DropDownButton } from 'devextreme-react/drop-down-button';
  • import { Tooltip } from 'devextreme-react/tooltip';
  •  
  • const items = [
  • { id: 1, name: 'Item 1', description: 'Detailed description 1' },
  • { id: 2, name: 'Item 2', description: 'Detailed description 2' }
  • ];
  •  
  • function ItemTemplate({ data }: { data: { id: number; name: string; description: string } }) {
  • return (
  • <div>
  • <span id={`item_${data.id}`}>{data.name}</span>
  • <Tooltip
  • target={`#item_${data.id}`}
  • showEvent="mouseenter"
  • hideEvent="mouseleave">
  • {data.description}
  • </Tooltip>
  • </div>
  • );
  • }
  •  
  • export default function App() {
  • return (
  • <DropDownButton
  • items={items}
  • displayExpr="name"
  • valueExpr="id"
  • itemRender={(data) => <ItemTemplate data={data} />}
  • />
  • );
  • }

You can replace "mouseenter" and "mouseleave" with other events such as "click", "dblclick", "focus", or "blur" to control when the Tooltip is displayed.

To display a tooltip only for truncated items, compare element.scrollWidth with element.clientWidth and set the title attribute or display the Tooltip conditionally.

To display HTML content in the tooltip, return an HTML string from contentTemplate in the Tooltip component or from itemTemplate.