ion-select
Selects are form controls to select an option, or options, from a set of options, similar to a native <select>
element. When a user taps the select, a dialog appears with all of the options in a large, easy to select list.
A select should be used with child <ion-select-option>
elements. If the child option is not given a value
attribute then its text will be used as the value.
If value
is set on the <ion-select>
, the selected option will be chosen based on that value.
#
InterfacesBy default, select uses ion-alert to open up the overlay of options in an alert. The interface can be changed to use ion-action-sheet or ion-popover by passing action-sheet
or popover
, respectively, to the interface
property. Read on to the other sections for the limitations of the different interfaces.
#
Single SelectionBy default, the select allows the user to select only one option. The alert interface presents users with a radio button styled list of options. The action sheet interface can only be used with a single value select. The select component's value receives the value of the selected option's value.
#
Multiple SelectionBy adding the multiple
attribute to select, users are able to select multiple options. When multiple options can be selected, the alert overlay presents users with a checkbox styled list of options. The select component's value receives an array of all of the selected option values.
Note: the action-sheet
and popover
interfaces will not work with multiple selection.
#
Object Value ReferencesWhen using objects for select values, it is possible for the identities of these objects to change if they are coming from a server or database, while the selected value's identity remains the same. For example, this can occur when an existing record with the desired object value is loaded into the select, but the newly retrieved select options now have different identities. This will result in the select appearing to have no value at all, even though the original selection in still intact.
By default, the select uses object equality (===
) to determine if an option is selected. This can be overridden by providing a property name or a function to the compareWith
property.
#
Select ButtonsThe alert supports two buttons: Cancel
and OK
. Each button's text can be customized using the cancelText
and okText
properties.
The action-sheet
and popover
interfaces do not have an OK
button, clicking on any of the options will automatically close the overlay and select that value. The popover
interface does not have a Cancel
button, clicking on the backdrop will close the overlay.
#
Interface OptionsSince select uses the alert, action sheet and popover interfaces, options can be passed to these components through the interfaceOptions
property. This can be used to pass a custom header, subheader, css class, and more.
See the ion-alert docs, ion-action-sheet docs, and ion-popover docs for the properties that each interface accepts.
Note: interfaceOptions
will not override inputs
or buttons
with the alert
interface.
#
CustomizationThere are two units that make up the Select component and each need to be styled separately. The ion-select
element is represented on the view by the selected value(s), or placeholder if there is none, and dropdown icon. The interface, which is defined in the Interfaces section above, is the dialog that opens when clicking on the ion-select
. The interface contains all of the options defined by adding ion-select-option
elements. The following sections will go over the differences between styling these.
#
Styling Select ElementAs mentioned, the ion-select
element consists only of the value(s), or placeholder, and icon that is displayed on the view. To customize this, style using a combination of CSS and any of the CSS custom properties:
Alternatively, depending on the browser support needed, CSS shadow parts can be used to style the select:
Notice that by using ::part
, any CSS property on the element can be targeted.
#
Styling Select InterfaceCustomizing the interface dialog should be done by following the Customization section in that interface's documentation:
However, the Select Option does set a class for easier styling and allows for the ability to pass a class to the overlay option, see the Select Options documentation for usage examples of customizing options.
#
Usage- ANGULAR
- JAVASCRIPT
- REACT
- STENCIL
- VUE
#
Properties#
cancelTextDescription | The text to display on the cancel button. |
Attribute | cancel-text |
Type | string |
Default | 'Cancel' |
#
compareWithDescription | A property name or function used to compare object values |
Attribute | compare-with |
Type | ((currentValue: any, compareValue: any) => boolean) \| null \| string \| undefined |
Default | undefined |
#
disabledDescription | If true , the user cannot interact with the select. |
Attribute | disabled |
Type | boolean |
Default | false |
#
interfaceDescription | The interface the select should use: action-sheet , popover or alert . |
Attribute | interface |
Type | "action-sheet" \| "alert" \| "popover" |
Default | 'alert' |
#
interfaceOptionsDescription | Any additional options that the alert , action-sheet or popover interfacecan 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. |
Attribute | interface-options |
Type | any |
Default | {} |
#
modeDescription | The mode determines which platform styles to use. |
Attribute | mode |
Type | "ios" \| "md" |
Default | undefined |
#
multipleDescription | If true , the select can accept multiple values. |
Attribute | multiple |
Type | boolean |
Default | false |
#
nameDescription | The name of the control, which is submitted with the form data. |
Attribute | name |
Type | string |
Default | this.inputId |
#
okTextDescription | The text to display on the ok button. |
Attribute | ok-text |
Type | string |
Default | 'OK' |
#
placeholderDescription | The text to display when the select is empty. |
Attribute | placeholder |
Type | null \| string \| undefined |
Default | undefined |
#
selectedTextDescription | The text to display instead of the selected option's value. |
Attribute | selected-text |
Type | null \| string \| undefined |
Default | undefined |
#
valueDescription | the value of the select. |
Attribute | value |
Type | any |
Default | undefined |
#
EventsName | Description |
---|---|
ionBlur | Emitted when the select loses focus. |
ionCancel | Emitted when the selection is cancelled. |
ionChange | Emitted when the value has changed. |
ionFocus | Emitted when the select has focus. |
#
Methods#
openDescription | Open the select overlay. The overlay is either an alert, action sheet, or popover, depending on the interface property on the ion-select . |
Signature | open(event?: UIEvent \| undefined) => Promise<any> |
#
CSS Shadow PartsName | Description |
---|---|
icon | The select icon container. |
placeholder | The text displayed in the select when there is no value. |
text | The displayed value of the select. |
#
CSS Custom PropertiesName | Description |
---|---|
--padding-bottom | Bottom padding of the select |
--padding-end | Right padding if direction is left-to-right, and left padding if direction is right-to-left of the select |
--padding-start | Left padding if direction is left-to-right, and right padding if direction is right-to-left of the select |
--padding-top | Top padding of the select |
--placeholder-color | Color of the select placeholder text |
--placeholder-opacity | Opacity of the select placeholder text |