Combo (コンボ) テンプレート
Ignite UI for Web Components Combo のテンプレートは、Combo の各領域 (項目、グループ ヘッダー、リストのヘッダーとフッター、空のリスト、トグル アイコンとクリア アイコン、入力フィールド) の既定のコンテンツを置き換えます。
Web Components の Combo は、itemTemplate プロパティと groupHeaderTemplate プロパティで項目とグループ ヘッダーを描画し、その他の領域のコンテンツはスロットで受け取ります。単純なテキストや記号には <span> を、アイコンには Icon を使用します。header スロットと footer スロットは、<header> 要素と <footer> 要素も受け取ります。
ライブ デモ
このサンプルでは、都市を地域別にグループ化した Destinations Combo を示します。各スイッチは Combo にテンプレートを 1 つずつ適用し、Combo の下の Selection リストには選択された都市が表示されます。
タイプ
Web Components の Combo には、描画する各領域のテンプレートがあります。以下のセクションでは、各テンプレートを受け取るプロパティまたはスロットを示します。
スニペットでは、id、name、country、countryCode、region、UTC の offset を持つ目的地のデータに Combo をバインドします。valueKey は id、displayKey は name、groupKey は region です。
Item Template (項目テンプレート)
項目テンプレートは、リスト内の各オプションのコンテンツを描画します。既定では、オプションは displayKey の値を表示し、オプションのチェックボックスはテンプレートのコンテンツの前に残ります。
項目テンプレートとグループ ヘッダー テンプレートは Combo のシャドウ DOM 内に描画されるため、ページのスタイルは適用されません。コンテナーのスタイルは item と group-header の CSS パーツで設定し、コンテンツのレイアウトはインライン スタイルで指定します。Combo はテンプレートの各ルート要素をブロックとしてレイアウトするため、テンプレートのコンテンツは 1 つの要素でラップしてください。
itemTemplate プロパティに、項目を受け取って lit-html テンプレートを返す関数を設定します:
import { IgcComboComponent, ComboItemTemplate } from 'igniteui-webcomponents';
import { html } from 'lit-html';
const itemTemplate: ComboItemTemplate<Destination> = ({ item }) => html`
<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>
`;
const combo = document.querySelector('igc-combo') as IgcComboComponent<Destination>;
combo.itemTemplate = itemTemplate;
Header Item Template (ヘッダー項目テンプレート)
ヘッダー項目テンプレートは、Combo に groupKey が設定されている場合に、各グループのヘッダーを描画します。既定では、グループ ヘッダーにグループの値が表示されます。テンプレートが受け取るのはデータ項目ではなくヘッダー レコードで、その groupKey フィールドにグループの値が入ります。そのため、グループ内のオプション数は、スニペットの citiesIn メソッドのように Combo のデータから取得します。
groupHeaderTemplate プロパティを設定します:
const groupHeaderTemplate: ComboItemTemplate<Destination> = ({ item }) => html`
<span style="display: flex; gap: 0.5rem; width: 100%;">
<strong>${item.region}</strong>
<span style="margin-inline-start: auto;">${citiesIn(item.region)} cities</span>
</span>
`;
combo.groupHeaderTemplate = groupHeaderTemplate;
Header Template (ヘッダー テンプレート)
ヘッダー テンプレートは、検索入力とオプションのリストの間に、リストのタイトルやリストに適用するアクションなどのコンテンツを描画します。Combo は既定ではヘッダーを描画しません。
コンテンツを header スロットに渡します。このスニペットでは、引数なしで呼び出す select メソッドで、すべてのオプションを選択するボタンを追加します。Button トピックに示すように、Button コンポーネントを登録してください:
<igc-combo id="destinations" value-key="id" display-key="name" group-key="region">
<header slot="header" class="list-header">
<span>Destinations by region</span>
<igc-button id="select-all" variant="flat">Select all</igc-button>
</header>
</igc-combo>
const combo = document.getElementById('destinations') as IgcComboComponent<Destination>;
document.getElementById('select-all').addEventListener('click', () => combo.select());
Footer Template (フッター テンプレート)
フッター テンプレートは、オプションのリストの下に、選択の概要や選択に適用するアクションなどのコンテンツを描画します。Combo は既定ではフッターを描画しません。
コンテンツを footer スロットに渡します。このスニペットでは、igcChange イベントから選択された項目数を表示し、引数なしで呼び出す deselect メソッドで選択をクリアします:
<igc-combo id="destinations" value-key="id" display-key="name" group-key="region">
<footer slot="footer" class="list-footer">
<span id="selected-count">0 selected</span>
<igc-button id="clear" variant="flat">Clear</igc-button>
</footer>
</igc-combo>
const count = document.getElementById('selected-count');
combo.addEventListener('igcChange', (e) => {
count.textContent = `${e.detail.newValue.length} selected`;
});
document.getElementById('clear').addEventListener('click', () => combo.deselect());
Empty Template (空のリスト テンプレート)
空のリスト テンプレートは、検索テキストに一致するオプションがない場合、または Combo にデータがない場合に、リストの代わりに描画されます。既定では、Combo はリストが空であることを示すメッセージを表示します。
コンテンツを empty スロットに渡します:
<igc-combo value-key="id" display-key="name" group-key="region">
<span slot="empty" class="empty-list">No city matches your search. Try another spelling.</span>
</igc-combo>
Toggle Icon Template (トグル アイコン テンプレート)
トグル アイコン テンプレートは、入力フィールド内のトグル ボタン (ドロップダウンを開閉するボタン) のアイコンを置き換えます。
アイコンを toggle-icon スロットに渡します。スロットのコンテンツは、ドロップダウンが閉じているときも開いているときも表示されます。Icon トピックに示すように、最初にアイコンを登録してください:
import { registerIconFromText } from 'igniteui-webcomponents';
registerIconFromText('flight', flightIcon, 'material');
<igc-combo value-key="id" display-key="name">
<igc-icon slot="toggle-icon" name="flight" collection="material"></igc-icon>
</igc-combo>
Clear Icon Template (クリア アイコン テンプレート)
クリア アイコン テンプレートは、入力フィールド内のクリア ボタンのアイコンを置き換えます。Combo は、項目が選択されている場合にのみクリア ボタンを表示します。disableClear が設定されている場合は非表示になります。
アイコンを clear-icon スロットに渡します:
<igc-combo value-key="id" display-key="name">
<igc-icon slot="clear-icon" name="close" collection="material"></igc-icon>
</igc-combo>
Templated Input (テンプレート化された入力)
Combo の入力フィールドは、Input と同様に、ラベル、テキストの前後のコンテンツ、およびその下のヘルパー テキストを受け取ります。
label 属性を設定し、テキストの前後のコンテンツを prefix スロットと suffix スロットに、ヘルパー テキストを helper-text スロットに渡します。suffix スロットは、クリア アイコンの後、トグル アイコンの前に描画されます:
<igc-combo value-key="id" display-key="name" label="Destinations" placeholder="Pick one or more cities">
<igc-icon slot="prefix" name="place" collection="material"></igc-icon>
<igc-badge slot="suffix">2</igc-badge>
<span slot="helper-text">2 of 19 destinations selected</span>
</igc-combo>
API リファレンス
Web Components Combo の API リファレンスには、対象プラットフォームの完全な API が記載されています。
その他のリソース
次のリソースでは、Web Components Combo に関するその他のガイダンスとプロジェクトのサポートを提供します: