Combo Templates
The Ignite UI for React Combo Templates replace the default content of the Combo areas: the items, the group headers, the header and the footer of the list, the empty list, the toggle and the clear icons, and the input field.
The React Combo renders its items and group headers through the itemTemplate and groupHeaderTemplate properties, and takes the content of the other areas through its slots. Use a <span> for simple text or symbols, and an Icon for icons. The header and footer slots also accept the <header> and <footer> elements.
Live Demo
The sample shows a Destinations combo with cities grouped by region. Each switch applies one template to the combo, and the Selection list below the combo shows the selected cities.
Types
The React Combo has a template for each area that it renders. The following sections show the property or the slot that takes each template.
The snippets bind the Combo to destinations that have an id, a name, a country, a countryCode, a region, and a UTC offset, with id as the valueKey, name as the displayKey, and region as the groupKey.
Item Template
The item template renders the content of each option in the list. By default, an option shows its displayKey value, and the checkbox of the option stays in front of the template content.
The item and the group header templates render inside the shadow DOM of the Combo, where the styles of the page don’t reach. Style their containers through the item and group-header CSS parts, and lay out their content with inline styles. The Combo lays out each root element of a template as a block, so wrap the content of a template in a single element.
Pass a function that receives the item and returns its content to the itemTemplate property:
import { IgrCombo, ComboTemplateProps } from 'igniteui-react';
const renderItem = ({ item }: ComboTemplateProps<Destination>) => (
<span>
<img src={`https://dl.infragistics.com/x/img/flags/${item.countryCode}.png`} alt="" style={{ width: '1.25rem' }} />
{item.name} — {item.country} · {item.id} · {item.offset}
</span>
);
<IgrCombo data={destinations} valueKey="id" displayKey="name" groupKey="region" itemTemplate={renderItem} />
Header Item Template
The header item template renders the header of each group when the Combo has a groupKey. By default, a group header shows the group value. The template receives a header record, not a data item: its groupKey field holds the group value, so a count of the options in the group comes from the data of the Combo, as the citiesIn method of the snippets does.
Pass the function to the groupHeaderTemplate property:
const renderGroupHeader = ({ item }: ComboTemplateProps<Destination>) => (
<span style={{ display: 'flex', gap: '0.5rem', width: '100%' }}>
<strong>{item.region}</strong>
<span style={{ marginInlineStart: 'auto' }}>{citiesIn(item.region)} cities</span>
</span>
);
<IgrCombo data={destinations} valueKey="id" displayKey="name" groupKey="region" groupHeaderTemplate={renderGroupHeader} />
Header Template
The header template renders content between the search input and the list of options, such as a title of the list or an action that applies to it. The Combo renders no header by default.
Pass the content to the header slot. The snippet adds a button that selects all options, through the select method called without an argument:
const comboRef = useRef<IgrCombo>(null);
<IgrCombo ref={comboRef} data={destinations} valueKey="id" displayKey="name" groupKey="region">
<header slot="header" className="list-header">
<span>Destinations by region</span>
<IgrButton variant="flat" onClick={() => comboRef.current?.select()}>Select all</IgrButton>
</header>
</IgrCombo>
Footer Template
The footer template renders content below the list of options, such as a summary of the selection or an action that applies to it. The Combo renders no footer by default.
Pass the content to the footer slot. The snippet keeps the selected values in the state, and clears the selection through the deselect method called without an argument:
const [selected, setSelected] = useState<string[]>([]);
<IgrCombo ref={comboRef} data={destinations} valueKey="id" displayKey="name" groupKey="region"
onChange={(e) => setSelected(e.detail.newValue as string[])}>
<footer slot="footer" className="list-footer">
<span>{selected.length} selected</span>
<IgrButton variant="flat" onClick={() => comboRef.current?.deselect()}>Clear</IgrButton>
</footer>
</IgrCombo>
Empty Template
The empty template renders in place of the list when the search text matches no option, or when the Combo has no data. By default, the Combo shows a message that the list is empty.
Pass the content to the empty slot:
<IgrCombo data={destinations} valueKey="id" displayKey="name" groupKey="region">
<span slot="empty" className="empty-list">No city matches your search. Try another spelling.</span>
</IgrCombo>
Toggle Icon Template
The toggle icon template replaces the icon of the toggle button in the input field, which opens and closes the dropdown.
Pass the icon to the toggle-icon slot. The slot content shows both when the dropdown is closed and when it is open. Register the icon first, as the Icon topic shows:
import { IgrCombo, IgrIcon, registerIconFromText } from 'igniteui-react';
registerIconFromText('flight', flightIcon, 'material');
<IgrCombo data={destinations} valueKey="id" displayKey="name">
<IgrIcon slot="toggle-icon" name="flight" collection="material" />
</IgrCombo>
Clear Icon Template
The clear icon template replaces the icon of the clear button in the input field. The Combo shows the clear button only when it has a selection, and hides it when disableClear is set.
Pass the icon to the clear-icon slot:
<IgrCombo data={destinations} valueKey="id" displayKey="name">
<IgrIcon slot="clear-icon" name="close" collection="material" />
</IgrCombo>
Templated Input
The input field of the Combo takes a label, content before and after its text, and a helper text below it, the way the Input does.
Set the label property, pass the content before and after the text to the prefix and suffix slots, and the helper text to the helper-text slot. The suffix slot renders after the clear icon and before the toggle icon:
<IgrCombo data={destinations} valueKey="id" displayKey="name" label="Destinations" placeholder="Pick one or more cities">
<IgrIcon slot="prefix" name="place" collection="material" />
<IgrBadge slot="suffix">{selected.length}</IgrBadge>
<span slot="helper-text">{selected.length} of {destinations.length} destinations selected</span>
</IgrCombo>
API References
Additional Resources
The following resources provide more React Combo guidance and project support: