JGuang

SearchBar

SearchBar · Ant Design Mobile

Preview
npm install antd-mobile@5.43.0

SearchBar

The input box component of the search scene.

Install

npm install antd-mobile@5.43.0

antd-mobile is for mobile web, not React Native or Mini Programs. React 19 projects must register createRoot with unstableSetRender at the entry, or imperative APIs such as Toast and Dialog cannot render. Switch dark mode with <html data-prefers-color-scheme="dark">.

Examples come from the official Ant Design Mobile repository (MIT) at the commit published as npm 5.43.0. Presentation helpers such as DemoBlock are JGuang rewrites, not antd-mobile APIs.

The input box component of the search scene.

When to Use

Narrow down the information pool to get targeted information quickly and easily.

Demos

SearchBar

Props

NameDescriptionTypeDefault
cancelTextText of the cancel buttonstring'取消'
clearOnCancelWhether to clear the input after the cancel button is clickedbooleantrue
clearableWhether to enable the clear icon, the input would be cleared after the clear icon is clickedbooleantrue
searchIconCustom search iconReactNode<SearchOutline />
maxLengthThe maximum number of characters the user can enternumber-
onBlurTriggered when the input lose focus(e: React.FocusEvent<HTMLInputElement>) => void-
onCancelTriggered when the cancel button is clicked() => void-
onChangeTriggered when the input content is changed(value: string) => void-
onClearTriggered when the clear button is clicked() => void-
onFocusTriggered when the input get focus(e: React.FocusEvent<HTMLInputElement>) => void-
onSearchTriggered when the enter key is input(value: string) => void-
onlyShowClearWhenFocusIf true, the clear button will only be displayed when the input box is focused. If false, the clear button will still be displayed after the input box loses focus.booleanfalse
placeholderHint textstring-
showCancelButtonWhether to display the cancel button on the right side of the search inputboolean | ((focus: boolean, value: string) => boolean)false
valueInput valuestring-

In addition, the following native attributes are supported: onCompositionStart onCompositionEnd autoFocus

CSS Variables

NameDescriptionDefault
--backgroundbackground color#f5f5f5
--border-radiuscircle corner6px
--heightinput height32px
--padding-leftThe left padding.8px
--placeholder-colorplaceholder font colorvar(--adm-color-weak)

Ref

The same as Input

FAQ

The keyboard button does not say Search but Return

This property enterkeyhinit can only be used in Android version 4.4.5 or later 、IOS version 13.4 or later, see issue for more details