import{o as s,a,y as e}from"./vue-libs.b44bc779.js";const n={class:"van-doc-markdown-body"},d=e(`

Search

Intro

Input box component for search scenarios.

Install

Register component globally via app.use, refer to Component Registration for more registration ways.

import { createApp } from 'vue';
import { Search } from 'vant';

const app = createApp();
app.use(Search);

Usage

Basic Usage

<van-search v-model="value" placeholder="Placeholder" />
import { ref } from 'vue';

export default {
  setup() {
    const value = ref('');
    return { value };
  },
};

Listen to Events

search event will be Emitted when click the search button on the keyboard, cancel event will be Emitted when click the cancel button.

<form action="/">
  <van-search
    v-model="value"
    show-action
    placeholder="Placeholder"
    @search="onSearch"
    @cancel="onCancel"
  />
</form>
import { ref } from 'vue';
import { Toast } from 'vant';

export default {
  setup() {
    const value = ref('');
    const onSearch = (val) => Toast(val);
    const onCancel = () => Toast('Cancel');
    return {
      value,
      onSearch,
      onCancel,
    };
  },
};

Tips: There will be a search button on the keyboard when Search is inside a form in iOS.

Input Align

<van-search v-model="value" input-align="center" placeholder="Placeholder" />

Disabled

<van-search v-model="value" disabled placeholder="Placeholder" />

Custom Background Color

<van-search
  v-model="value"
  shape="round"
  background="#4fc08d"
  placeholder="Placeholder"
/>

Custom Action Button

Use action slot to custom right button, cancel event will no longer be Emitted when use this slot.

<van-search
  v-model="value"
  show-action
  label="Address"
  placeholder="Placeholder"
  @search="onSearch"
>
  <template #action>
    <div @click="onClickButton">Search</div>
  </template>
</van-search>
import { ref } from 'vue';
import { Toast } from 'vant';

export default {
  setup() {
    const value = ref('');
    const onSearch = (val) => Toast(val);
    const onClickButton = () => Toast(value.value);
    return {
      value,
      onSearch,
      onClickButton,
    };
  },
};

API

Props

AttributeDescriptionTypeDefault
v-modelInput valuenumber | string-
labelLeft side labelstring-
name v3.2.3As the identifier when submitting the formstring-
shapeShape of field, can be set to roundstringsquare
id v3.2.2Input id, the for attribute of the label also will be setstringvan-search-n-input
backgroundBackground color of fieldstring#f2f2f2
maxlengthMax length of valuenumber | string-
placeholderPlaceholderstring-
clearableWhether to be clearablebooleantrue
clear-icon v3.0.12Clear icon namestringclear
clear-triggerWhen to display the clear icon, always means to display the icon when value is not empty, focus means to display the icon when input is focusedstringfocus
autofocusWhether to auto focus, unsupported in iOSbooleanfalse
show-actionWhether to show right action buttonbooleanfalse
action-textText of action buttonstringCancel
disabledWhether to disable fieldbooleanfalse
readonlyWhether to be readonlybooleanfalse
errorWhether to mark the input content in redbooleanfalse
error-message v3.0.12Error messagestring-
formatter v3.0.12Input value formatter(val: string) => string-
format-trigger v3.0.12When to format value, can be set to onBlurstringonChange
input-alignText align of field, can be set to center rightstringleft
left-iconLeft icon namestringsearch
right-iconRight icon namestring-
autocomplete v3.2.3autocomplete attribute of native input elementstring-

Events

EventDescriptionArguments
searchEmitted when confirming searchvalue: string
update:model-valueEmitted when input value changedvalue: string
focusEmitted when input is focusedevent: Event
blurEmitted when input is blurredevent: Event
click-inputEmitted when the input is clickedevent: MouseEvent
click-left-icon v3.4.0Emitted when the left icon is clickedevent: MouseEvent
click-right-icon v3.4.0Emitted when the right icon is clickedevent: MouseEvent
clearEmitted when the clear icon is clickedevent: MouseEvent
cancelEmitted when the cancel button is clicked-

Methods

Use ref to get Search instance and call instance methods.

NameDescriptionAttributeReturn value
focusTrigger input focus--
blurTrigger input blur--

Types

The component exports the following type definitions:

import type { SearchProps, SearchShape, SearchInstance } from 'vant';

SearchInstance is the type of component instance:

import { ref } from 'vue';
import type { SearchInstance } from 'vant';

const searchRef = ref<SearchInstance>();

searchRef.value?.focus();

Slots

NameDescription
leftCustom left side content
actionCustom right button, displayed when show-action is true
labelCustom Search label
left-iconCustom left icon
right-iconCustom right icon

Theming

CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

NameDefault ValueDescription
--van-search-padding10px var(--van-padding-sm)-
--van-search-background-colorvar(--van-background-color-light)-
--van-search-content-background-colorvar(--van-gray-1)-
--van-search-input-height34px-
--van-search-label-padding0 5px-
--van-search-label-colorvar(--van-text-color)-
--van-search-label-font-sizevar(--van-font-size-md)-
--van-search-left-icon-colorvar(--van-gray-6)-
--van-search-action-padding0 var(--van-padding-xs)-
--van-search-action-text-colorvar(--van-text-color)-
--van-search-action-font-sizevar(--van-font-size-md)-
`,18),l=[d],i={__name:"README",setup(c,{expose:t}){return t({frontmatter:{}}),(r,p)=>(s(),a("div",n,l))}};export{i as default};