Skip to content

InputField 标签输入框

基础用法

  • 自带标签与输入区的字段组件,相当于"标签 + 输入框 + 错误提示"的一体化封装,移动端表单输入标配。
  • 支持 v-model 双向绑定、密码明文/密文切换(password)、一键清空(clearable)、加载状态(loading)、禁用与只读配色。
  • 放在 sn-form 内设置 field + rule 即参与整表校验,无需再包一层 sn-form-field
  • 尺寸与颜色属性支持 $ 简写(如 label-size="$18"label-color="$primaryDark"),随风格乘数与主题自动缩放换色。
vue
<template>
	<sn-input-field v-model="value" label="用户账号" required placeholder="请输入账号" clearable />
	<sn-input-field v-model="pwd" label="密码" password placeholder="请输入密码" clearable />
</template>

<script lang="uts" setup>
	const value = ref<string>('')
	const pwd = ref<string>('')
</script>

更多演示请下载 demo 查看

布局与标签

direction 切换水平/垂直布局:horizontal 标签在左(默认),vertical 标签在上。label-align 控制标签文字水平对齐,label-justify 控制标签在容器内竖向对齐(top / center / bottom),label-widthcolumn-gap 控制标签宽度与间距。

vue
<template>
	<sn-input-field v-model="valueV" direction="vertical" label="商品名称" placeholder="请输入商品名称" clearable />
	<sn-input-field v-model="valueR" label="标签靠右" label-align="right" label-width="80px" placeholder="标签靠右" />
</template>

<script lang="uts" setup>
	const valueV = ref<string>('')
	const valueR = ref<string>('')
</script>

配合 sn-form 使用

设置 fieldrule 后直接参与 sn-form 整表校验,校验失败信息通过 error 属性展示。

vue
<template>
	<sn-form ref="formRef" v-model="formData">
		<sn-input-field field="username" :rule="userRule" v-model="(formData['username'] as string)" label="用户名" :error="getFieldError('username')" placeholder="4-16 位字母数字下划线" clearable />
		<sn-input-field field="phone" :rule="phoneRule" v-model="(formData['phone'] as string)" label="手机号" :error="getFieldError('phone')" type="number" :maxlength="11" clearable />
	</sn-form>
	<sn-button text="提交" type="primary" custom-style="margin-top:16px;" @click="onFormSubmit" />
</template>

<script lang="uts" setup>
	import type { SnFormItemRule, SnFormItemVerifyResult, SnFormValidResult } from '@/uni_modules/sinle-ui'

	const formRef = ref<SnInputFieldComponentPublicInstance | null>(null)
	const formData = ref<UTSJSONObject>({
		username: '',
		phone: ''
	} as UTSJSONObject)
	const failResults = ref<SnFormItemVerifyResult[]>([])

	function getFieldError(field: string): string {
		const list = failResults.value
		for (let i = 0; i < list.length; i++) {
			if (list[i].field == field) {
				return list[i].message ?? ''
			}
		}
		return ''
	}

	const userRule = ref<SnFormItemRule>({
		pattern: /^[a-zA-Z0-9_]{4,16}$/,
		message: '用户名需为 4-16 位字母、数字或下划线'
	} as SnFormItemRule)

	const phoneRule = ref<SnFormItemRule>({
		type: 'phone',
		required: true,
		message: '请填写正确的手机号'
	} as SnFormItemRule)

	function onFormSubmit(): void {
		formRef.value?.$callMethod('submit', {
			success: () => {
				failResults.value = []
			},
			fail: (results: SnFormItemVerifyResult[]) => {
				failResults.value = results
			}
		} as SnFormValidResult)
	}
</script>

属性

参数说明类型默认值可选值
v-model绑定值String''-
direction布局方向,horizontal 标签在左、vertical 标签在上Stringhorizontalhorizontal | vertical
label标签文字String''-
labelSize标签字体大小String | Number''(默认 14px × 字体乘数)-
labelColor标签颜色String''(默认主题 title 色)-
labelWidth横向布局时标签宽度String | Number''(默认 80px × 间距乘数)-
labelAlign标签文字水平对齐Stringleftleft | right | center
labelJustify标签在容器内的竖向对齐Stringtoptop | center | bottom
required是否必填,标签后追加红色 * 标记Booleanfalsetrue | false
error错误提示文字(存在 error 插槽时不显示)String''-
errorSize错误文字大小String | Number''(默认 12px × 字体乘数)-
errorColor错误文字颜色String''(默认主题 error 色)-
columnGap标签与内容的间距String | Number''(默认 12px × 间距乘数)-
name表单的控件名称,作为键值对的一部分与表单一同提交String''-
field表单字段名,非空且位于 sn-form 内时注册参与整表校验String''-
rule校验规则,配合 field 使用SnFormItemRule{}-
type输入框类型Stringtexttext | number | digit | tel | idcard | nickname
password是否密码类型,为 true 时右侧显示明文/密文切换按钮Booleanfalsetrue | false
placeholder输入框为空时的占位符String''-
placeholderStyle占位符内联样式String''-
placeholderClass占位符外部类String''-
maxlength最大输入长度,-1 表示不限制String | Number-1-
focus获取焦点Booleanfalsetrue | false
autoFocus自动获取焦点,与 focus 相比只在首次生效Booleanfalsetrue | false
cursor指定聚焦时的光标位置,-1 表示不指定String | Number-1-
cursorColor光标颜色String''(默认主题 primary 色)-
cursorSpacing光标与键盘的距离(px)String | Number0-
selectionStart光标起始位置,自动聚焦时有效,需与 selectionEnd 搭配String | Number-1-
selectionEnd光标结束位置,自动聚焦时有效,需与 selectionStart 搭配String | Number-1-
confirmType键盘右下角按钮的文字Stringdonesend | search | next | go | done
confirmHold点击键盘右下角按钮时是否保持键盘不收起Booleanfalsetrue | false
holdKeyboard聚焦时点击页面不收起键盘Booleanfalsetrue | false
adjustPosition键盘弹起时是否自动上推页面Booleantruetrue | false
controlled是否为受控属性(原生 input 的 controlled)Booleanfalsetrue | false
disabled是否禁用,禁用后不可输入且背景、文字使用禁用配色Booleanfalsetrue | false
readonly是否只读,只读时颜色不变Booleanfalsetrue | false
loading是否加载中,输入区右侧显示加载指示器(延迟 100ms 显示避免闪烁)Booleanfalsetrue | false
loadingClass加载指示器外部类String''-
loadingStyle加载指示器样式UTSJSONObject | String''-
clearable是否显示清除按钮(有内容且非禁用/只读时显示,点击清空)Booleanfalsetrue | false
showBorder是否显示边框Booleantruetrue | false
showActiveBorder聚焦时是否切换为激活边框颜色Booleanfalsetrue | false
borderColor边框颜色String''(默认透明)-
activeBorderColor聚焦激活时的边框颜色String''(默认主题 primary 色)-
bgColor背景颜色String''(默认主题 front 色)-
activeBgColor聚焦时的背景颜色String''(默认主题 front 色)-
disabledBgColor禁用时的背景颜色String''(默认主题 disabled 色)-
textColor输入文字颜色String''(默认主题 text 色)-
disabledTextColor禁用时的文字颜色String''(默认主题 disabledText 色)-
iconColor前后置图标颜色String''(默认主题 text 色)-
borderRadius圆角大小String | Number0px-
borderWidth边框宽度String | Number2-
padding内边距String10px 13px-
textSize输入文字大小String | Number''(默认 14px × 字体乘数)-
textFont输入文字字体String''-
align输入文字对齐方式Stringleftleft | center | right
prefixIcon前置图标名称,存在 prefix 插槽时不显示String''-
suffixIcon后置图标名称,存在 suffix 插槽时不显示String''-
iconSize前后置图标大小String | Number''(默认 16px × 字体乘数)-
labelStyle标签节点自定义样式UTSJSONObject | String''-
labelClass标签节点外部类String''-
errorStyle错误节点自定义样式UTSJSONObject | String''-
errorClass错误节点外部类String''-
inputStyle自定义输入框本体样式UTSJSONObject | String''-
customStyle自定义根节点样式UTSJSONObject | String''-
customClass根节点外部类String''-
prefixIconStyle自定义前置图标样式UTSJSONObject | String''-
prefixIconClass前置图标的外部类String''-
suffixIconStyle自定义后置图标样式UTSJSONObject | String''-
suffixIconClass后置图标的外部类String''-

sn-form-itemrule 属性校验规则。

名称类型必填描述
typeString字段类型(如 string / number / array / url / email / phone 等)
requiredBoolean是否必填
messageString校验失败提示信息
patternRegExp正则校验规则
minNumber最小长度
maxNumber最大长度
lenNumber固定长度
enumany[]枚举校验,值须在列表内
transform(value: any) => any校验前对值进行转换
valid(value: any) => string自定义校验函数,返回非空字符串表示失败信息

事件

名称类型说明
input(event: UniInputEvent) => Void键盘输入时触发,event.detail = { value, cursor }
focus(event: UniInputFocusEvent) => Void输入框聚焦时触发
blur(event: UniInputBlurEvent) => Void输入框失去焦点时触发
confirm(event: UniInputConfirmEvent) => Void点击键盘右下角按钮时触发
keyboardheightchange(event: UniInputKeyboardHeightChangeEvent) => Void键盘高度变化时触发
prefix-click() => Void点击前置图标时触发
suffix-click() => Void点击后置图标时触发

方法

名称参数返回值描述
verify(value: any, callback: (res: SnFormItemVerifyResult) => Void)-按 rule 校验当前表单项并回调结果;一般由 sn-form 自动调用,无需手动调用

插槽

名称说明
label自定义标签内容,替换 label 属性文字
prefix自定义前置内容,使用后前置图标不显示
suffix自定义后置内容,使用后后置图标不显示
error自定义错误提示内容,替换 error 属性文字

使用 MIT 协议