メインコンテンツまでスキップ
バージョン: v8

ion-select

shadow

セレクトは、一連のオプションから1つまたは複数のオプションを選択するためのフォームコントロールです。ユーザーがセレクトをタップすると、ダイアログが表示され、すべてのオプションが大きく選択しやすいリストに表示されます。

selectは、子要素 <ion-select-option> とともに使用する必要があります。子要素のオプションにvalue属性が指定されていない場合、そのtextが値として使用されます。

value<ion-select> にセットされている場合、オプションはその値に基づいて選択済みになります。

Labels

ラベルは、Selectを説明するために使用します。これらは視覚的に使用することができ、また、ユーザーがSelectにフォーカスしているときには、スクリーンリーダーによって読み上げられます。これにより、ユーザーはSelectの意図を理解しやすくなります。セレクトには、ラベルを割り当てるいくつかの方法があります:

セレクトには、コンポーネントにラベルを指定するためのいくつかのオプションがあります:

  • labelプロパティ:プレーンテキストのラベルに使用します。
  • labelスロット:カスタム HTML ラベルに使用する。
  • aria-label:スクリーンリーダー用のラベルとして使用されるが、ラベルは表示されない。

Label Placement

ラベルはデフォルトではコンテンツの幅を占めます。開発者は labelPlacement プロパティを使用して、コントロールに対するラベルの配置を制御することができます。ここでは label プロパティを使用しているが、labelPlacementlabel スロットと一緒に使用することもできます。

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 を使ってAlertのオプションのオーバーレイを開きます。インターフェイスを変更して、ion-action-sheet または ion-popover を使用するには、 action-sheet または popoverinterface プロパティに渡します。各インタフェースの制限については、他のセクションを参照してください。

Alert

Action Sheet

Popover

インタラクションの処理

Select とユーザのインタラクションを処理する主な方法は、 ionChange イベント、 ionDismiss イベント、 ionCancel イベントです。これらのイベントやselectが発生するその他のイベントの詳細については、Eventsを参照してください。

Console
Console messages will appear here when logged from the example above.

オブジェクト値の参照

Selectの値にオブジェクトを使用する場合、Selectの値のidentityはそのままで、サーバやデータベースから取得したオブジェクトのidentityが変わってしまうことがあります。例えば、希望するオブジェクト値を持つ既存のレコードがSelectに読み込まれたが、新しく取得されたselectオプションのIDが異なる場合、このようなことが起こりえます。その結果、Selectは、元のSelectがそのまま残っているにもかかわらず、全く値がないように見えることになります。

デフォルトでは、Select はオプションが選択されているかどうかを決定するために厳密な等式 (===) を使用します。これは、プロパティ名または関数を compareWith プロパティに指定することでオーバーライドできます。

Using compareWith

Console
Console messages will appear here when logged from the example above.

Object Values and Multiple Selection

Console
Console messages will appear here when logged from the example above.

Justification

開発者は justify プロパティを使用して、ラベルとコントロールの行の詰め方を制御することができます。

Filled Selects

Material DesignはセレクトにFilledスタイルを提供します。select の fill プロパティは "solid" または "outline" のいずれかに設定できます。

fill スタイルはセレクトコンテナを視覚的に定義するので、fill を使用するセレクトは ion-item では使用しないでください。

Select Buttons

アラートは CancelOK の2つのボタンをサポートしている。それぞれのボタンのテキストは cancelTextokText プロパティを使ってカスタマイズすることができます。

action-sheetpopover インターフェースには OK ボタンがありません。オプションのいずれかをクリックすると自動的にオーバーレイが閉じ、その値が選択されます。 popover インターフェースには Cancel ボタンがなく、背景をクリックするとオーバーレイが閉じます。

インターフェイスオプション

Selectはalert、action sheet、popoverインタフェースを使用するので、interfaceOptionsプロパティを通してこれらのコンポーネントにオプションを渡すことができます。これを使用して、カスタムヘッダ、サブヘッダ、CSS クラスなどを渡すことができます。

各インタフェースが受け付けるプロパティについては、ion-alert docs, ion-action-sheet docs, ion-popover docs を参照してください。

注意: alert インターフェイスでは、interfaceOptionsinputsbuttons を上書きしません。

Start and End Slots

The start and end slots can be used to place icons, buttons, or prefix/suffix text on either side of the select. If the slot content is clicked, the select will not open.

注記

In most cases, Icon components placed in these slots should have aria-hidden="true". See the Icon accessibility docs for more information.

If slot content is meant to be interacted with, it should be wrapped in an interactive element such as a Button. This ensures that the content can be tabbed to.

カスタマイズ

Selectコンポーネントには2つのユニットがあり、それぞれ別々にスタイルを設定する必要があります。 ion-select要素は、ビュー上で選択された値、ない場合はプレースホルダ、ドロップダウンのアイコンによって表現されます。インターフェイスは上記のインターフェイスセクションで定義されており、ion-selectをクリックしたときに開くダイアログです。インターフェイスには ion-select-option 要素を追加することで定義されるすべてのオプションが含まれています。以下のセクションでは、これらのスタイリングの違いについて説明します。

Select要素のスタイリング

前述の通り、ion-select要素は値、プレースホルダ、ビューに表示されるアイコンのみで構成されています。これをカスタマイズするには、CSSとCSSカスタムプロパティを組み合わせてスタイルを設定します。

また、必要なブラウザサポートによっては、CSSのシャドウパーツを使用してセレクトのスタイルを設定することもできます。 part を使用することで、要素上の任意の CSS プロパティを対象とすることができることに注意してください。

セレクトインターフェースのスタイリング

インターフェイスのダイアログをカスタマイズするには、そのインターフェイスのドキュメントのカスタマイズのセクションに従ってください:

しかし、セレクト・オプションは、スタイルを簡単にするためにクラスを設定し、オーバーレイ・オプションにクラスを渡すことができます。オプションをカスタマイズする使用例については、セレクト・オプションのドキュメントを参照してください。

Custom Toggle Icons

選択テキストの隣に表示されるアイコンは、toggleIcon プロパティと expandedIcon プロパティを使用して、任意の Ionicon に設定することができます。

アイコンの反転動作

デフォルトでは、セレクトを開いているとき、トグルアイコンは md モードでは自動的に回転し、ios モードでは静止します。この動作はCSSを使ってカスタマイズすることができます。

以下の例ではcustom toggleIconを使って、iosモードでのトグルアイコンの反転動作をより分かりやすく説明しています。

Typeahead Component

Typeaheadまたはオートコンプリート機能は、既存のIonicコンポーネントを使用して構築できます。利用可能なスクリーンスペースを最大限に活用するために、ion-modalを使用することをお勧めします。

Interfaces

SelectChangeEventDetail

interface SelectChangeEventDetail<T = any> {
value: T;
}

SelectCustomEvent

必須ではありませんが、このインターフェイスを CustomEvent インターフェイスの代わりに使用することで、このコンポーネントから発行される Ionic イベントをより強力に片付けすることができます。

interface SelectCustomEvent<T = any> extends CustomEvent {
detail: SelectChangeEventDetail<T>;
target: HTMLIonSelectElement;
}

従来のセレクト構文からの移行

Ionic 7.0では、よりシンプルなセレクト構文が導入されました。この新しい構文は、セレクトのセットアップに必要な定型文を減らし、アクセシビリティの問題を解決し、開発者のエクスペリエンスを向上させます。

開発者はこの移行をセレクトごとに行うことができます。開発者は従来の構文を使い続けることもできますが、できるだけ早く移行することをお勧めします。

最新構文の使用

最新の構文を使用するには、2つのステップがあります:

    1. ion-label を削除し、代わりに ion-selectlabel プロパティを使用する。ラベルの配置は ion-selectlabelPlacement プロパティで設定できる。
  1. fillshapeion-itemからion-select` に移動する。
<!-- Label and Label Position -->

<!-- Before -->
<ion-item>
<ion-label position="floating">Favorite Fruit:</ion-label>
<ion-select>...</ion-select>
</ion-item>

<!-- After -->
<ion-item>
<ion-select label="Favorite Fruit:" label-placement="floating">...</ion-select>
</ion-item>


<!-- Fill -->

<!-- Before -->
<ion-item fill="outline" shape="round">
<ion-label position="floating">Favorite Fruit:</ion-label>
<ion-select>...</ion-select>
</ion-item>

<!-- After -->

<!-- Inputs using `fill` should not be placed in ion-item -->
<ion-select fill="outline" shape="round" label="Favorite Fruit:" label-placement="floating">...</ion-select>

レガシー構文の使用

Ionicは、アプリがモダンなセレクト構文を使用しているかどうかをヒューリスティックで検出します。場合によっては、レガシー構文を使い続けた方が望ましいこともあります。開発者は ion-selectlegacy プロパティを true に設定することで、その入力インスタンスにレガシー構文を使用させることができます。

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.
KeyDescription
EnterOpens the overlay and focuses on the first selected option. If there is no selected option, then it focuses on the first option.
SpaceOpens 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.

These keyboard interactions apply to ion-action-sheet, ion-alert, and ion-popover elements when the overlay is presented and focused.

KeyDescription
ArrowDownFocuses and selects the next option in the list. If there is no next option, selection will cycle to the first option.
ArrowLeftFocuses and selects the previous option in the list. If there is no previous option, selection will cycle to the last option.
ArrowRightFocuses and selects the next option in the list. If there is no next option, selection will cycle to the first option.
ArrowUpFocuses and selects the previous option in the list. If there is no previous option, selection will cycle to the last option.
EnterIf 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.
EscapeCloses the overlay without changing the submitted option. Returns the focus back to the ion-select element.
SpaceIf 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.
TabMoves 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.

These keyboard interactions apply to ion-alert and ion-popover elements when the overlay is presented and multiple selection is enabled.

KeyDescription
EnterWhen the 'OK' button is focused, it will save the user's selection, dismiss the overlay, and return focus to the ion-select element.
EscapeCloses the overlay without changing the submitted option(s). Returns the focus back to the ion-select element.
SpaceSelects 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.
TabMove 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

DescriptionThe text to display on the cancel button.
Attributecancel-text
Typestring
Default'Cancel'

color

DescriptionThe color to use from your application's color palette. Default options are: "primary", "secondary", "tertiary", "success", "warning", "danger", "light", "medium", and "dark". For more information on colors, see theming.

This property is only available when using the modern select syntax.
Attributecolor
Type"danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string | undefined
Defaultundefined

compareWith

DescriptionThis property allows developers to specify a custom function or property name for comparing objects when determining the selected option in the ion-select. When not specified, the default behavior will use strict equality (===) for comparison.
Attributecompare-with
Type((currentValue: any, compareValue: any) => boolean) | null | string | undefined
Defaultundefined

disabled

DescriptionIf true, the user cannot interact with the select.
Attributedisabled
Typeboolean
Defaultfalse

expandedIcon

DescriptionThe toggle icon to show when the select is open. If defined, the icon rotation behavior in md mode will be disabled. If undefined, toggleIcon will be used for when the select is both open and closed.
Attributeexpanded-icon
Typestring | undefined
Defaultundefined

fill

DescriptionThe fill for the item. If "solid" the item will have a background. If "outline" the item will be transparent with a border. Only available in md mode.
Attributefill
Type"outline" | "solid" | undefined
Defaultundefined

interface

DescriptionThe interface the select should use: action-sheet, popover or alert.
Attributeinterface
Type"action-sheet" | "alert" | "popover"
Default'alert'

interfaceOptions

DescriptionAny additional options that the alert, action-sheet or popover interface can take. See the ion-alert docs, the ion-action-sheet docs and the ion-popover docs for the create options for each interface.

Note: interfaceOptions will not override inputs or buttons with the alert interface.
Attributeinterface-options
Typeany
Default{}

justify

DescriptionHow to pack the label and select within a line. justify does not apply when the label and select are on different lines when labelPlacement is set to "floating" or "stacked". "start": The label and select will appear on the left in LTR and on the right in RTL. "end": The label and select will appear on the right in LTR and on the left in RTL. "space-between": The label and select will appear on opposite ends of the line with space between the two elements.
Attributejustify
Type"end" | "space-between" | "start"
Default'space-between'

label

DescriptionThe visible label associated with the select.

Use this if you need to render a plaintext label.

The label property will take priority over the label slot if both are used.
Attributelabel
Typestring | undefined
Defaultundefined

labelPlacement

DescriptionWhere to place the label relative to the select. "start": The label will appear to the left of the select in LTR and to the right in RTL. "end": The label will appear to the right of the select in LTR and to the left in RTL. "floating": The label will appear smaller and above the select when the select is focused or it has a value. Otherwise it will appear on top of the select. "stacked": The label will appear smaller and above the select regardless even when the select is blurred or has no value. "fixed": The label has the same behavior as "start" except it also has a fixed width. Long text will be truncated with ellipses ("..."). When using "floating" or "stacked" we recommend initializing the select with either a value or a placeholder.
Attributelabel-placement
Type"end" | "fixed" | "floating" | "stacked" | "start" | undefined
Default'start'

mode

DescriptionThe mode determines which platform styles to use.
Attributemode
Type"ios" | "md"
Defaultundefined

multiple

DescriptionIf true, the select can accept multiple values.
Attributemultiple
Typeboolean
Defaultfalse

name

DescriptionThe name of the control, which is submitted with the form data.
Attributename
Typestring
Defaultthis.inputId

okText

DescriptionThe text to display on the ok button.
Attributeok-text
Typestring
Default'OK'

placeholder

DescriptionThe text to display when the select is empty.
Attributeplaceholder
Typestring | undefined
Defaultundefined

selectedText

DescriptionThe text to display instead of the selected option's value.
Attributeselected-text
Typenull | string | undefined
Defaultundefined

shape

DescriptionThe shape of the select. If "round" it will have an increased border radius.
Attributeshape
Type"round" | undefined
Defaultundefined

toggleIcon

DescriptionThe toggle icon to use. Defaults to chevronExpand for ios mode, or caretDownSharp for md mode.
Attributetoggle-icon
Typestring | undefined
Defaultundefined

value

DescriptionThe value of the select.
Attributevalue
Typeany
Defaultundefined

イベント

NameDescriptionBubbles
ionBlurEmitted when the select loses focus.true
ionCancelEmitted when the selection is cancelled.true
ionChangeEmitted when the value has changed.true
ionDismissEmitted when the overlay is dismissed.true
ionFocusEmitted when the select has focus.true

メソッド

open

DescriptionOpen the select overlay. The overlay is either an alert, action sheet, or popover, depending on the interface property on the ion-select.
Signatureopen(event?: UIEvent) => Promise<any>

CSS Shadow Parts

NameDescription
containerThe container for the selected text or placeholder.
iconThe select icon container.
labelThe label text describing the select.
placeholderThe text displayed in the select when there is no value.
textThe displayed value of the select.

CSS カスタムプロパティ

NameDescription
--backgroundBackground of the select
--border-colorColor of the select border
--border-radiusRadius of the select border. A large radius may display unevenly when using fill="outline"; if needed, use shape="round" instead or increase --padding-start.
--border-styleStyle of the select border
--border-widthWidth of the select border
--highlight-color-focusedThe color of the highlight on the select when focused
--highlight-color-invalidThe color of the highlight on the select when invalid
--highlight-color-validThe color of the highlight on the select when valid
--highlight-heightThe height of the highlight on the select. Only applies to md mode.
--padding-bottomBottom padding of the select
--padding-endRight padding if direction is left-to-right, and left padding if direction is right-to-left of the select
--padding-startLeft padding if direction is left-to-right, and right padding if direction is right-to-left of the select
--padding-topTop padding of the select
--placeholder-colorColor of the select placeholder text
--placeholder-opacityOpacity of the select placeholder text
--ripple-colorThe color of the ripple effect on MD mode.

Slots

NameDescription
endContent to display at the trailing edge of the select.
labelThe label text to associate with the select. Use the labelPlacement property to control where the label is placed relative to the select. Use this if you need to render a label with custom HTML.
startContent to display at the leading edge of the select.