Close
Angular React Web Components Blazor React
Open Source

Combo (コンボ) テンプレート

Ignite UI for React Combo のテンプレートは、Combo の各領域 (項目、グループ ヘッダー、リストのヘッダーとフッター、空のリスト、トグル アイコンとクリア アイコン、入力フィールド) の既定のコンテンツを置き換えます。

React の Combo は、itemTemplate プロパティと groupHeaderTemplate プロパティで項目とグループ ヘッダーを描画し、その他の領域のコンテンツはスロットで受け取ります。単純なテキストや記号には <span> を、アイコンには Icon を使用します。header スロットと footer スロットは、<header> 要素と <footer> 要素も受け取ります。

ライブ デモ

このサンプルでは、都市を地域別にグループ化した Destinations Combo を示します。各スイッチは Combo にテンプレートを 1 つずつ適用し、Combo の下の Selection リストには選択された都市が表示されます。

タイプ

React の 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 プロパティに渡します:

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 (ヘッダー項目テンプレート)

ヘッダー項目テンプレートは、Combo に groupKey が設定されている場合に、各グループのヘッダーを描画します。既定では、グループ ヘッダーにグループの値が表示されます。テンプレートが受け取るのはデータ項目ではなくヘッダー レコードで、その groupKey フィールドにグループの値が入ります。そのため、グループ内のオプション数は、スニペットの citiesIn メソッドのように Combo のデータから取得します。

関数を groupHeaderTemplate プロパティに渡します:

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 (ヘッダー テンプレート)

ヘッダー テンプレートは、検索入力とオプションのリストの間に、リストのタイトルやリストに適用するアクションなどのコンテンツを描画します。Combo は既定ではヘッダーを描画しません。

コンテンツを header スロットに渡します。このスニペットでは、引数なしで呼び出す select メソッドで、すべてのオプションを選択するボタンを追加します:

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>

フッター テンプレートは、オプションのリストの下に、選択の概要や選択に適用するアクションなどのコンテンツを描画します。Combo は既定ではフッターを描画しません。

コンテンツを footer スロットに渡します。このスニペットでは、選択された値を state に保持し、引数なしで呼び出す deselect メソッドで選択をクリアします:

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 (空のリスト テンプレート)

空のリスト テンプレートは、検索テキストに一致するオプションがない場合、または Combo にデータがない場合に、リストの代わりに描画されます。既定では、Combo はリストが空であることを示すメッセージを表示します。

コンテンツを empty スロットに渡します:

<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 (トグル アイコン テンプレート)

トグル アイコン テンプレートは、入力フィールド内のトグル ボタン (ドロップダウンを開閉するボタン) のアイコンを置き換えます。

アイコンを toggle-icon スロットに渡します。スロットのコンテンツは、ドロップダウンが閉じているときも開いているときも表示されます。Icon トピックに示すように、最初にアイコンを登録してください:

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 (クリア アイコン テンプレート)

クリア アイコン テンプレートは、入力フィールド内のクリア ボタンのアイコンを置き換えます。Combo は、項目が選択されている場合にのみクリア ボタンを表示します。disableClear が設定されている場合は非表示になります。

アイコンを clear-icon スロットに渡します:

<IgrCombo data={destinations} valueKey="id" displayKey="name">
    <IgrIcon slot="clear-icon" name="close" collection="material" />
</IgrCombo>

Templated Input (テンプレート化された入力)

Combo の入力フィールドは、Input と同様に、ラベル、テキストの前後のコンテンツ、およびその下のヘルパー テキストを受け取ります。

label プロパティを設定し、テキストの前後のコンテンツを prefix スロットと suffix スロットに、ヘルパー テキストを helper-text スロットに渡します。suffix スロットは、クリア アイコンの後、トグル アイコンの前に描画されます:

<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 リファレンス

React Combo の API リファレンスには、対象プラットフォームの完全な API が記載されています。

その他のリソース

次のリソースでは、React Combo に関するその他のガイダンスとプロジェクトのサポートを提供します: