React ComboBox Templates
Ignite UI for React ComboBox 구성 요소를 사용하면 항목, 그룹 헤더, 빈 목록 및 아이콘과 같은 다양한 영역에 대한 사용자 지정 템플릿을 정의할 수 있습니다.
ComboBox Templates Example
export interface City {
id: string;
name: string;
country: string;
}
export const Cities: City[] = [
{ name: "London", id: "UK01", country: "UK" },
{ name: "Manchester", id: "UK02", country: "UK" },
{ name: "Birmingham", id: "UK03", country: "UK" },
{ name: "Glasgow", id: "UK04", country: "UK" },
{ name: "Liverpool", id: "UK05", country: "UK" },
{ name: "New York", id: "US01", country: "USA" },
{ name: "Miami", id: "US02", country: "USA" },
{ name: "Philadelphia", id: "US03", country: "USA" },
{ name: "Chicago", id: "US04", country: "USA" },
{ name: "Springfield", id: "US05", country: "USA" },
{ name: "Los Angeles", id: "US06", country: "USA" },
{ name: "Houston", id: "US07", country: "USA" },
{ name: "Phoenix", id: "US08", country: "USA" },
{ name: "San Diego", id: "US09", country: "USA" },
{ name: "Dallas", id: "US010", country: "USA" },
{ name: "Sofia", id: "BG01", country: "Bulgaria" },
{ name: "Plovdiv", id: "BG02", country: "Bulgaria" },
{ name: "Varna", id: "BG03", country: "Bulgaria" },
{ name: "Burgas", id: "BG04", country: "Bulgaria" },
{ name: "Rome", id: "IT01", country: "Italy" },
{ name: "Milan", id: "IT02", country: "Italy" },
{ name: "Naples", id: "IT03", country: "Italy" },
{ name: "Turin", id: "IT04", country: "Italy" },
{ name: "Palermo", id: "IT05", country: "Italy" },
{ name: "Florence", id: "IT06", country: "Italy" },
];
ts
import React, { useEffect } from "react";
import ReactDOM from "react-dom/client";
import {
IgrCombo,
ComboTemplateProps,
IgrIcon,
registerIconFromText
} from "@infragistics/igniteui-react";
import "igniteui-webcomponents/themes/light/bootstrap.css";
import { Cities, City } from "./data";
import "./index.css";
export default function ComboTemplates() {
const downIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#1f1f1f"><path d="M480-200 240-440l56-56 184 183 184-183 56 56-240 240Zm0-240L240-680l56-56 184 183 184-183 56 56-240 240Z"/></svg>';
const clearIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#1f1f1f"><path d="M120-280v-80h560v80H120Zm80-160v-80h560v80H200Zm80-160v-80h560v80H280Z"/></svg>';
useEffect(() => {
registerIconFromText("down", downIcon, "material");
registerIconFromText("clear", clearIcon, "material");
}, []);
const renderGroupHeaderTemplate = (args: ComboTemplateProps<City>) => {
return <span>Country of {args.item.country}</span>;
};
const renderItemTemplate = (args: ComboTemplateProps<City>) => {
const item = args.item;
return (
<span>
<b>{item.name}</b> [{item.id}] - {item.country}
</span>
);
};
return (
<div className="sample">
<IgrCombo
valueKey="id"
displayKey="name"
groupKey="country"
data={Cities}
itemTemplate={renderItemTemplate}
groupHeaderTemplate={renderGroupHeaderTemplate}
>
<header slot="header" className="combo-padding">
<strong>Select a City</strong>
<div>List of countries and their most popular cities</div>
</header>
<footer slot="footer" className="combo-padding">
<em>
Tip: Start typing to find your city if you have troubles finding it.
</em>
</footer>
<span slot="toggle-icon">
<IgrIcon name="down" collection="material"></IgrIcon>
</span>
<span slot="clear-icon">
<IgrIcon name="clear" collection="material"></IgrIcon>
</span>
</IgrCombo>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<ComboTemplates />);
tsx
.combo-padding {
padding: 12px 16px;
}css
Template Types
Item Template
정의된 itemTemplate 경우 옵션 목록에서 항목을 렌더링할 때 사용해야 하는 사용자 지정 템플릿입니다.
type City = {
name: string;
id: string;
country: string;
};
const renderItemTemplate = (args: ComboTemplateProps<City>) => {
const item = args.item;
return (
<span>
<b>{item.name}</b> [{item.id}] - {item.country}
</span>
);
};
<IgrCombo
valueKey="id"
displayKey="name"
groupKey="country"
data={Cities}
itemTemplate={renderItemTemplate}
></IgrCombo>
tsx
정의된 groupHeaderTemplate 경우 옵션 목록에서 그룹 헤더를 렌더링할 때 사용해야 하는 사용자 지정 템플릿입니다.
<IgrCombo
valueKey="id"
displayKey="name"
groupKey="country"
data={cities}
groupHeaderTemplate={renderGroupHeaderTemplate}
></IgrCombo>
const renderGroupHeaderTemplate = (args: ComboTemplateProps<City>) => {
return (
<span>Country of {args.item.country}</span>
);
}
tsx
Slots
사용자 지정 템플릿 외에도 Ignite UI for React ComboBox 구성 요소는 사용자가 사용자 지정 콘텐츠를 다른 콤보 부분에 전달할 수 있는 여러 슬롯을 노출합니다.
옵션 목록 위에 사용자 정의 헤더를 렌더링하려면 컨텐츠를 header 슬롯에 전달하십시오.
<IgrCombo>
<header slot="header">
Header content goes here
</header>
</IgrCombo>
tsx
옵션 목록 아래에 사용자 정의 바닥글을 렌더링하려면 콘텐츠를 footer 슬롯에 전달합니다.
<IgrCombo>
<footer slot="footer">
Footer content goes here
</footer>
</IgrCombo>
tsx
Empty List Slot
필터링 작업에서 결과가 반환되지 않을 때 사용자 지정 콘텐츠를 렌더링하려면 empty 슬롯을 사용하세요.
<IgrCombo>
<div slot="empty">¯\_(ツ)_/¯</div>
</IgrCombo>
tsx
Toggle Icon Slot
콤보 입력의 토글 아이콘은 toggle-icon 슬롯을 통해 수정할 수도 있습니다.
<IgrCombo>
<span slot="toggle-icon">
<IgrIcon name="down" collection="material"></IgrIcon>
</span>
</IgrCombo>
tsx
Clear Icon Slot
클리어 아이콘은 clear-icon 슬롯을 통해 변경할 수 있습니다.
<IgrCombo>
<span slot="clear-icon">
<IgrIcon name="clear" collection="material"></IgrIcon>
</span>
</IgrCombo>
tsx
Additional Resources