| title | ion-select |
|---|
import Props from '@ionic-internal/component-api/v7/select/props.md'; import Events from '@ionic-internal/component-api/v7/select/events.md'; import Methods from '@ionic-internal/component-api/v7/select/methods.md'; import Parts from '@ionic-internal/component-api/v7/select/parts.md'; import CustomProps from '@ionic-internal/component-api/v7/select/custom-props.md'; import Slots from '@ionic-internal/component-api/v7/select/slots.md';
<title>ion-select: Select One or Multiple Value Boxes or Placeholders</title>import EncapsulationPill from '@components/page/api/EncapsulationPill';
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.
By 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 select component's value receives the value of the selected option's value.
import SingleSelectionExample from '@site/static/usage/v7/select/basic/single-selection/index.md';
By 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.
import ActionSheetExample from '@site/static/usage/v7/select/interfaces/action-sheet/index.md';
import PopoverExample from '@site/static/usage/v7/select/interfaces/popover/index.md';
By adding the multiple attribute to select, users are able to select multiple options. When multiple options can be selected, the alert or popover 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 interface will not work with multiple selection.
import MultipleSelectionExample from '@site/static/usage/v7/select/basic/multiple-selection/index.md';
The main ways of handling user interaction with the select are the ionChange, ionDismiss, and ionCancel events. See Events for more details on these and other events that select fires.
import RespondingToInteractionExample from '@site/static/usage/v7/select/basic/responding-to-interaction/index.md';
When 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.
import UsingCompareWithExample from '@site/static/usage/v7/select/objects-as-values/using-comparewith/index.md';
import ObjectValuesAndMultipleSelectionExample from '@site/static/usage/v7/select/objects-as-values/multiple-selection/index.md';
Labels will take up the width of their content by default. Developers can use the labelPlacement property to control how the label is placed relative to the control.
import LabelPlacement from '@site/static/usage/v7/select/label-placement/index.md';
Developers can use the justify property to control how the label and control are packed on a line.
import JustifyExample from '@site/static/usage/v7/select/justify/index.md';
Material Design offers filled styles for a select. The fill property on the select can be set to either "solid" or "outline".
Since the fill styles visually defines the select container, selects that use fill should not be used in ion-item.
import FillExample from '@site/static/usage/v7/select/fill/index.md';
The 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.
import ButtonTextExample from '@site/static/usage/v7/select/customization/button-text/index.md';
Since 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.
import InterfaceOptionsExample from '@site/static/usage/v7/select/customization/interface-options/index.md';
There 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.
As 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.
import StylingSelectExample from '@site/static/usage/v7/select/customization/styling-select/index.md';
Customizing 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.
Typeahead or autocomplete functionality can be built using existing Ionic components. We recommend using an ion-modal to make the best use of the available screen space.
import TypeaheadExample from '@site/static/usage/v7/select/typeahead/index.md';
interface SelectChangeEventDetail<T = any> {
value: T;
}While not required, this interface can be used in place of the CustomEvent interface for stronger typing with Ionic events emitted from this component.
interface SelectCustomEvent<T = any> extends CustomEvent {
detail: SelectChangeEventDetail<T>;
target: HTMLIonSelectElement;
}A simpler select syntax was introduced in Ionic 7.0. This new syntax reduces the boilerplate required to setup an select, resolves accessibility issues, and improves the developer experience.
Developers can perform this migration one select at a time. While developers can continue using the legacy syntax, we recommend migrating as soon as possible.
Using the modern syntax involves two steps:
- Remove
ion-labeland use thelabelproperty onion-selectinstead. The placement of the label can be configured using thelabelPlacementproperty onion-select. - Move any usage of
fillandshapefromion-itemon toion-select.
import Migration from '@site/static/usage/v7/select/migration/index.md';
Ionic uses heuristics to detect if an app is using the modern select syntax. In some instances, it may be preferable to continue using the legacy syntax. Developers can set the legacy property on ion-select to true to force that instance of the input to use the legacy syntax.