Skip to content

Input 输入框 ​

用于单行文本输入,并提供搜索、密码和组合输入等扩展形态。

基础用法 ​

vue
<template>
  <x-input v-model="value" placeholder="请输入" allow-clear />
</template>

<script setup lang="ts">
import { ref } from 'vue';

const value = ref('');
</script>

前缀与后缀 ​

https://.com
vue
<template>
  <x-input v-model="value" placeholder="请输入">
    <template #prefix>https://</template>
    <template #suffix>.com</template>
  </x-input>
</template>

<script setup lang="ts">
import { ref } from 'vue';

const value = ref('');
</script>

尺寸 ​

vue
<template>
  <x-input v-model="value" size="mini" />
  <x-input v-model="value" size="small" />
  <x-input v-model="value" size="medium" />
  <x-input v-model="value" size="large" />
</template>

<script setup lang="ts">
import { ref } from 'vue';
const value = ref('当前内容');
</script>

禁用、只读和错误 ​

vue
<template>
  <x-input v-model="value" disabled allow-clear aria-label="禁用输入" />
  <x-input v-model="value" readonly allow-clear aria-label="只读输入" />
  <x-input v-model="value" error allow-clear aria-label="错误输入" />
</template>

<script setup lang="ts">
import { ref } from 'vue';
const value = ref('当前内容');
</script>

字数限制 ​

0/1215/12
vue
<template>
  <x-input v-model="value" :max-length="12" show-word-limit allow-clear />
  <x-input v-model="overflow" :max-length="{ length: 12, errorOnly: true }" show-word-limit allow-clear />
</template>

<script setup lang="ts">
import { ref } from 'vue';
const value = ref('');
const overflow = ref('超长内容只提示错误,不会被截断');
</script>

errorOnly 允许输入超过限制,并展示错误状态。Input 默认按 JavaScript 字符串长度计数;需要自定义计数时,同时提供 wordLength 和 wordSlice。

搜索、加载和禁用 ​

点击搜索按钮查看回调

vue
<template>
  <x-checkbox v-model="disabled">禁用搜索</x-checkbox>
  <x-checkbox v-model="loading">加载中</x-checkbox>
  <x-input-search v-model="value" :disabled="disabled" :loading="loading" allow-clear @search="handleSearch" />
  <x-input-search v-model="value" :disabled="disabled" :loading="loading" search-button button-text="搜索" @search="handleSearch" />
  <p role="status">{{ result }}</p>
</template>

<script setup lang="ts">
import { ref } from 'vue';
const value = ref('');
const disabled = ref(false);
const loading = ref(false);
const result = ref('等待搜索');
const handleSearch = (value: string) => { result.value = `搜索:${value || '空内容'}`; };
</script>

search 在点击搜索按钮时触发。需要回车搜索时,可监听继承自 Input 的 press-enter,用当前 v-model 值执行搜索。禁用状态同时约束输入和搜索按钮,并可继承 Form 的禁用状态与尺寸。

输入和提交事件 ​

等待输入

vue
<template>
  <x-input v-model="value" allow-clear
    @input="(next) => result = `input:${next}`"
    @change="(next) => result = `change:${next || '空内容'}`"
    @press-enter="result = `press-enter:${value}`"
    @clear="result = 'clear:已清空'"
  />
  <p role="status">{{ result }}</p>
</template>

<script setup lang="ts">
import { ref } from 'vue';
const value = ref('');
const result = ref('等待输入');
</script>

input 表示键入,change 在值有变化时由失焦、回车或清空触发。清空会通知 Form 的 input 校验通道,公开事件仍通过 clear 表示按钮操作。

前置与后置标签 ​

https://.com
vue
<template>
  <x-input v-model="value" placeholder="请输入域名">
    <template #prepend>https://</template>
    <template #append>.com</template>
  </x-input>
</template>

<script setup lang="ts">
import { ref } from 'vue';

const value = ref('');
</script>

在 Form 中校验 ​

x-input 接入 x-form 后会自动继承尺寸与禁用状态,并按 validateTrigger 触发校验。默认 change 表示文本输入在失焦时校验;需要每次输入即校验时设置 validateTrigger="input"。

0/6

vue
<template>
  <x-form ref="formRef" :model="form" :rules="formRules">
    <x-form-item field="email" label="邮箱">
      <x-input v-model="form.email" placeholder="请输入邮箱" allow-clear />
    </x-form-item>
    <!-- validate-trigger 是 FormItem 的 prop:每次输入即触发校验 -->
    <x-form-item field="code" label="验证码" validate-trigger="input">
      <x-input v-model="form.code" placeholder="请输入验证码" />
    </x-form-item>
    <x-form-item>
      <x-button type="primary" @click="onSubmit">提交</x-button>
    </x-form-item>
  </x-form>
  <p role="status">{{ formResult }}</p>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import type { FormInstance, FieldRule } from 'x-next';

const form = reactive({ email: '', code: '' });
const formRules: Record<string, FieldRule[]> = {
  email: [
    { required: true, message: '请输入邮箱' },
    { type: 'email', message: '邮箱格式不正确' },
  ],
  code: [{ required: true, message: '请输入验证码' }],
};
const formRef = ref<FormInstance>();
const formResult = ref('');

const onSubmit = async () => {
  const errors = await formRef.value!.validate();
  formResult.value = errors ? '校验失败,请检查表单' : '校验通过,已提交';
};
</script>

关联组件 ​

vue
<template>
  <x-input-search placeholder="搜索" />
  <x-input-password placeholder="密码" />
  <x-input-group>
    <x-input placeholder="省" />
    <x-input placeholder="市" />
  </x-input-group>
</template>

密码可见性 ​

当前:密码已隐藏
vue
<template>
  <x-input-password
    v-model="password"
    v-model:visibility="visible"
    placeholder="请输入密码"
  />
  <span>{{ visible ? '明文可见' : '密码已隐藏' }}</span>
</template>

<script setup lang="ts">
import { ref } from 'vue';

const password = ref('x-next-password');
const visible = ref(false);
</script>

visibility=true 表示显示明文,false 表示使用密码遮蔽。显隐按钮会触发 visibility-change 和 update:visibility。

非受控密码默认 defaultVisibility=true。需要默认遮蔽时设置 :default-visibility="false";禁用时显隐按钮也不可操作。

id、name、aria-label、aria-labelledby、aria-describedby、inputmode 等原生属性会传给内部 input;class / style 保留在组件根节点。也可以通过 inputAttrs 集中传入原生属性。

按需导入 ​

ts
import { Input, InputSearch, InputPassword, InputGroup } from 'x-next';

样式按需引入(base.css 为共享基础层,多个组件只需引入一次):

ts
import 'x-next/style/base.css';
import 'x-next/style/form-input.css';

Input Props ​

参数说明类型默认值
modelValue绑定值string-
defaultValue默认值,非受控模式string''
size输入框尺寸'mini' | 'small' | 'medium' | 'large'跟随全局配置
allowClear是否允许清空booleanfalse
disabled是否禁用booleanfalse
readonly是否只读booleanfalse
error是否为错误状态booleanfalse
placeholder占位提示string-
maxLength最大长度,支持仅提示错误number | { length: number; errorOnly?: boolean }0
showWordLimit是否显示字数统计booleanfalse
wordLength字符长度计算方法(value: string) => number-
wordSlice超长时的截取方法(value: string, maxLength: number) => string-
inputAttrs内部 input 元素属性object-

Input Events ​

事件名说明回调参数
update:modelValue值更新时触发value
input用户输入时触发value, event
change失焦或按下回车且值变化时触发value, event
press-enter按下回车时触发event
clear点击清除按钮时触发event
focus获取焦点时触发event
blur失去焦点时触发event

InputPassword Props ​

除 Input 的属性外,InputPassword 还支持:

参数说明类型默认值
visibility密码是否以明文显示,受控属性boolean-
defaultVisibility非受控模式下默认是否显示明文booleantrue
invisibleButton是否显示密码显隐按钮booleantrue
disabled禁用输入和显隐按钮,继承 Formbooleanfalse

InputSearch Props ​

除 Input 的属性外,InputSearch 还支持:

参数说明类型默认值
searchButton使用后置搜索按钮booleanfalse
loading搜索加载状态booleanfalse
disabled禁用输入和搜索按钮booleanfalse
size尺寸,继承 Form / 全局配置'mini' | 'small' | 'medium' | 'large'跟随全局配置
buttonText搜索按钮文字string-
buttonProps搜索按钮属性ButtonProps-

InputSearch Events ​

事件名说明回调参数
search点击可用搜索按钮时触发value: string, event: MouseEvent

InputSearch Slots ​

支持 Input 的四个插槽,以及按钮模式下的 button-default(按钮内容)、button-icon(按钮图标)。

InputPassword Events ​

事件名说明回调参数
visibility-change密码可见性变化时触发visible: boolean
update:visibility用于 v-model:visibility 同步visible: boolean

Input Slots ​

插槽名说明
prefix前缀内容
suffix后缀内容
prepend前置标签
append后置标签

Input Methods ​

方法说明
focus使输入框获取焦点
blur使输入框失去焦点