1.0.1 • Published 1 month ago

@tanzhenxing/zx-password-input v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
1 month ago

zx-password-input 密码输入框

带网格的输入框组件,可以用于输入密码、短信验证码等场景,通常与数字键盘组件 (zx-number-keyboard) 配合使用。

特性

  • 支持 H5、小程序、App 端。
  • 高度可定制,支持自定义长度、格子间距、明暗文切换等。
  • 样式基于 Vant PasswordInput,并适配 uniapp 项目。

引入

<template>
  <zx-password-input v-model="password" />
</template>

<script setup>
import { ref } from 'vue';
// import zxPasswordInput from '@/components/zx-password-input/zx-password-input.vue'; // 根据实际路径引入

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

代码演示

基础用法

搭配数字键盘组件来实现密码输入功能。

<template>
  <zx-password-input v-model="value" :focused="showKeyboard" @focus="showKeyboard = true" />
  <zx-number-keyboard v-model="value" :show="showKeyboard" @blur="showKeyboard = false" />
</template>

<script setup>
import { ref } from 'vue';
const value = ref('');
const showKeyboard = ref(false);
</script>

自定义长度

通过 length 属性来设置密码长度。

<zx-password-input v-model="value" :length="4" />

格子间距

通过 gutter 属性来设置格子之间的间距 (单位 rpx)。

<zx-password-input v-model="value" :gutter="10" />

明文展示

mask 设置为 false 可以明文展示输入的内容,适用于短信验证码等场景。

<zx-password-input v-model="value" :mask="false" />

提示信息

通过 info 属性设置提示信息,通过 error-info 属性设置错误提示。

<template>
  <zx-password-input
    v-model="value"
    info="密码为 6 位数字"
    :error-info="errorInfo"
    :focused="showKeyboard"
    @focus="showKeyboard = true"
  />
  <zx-number-keyboard v-model="value" :show="showKeyboard" @blur="showKeyboard = false" />
</template>

<script setup>
import { ref, watch } from 'vue';

const value = ref('');
const errorInfo = ref('');
const showKeyboard = ref(false);

watch(value, (newVal) => {
  if (newVal.length === 6 && newVal !== '123456') {
    errorInfo.value = '密码错误';
  } else {
    errorInfo.value = '';
  }
});
</script>

API

Props

参数说明类型默认值
value密码值 (支持 v-model)String''
info输入框下方文字提示String''
error-info输入框下方错误提示String''
length密码最大长度Number | String6
gutter输入框格子之间的间距 (单位 rpx)Number | String0
mask是否隐藏密码内容Booleantrue
focused是否已聚焦,聚焦时会显示光标Booleanfalse
item-width每个格子的宽度 (单位 rpx 或其他合法 CSS 单位)Number | String'auto'
item-height每个格子的高度 (单位 rpx 或其他合法 CSS 单位)Number | String50
custom-style自定义根节点样式String | Object''

Events

事件名说明回调参数
focus输入框聚焦时触发event
update:valuev-model 更新事件value

样式变量

组件提供了一些 CSS 变量,可用于自定义样式。你可以在项目的全局样式文件或者页面的 <style> 标签中覆盖这些变量。

名称默认值 (rpx)描述
--zx-password-input-height100rpx输入框总高度
--zx-password-input-margin0 30rpx输入框外边距
--zx-password-input-font-size40rpx格子内文字/点的大小
--zx-password-input-radius12rpx输入框/格子圆角
--zx-password-input-background#f7f8fa格子背景色
--zx-password-input-info-color#969799提示文字颜色
--zx-password-input-info-font-size28rpx提示文字大小
--zx-password-input-error-info-color#ee0a24错误提示文字颜色
--zx-password-input-dot-size20rpx密码掩码点的大小
--zx-password-input-dot-color#323233密码掩码点的颜色
--zx-password-input-text-color#323233明文展示时文字颜色
--zx-password-input-cursor-color#323233光标颜色
--zx-password-input-cursor-width2rpx光标宽度
--zx-password-input-cursor-height40%光标高度
--zx-password-input-cursor-duration1s光标闪烁动画周期
--zx-padding-md20rpx用于提示信息的上边距

注意:

  • item-width 默认为 auto,当 gutter0 时,格子会自动均分宽度。如果设置了 gutter,建议同时设置一个明确的 item-width 以获得最佳布局。
  • zx-number-keyboard 是一个假设存在的数字键盘组件,你需要确保项目中已经有此组件或替换为其他可用的键盘组件。
1.0.1

1 month ago