组件 / 表单 / cu-code-input 验证码输入

cu-code-input

验证码输入用于分格展示已输入的验证码,支持方框与横线两种格子样式。

01 / Install

引入组件

整目录放进工程即用:easycom 按标签逐个解析,页面写哪个组件就只编译哪个,不写 import,也没有全局注册这一步;单个组件也可以显式引入(第 4 步,可选);主题层在 App.uvue 引入一次即可。

install
# 1  整目录放进工程(easycom 自动解析)
your-project/
└── uni_modules/
    └── colorui-x/

# 2  App.uvue 全局引入主题层(一次)
@import 'uni_modules/colorui-x/theme/base.css';
@import 'uni_modules/colorui-x/theme/layout.css';
@import 'uni_modules/colorui-x/theme/color.scss';  // 颜色工具类(.bg-* / .text-*)

# 3  页面里直接用,不写 import(只编译用到的组件)
<cu-code-input />

# 4  单个组件也可以显式引入(可选,不用 easycom 时)
import CuCodeInput from '@/uni_modules/colorui-x/components/cu-code-input/cu-code-input.uvue'

组件样式自包含(只依赖包内 token.scss),主题层是给页面工具类用的,不参与组件注册。两种安装方式见 快速上手。

02 / Platform

平台支持

AndroidiOSHarmonyOSWeb(H5)微信小程序支付宝小程序
√ — — √ — —

Android 列取自本仓真机逐屏判定(√ 已验 / — 待复核 / × 已登记缺陷,本件当前为「真机已验」)。

Web 列只表示已编出可运行的预览产物,非真机渲染。

其余三端没有任何实测证据,不作支持承诺。

03 / Demo

代码演示

01

默认

uvue
<cu-code-input :value="v1" @input="onV1" @change="onChange" @finish="onFinish" />
02

样式

uvue
<cu-code-input mode="box" :value="v2a" @input="onV2a" />
<cu-code-input mode="line" :value="v2b" @input="onV2b" />
03

位数

uvue
<cu-code-input :maxlength="1" />
<cu-code-input :maxlength="4" :value="v3" @input="onV3" />
<cu-code-input :maxlength="8" />
04

圆点

uvue
<cu-code-input :dot="true" value="123" />
<cu-code-input :dot="true" :maxlength="4" />
05

发丝线

uvue
<cu-code-input :hairline="false" :maxlength="4" />
<cu-code-input :hairline="true" :maxlength="4" />
<cu-code-input mode="line" :hairline="true" :maxlength="4" />
06

排版

uvue
<cu-code-input :size="20" :maxlength="4" />
07

颜色

uvue
<cu-code-input value="123" color="#e54d42" />
<cu-code-input value="123" borderColor="#0081ff" />
<cu-code-input mode="line" value="123" borderColor="#0081ff" />
<cu-code-input :dot="true" value="123" color="#e54d42" />
08

输入限制

uvue
<cu-code-input :disabledDot="true" :maxlength="6" />
09

聚焦

uvue
<cu-code-input :focus="autoFocus" :maxlength="4" />
<cu-code-input :adjustPosition="true" :maxlength="6" value="12" />
10

外部改值

uvue
<cu-code-input :maxlength="4" :value="v10" @input="onV10" @finish="onFinish10" />
11

边界

uvue
<cu-code-input value="" />
<cu-code-input :maxlength="0" />
12

自定义样式

uvue
<cu-code-input :customStyle="styleDemo" :maxlength="4" />
<cu-code-input customClass="custom-class" :maxlength="4" />
13

未消费项

这一节的交互由演示页脚本驱动,效果见右侧实时预览。

14

取值与事件

这一节的交互由演示页脚本驱动,效果见右侧实时预览。

04 / Props · Events · Slots

全量 API

props

参数说明类型默认值
输入与取值
value已输入的验证码文本,超出 maxlength 的部分截断;回写该值可做受控String''
maxlength格子数量,同时是最大输入位数Number6
focustrue ⇒ 自动聚焦并拉起系统键盘Booleanfalse
disabledKeyboardtrue ⇒ 禁用原生输入域,配合自绘数字键盘使用Booleanfalse
disabledDottrue ⇒ 输入小数点时自动剔除,且该次不抛事件Booleantrue
格子外观
modebox 方框 / line 底部横线,两种格子形态String'box'
dottrue ⇒ 已输入的格子显示圆点,不露明文Booleanfalse
size单格边长,px 契约值;运行时 ×2 换算成 rpx,宽高同值Number35
space格子间距,px 契约值;运行时 ×2 换算成 rpx,为 0 时方框合并成一条Number10
hairlinetrue ⇒ 底部横线改细档(4rpx 对默认 8rpx)Booleanfalse
文字与描边样式
color单格文字与圆点的颜色;空串走 token 默认String''
fontSize单格文字字号,px 契约值;运行时 ×2 换算成 rpxNumber18
boldtrue ⇒ 单格文字加粗Booleanfalse
borderColor方框描边与底部横线的颜色;空串走 token 默认String''
交互与样式
customStyle根节点内联样式Object(): UTSJSONObject => ({})
customClass追加到根节点的类名String''
声明保留但不被消费
adjustPosition原生输入域获焦时是否顶起页面;本仓不读取,传参不生效Booleantrue
url不产生跳转,请在 finish 里自行跳转String''
linkType不产生跳转,同上String'navigateTo'

events

事件说明类型默认值平台差异说明
@change输入变化时触发,回传当前完整文本;把该值回写 value 不会再次触发Handler-各端一致
@input与 change 同一次变化内紧接抛出,回传同一份文本Handler-各端一致
@finish输入长度达到 maxlength 时触发,回传当前文本Handler-各端一致

slots

slot说明
(默认)自定义内容;不填时按 maxlength 渲染对应数量的格子
05 / Notes

注意事项

自绘键盘 :开 disabledKeyboard 后点格子与 focus 都拉不起键盘,只能由自绘键盘回写 value;而回写只更新格子,不会补发 change 或 finish,完成判断要自己做。

圆点非加密 :dot 只把已输入的格子画成圆点,value 与三个事件里仍是明文,做密码或 PIN 场景时不要把它当脱敏。