Skip to content

Input 输入框

查看 sn-input 的 2.0 版本差异

基础用法

  • 基于 input 组件封装的输入框,通过 v-model 双向绑定输入内容。
  • 内置聚焦激活态(边框/背景自动切换)、禁用与只读配色、加载状态、清除按钮、密码明文/密文切换。
  • 通过 prefixIcon / suffixIcon 设置前后置图标,点击时触发 prefix-click / suffix-click 事件;也可用插槽自定义前后缀内容。
  • 尺寸与颜色属性支持 $ 简写(如 border-radius="$16"bg-color="$info"),随风格乘数与主题自动缩放换色。
vue
<template>
	<sn-input v-model="value" placeholder="请输入内容"></sn-input>
</template>

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

更多演示请下载 demo 查看

密码输入

设置 password 后输入内容以密文显示,右侧自动出现明文/密文切换按钮,点击即可切换显示。

vue
<template>
	<sn-input v-model="pwd" password placeholder="请输入密码"></sn-input>
</template>

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

自定义样式

通过 customStyle 定制根容器,inputStyle 内联作用于输入框本体,前后图标可用 prefixIconClass / suffixIconClass(外部类)或 prefixIconStyle / suffixIconStyle(内联样式)定制。

vue
<template>
	<sn-input v-model="value" input-style="font-size: 18px; font-weight: bold;"
		prefix-icon="search-line" prefix-icon-style="color: #f56c6c;"
		placeholder="大字加粗 + 红色图标"></sn-input>
</template>

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

属性

参数说明类型默认值可选值
v-model绑定值String''-
name表单的控件名称,作为键值对的一部分与表单一同提交String''-
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聚焦时是否切换为激活边框颜色Booleantruetrue | false
borderColor边框颜色String'$line'-
activeBorderColor聚焦时的边框颜色String'$line'-
bgColor背景颜色String''(默认主题 info 色)-
activeBgColor聚焦时的背景颜色String''(默认主题 info 色)-
disabledBgColor禁用时的背景颜色String''(默认主题 disabled 色)-
textColor文本颜色String''(默认主题 text 色)-
disabledTextColor禁用时的文本颜色String''(默认主题 disabledText 色)-
iconColor前后置图标颜色String''(默认主题 text 色)-
borderRadius圆角大小String | Number''(默认 8px × 圆角乘数)-
borderWidth边框宽度String | Number1px-
padding内边距String8px 13px-
textSize文本字体大小String | Number''(默认 14px × 字体乘数)-
textFont文本字体String''-
align文本对齐方式Stringleftleft | center | right
prefixIcon前置图标名称,存在 prefix 插槽时不显示String''-
suffixIcon后置图标名称,存在 suffix 插槽时不显示String''-
iconSize图标大小String | Number''(默认 16px × 字体乘数)-
inputStyle自定义输入框本体样式UTSJSONObject | String''-
inputClass输入框本体的外部类String''-
prefixIconStyle自定义前置图标样式UTSJSONObject | String''-
prefixIconClass前置图标的外部类String''-
suffixIconStyle自定义后置图标样式UTSJSONObject | String''-
suffixIconClass后置图标的外部类String''-
customStyle自定义根节点样式UTSJSONObject | String''-
customClass根节点外部类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点击后置图标时触发

插槽

名称说明
prefix自定义前置内容,使用后前置图标不显示
suffix自定义后置内容,使用后后置图标不显示

使用 MIT 协议