Field input box

introduce

Users can enter or edit text in the text box.

Code Demo

Basic usage

You can use v-model to bidirectionally bind the value of the input box, and use placeholder to set the placeholder text.

  <z-field v-model="value" label="text" placeholder="Please enter text" />
import { ref } from 'vue'
const value = ref('')

Custom type

Different types of input boxes are defined according to the type attribute. The default value is text.

   <!-- Enter any text -->
   <z-field v-model="text" label="text" />
   <!-- Enter the mobile phone number and bring up the mobile phone number keyboard -->
   <z-field v-model="tel" type="tel" label="Mobile phone number" />
   <!-- Allow the input of positive integers and bring up the pure numeric keyboard -->
   <z-field v-model="digit" type="digit" label="integer" />
   <!-- Allows you to enter numbers and calls up a pure numeric keyboard with symbols -->
   <z-field v-model="number" type="number" label="number" />
   <!-- Enter password -->
   <z-field v-model="password" type="password" label="password" />

Disable input box

Use readonly to set the input box to a read-only state, and use disabled to set the input box to a disabled state.

   <z-field label="Text" model-value="Input box read-only" readonly />
   <z-field label="text" model-value="Input box is disabled" disabled />

Show icon

Configure the icons on both sides of the input box through left-icon and right-icon, and display the clear icon during the input process by setting clearable.

<z-cell-group inset>
   <z-field
     v-model="value1"
     label="text"
     left-icon="detail"
     right-icon="info-circle"
     placeholder="show icon"
   />
   <z-field
     v-model="value2"
     clearable
     label="text"
     left-icon="safetycertificate"
     placeholder="Show clear icon"
   />
</z-cell-group>

Required asterisk

Set the required attribute to display required asterisks.

   <z-field
     v-model="username"
     required
     label="username"
     placeholder="Please enter username"
   />
   <z-field
     v-model="phone"
     required
     label="mobile phone number"
     placeholder="Please enter your mobile phone number"
   />

Please note that the required attribute is only used to control style display. When performing form validation, you need to use the rule.required option to control the validation logic.

Automatically display asterisks

You can set required="auto" on the Form component. At this time, all Fields in the Form will automatically determine whether asterisks need to be displayed based on rule.required.

<z-form required="auto">
   <z-field
     v-model="username"
     :rules="[{ required: true }]"
     label="username"
     placeholder="Please enter username"
   />
   <z-field
     v-model="phone"
     :rules="[{ required: false }]"
     label="mobile phone number"
     placeholder="Please enter your mobile phone number"
   />
</z-form>

Error message

Setting the required attribute indicates that this is a required item, and can be used with the error or error-message attribute to display the corresponding error prompt.

   <z-field
     v-model="username"
     error
     label="username"
     placeholder="Please enter username"
   />
   <z-field
     v-model="phone"
     label="mobile phone number"
     placeholder="Please enter your mobile phone number"
     error-message="Mobile phone number format error"
   />

Insert button

The button slot allows you to insert buttons at the end of the input box.

   <z-field
     v-model="sms"
     center
     clearable
     label="SMS verification code"
     placeholder="Please enter the SMS verification code"
   >
     <template #button>
       <z-button size="small" type="primary">Send verification code</z-button>
     </template>
   </z-field>

Format input content

The input content can be formatted through the formatter attribute, and the timing of formatting can be specified through the format-trigger attribute. By default, formatting is performed during input.

   <z-field
     v-model="value1"
     label="text"
     :formatter="formatter"
     placeholder="Perform formatting as you type"
   />
   <z-field
     v-model="value2"
     label="text"
     :formatter="formatter"
     format-trigger="onBlur"
     placeholder="Perform formatting when out of focus"
   />
// Filter the input numbers
const formatter = (value) => value.replace(/\d/g, '');

Highly adaptive

For textarea, height adaptation can be set through the autosize property.

   <z-field
     v-model="message"
     rows="1"
     autosize
     label="Message"
     type="textarea"
     placeholder="Please enter a message"
   />

Show word count statistics

Setting the maxlength and show-word-limit properties will display the word count at the bottom.

   <z-field
     v-model="message"
     rows="2"
     autosize
     label="Message"
     type="textarea"
     maxlength="50"
     placeholder="Please enter a message"
     show-word-limit
   />

Alignment of input box content

The alignment of the input box content can be set through the input-align attribute. The optional values are center and right.

   <z-field
     v-model="value"
     label="text"
     placeholder="Input box content right aligned"
     input-align="right"
   />

Input box text position

The position of the input box text can be set through the label-align attribute. The optional values ​​are center, right, and top.

   <z-field
     v-model="value"
     label="text"
     placeholder="top alignment"
     label-align="top"
   />
   <z-field
     v-model="value2"
     label="text"
     placeholder="left aligned"
     label-align="left"
   />
   <z-field
     v-model="value3"
     label="text"
     placeholder="center aligned"
     label-align="center"
   />
   <z-field
     v-model="value4"
     label="text"
     placeholder="right aligned"
     label-align="right"
   />

API

Props

ParametersDescriptionTypeDefault value
v-modelCurrent input valuenumber | string-
labelText on the left side of the input boxstring-
nameName, used as an identifier when submitting the formstring-
typeInput box type, same as uniapp's input componentFieldTypetext
sizeSize, optional values are large normalstring-
maxlengthMaximum number of characters enterednumber | string-
placeholderInput box placeholder prompt textstring-
borderWhether to display the inner borderbooleantrue
disabledWhether to disable the input boxbooleanfalse
readonlyWhether it is in the read-only state. Content cannot be entered in the read-only statebooleanfalse
colonWhether to add a colon after labelbooleanfalse
requiredWhether to display the form required asteriskboolean | 'auto'null
centerWhether to center the content verticallybooleanfalse
clearableWhether to enable the clear icon. Clicking the clear icon will clear the input boxbooleanfalse
clear-iconClear the icon name or image link, which is equivalent to the name attribute of the Icon componentstringclose-circle-fill
clear-triggerThe timing to display the clear icon, always means it will be displayed when the input box is not empty,
focus means it will be displayed when the input box is focused and not empty
FieldClearTriggerfocus
clickableWhether to enable click feedbackbooleanfalse
is-linkWhether to display the right arrow and enable click feedbackbooleanfalse
show-word-limitWhether to display word count statistics, you need to set the maxlength attributebooleanfalse
errorWhether to mark the input content in redbooleanfalse
error-messageBottom error message text, will not be displayed if emptystring-
error-message-alignError text alignment, optional values are center rightFieldTextAlignleft
formatterInput content formatting function(val: string) => string-
format-triggerThe timing when the format function is triggered, the optional value is onBlurFieldFormatTriggeronChange
arrow-directionArrow direction, optional values are left up downstringright
label-classAdditional class name for left textstring | Array | object-
label-widthLeft text width, default unit is pxnumber | string6.2em
label-alignLeft text alignment, optional values are center right topFieldTextAlignleft
input-alignInput box alignment, optional values are center rightFieldTextAlignleft
autosizeWhether to adapt the content height, only valid for textarea,
objects can be passed in, such as { maxHeight: 100, minHeight: 50 },
the unit is px
boolean | FieldAutosizeConfigfalse
left-iconThe left icon name or image link, equivalent to the name attribute of the Icon componentstring-
right-iconThe right icon name or image link, which is equivalent to the name attribute of the Icon componentstring-
icon-prefixIcon class name prefix, equivalent to the class-prefix attribute of the Icon componentstringz-icon
rulesForm validation rules, see Form componentFieldRule-
custom-stylecustom styleobject-

Events

Event nameDescriptionCallback parameters
update:model-valueTriggered when the content of the input box changesvalue: string (currently entered value)
focusTriggered when the input box gains focusevent: Event
blurTriggered when the input box loses focusevent: Event
clearTriggered when the clear button is clickedevent: MouseEvent
clickTriggered when the component is clickedevent: MouseEvent
click-inputTriggered when the input area is clickedevent: MouseEvent
click-left-iconTriggered when the left icon is clickedevent: MouseEvent
click-right-iconTriggered when the right icon is clickedevent: MouseEvent
start-validateTriggered when starting form validation-
end-validateTriggered when form validation ends{ status: string, message: string }

method

Field instances can be obtained through ref and instance methods can be called.

Method nameDescriptionParametersReturn value
focusGet input box focus--
blurCancel focus of input box--

Slots

NameDescriptionParameters
labelCustomize the text on the left side of the input box-
inputCustom input box, after using this slot, the properties and events related to the input box will be invalid-
left-iconCustom input box header icon-
right-iconCustomize the input box tail icon-
buttonCustom input box tail button-
error-messageCustomized bottom error message text
extraExtra content on the far right side of the custom input box-

Theme customization

Style variables

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

NameDefaultDescription
--z-field-label-width6.2em-
--z-field-label-colorvar(--z-text-color)-
--z-field-label-margin-rightvar(--z-padding-sm)-
--z-field-input-text-colorvar(--z-text-color)-
--z-field-input-error-text-colorvar(--z-danger-color)-
--z-field-input-disabled-text-colorvar(--z-text-color-3)-
--z-field-placeholder-text-colorvar(--z-text-color-3)-
--z-field-icon-size36rpx-
--z-field-clear-icon-size36rpx-
--z-field-clear-icon-colorvar(--z-gray-5)-
--z-field-right-icon-colorvar(--z-gray-6)-
--z-field-error-message-colorvar(--z-danger-color)-
--z-field-error-message-font-size24rpx-
--z-field-text-area-min-height120rpx-
--z-field-word-limit-colorvar(--z-gray-7)-
--z-field-word-limit-font-sizevar(--z-font-size-sm)-
--z-field-word-limit-line-height32rpx-
--z-field-disabled-text-colorvar(--z-text-color-3)-
--z-field-required-mark-colorvar(--z-red)-