React コンボボックスの概要
React コンボボックスは、ユーザーが提供されたリストでさまざまな定義済みオプションを簡単に選択、フィルタリング、およびグループ化できるようにする軽量のエディターです。このコンポーネントは、React コンボボックス キーボード ナビゲーションのオプション、項目、ヘッダー、およびフッターの表示方法をカスタマイズするためのテンプレートもサポートしています。
Ignite UI for React コンボボックス コンポーネントは、ユーザーが選択できるオプションのリストを提供します。仮想化された項目のリストにすべてのオプションが表示されます。つまり、コンボボックスは、1 つ以上のオプションを選択できる数千のレコードを同時に表示できます。さらに、このコンポーネントには、大文字と小文字を区別するフィルタリング、グループ化、複雑なデータ バインディングなどの機能があります。
React コンボボックスの例
import React from "react" ;
import ReactDOM from "react-dom/client" ;
import { IgrComboModule, IgrCombo } from "@infragistics/igniteui-react" ;
import "igniteui-webcomponents/themes/light/bootstrap.css" ;
IgrComboModule.register();
interface City {
id: string ;
name: string ;
}
const cities : City [] = [
{ name: "London" , id: "UK01" },
{ name: "Sofia" , id: "BG01" },
{ name: "New York" , id: "NY01" },
];
export default function ComboOverview() {
return (
<div className ="sample" >
<IgrCombo
valueKey ="id"
displayKey ="name"
data ={cities}
value ={[ "BG01 "]}
> </IgrCombo >
</div >
);
}
const root = ReactDOM.createRoot (document.getElementById("root" ));
root.render (<ComboOverview /> );
tsx コピー
このサンプルが気に入りましたか? 完全な Ignite UI for Reactツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
React コンボボックス コンポーネントを使用した作業の開始
まず、次のコマンドを実行して、対応する Ignite UI for React npm パッケージをインストールする必要があります:
npm install igniteui-react
cmd
次に、以下のように、React ComboBox
とそれに必要な CSS をインポートし、そのモジュールを登録する必要があります:
import { IgrComboModule, IgrCombo } from 'igniteui-react' ;
import 'igniteui-webcomponents/themes/light/bootstrap.css' ;
IgrComboModule.register();
tsx
IgrCombo コンポーネントは標準の <form> 要素では機能しません。代わりに Form を使用してください。
次に、オプションのリストを構築するコンボ データ ソースに、オブジェクトの配列をバインドします。
interface City {
id: string ;
name: string ;
}
const cities : City [] = [
{ name: "London" , id: "UK01" },
{ name: "Sofia" , id: "BG01" },
{ name: "New York" , id: "NY01" },
];
<IgrCombo
valueKey ="id"
displayKey ="name"
data ={cities}
value ={[ "BG01 "]}
> </IgrCombo >
tsx
データ値と表示プロパティ
コンボは複雑なデータ (オブジェクト) の配列にバインドされている場合、コントロールが項目の選択を処理するために使用するプロパティを指定する必要があります。コンポーネントは以下のプロパティを公開します:
valueKey
- オプション、複雑なデータ オブジェクトに必須 - データ ソースのどのフィールドを選択に使用するかを決定します。valueKey
が省略された場合、選択 API はオブジェクト参照を使用して項目を選択します。
displayKey
- オプション、複雑なデータ オブジェクトに推奨 - データ ソース内のどのフィールドが表示値として使用されるかを決定します。 displayKey
に値が指定されていない場合、コンボは指定された valueKey
(存在する場合) を使用します。
この例では、コンボで各都市の name
を表示し、項目の選択と各項目の基礎となる値として id
フィールドを使用するようにします。したがって、これらのプロパティをコンボの valueKey
と displayKey
にそれぞれ提供します。
データ ソースがプリミティブ型 (例: strings、numbers など) で構成されている場合、valueKey や displayKey を指定しないでください。
値の設定
ComboBox コンポーネントは、属性 (値とも呼ばれます) に加えて、value
ゲッターとセッターを公開します。value 属性を使用して、コンポーネントの初期化時に選択した項目を設定できます。
値 (現在選択されている項目のリスト) を読み取る場合、または値を更新する場合は、それぞれ値ゲッターとセッターを使用します。値ゲッターは、valueKey
で表される選択されたすべての項目のリストを返します。同様に、値セッターを使用して選択した項目のリストを更新する場合は、valueKey
によって項目のリストを提供する必要があります。
例:
const comboRef = useRef<IgrCombo > (null );
console.log(comboRef.current.value);
comboRef.current.value = ['NY01' , 'UK01' ];
tsx
選択 API
コンボ コンポーネントは、現在選択されている項目を変更できる API を公開します。
ユーザーの操作によってオプションのリストから項目を選択する以外に、プログラムで項目を選択することもできます。これは、select
および deselect
メソッドを介して行われます。項目の配列をこれらのメソッドに渡すことができます。メソッドが引数なしで呼び出された場合、呼び出されたメソッドに応じて、すべての項目が選択 / 選択解除されます。コンボ コンポーネントに valueKey
を指定した場合は、選択 / 選択解除する項目の値キーを渡す必要があります。
一部の項目を選択 / 選択解除:
comboRef.current.select(["UK01" , "UK02" , "UK03" , "UK04" , "UK05" ]);
comboRef.current.deselect(["UK01" , "UK02" , "UK03" , "UK04" , "UK05" ]);
tsx
すべての項目を選択 / 選択解除:
comboRef.current.select([]);
comboRef.current.deselect([]);
tsx
valueKey
プロパティを省略した場合は、オブジェクト参照として選択 / 選択解除する項目を列挙する必要があります。
comboRef.current.select([cities[1 ], cities[5 ]]);
comboRef.current.deselect([cities[1 ], cities[5 ]]);
tsx
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 , { useRef } from "react" ;
import ReactDOM from "react-dom/client" ;
import { IgrComboModule, IgrCombo, IgrButtonModule, IgrButton } from "@infragistics/igniteui-react" ;
import "./index.css" ;
import "igniteui-webcomponents/themes/light/bootstrap.css" ;
import { Cities } from "./ComboData" ;
IgrComboModule.register();
IgrButtonModule.register();
export default function ComboSelection() {
const comboRef = useRef<IgrCombo > (null );
const selectCities = () => {
comboRef.current.select(["UK01" , "UK02" , "UK03" , "UK04" , "UK05" ]);
};
const deselectCities = () => {
comboRef.current.deselect(["UK01" , "UK02" , "UK03" , "UK04" , "UK05" ]);
};
const selectAll = () => {
comboRef.current.select([]);
};
const deselectAll = () => {
comboRef.current.deselect([]);
};
return (
<div className ="sample" >
<IgrCombo
valueKey ="id"
displayKey ="name"
label ="Cities"
placeholder ="Pick a city"
placeholderSearch ="Search for a city"
data ={Cities}
ref ={comboRef}
> </IgrCombo >
<div className ="button-container" >
<IgrButton clicked ={selectCities} >
<span key ="selectUK" > Select UK Cities</span >
</IgrButton >
<IgrButton clicked ={deselectCities} >
<span key ="deselectUK" > Deselect UK Favorites</span >
</IgrButton >
<IgrButton clicked ={selectAll} >
<span key ="selectAll" > Select All</span >
</IgrButton >
<IgrButton clicked ={deselectAll} >
<span key ="deselectAll" > Deselect All</span >
</IgrButton >
</div >
</div >
);
}
const root = ReactDOM.createRoot (document.getElementById("root" ));
root.render (<ComboSelection /> );
tsx コピー
.button-container {
display : flex;
margin-top : 16px ;
gap: 8px ;
}
css コピー
検証
Ignite UI for React Combo コンポーネントは、required
、disabled
、autofocus
、invalid
など、ほとんどの IgrInput
プロパティをサポートしています。このコンポーネントは、その検証にバインドされた 2 つのメソッドも公開しています。
reportValidity
- 有効性をチェックし、コンポーネントが検証の制約を満たしている場合は true を返します。
checkValidity
- ネイティブ入力 API に準拠するための reportValidity のラッパー。
キーボード ナビゲーション
コンボ コンポーネントがフォーカスされていて、オプションのリストが表示されていない 場合:
↓ / Alt + ↓ キーを使用してオプションのリストを開きます。
コンボ コンポーネントがフォーカスされ、オプションのリストが表示されている場合:
↓ キーを使用すると、リスト内の次の項目がアクティブになります。
↑ キーを使用すると、リスト内の前の項目がアクティブになります。最初の項目がすでにアクティブな場合、入力にフォーカスします。
Home または End キーを使用すると、リストの最初または最後の項目がアクティブになります。
Space キーを使用すると、アクティブな項目が選択されます。
Enter キーを使用すると、アクティブな項目が選択され、オプションのリストが閉じます。
Esc または Tab/Shift + Tab キーを使用すると、オプションのリストが閉じます。
スタイル設定
IgrCombo
コンポーネントとその項目の外観は、以下に示す公開 CSS パーツを使用して変更できます。
パーツ名
説明
label
カプセル化されたテキスト ラベル。
input
メイン入力フィールド。
native-input
メイン入力フィールドのネイティブ入力。
prefix
プレフィックス ラッパー。
suffix
サフィックス ラッパー。
toggle-icon
トグル アイコン ラッパー。
clear-icon
クリア アイコン ラッパー。
case-icon
フィルター入力のサフィックス内のコンテンツを描画するケース アイコン ラッパー。
helper-text
ヘルパー テキスト ラッパー。
search-input
検索入力フィールド。
list-wrapper
オプション ラッパーのリスト。
list
オプションボックスのリスト。
item
オプションのリスト内の各項目を表します。
group-header
オプションのリストの各ヘッダーを表します。
active
項目がアクティブな場合に、項目パーツ リストに追加されます。
selected
項目が選択されている場合に、項目パーツ リストに追加されます。
checkbox
各リスト項目の各チェックボックスを表します。
checkbox-indicator
各リスト項目のチェックボックス インジケーターを表します。
checked
チェックボックスがチェックされている場合に、チェックボックス パーツ リストに追加されます。
header
ヘッダー コンテンツを保持するコンテナー。
footer
フッター コンテンツを保持するコンテナー。
empty
空のコンテンツを保持するコンテナー。
CSS パーツを使用すると、コンボのスタイルを完全に制御できます。
igc-combo::part (search-input ) {
background-color : var (--ig-gray-100 );
border-radius : 2px ;
}
igc-combo::part (input ) {
background-color : var (--ig-gray-100 );
}
igc-combo::part (prefix) {
background-color : var (--ig-secondary-50 );
color : var (--ig-primary-500 );
}
igc-combo::part (toggle-icon ) {
color : var (--ig-primary-500 );
}
css
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, useRef } from "react" ;
import ReactDOM from "react-dom/client" ;
import { IgrComboModule, IgrCombo, IgrIconModule, IgrIcon } from "@infragistics/igniteui-react" ;
import "./index.css" ;
import "igniteui-webcomponents/themes/light/bootstrap.css" ;
import { Cities } from "./ComboData" ;
IgrComboModule.register();
IgrIconModule.register();
const placeSvg =
'<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px"><path d="M0 0h24v24H0z" fill="none"/><path d="M12 12c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zm6-1.8C18 6.57 15.35 4 12 4s-6 2.57-6 6.2c0 2.34 1.95 5.44 6 9.14 4.05-3.7 6-6.8 6-9.14zM12 2c4.2 0 8 3.22 8 8.2 0 3.32-2.67 7.25-8 11.8-5.33-4.55-8-8.48-8-11.8C4 5.22 7.8 2 12 2z"/></svg>' ;
export default function ComboStyling() {
const iconPlace = useRef<IgrIcon > (null );
useEffect(() => {
if (iconPlace?.current) {
iconPlace.current.registerIconFromText("place" , placeSvg, "material" );
}
}, []);
return (
<div className ="sample" >
<IgrCombo
valueKey ="id"
displayKey ="name"
groupKey ="country"
label ="Destinations"
caseSensitiveIcon
data ={Cities}
>
<span slot ="helper-text" key ="helperText" > Choose the desired place</span >
<span slot ="prefix" key ="prefix" >
<IgrIcon name ="place" ref ={iconPlace} collection ="material" > </IgrIcon >
</span >
</IgrCombo >
</div >
);
}
const root = ReactDOM.createRoot (document.getElementById("root" ));
root.render (<ComboStyling /> );
tsx コピー
igc-combo::part (label ),
igc-combo::part (empty),
igc-combo::part (group-header ) {
color : #2b3a55 ;
}
igc-combo::part (native-input ),
igc-combo::part (list-wrapper) {
background-color : #f2e5e5 ;
}
igc-combo::part (item selected) {
background : #e8c4c4 ;
}
igc-combo::part (item selected active),
igc-combo::part (item selected):hover {
background : #ce7777 ;
}
igc-combo::part (prefix),
igc-combo::part (suffix),
igc-combo::part (case-icon ) {
color : #f2e5e5 ;
background-color : #2b3a55 ;
}
igc-combo::part (checkbox checked)::after {
background : #2b3a55 ;
box-shadow : none;
}
css コピー
API リファレンス
その他のリソース