Searchbars represent a text field that can be used to search through a collection. They can be displayed inside of a toolbar or the main content. A searchbar should be used instead of an input to search lists.
A clear button is displayed when a searchbar has a value or upon entering input in the searchbar's text field. Clicking on the clear button will erase the text field and the input will remain focused. By default, the clear button is set to show when focusing the searchbar, but it can be set to always show or never show. The icon inside of the clear button can also be customized to any Ionicon.
<ion-searchbarshow-clear-button="focus"value="Show on Focus"></ion-searchbar><ion-searchbarshow-clear-button="always"value="Always Show"></ion-searchbar><ion-searchbarshow-clear-button="never"value="Never Show"></ion-searchbar><ion-searchbarshow-clear-button="always"clear-icon="trash-bin"value="Custom Clear Icon"></ion-searchbar>
Cancel Button
A cancel button can be enabled which will clear the input and lose the focus upon click. By default, cancel buttons are set to never show, but they can be set to always show or only show when focusing the searchbar. The cancel button is displayed as text in ios mode, and as an icon in md mode. Both the text and icon can be customized using different properties, with the icon accepting any Ionicon.
<ion-searchbarshow-cancel-button="focus"placeholder="Show on Focus"></ion-searchbar><ion-searchbarshow-cancel-button="always"placeholder="Always Show"></ion-searchbar><ion-searchbarshow-cancel-button="never"placeholder="Never Show"></ion-searchbar><ion-searchbarshow-cancel-button="always"cancel-button-text="Custom Cancel"cancel-button-icon="trash"placeholder="Custom Cancel Button"></ion-searchbar>
Searchbars in Toolbars
Searchbars are styled to look native when placed inside of a toolbar. In iOS, searchbars should be placed in their own toolbar, under a toolbar that contains the page title. In Material Design, searchbars are either persistently displayed in their own toolbar, or expand over a toolbar containing the page title.
A debounce can be set on the searchbar in order to delay triggering the ionInput event. This is useful when querying data, as it can be used to wait to make a request instead of requesting the data each time a character is entered in the input.
<ion-searchbar[debounce]="1000"(ionInput)="handleInput($event)"></ion-searchbar><ion-list> @for (result of results; track result) {<ion-item><ion-label>{{ result }}</ion-label></ion-item> }</ion-list>
Searchbar uses scoped encapsulation, which means it will automatically scope its CSS by appending each of the styles with an additional class at runtime. Overriding scoped selectors in CSS requires a higher specificity selector. Targeting the ion-searchbar for customization will not work, therefore we recommend adding a class and customizing it that way.
By default, tapping the input will cause the keyboard to appear with a magnifying glass icon on the submit button. You can optionally set the inputmode property to "search", which will change the icon from a magnifying glass to a carriage return.
iOS
By default, tapping the input will cause the keyboard to appear with the text "return" on a gray submit button. You can optionally set the inputmode property to "search", which will change the text from "return" to "go", and change the button color from gray to blue. Alternatively, you can wrap the ion-searchbar in a form element with an action property. This will cause the keyboard to appear with a blue submit button that says "search".
While not required, this interface can be used in place of the CustomEvent interface for stronger typing with Ionic events emitted from this component.
Indicates whether and how the text value should be automatically capitalized as it is entered/edited by the user. Available options: "off", "none", "on", "sentences", "words", "characters".
Set the cancel button icon. Only applies to md mode. Defaults to arrow-back-sharp.
Attribute
cancel-button-icon
Type
string
Default
config.get('backButtonIcon', arrowBackSharp) as string
cancelButtonText
Description
Set the cancel button text. Only applies to ios mode.
Attribute
cancel-button-text
Type
string
Default
'Cancel'
clearIcon
Description
Set the clear icon. Defaults to close-circle for ios and close-sharp for md.
Attribute
clear-icon
Type
string | undefined
Default
undefined
color
Description
The 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 attribute specifies the maximum number of characters that the user can enter.
Attribute
maxlength
Type
number | undefined
Default
undefined
minlength
Description
This attribute specifies the minimum number of characters that the user can enter.
Attribute
minlength
Type
number | undefined
Default
undefined
mode
Description
The mode determines which platform styles to use.
This is a virtual property that is set once during initialization and will not update if you change its value after the initial render.
Attribute
mode
Type
"ios" | "md"
Default
undefined
name
Description
If used in a form, set the name of the control, which is submitted with the form data.
Attribute
name
Type
string
Default
this.inputId
placeholder
Description
Set the input's placeholder. placeholder can accept either plaintext or HTML as a string. To display characters normally reserved for HTML, they must be escaped. For example <Ionic> would become <Ionic>
The icon to use as the search icon. Defaults to search-outline in ios mode and search-sharp in md mode.
Attribute
search-icon
Type
string | undefined
Default
undefined
showCancelButton
Description
Sets the behavior for the cancel button. Defaults to "never". Setting to "focus" shows the cancel button on focus. Setting to "never" hides the cancel button. Setting to "always" shows the cancel button regardless of focus state.
Attribute
show-cancel-button
Type
"always" | "focus" | "never"
Default
'never'
showClearButton
Description
Sets the behavior for the clear button. Defaults to "focus". Setting to "focus" shows the clear button on focus if the input is not empty. Setting to "never" hides the clear button. Setting to "always" shows the clear button regardless of focus state, but only if the input is not empty.
The ionChange event is fired for <ion-searchbar> elements when the user modifies the element's value. Unlike the ionInput event, the ionChange event is not necessarily fired for each alteration to an element's value.
The ionChange event is fired when the value has been committed by the user. This can happen when the element loses focus or when the "Enter" key is pressed. ionChange can also fire when clicking the clear or cancel buttons.
This event will not emit when programmatically setting the value property.
true
ionClear
Emitted when the clear input button is clicked.
true
ionFocus
Emitted when the input has focus.
true
ionInput
Emitted when the value of the ion-searchbar element has changed.
true
Methods
getInputElement
Description
Returns the native <input> element used under the hood.
Signature
getInputElement() => Promise<HTMLInputElement>
setFocus
Description
Sets focus on the native input in ion-searchbar. Use this method instead of the global input.focus(). Developers who wish to focus an input when a page enters should call setFocus() in the ionViewDidEnter() lifecycle method. Developers who wish to focus an input when an overlay is presented should call setFocus after didPresent has resolved.