React Select (選択)
選択コンポーネントは、ドロップダウンに配置された項目のリストからの単一選択できます。このフォーム コントロールは、1 つまたは複数の文字の一致に基づく選択を含む、項目リストの迅速なナビゲーションを提供します。
React 選択の例
import React from "react";
import ReactDOM from "react-dom/client";
import { IgrSelectModule, IgrSelectItemModule, IgrSelect, IgrSelectItem } from "@infragistics/igniteui-react";
import "igniteui-webcomponents/themes/light/bootstrap.css";
IgrSelectModule.register();
IgrSelectItemModule.register();
export default function SelectOverview() {
return (
<div className="sample">
<IgrSelect>
<IgrSelectItem value="Orange"><span>Orange</span></IgrSelectItem>
<IgrSelectItem value="Apple"><span>Apple</span></IgrSelectItem>
<IgrSelectItem value="Banana"><span>Banana</span></IgrSelectItem>
<IgrSelectItem value="Mango"><span>Mango</span></IgrSelectItem>
</IgrSelect>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<SelectOverview />);
tsx
このサンプルが気に入りましたか? 完全な Ignite UI for Reactツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
使用方法
まず、次のコマンドを実行して、対応する Ignite UI for React npm パッケージをインストールする必要があります:
npm install igniteui-react
cmd
次に、以下のように、IgrSelect
および IgrSelectItem
とそれに必要な CSS をインポートし、そのモジュールを登録する必要があります:
import { IgrSelectModule, IgrSelect, IgrSelectItemModule, IgrSelectItem } from 'igniteui-react';
import 'igniteui-webcomponents/themes/light/bootstrap.css';
IgrSelectModule.register();
IgrSelectItemModule.register();
tsx
ヘッダーとグループの選択コンポーネントは、使用しない限り必須ではないことに注意してください。
コンポーネントの使用を開始するには、選択する IgrSelectItem
のリストとともに IgrSelect
を追加します。
<IgrSelect>
<IgrSelectItem value="Orange"><span>Orange</span></IgrSelectItem>
<IgrSelectItem value="Apple"><span>Apple</span></IgrSelectItem>
<IgrSelectItem value="Banana"><span>Banana</span></IgrSelectItem>
<IgrSelectItem value="Mango"><span>Mango</span></IgrSelectItem>
</IgrSelect>
tsx
Select (選択)
IgrSelect
コンポーネントは Form
コンポーネント内で使用できるため、登録する name
プロパティを公開します。また、label
および placeholder
プロパティもあります。outlined
プロパティは、Material テーマに関してのみ、スタイリング目的で使用されます。デフォルトのスロットを除いて、コンポーネントは、header
、footer
、helper-text
、prefix
、suffix
、および toggle-icon
を含む他のいくつかのスロットを提供します。コンポーネントのサイズは、--ig-size
CSS 変数を使用して変更できます。
Item (項目)
IgrSelectItem
コンポーネントを使用すると、ユーザーは IgrSelect
コントロールで使用されるオプションのリストを宣言的に指定できます。各項目は、選択時に保持されるデータを表す value
プロパティを提供します。IgrSelectItem
には、項目のテキスト コンテンツを指定できるデフォルトのスロットがあります。このテキスト コンテンツは、value
プロパティが IgrSelectItem
に存在しない場合に値として使用されます。prefix
スロットと suffix
スロットを使用して、IgrSelectItem
コンテンツの前後に描画されるカスタム コンテンツを提供することもできます。Selected
プロパティを設定することにより、選択した項目を事前定義できます。disabled
プロパティを使用して、一部またはすべての項目を無効にすることもできます。
import React, { useEffect, useRef } from "react";
import ReactDOM from "react-dom/client";
import {
IgrSelectModule,
IgrSelectItemModule,
IgrIconModule,
IgrSelect,
IgrSelectItem,
IgrIcon,
} from "@infragistics/igniteui-react";
import "./index.css";
import "igniteui-webcomponents/themes/light/bootstrap.css";
IgrSelectModule.register();
IgrSelectItemModule.register();
IgrIconModule.register();
const hotelSvg =
'<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M7 14c1.66 0 3-1.34 3-3S8.66 8 7 8s-3 1.34-3 3 1.34 3 3 3zm0-4c.55 0 1 .45 1 1s-.45 1-1 1-1-.45-1-1 .45-1 1-1zm12-3h-8v8H3V5H1v15h2v-3h18v3h2v-9c0-2.21-1.79-4-4-4zm2 8h-8V9h6c1.1 0 2 .9 2 2v4z"/></svg>';
const grocerySvg =
'<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M7 18c-1.1 0-1.99.9-1.99 2S5.9 22 7 22s2-.9 2-2-.9-2-2-2zm10 0c-1.1 0-1.99.9-1.99 2s.89 2 1.99 2 2-.9 2-2-.9-2-2-2zm-1.45-5c.75 0 1.41-.41 1.75-1.03l3.58-6.49c.37-.66-.11-1.48-.87-1.48H5.21l-.94-2H1v2h2l3.6 7.59-1.35 2.44C4.52 15.37 5.48 17 7 17h12v-2H7l1.1-2h7.45zM6.16 6h12.15l-2.76 5H8.53L6.16 6z"/></svg>';
const restaurantSvg =
'<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M16 6v8h3v8h2V2c-2.76 0-5 2.24-5 4zm-5 3H9V2H7v7H5V2H3v7c0 2.21 1.79 4 4 4v9h2v-9c2.21 0 4-1.79 4-4V2h-2v7z"/></svg>';
export default function SelectItem() {
const iconHotel = useRef<IgrIcon>(null);
const iconGrocery = useRef<IgrIcon>(null);
const iconReastaurant = useRef<IgrIcon>(null);
useEffect(() => {
if (iconHotel?.current) {
iconHotel.current.registerIconFromText("hotel", hotelSvg, "material");
}
if (iconGrocery?.current) {
iconGrocery.current.registerIconFromText("grocery", grocerySvg, "material");
}
if (iconReastaurant?.current) {
iconReastaurant.current.registerIconFromText("restaurant", restaurantSvg, "material");
}
}, []);
return (
<div className="sample">
<IgrSelect>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="hotel" ref={iconHotel} collection="material"></IgrIcon>
</span>
<span>Hotel</span>
</IgrSelectItem>
<IgrSelectItem disabled>
<span slot="prefix">
<IgrIcon name="grocery" ref={iconGrocery} collection="material"></IgrIcon>
</span>
<span>Grocery</span>
</IgrSelectItem>
<IgrSelectItem selected>
<span slot="prefix">
<IgrIcon name="restaurant" ref={iconReastaurant} collection="material"></IgrIcon>
</span>
<span>Restaurant</span>
</IgrSelectItem>
</IgrSelect>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<SelectItem />);
tsx
span[slot="prefix"] {
height: 20px;
}
css
IgrSelectHeader
を使用して、項目のグループのヘッダーを提供できます。
import React from "react";
import ReactDOM from "react-dom/client";
import {
IgrSelectModule,
IgrSelectHeaderModule,
IgrSelectItemModule,
IgrSelect,
IgrSelectItem,
IgrSelectHeader
} from "@infragistics/igniteui-react";
import "igniteui-webcomponents/themes/light/bootstrap.css";
IgrSelectModule.register();
IgrSelectHeaderModule.register();
IgrSelectItemModule.register();
export default function SelectHeader() {
return (
<div className="sample">
<IgrSelect>
<IgrSelectHeader><span>Tasks</span></IgrSelectHeader>
<IgrSelectItem value="spec" selected><span>Specification</span></IgrSelectItem>
<IgrSelectItem value="implement"><span>Implementation</span></IgrSelectItem>
<IgrSelectItem value="test" disabled><span>Testing</span></IgrSelectItem>
<IgrSelectItem value="docs"><span>Documentation</span></IgrSelectItem>
</IgrSelect>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<SelectHeader />);
tsx
<IgrSelect>
<IgrSelectHeader>
<span>Tasks</span>
</IgrSelectHeader>
</IgrSelect>
tsx
Group (グループ)
複数の IgrSelectItem
を IgrSelectGroup
コンポーネントの開く括弧と閉じる括弧の間に配置して、ユーザーがそれらを視覚的にグループ化できるようにすることができます。IgrSelectGroup
は、その label
スロットを介してラベルを付け、その disabled
プロパティを介して無効にすることができます。
選択グループが無効になっている場合、そのグループの個別の項目を有効にすることはできないことに注意してください。
import React, { useEffect, useRef } from "react";
import ReactDOM from "react-dom/client";
import {
IgrSelectModule,
IgrSelectItemModule,
IgrIconModule,
IgrSelect,
IgrSelectItem,
IgrIcon,
IgrSelectGroupModule,
IgrSelectGroup,
} from "@infragistics/igniteui-react";
import "./index.css";
import "igniteui-webcomponents/themes/light/bootstrap.css";
IgrSelectModule.register();
IgrSelectGroupModule.register();
IgrSelectItemModule.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 SelectGroup() {
const iconPlace = useRef<IgrIcon>(null);
useEffect(() => {
if (iconPlace?.current) {
iconPlace.current.registerIconFromText("place", placeSvg, "material");
}
}, []);
return (
<div className="sample">
<IgrSelect>
<IgrSelectGroup>
<span slot="label">Europe</span>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> Germany </span>
<span slot="suffix">DE</span>
</IgrSelectItem>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> France </span>
<span slot="suffix">FR</span>
</IgrSelectItem>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> Spain </span>
<span slot="suffix">ES</span>
</IgrSelectItem>
</IgrSelectGroup>
<IgrSelectGroup disabled>
<span slot="label">Asia</span>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> China </span>
<span slot="suffix">CN</span>
</IgrSelectItem>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> Japan </span>
<span slot="suffix">JP</span>
</IgrSelectItem>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> India </span>
<span slot="suffix">IN</span>
</IgrSelectItem>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> Thailand </span>
<span slot="suffix">TH</span>
</IgrSelectItem>
</IgrSelectGroup>
<IgrSelectGroup>
<span slot="label">North America</span>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> USA </span>
<span slot="suffix">US</span>
</IgrSelectItem>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> Canada </span>
<span slot="suffix">CA</span>
</IgrSelectItem>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> Mexico </span>
<span slot="suffix">MX</span>
</IgrSelectItem>
</IgrSelectGroup>
</IgrSelect>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<SelectGroup />);
tsx
span[slot="prefix"] {
height: 20px;
}
css
<IgrSelect>
<IgrSelectGroup>
<span slot="label">Europe</span>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> Germany </span>
<span slot="suffix">DE</span>
</IgrSelectItem>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> France </span>
<span slot="suffix">FR</span>
</IgrSelectItem>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> Spain </span>
<span slot="suffix">ES</span>
</IgrSelectItem>
</IgrSelectGroup>
</IgrSelect>
tsx
検証
さらに、IgrSelect
は、required
、disabled
、autofocus
など、ほとんどの IgrInput
プロパティをサポートします。コンポーネントは、その検証にバインドされたメソッドも公開します。
reportValidity
- 有効性をチェックし、無効な場合はコンポーネントにフォーカスします。
キーボード ナビゲーション
選択がフォーカスされ、オプションのリストが表示されていない場合:
- ALT + ↑ ↓ の組み合わせを使用するか、Space または Enter キーをクリックして、
select
を開きます。
- ALT + ↑ または ↓ の組み合わせ、または Enter、Space、Esc、
IgrTab
キーのいずれかを使用して、IgrSelect
を閉じます。
- ← → キーを使用すると、リスト内の前の項目が選択されます。
- ↑ ↓ キーを使用すると、リスト内の次の項目が選択されます。
- Home キーまたは End キーを使用すると、リストの最初または最後の項目が選択されます。
- 文字を入力すると、項目のリストが照会され、現在のユーザー入力に最も近いものが選択されます。
選択がフォーカスされ、オプションのリストが表示されている場合:
- Enter キーまたは Space キーを使用すると、項目が選択され、リストが閉じます。
- ← → キーを使用すると、リスト内の前の項目がアクティブになります。
- ↑ ↓ キーを使用すると、リスト内の次の項目がアクティブになります。
- Home キーまたは End キーを使用すると、リストの最初または最後の項目がアクティブになります。
IgrSelect コンポーネントは、項目の単一選択のみをサポートします。
スタイル設定
以下に示す公開された CSS パーツを使用して、Ignite UI for React IgrSelect
コンポーネントとその項目の外観を変更できます。
Select コンポーネント
パーツ名 |
説明 |
input |
カプセル化された igc-input。 |
label |
カプセル化されたテキスト ラベル。 |
list |
オプションのリストを保持するラッパー。 |
prefix |
入力の前にコンテンツを描画するプレフィックス ラッパー。 |
suffix |
入力後にコンテンツを描画するサフィックス ラッパー。 |
toggle-icon |
サフィックス ラッパー内のコンテンツを描画するトグル アイコン ラッパー。 |
helper-text |
入力の下にコンテンツを描画するヘルパー テキスト ラッパー。 |
Select Item コンポーネント
パーツ名 |
説明 |
content |
項目のテキスト コンテンツを保持するメイン ラッパー。 |
prefix |
メイン ラッパーの前にコンテンツを描画するプレフィックス ラッパー。 |
suffix |
メイン ラッパーの後にコンテンツを描画するサフィックス ラッパー。 |
Select Group コンポーネント
パーツ名 |
説明 |
label |
選択したグループ項目の上にコンテンツを描画するラベル ラッパー。 |
igc-select::part(base) {
background: var(--ig-primary-50);
}
igc-select-item[active] {
background: var(--ig-secondary-300);
}
igc-select::part(input) {
background-color: var(--ig-primary-50);
}
igc-select::part(prefix),
igc-select::part(suffix) {
color: var(--ig-secondary-500-contrast);
background: var(--ig-secondary-500);
}
css
import React, { useEffect, useRef } from "react";
import ReactDOM from "react-dom/client";
import {
IgrSelectModule,
IgrSelectItemModule,
IgrIconModule,
IgrSelect,
IgrSelectItem,
IgrIcon,
IgrSelectGroupModule,
IgrSelectGroup,
} from "@infragistics/igniteui-react";
import "./index.css";
import "igniteui-webcomponents/themes/light/bootstrap.css";
IgrSelectModule.register();
IgrSelectGroupModule.register();
IgrSelectItemModule.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 SelectGroup() {
const iconPlace = useRef<IgrIcon>(null);
useEffect(() => {
if (iconPlace?.current) {
iconPlace.current.registerIconFromText("place", placeSvg, "material");
}
}, []);
return (
<div className="sample">
<IgrSelect label="Destinations:">
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span slot="helper-text">Choose the desired place</span>
<IgrSelectGroup disabled>
<span slot="label">Europe</span>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> Germany </span>
<span slot="suffix">DE</span>
</IgrSelectItem>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> France </span>
<span slot="suffix">FR</span>
</IgrSelectItem>
<IgrSelectItem selected>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> Spain </span>
<span slot="suffix">ES</span>
</IgrSelectItem>
</IgrSelectGroup>
<IgrSelectGroup>
<span slot="label">North America</span>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> USA </span>
<span slot="suffix">US</span>
</IgrSelectItem>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> Canada </span>
<span slot="suffix">CA</span>
</IgrSelectItem>
<IgrSelectItem>
<span slot="prefix">
<IgrIcon name="place" ref={iconPlace} collection="material"></IgrIcon>
</span>
<span> Mexico </span>
<span slot="suffix">MX</span>
</IgrSelectItem>
</IgrSelectGroup>
</IgrSelect>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<SelectGroup />);
tsx
span[slot="prefix"] {
height: 20px;
}
igc-select::part(base) {
background: rgb(225, 241, 246);
}
igc-select-item::part(prefix),
igc-select-item::part(suffix) {
color: rgb(1, 42, 74);
}
igc-select-item[active] {
background: rgb(42, 111, 151);
}
igc-select::part(label) {
color: rgb(1, 42, 74);
}
igc-select::part(input){
background-color: rgb(225, 241, 246);
}
igc-select::part(prefix),
igc-select::part(suffix){
color: white;
background: rgb(42, 111, 151);
}
css
API リファレンス
その他のリソース