ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

cube-ui Input 输入框组件完整指南:v-model 双向绑定、清空按钮与密码眼睛实战解析

cube-ui Input 输入框组件完整指南:v-model 双向绑定、清空按钮与密码眼睛实战解析 前端UI组件移动开发【免费下载链接】cube-ui:large_orange_diamond: A fantastic mobile ui lib implement by Vue项目地址https://gitcode.com/gh_mirrors/cu/cube-ui点击查看免费下载导读本文基于 cube-ui 官方中文文档 input.md 并结合仓库源码input.vue、input.spec.js、示例页系统讲解cube-input输入框组件的完整用法从最基础的v-model双向绑定、通过 watch 手工控制输入长度到clearable清空按钮在 1.11.0 前后的布尔/对象双形态配置再到eye密码眼睛的可见性控制逻辑。读完本文你将掌握该组件全部 Props、插槽、事件与实例方法并能直接照抄可运行的代码块落地到自己的 Vue 项目中。适用版本说明cube-input自1.5.0起引入clearable对象形态自1.11.0起可用focus/blur实例方法自1.12.10起提供。文中所有行为均以当前仓库源码为准。一、组件定位与引入方式cube-input是 cube-ui 提供的单行输入框组件核心能力有两个使用v-model对输入内容进行双向绑定支持一键清空输入内容清空按钮。在源码层面该组件位于 src/components/input/input.vue其安装注册逻辑在 src/modules/input/index.jsimport Input from ../../components/input/input.vue Input.install function (Vue) { Vue.component(Input.name, Input) } export default Input因此可通过Vue.use(Input)全量注册也可按需引入单个组件。组件对外暴露的 TypeScript 类型声明见 types/components/Input.d.ts其中明确约束了value?: string | number、type的可选字面量text | number | password | date以及clearable/eye的布尔或对象形态。二、基础用法v-model 双向绑定最小示例cube-input v-modelvalue/cube-inputexport default { data() { return { value: } } }组件内部通过inputValue中间态与v-model打通双向绑定。从源码input.vue可以看到两条 watch 链路value变化 → 同步给inputValue外部赋值驱动 UIinputValue变化 →$emit(input, newValue)用户输入驱动外部数据。同时源码以v-bind$props将全部 Props 透传给原生input元素input.vue这也是下文支持原生 Input 大部分配置的实现基础。控制最大长度watch 方案cube-input本身提供maxlength属性但若需精确限制输入值长度官方文档推荐通过 watch 组件value值手工实现cube-input v-modelvalue/cube-inputexport default { data() { return { value: } }, watch: { value(newV) { if (newV.length 10) { newV newV.slice(0, 10) this.$nextTick(() { this.value newV }) } } } }上述代码即实现输入内容长度不得超过 10 位。关键点在于先截断字符串再用$nextTick回写value——直接赋值会导致 watch 递归触发借助 nextTick 可避免同一轮更新内的死循环。在官方示例页 example/pages/input.vue 中maxlength10开关的updateMaxLength也复用了这一套 watch 截断逻辑example/pages/input.vue属于组件文档推荐的官方实践。三、一键清空clearable 清空按钮布尔形态1.11.0 之前在1.11.0 之前clearable为布尔类型仅表示是否展示清除按钮cube-input v-modelvalue :clearabletrue/cube-input对象形态1.11.0 之后1.11.0 之后clearable除布尔值外还可配置为对象包含两个 key| key | 含义 | | - | - | |visible| 是否展示清空按钮 | |blurHidden| Input 元素离焦失焦时是否隐藏清空按钮 |cube-input v-modelvalue :clearableclearable /cube-inputexport default { data() { return { value: , clearable: { visible: true, blurHidden: true } } } }源码级显示逻辑清空按钮的显示由_showClear计算属性决定input.vue_showClear() { let visible this.formatedClearable.visible this.inputValue !this.readonly !this.disabled if (this.formatedClearable.blurHidden !this.isFocus) { visible false } return visible }可见清空按钮的展示需同时满足visible为真、有输入内容inputValue非空、非只读、非禁用若blurHidden为真则仅在聚焦状态下显示。formatClearable方法负责把布尔值或对象统一规整为内部formatedClearableinput.vue。点击清空按钮时触发handleClear将inputValue置空并自动让输入框重新获得焦点方便连续输入下一段内容input.vue。测试用例印证test/unit/specs/input.spec.js 中有一组针对性用例无文本时不渲染.cube-input-clearshould not show clear button when no text有文本时渲染清空按钮点击清空按钮后 input 的 value 变为空串value should be empty when clear button clicked对象形态下blurHidden由true改为false后失焦状态清空按钮从无到有should show clearable icon and work correctly。这组测试完整验证了visible与blurHidden两个 key 的实际作用。四、密码输入与眼睛eye 配置基本配置密码眼睛通过eye属性开启同时依赖typepassword。官方文档给出的是组合配置示例cube-input v-modelvalue :placeholderplaceholder :typetype :maxlengthmaxlength :readonlyreadonly :disableddisabled :autofocusautofocus :autocompleteautocomplete :eyeeye /cube-inputexport default { data() { return { value: , placeholder: 请输入内容, type: password, readonly: true, maxlength: 100, disabled: true, autofocus: true, autocomplete: true, eye: { open: true, reverse: false } } } }eye 的取值语义eye支持布尔与对象两种形态默认false即密码眼睛不可见| 取值 | 语义 | | - | - | |false默认 | 不显示密码眼睛 | |true| 等价于{ open: true, reverse: false }| |{ open: true/false, reverse: true/false }| 使用密码眼睛open控制眼睛张开/闭合reverse控制密码可见状态与眼睛开闭是否相反 |源码级可见性与类型联动从 input.vue 看核心实现_type() { const type this.type if (type password this.eye this.pwdVisible) { return text } return type }, pwdVisible() { const eye this.formatedEye return eye.reverse ? !eye.open : eye.open }, _showPwdEye() { return this.type password this.eye !this.disabled }要点拆解类型联动typepassword且eye开启时若pwdVisible为真则实际渲染类型变为text明文可见否则保持passwordreverse 反转reverse: true时密码可见 ⇔ 眼睛闭合眼睛睁开表示密码被隐藏默认为密码可见 ⇔ 眼睛睁开禁用联动_showPwdEye中!this.disabled即组件处于禁用状态时不显示眼睛图标点击切换点击眼睛图标触发handlePwdEye翻转formatedEye.openinput.vue并配合eyeClass切换cubeic-eye-visible/cubeic-eye-invisible图标。test/unit/specs/input.spec.js 的should show password eye and work correctly用例完整覆盖了该链路eye: true时 input 的 type 变为text→ 改为{open: false}后恢复password→ 点击.cube-input-eye后再回到password。五、Props 配置总览官方文档 Props 表如下结合源码 input.vue 与类型声明 types/components/Input.d.ts 整理| 参数 | 说明 | 类型 | 可选值 | 默认值 | | - | - | - | - | - | | type | input 类型 | String | text / number / password / date | text | | v-modelvalue | 绑定的值 | String / Number | - | 空 | | disabled | 禁用状态 | Boolean | true / false | false | | readonly | 只读状态 | Boolean | true / false | false | | maxlength | 最大输入长度 | Number | - | 60 | | placeholder | 占位文本 | String | - | 空 | | autofocus | 自动对焦 | Boolean | true / false | false | | autocomplete | 自动补全 | Boolean | true / false | false | | clearable | 是否使用清空按钮1.11.0 后可配置为对象{visible: true, blurHidden: true}visible控制是否显示blurHidden控制离焦后是否隐藏 | Boolean / Object1.11.0| true / false / {visible: true, blurHidden: true} | false | | eye | 默认 false 表示密码眼睛不可见为 true 时等价于{ open: true, reverse: false }对象形式表示使用密码眼睛open控制眼睛张开/闭合reverse控制密码可见与眼睛开闭是否相反 | Boolean / Object | true / false / {open: true/false, reverse: true/false} | false |更多原生 Props除上表外源码还透传并声明了以下原生属性input.vuename/id/form表单相关minlength/maxlength长度约束min/max/step数字输入相关配合typenumberresize/pattern/tabindex。对应的透传验证在 input.spec.js 的should support more native props用例中其断言了min、max、autocomplete、disabled、readOnly等属性确实落到了原生 input 元素上。六、插槽prepend 与 appendcube-input提供两个具名插槽用于在输入框前后插入自定义内容| 插槽名 | 说明 | | - | - | | prepend | 前置内容 | | append | 后置内容 |模板实现input.vue中prepend渲染在输入框左侧.cube-input-prepend容器内append渲染在右侧.cube-input-append容器内且与清空按钮、密码眼睛同处一个 flex 容器——当$slots.append或_showClear或_showPwdEye任一为真时该容器才会被渲染。典型用法如前置搜索图标、后置单位文本等。七、事件组件对外暴露 4 个事件触发逻辑见 input.vue 与 mixins/input.js| 事件名 | 说明 | 参数 | | - | - | - | | focus | 输入框聚焦后触发若处于禁用状态则不触发 | e - 事件对象 | | blur | 输入框失焦后触发 | e - 事件对象 | | change | 绑定值改变且输入框失去焦点后触发 | e - 事件对象 | | input | 绑定值变化时触发 | 更新后的绑定值 |其中focus/blur/change分别由模板中的focushandleFocus、blurhandleBlur、changechangeHander转发change的转发逻辑定义在 src/common/mixins/input.jsinput事件则由inputValue的 watch 在每次输入变化时发出。测试用例should trigger eventsinput.spec.js通过sinon.spy断言了 focus、blur、change 三者各被调用一次。八、实例方法组件实例提供两个方法实现在 src/common/mixins/input.js| 方法名 | 说明 | 可用版本 | | - | - | - | | focus | 获得焦点 | 1.12.10 | | blur | 离焦 | 1.12.10 |调用方式// 通过 ref 拿到组件实例 this.$refs.input.focus() // 获得焦点 this.$refs.input.blur() // 离焦内部实现即调用原生 input 元素的focus()/blur()。类型声明 types/components/Input.d.ts 中亦包含focus: (e?: FocusEvent) void与blur: (e?: FocusEvent) void。九、完整可运行示例综合以上能力一个覆盖密码 眼睛 清空 前置/后置插槽的完整示例cube-input v-modelvalue typepassword placeholder请输入密码 :clearable{ visible: true, blurHidden: true } :eye{ open: false, reverse: false } :maxlength20 template slotprepend/template template slotappend确认/template /cube-input官方示例页 example/pages/input.vue 提供了更全面的交互演示通过switch-option开关可实时切换disabled、readonly、maxlength、clearable、blurHidden、password、show eye、reverse、password visible等状态对应路由为example/router/routes.js中的/inputroutes.js。读者可运行 example 工程npm run dev在浏览器中逐一体验各配置项的实际效果。十、总结cube-input的核心要点可浓缩为四句话双向绑定v-model 内部inputValuewatch 双链路外部赋值与用户输入始终同步长度控制优先用 watch 截断 $nextTick回写避免 watch 死循环清空按钮clearable支持布尔与对象两种形态blurHidden决定离焦是否隐藏点击后自动重新聚焦密码眼睛eye与typepassword配合reverse可反转密码可见 ⇔ 眼睛开闭的对应关系。所有行为均有源码input.vue与单元测试input.spec.js双重佐证可按需直接落地到移动端 Vue 表单场景。赞分享前端UI组件移动开发【免费下载链接】cube-ui:large_orange_diamond: A fantastic mobile ui lib implement by Vue项目地址https://gitcode.com/gh_mirrors/cu/cube-ui点击查看免费下载相关推荐cube-ui Input 输入框组件完全指南从 v-model 双向绑定到密码眼睛的移动端实践cube ui Input 输入框组件完全指南从 v model 双向绑定到密码眼睛的移动端实践 cube ui 是滴滴开源的移动端 Vue 组件库Vue前端UI组件移动开发深度解析QP/C嵌入式系统的实时事件框架架构揭秘深度解析QP/C嵌入式系统的实时事件框架架构揭秘 在资源受限的嵌入式系统中实现可靠的并发处理和确定性实时性能是每个嵌入式开发者面临的挑战。QP/C实时事件框前端UI组件移动开发cube-ui Switch 滑动开关组件完全指南v-model 双向绑定、禁用状态与源码实现剖析cube ui Switch 滑动开关组件完全指南v model 双向绑定、禁用状态与源码实现剖析 滑动开关Switch是移动端表单与设置界面中最常见的开前端UI组件移动开发上一篇URL Pages 开源项目教程下一篇Orbit Model 开源项目教程创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表