跳到主要内容
版本:v6

ion-searchbar

scoped

搜索栏(Searchbar)代表一个可用于搜索集合的文本字段。它们可以显示在工具栏内部或主要内容中。搜索列表时应使用搜索栏而不是输入框。

基本用法

搜索图标

搜索图标显示在搜索栏输入字段的左侧。它可以自定义为任何 Ionicon

清除按钮

当搜索栏有值时,或在搜索栏的文本字段中输入时,会显示清除按钮。点击清除按钮将擦除文本字段,输入框保持聚焦。默认情况下,清除按钮设置为在聚焦搜索栏时显示,但可以设置为始终显示或从不显示。清除按钮中的图标也可以自定义为任何 Ionicon

取消按钮

可以启用取消按钮,点击后将清除输入并失去焦点。默认情况下,取消按钮设置为从不显示,但可以设置为始终显示或仅在聚焦搜索栏时显示。取消按钮在 ios 模式下显示为文本,在 md 模式下显示为图标。文本和图标都可以使用不同的属性进行自定义,图标接受任何 Ionicon

工具栏中的搜索栏

搜索栏放置在工具栏内时,样式设计为看起来像原生样式。在 iOS 中,搜索栏应放在自己的工具栏中,位于包含页面标题的工具栏下方。在 Material Design 中,搜索栏要么持续显示在自己的工具栏中,要么扩展到包含页面标题的工具栏上方。

防抖

可以在搜索栏上设置防抖,以延迟触发 ionInput 事件。这在查询数据时很有用,可以用来等待发起请求,而不是每次在输入框中输入字符都请求数据。

主题

颜色

CSS 自定义属性

Searchbar 使用 scoped 封装,这意味着它会在运行时通过为每个样式追加额外类来自动限定 CSS 作用域。在 CSS 中覆盖 scoped 选择器需要更高的特异性选择器。直接定位 ion-searchbar 进行自定义将不起作用,因此我们建议添加一个类并以这种方式进行自定义。

键盘显示

Android

默认情况下,点击输入框会导致键盘出现,并在提交按钮上显示放大镜图标。你可以选择将 inputmode 属性设置为 "search",这将把图标从放大镜更改为回车符。

iOS

默认情况下,点击输入框会导致键盘出现,并在灰色提交按钮上显示文本"return"。你可以选择将 inputmode 属性设置为 "search",这将把文本从"return"更改为"go",并将按钮颜色从灰色更改为蓝色。或者,你可以将 ion-searchbar 包装在带有 action 属性的 form 元素中。这将导致键盘出现一个带有"search"文本的蓝色提交按钮。

接口

SearchbarChangeEventDetail

interface SearchbarChangeEventDetail {
value?: string;
}

SearchbarCustomEvent

虽然不是必需的,但此接口可以替代 CustomEvent 接口,为此组件发出的 Ionic 事件提供更强的类型支持。

interface SearchbarCustomEvent extends CustomEvent {
detail: SearchbarChangeEventDetail;
target: HTMLIonSearchbarElement;
}

属性

animated

说明If true, enable searchbar animation.
属性animated
类型boolean
默认值false

autocapitalize

说明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".
属性autocapitalize
类型string
默认值'default'

autocomplete

说明Set the input's autocomplete property.
属性autocomplete
类型"name" | "email" | "tel" | "url" | "on" | "off" | "honorific-prefix" | "given-name" | "additional-name" | "family-name" | "honorific-suffix" | "nickname" | "username" | "new-password" | "current-password" | "one-time-code" | "organization-title" | "organization" | "street-address" | "address-line1" | "address-line2" | "address-line3" | "address-level4" | "address-level3" | "address-level2" | "address-level1" | "country" | "country-name" | "postal-code" | "cc-name" | "cc-given-name" | "cc-additional-name" | "cc-family-name" | "cc-number" | "cc-exp" | "cc-exp-month" | "cc-exp-year" | "cc-csc" | "cc-type" | "transaction-currency" | "transaction-amount" | "language" | "bday" | "bday-day" | "bday-month" | "bday-year" | "sex" | "tel-country-code" | "tel-national" | "tel-area-code" | "tel-local" | "tel-extension" | "impp" | "photo"
默认值'off'

autocorrect

说明Set the input's autocorrect property.
属性autocorrect
类型"off" | "on"
默认值'off'

cancelButtonIcon

说明Set the cancel button icon. Only applies to md mode. Defaults to arrow-back-sharp.
属性cancel-button-icon
类型string
默认值config.get('backButtonIcon', arrowBackSharp) as string

cancelButtonText

说明Set the the cancel button text. Only applies to ios mode.
属性cancel-button-text
类型string
默认值'Cancel'

clearIcon

说明Set the clear icon. Defaults to close-circle for ios and close-sharp for md.
属性clear-icon
类型string | undefined
默认值undefined

color

说明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.
属性color
类型"danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string | undefined
默认值undefined

debounce

说明Set the amount of time, in milliseconds, to wait to trigger the ionInput event after each keystroke.
属性debounce
类型number | undefined
默认值undefined

disabled

说明If true, the user cannot interact with the input.
属性disabled
类型boolean
默认值false

enterkeyhint

说明A hint to the browser for which enter key to display. Possible values: "enter", "done", "go", "next", "previous", "search", and "send".
属性enterkeyhint
类型"done" | "enter" | "go" | "next" | "previous" | "search" | "send" | undefined
默认值undefined

inputmode

说明A hint to the browser for which keyboard to display. Possible values: "none", "text", "tel", "url", "email", "numeric", "decimal", and "search".
属性inputmode
类型"decimal" | "email" | "none" | "numeric" | "search" | "tel" | "text" | "url" | undefined
默认值undefined

maxlength

说明This attribute specifies the maximum number of characters that the user can enter.
属性maxlength
类型number | undefined
默认值undefined

minlength

说明This attribute specifies the minimum number of characters that the user can enter.
属性minlength
类型number | undefined
默认值undefined

mode

说明The mode determines which platform styles to use.

这是一个虚拟属性,在初始化时设置一次,之后更改其值不会更新组件。
属性mode
类型"ios" | "md"
默认值undefined

name

说明If used in a form, set the name of the control, which is submitted with the form data.
属性name
类型string
默认值this.inputId

placeholder

说明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 &lt;Ionic&gt;

For more information: Security Documentation
属性placeholder
类型string
默认值'Search'

searchIcon

说明The icon to use as the search icon. Defaults to search-outline in ios mode and search-sharp in md mode.
属性search-icon
类型string | undefined
默认值undefined

showCancelButton

说明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.
属性show-cancel-button
类型"always" | "focus" | "never"
默认值'never'

showClearButton

说明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.
属性show-clear-button
类型"always" | "focus" | "never"
默认值'always'

spellcheck

说明If true, enable spellcheck on the input.
属性spellcheck
类型boolean
默认值false

type

说明Set the type of the input.
属性type
类型"email" | "number" | "password" | "search" | "tel" | "text" | "url"
默认值'search'

value

说明the value of the searchbar.
属性value
类型null | string | undefined
默认值''

事件

Name说明冒泡
ionBlurEmitted when the input loses focus.true
ionCancelEmitted when the cancel button is clicked.true
ionChangeThe 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.
true
ionClearEmitted when the clear input button is clicked.true
ionFocusEmitted when the input has focus.true
ionInputEmitted when the value of the ion-searchbar element has changed.true

方法

getInputElement

说明Returns the native <input> element used under the hood.
签名getInputElement() => Promise<HTMLInputElement>

setFocus

说明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.

See managing focus for more information.
签名setFocus() => Promise<void>

CSS 阴影部分

该组件没有可用的 CSS 阴影部分。

CSS 自定义属性

Name说明
--backgroundBackground of the searchbar input
--border-radiusBorder radius of the searchbar input
--box-shadowBox shadow of the searchbar input
--cancel-button-colorColor of the searchbar cancel button
--clear-button-colorColor of the searchbar clear button
--colorColor of the searchbar text
--icon-colorColor of the searchbar icon
--placeholder-colorColor of the searchbar placeholder
--placeholder-font-styleFont style of the searchbar placeholder
--placeholder-font-weightFont weight of the searchbar placeholder
--placeholder-opacityOpacity of the searchbar placeholder

插槽

该组件没有可用的插槽。