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

Field

Intro

Field component let users enter and edit text.

Install

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

import { createApp } from 'vue';
import { Field, CellGroup } from 'vant';

const app = createApp();
app.use(Field);
app.use(CellGroup);

Usage

Basic Usage

The value of field is bound with v-model.

<van-cell-group inset>
  <van-field v-model="value" label="Label" placeholder="Text" />
</van-cell-group>
import { ref } from 'vue';

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

Custom Type

Use type prop to custom different type fields.

<van-cell-group inset>
  <van-field v-model="text" label="Text" />
  <van-field v-model="tel" type="tel" label="Phone" />
  <van-field v-model="digit" type="digit" label="Digit" />
  <van-field v-model="number" type="number" label="Number" />
  <van-field v-model="password" type="password" label="Password" />
</van-cell-group>
import { ref } from 'vue';

export default {
  setup() {
    const tel = ref('');
    const text = ref('');
    const digit = ref('');
    const number = ref('');
    const password = ref('');

    return { tel, text, digit, number, password };
  },
};

Disabled

<van-cell-group inset>
  <van-field label="Text" model-value="Input Readonly" readonly />
  <van-field label="Text" model-value="Input Disabled" disabled />
</van-cell-group>

Show Icon

<van-cell-group inset>
  <van-field
    v-model="value1"
    label="Text"
    left-icon="smile-o"
    right-icon="warning-o"
    placeholder="Show Icon"
  />
  <van-field
    v-model="value2"
    clearable
    label="Text"
    left-icon="music-o"
    placeholder="Show Clear Icon"
  />
</van-cell-group>
import { ref } from 'vue';

export default {
  setup() {
    const value1 = ref('');
    const value2 = ref('123');
    return {
      value1,
      value2,
    };
  },
};

Error Info

Use error or error-message to show error info.

<van-cell-group inset>
  <van-field
    v-model="username"
    error
    required
    label="Username"
    placeholder="Username"
  />
  <van-field
    v-model="phone"
    required
    label="Phone"
    placeholder="Phone"
    error-message="Invalid phone"
  />
</van-cell-group>

Insert Button

Use button slot to insert button.

<van-cell-group inset>
  <van-field v-model="sms" center clearable label="SMS" placeholder="SMS">
    <template #button>
      <van-button size="small" type="primary">Send SMS</van-button>
    </template>
  </van-field>
</van-cell-group>

Format Value

Use formatter prop to format the input value.

<van-cell-group inset>
  <van-field
    v-model="value1"
    label="Text"
    :formatter="formatter"
    placeholder="Format On Change"
  />
  <van-field
    v-model="value2"
    label="Text"
    :formatter="formatter"
    format-trigger="onBlur"
    placeholder="Format On Blur"
  />
</van-cell-group>
import { ref } from 'vue';

export default {
  setup() {
    const value1 = ref('');
    const value2 = ref('');
    const formatter = (value) => value.replace(/\\d/g, '');

    return {
      value1,
      value2,
      formatter,
    };
  },
};

Auto Resize

Textarea Field can be auto resize when has autosize prop.

<van-cell-group inset>
  <van-field
    v-model="message"
    label="Message"
    type="textarea"
    placeholder="Message"
    rows="1"
    autosize
  />
</van-cell-group>

Show Word Limit

<van-cell-group inset>
  <van-field
    v-model="message"
    rows="2"
    autosize
    label="Message"
    type="textarea"
    maxlength="50"
    placeholder="Message"
    show-word-limit
  />
</van-cell-group>

Input Align

Use input-align prop to align the input value.

<van-cell-group inset>
  <van-field
    v-model="value"
    label="Text"
    placeholder="Input Align Right"
    input-align="right"
  />
</van-cell-group>

API

Props

AttributeDescriptionTypeDefault
v-modelInput valuenumber | string-
labelLeft side labelstring-
nameAs the identifier when submitting the formstring-
id v3.2.2Input id, the for attribute of the label also will be setstringvan-field-n-input
typeInput type, support all native types and digit typeFieldTypetext
sizeSize, can be set to largestring-
maxlengthMax length of valuenumber | string-
placeholderInput placeholderstring-
borderWhether to show inner borderbooleantrue
disabledWhether to disable fieldbooleanfalse
readonlyWhether to be readonlybooleanfalse
colonWhether to display colon after labelbooleanfalse
requiredWhether to show required markbooleanfalse
centerWhether to center content verticallybooleantrue
clearableWhether to be clearablebooleanfalse
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 focusedFieldClearTriggerfocus
clickableWhether to show click feedback when clickedbooleanfalse
is-linkWhether to show link iconbooleanfalse
autofocusWhether to auto focus, unsupported in iOSbooleanfalse
show-word-limitWhether to show word limit, need to set the maxlength propbooleanfalse
errorWhether to mark the input content in redbooleanfalse
error-messageError messagestring-
error-message-alignError message align, can be set to center rightFieldTextAlignleft
formatterInput value formatter(val: string) => string-
format-triggerWhen to format value, can be set to onBlurFieldFormatTriggeronChange
arrow-directionCan be set to left up downstringright
label-classLabel classNamestring | Array | object-
label-widthLabel widthnumber | string6.2em
label-alignLabel align, can be set to center rightFieldTextAlignleft
input-alignInput align, can be set to center rightFieldTextAlignleft
autosizeTextarea auto resize, can accept an object,
e.g. { maxHeight: 100, minHeight: 50 }
boolean | FieldAutosizeConfigfalse
left-iconLeft side icon namestring-
right-iconRight side icon namestring-
icon-prefixIcon className prefixstringvan-icon
rulesForm validation rulesFieldRule[]-
autocomplete v3.0.3HTML native attribute, see MDN - autocompletestring-
enterkeyhint v3.4.8HTML native attribute, see MDN - enterkeyhint
string-

Events

EventDescriptionArguments
update:model-valueEmitted when input value changedvalue: string
focusEmitted when input is focusedevent: Event
blurEmitted when input is blurredevent: Event
clearEmitted when the clear icon is clickedevent: MouseEvent
clickEmitted when component is clickedevent: MouseEvent
click-inputEmitted when the input is clickedevent: MouseEvent
click-left-iconEmitted when the left icon is clickedevent: MouseEvent
click-right-iconEmitted when the right icon is clickedevent: MouseEvent
start-validate v3.5.1Emitted when start validation-
end-validate v3.5.1Emitted when end validation{ status: string }

Methods

Use ref to get Field instance and call instance methods.

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

Types

The component exports the following type definitions:

import type {
  FieldType,
  FieldRule,
  FieldProps,
  FieldInstance,
  FieldTextAlign,
  FieldRuleMessage,
  FieldClearTrigger,
  FieldFormatTrigger,
  FieldRuleValidator,
  FiledRuleFormatter,
  FieldValidateError,
  FieldAutosizeConfig,
  FieldValidateTrigger,
  FieldValidationStatus,
} from 'vant';

FieldInstance is the type of component instance:

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

const fieldRef = ref<FieldInstance>();

fieldRef.value?.focus();

Slots

NameDescriptionSlotProps
labelCustom label-
inputCustom input-
left-iconCustom left icon-
right-iconCustom right icon-
buttonInsert button-
error-message v3.2.5Custom error message{ message: string }
extraCustom content on the right-

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-field-label-width6.2em-
--van-field-label-colorvar(--van-gray-7)-
--van-field-label-margin-rightvar(--van-padding-sm)-
--van-field-input-text-colorvar(--van-text-color)-
--van-field-input-error-text-colorvar(--van-danger-color)-
--van-field-input-disabled-text-colorvar(--van-text-color-3)-
--van-field-placeholder-text-colorvar(--van-text-color-3)-
--van-field-icon-size16px-
--van-field-clear-icon-size16px-
--van-field-clear-icon-colorvar(--van-gray-5)-
--van-field-right-icon-colorvar(--van-gray-6)-
--van-field-error-message-colorvar(--van-danger-color)-
--van-field-error-message-font-size12px-
--van-field-text-area-min-height60px-
--van-field-word-limit-colorvar(--van-gray-7)-
--van-field-word-limit-font-sizevar(--van-font-size-sm)-
--van-field-word-limit-line-height16px-
--van-field-disabled-text-colorvar(--van-text-color-3)-
--van-field-required-mark-colorvar(--van-red)-
`,22),d=[l],h={__name:"README",setup(p,{expose:s}){return s({frontmatter:{}}),(o,c)=>(t(),a("div",e,d))}};export{h as default};