ion-select
セレクトは、一連のオプションから1つまたは複数のオプションを選択するためのフォームコントロールです。ユーザーがセレクトをタップすると、ダイアログが表示され、すべてのオプションが大きく選択しやすいリストに表示されます。
selectは、子要素 <ion-select-option> とともに使用する必要があります。子要素のオプションにvalue属性が指定されていない場合、そのtextが値として使用されます。
value が <ion-select> にセットされている場合、オプションはその値に基づいて選択済みになります。
Labels
ラベルは、Selectを説明するために使用します。これらは視覚的に使用することができ、また、ユーザーがSelectにフォーカスしているときには、スクリーンリーダーによって読み上げられます。これにより、ユーザーはSelectの意図を理解しやすくなります。セレクトには、ラベルを割り当てるいくつかの方法があります:
セレクトには、コンポーネントにラベルを指定するためのいくつかのオプションがあります:
labelプロパティ:プレーンテキストのラベルに使用します。labelスロット:カスタム HTML ラベルに使用する。aria-label:スクリーンリーダー用のラベルとして使用されるが、ラベルは表示されない。
Label Placement
ラベルはデフォルトではコンテンツの幅を占めます。開発者は labelPlacement プロパティを使用して、コントロールに対するラベルの配置を制御することができます。ここでは label プロパティを使用しているが、labelPlacement は label スロットと一緒に使用することもできます。
Label Slot
プレーンテキストのラベルは label プロパティで渡すべきですが、カスタムHTMLが必要な場合は、代わりに label スロットで渡すことができます。
No Visible Label
表示するラベルが必要ない場合でも、開発者はaria-labelを指定する必要があります
Single Selection
デフォルトでは、selectを使用すると、ユーザは1つのOptionだけを選択できます。Alertのインターフェースでは、Optionのリストがradio button形式で表示されます。action sheetインタフェースは、1つの値選択でのみ使用できます。selectコンポーネントの値は、選択したオプションの値の値を受け取ります。
単一選択時のキーボード操作については、以下のキーボード操作のセクションで説明しています。
複数選択
select に multiple 属性を追加することで、ユーザは複数のオプションを選択することができます。複数のオプションを選択できる場合、アラート、ポップオーバー、またはモーダルオーバーレイは、チェックボックススタイルのオプションリストをユーザに提示します。select コンポーネントの値は、選択されたすべてのオプション値の配列を受け取ります。
注意: action-sheet インターフェースは複数選択では動作しません。
複数選択時のキーボード操作については、以下のキーボード操作のセクションで説明しています。
インターフェイス
デフォルトでは、selectは ion-alert を使用してアラートのオプションのオーバーレイを開きます。インターフェイスは、interface プロパティに action-sheet、popover、modal を渡すことで、ion-action-sheet、ion-popover、ion-modal を使用するように変更することができます。それぞれのインターフェースの制限事項については、他のセクションを参照してください。
Alert
Action Sheet
Popover
Modal
Responding to Interaction
Select とユーザのインタラクションを処理する主な方法は、 ionChange イベント、 ionDismiss イベント、 ionCancel イベントです。これらのイベントやselectが発生するその他のイベントの詳細については、Eventsを参照してください。
ConsoleConsole messages will appear here when logged from the example above.オブジェクト値の参照
Selectの値にオブジェクトを使用する場合、Selectの値のidentityはそのままで、サーバやデータベースから取得したオブジェクトのidentityが変わってしまうことがあります。例えば、希望するオブジェクト値を持つ既存のレコードがSelectに読み込まれたが、新しく取得されたselectオプションのIDが異なる場合、このようなことが起こりえます。その結果、Selectは、元のSelectがそのまま残っているにもかかわらず、全く値がないように見えることになります。
デフォルトでは、Select はオプションが選択されているかどうかを決定するために厳密な等式 (===) を使用します。これは、プロパティ名または関数を compareWith プロパティに指定することでオーバーライドできます。
Using compareWith
ConsoleConsole messages will appear here when logged from the example above.Object Values and Multiple Selection
ConsoleConsole messages will appear here when logged from the example above.Justification
開発者は justify プロパティを使って、ラベルとコントロールの行の詰め方をコントロールすることができる。
Filled Selects
Material DesignはセレクトにFilledスタイルを提供します。select の fill プロパティは "solid" または "outline" のいずれかに設定できます。
iOSでは、Selectの mode を md に設定することで、Filled Selectsを使うことができます。
コンポーネント間のスタイリングの競合のため、fillを使用するセレクトはion-item内で使用すべきではありません。
Select Buttons
アラートは Cancel と OK の2つのボタンをサポートしている。それぞれのボタンのテキストは cancelText と okText プロパティを使ってカスタマイズすることができます。
action-sheet と popover インターフェースには OK ボタンがありません。オプションのいずれかをクリックすると自動的にオーバーレイが閉じ、その値が選択されます。 popover インターフェースには Cancel ボタンがなく、背景をクリックするとオーバーレイが閉じます。
modalインターフェースには、ヘッダーに単一のCloseボタンがあります。このボタンは、モーダルを閉じることのみを担当します。
このボタンをクリックした後、または代替方法でモーダルが閉じられた場合でも、行われた選択は保持されます。
インターフェイスオプション
selectはalert、action sheet、popover、modalの各インターフェイスを使用するので、interfaceOptionsプロパティを通してこれらのコンポーネントにオプションを渡すことができます。これを使用して、カスタムヘッダー、サブヘッダー、CSS クラスなどを渡すことができます。
各インタフェースが受け付けるプロパティについては、ion-alert docs, ion-action-sheet docs, ion-popover docs, ion-modal docs を参照してください。 を参照してください。
注意: alert インターフェイスでは、interfaceOptions は inputs や buttons を上書きしません。
Start and End Slots
startとendスロットは、セレクトの両側にアイコン、ボタン、またはプレフィックス/サフィックステキストを配置するために使用できます。スロットコンテンツがクリックされた場合、セレクトは開きません。
ほとんどの場合、これらのスロットに配置されたIconコンポーネントにはaria-hidden="true"を設定すべきです。詳細については、Icon accessibility docsを参照してください。
スロットコンテンツが操作対象である場合、Buttonなどのインタラクティブ要素でラップする必要があります。これにより、 コンテンツにタブで移動できるようになります。
カスタマイズ
Selectコンポーネントには2つのユニットがあり、それぞれ別々にスタイルを設定する必要があります。 ion-select要素は、ビュー上で選択された値、ない場合はプレースホルダ、ドロップダウンのアイコンによって表現されます。インターフェイスは上記のインターフェイスセクションで定義されており、ion-selectをクリックしたときに開くダイアログです。インターフェイスには ion-select-option 要素を追加することで定義されるすべてのオプションが含まれています。以下のセクションでは、これらのスタイリングの違いについて説明します。
Select要素のスタイリング
前述の通り、ion-select要素は値、プレースホルダ、ビューに表示されるアイコンのみで構成されています。これをカスタマイズするには、CSSとCSSカスタムプロパティを組み合わせてスタイルを設定します。
また、必要なブラウザサポートによっては、CSSのシャドウパーツを使用してセレクトのスタイルを設定することもできます。 part を使用することで、要素上の任意の CSS プロパティを対象とすることができることに注意してください。
セレクトインターフェースのスタイリング
インターフェイス・ダイアログのカスタマイズは、そのインターフェイスのドキュメントのスタイリングセクション(CSS shadow parts, CSS custom properties, and slots)に 従って行ってください。
しかし、セレクト・オプションは、スタイルを簡単にするためにクラスを設定し、オーバーレイ・オプションにクラスを渡すことができます。オプションをカスタマイズする使用例については、セレクト・オプションのドキュメントを参照してください。
Custom Toggle Icons
選択テキストの隣に表示されるアイコンは、toggleIcon プロパティと expandedIcon プロパティを使用して、任意の Ionicon に設定することができます。
アイコンの反転動作
デフォルトでは、セレクトを開いているとき、トグルアイコンは md モードでは自動的に回転し、ios モードでは静止します。この動作はCSSを使ってカスタマイズすることができます。
以下の例ではcustom toggleIconを使って、iosモードでのトグルアイコンの反転動作をより分かりやすく説明しています。
Typeahead Component
Typeaheadまたはオートコンプリート機能は、既存のIonicコンポーネントを使用して構築できます。利用可能なスクリーンスペースを最大限に活用するために、ion-modalを使用することをお勧めします。
Helper & Error Text
ヘルパーテキストとエラーテキストは、helperTextとerrorTextプロパティを使用してセレクト内で使用できます。エラーテキストは、ion-invalidとion-touchedクラスがion-selectに追加されていない限り表示されません。これにより、ユーザーがデータを入力する前にエラーが表示されることはありません。
Angularでは、これはフォームバリデーションによって自動的に行われます。JavaScript、React、Vueでは、独自のバリデーションに基づいてクラスを手動で追加する必要があります。
Interfaces
SelectChangeEventDetail
interface SelectChangeEventDetail<T = any> {
value: T;
}
SelectCustomEvent
必須ではありませんが、このインターフェイスを CustomEvent インターフェイスの代わりに使用することで、このコンポーネントから発行される Ionic イベントをより強力に片付けすることができます。
interface SelectCustomEvent<T = any> extends CustomEvent {
detail: SelectChangeEventDetail<T>;
target: HTMLIonSelectElement;
}
Accessibility
Keyboard Interactions
Ionic's keyboard interactions follow the implementation patterns of the web instead of the native iOS select for a consistent experience across all platforms.
These keyboard interactions apply to all ion-select elements when the following conditions are met:
- The select is closed.
- The select is focused.
- The select is not disabled.
| Key | Description |
|---|---|
| Enter | Opens the overlay and focuses on the first selected option. If there is no selected option, then it focuses on the first option. |
| Space | Opens the overlay and focuses on the first selected option. If there is no selected option, then it focuses on the first option. |
Single Selection
Single selection keyboard interaction follows the ARIA implementation patterns of a radio.
これらのキーボード操作は、オーバーレイが表示され、フォーカスされている場合にion-action-sheet、ion-alert、ion-popover、およびion-modal要素に適用されます。
| Key | Description |
|---|---|
| ArrowDown | Focuses and selects the next option in the list. If there is no next option, selection will cycle to the first option. |
| ArrowLeft | Focuses and selects the previous option in the list. If there is no previous option, selection will cycle to the last option. |
| ArrowRight | Focuses and selects the next option in the list. If there is no next option, selection will cycle to the first option. |
| ArrowUp | Focuses and selects the previous option in the list. If there is no previous option, selection will cycle to the last option. |
| Enter | If an option is focused, it will select the option. Overlays without an 'OK' button will commit the value immediately, dismiss the overlay and return focus to the ion-select element.If the 'OK' button is focused, it will save the user's selection, dismiss the overlay and return focus to the ion-select element. |
| Escape | Closes the overlay without changing the submitted option. Returns the focus back to the ion-select element. |
| Space | If the focused radio button is not checked, unchecks the currently checked radio button and checks the focused radio button. Otherwise, does nothing. If the overlay does not have an 'OK' button, the value will be committed immediately and the overlay will dismiss. |
| Tab | Moves focus to the next focusable element (cancel button, 'OK' button, or either the selection or the first option) on the overlay. If the next focusable element is an option, then it will focus on the selected option, otherwise it will focus the first option. |
Multiple Selection
Multiple selection keyboard interaction follows the ARIA implementation patterns of a checkbox.
これらのキーボード操作は、オーバーレイが表示され、複数選択が有効になっている場合にion-alert、ion-popover、およびion-modal要素に適用されます。
| Key | Description |
|---|---|
| Enter | When the 'OK' button is focused, it will save the user's selection, dismiss the overlay, and return focus to the ion-select element. |
| Escape | Closes the overlay without changing the submitted option(s). Returns the focus back to the ion-select element. |
| Space | Selects or deselects the currently focused option. This does not deselect the other selected options. If the overlay does not have an 'OK' button, the value will be committed immediately. |
| Tab | Move focus to the next focusable element (cancel button, 'OK' button, or any of the options) on the overlay. If the next focusable element is the options list, then it should iterate through each option. |
Properties
cancelText
| Description | キャンセルボタンに表示するテキストです。 |
| Attribute | cancel-text |
| Type | string |
| Default | 'Cancel' |
color
| Description | アプリケーションのカラーパレットから使用する色を指定します。デフォルトのオプションは以下の通りです:"primary", "secondary", "tertiary", "success", "warning", "danger", "light", "medium", と "dark" です.色についての詳細は theming を参照してください。 このプロパティは、modern select構文を使用する場合にのみ利用可能です。 |
| Attribute | color |
| Type | "danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string | undefined |
| Default | undefined |
compareWith
| Description | このプロパティを使用すると、開発者は、ion-select で選択されたオプションを決定するときにオブジェクトを比較するためのカスタム関数またはプロパティ名を指定できます。指定しない場合、デフォルトの動作では比較に厳密な等式 (===) が使用されます。 |
| Attribute | compare-with |
| Type | ((currentValue: any, compareValue: any) => boolean) | null | string | undefined |
| Default | undefined |