Skip to content

Keyboard 键盘

自定义键盘组件,支持数字键盘、身份证键盘、完整键盘等多种类型,常配合验证码输入框、密码输入框等使用。

  • 支持数字、身份证、完整键盘等多种类型
  • 支持安全键盘(随机数字排列)
  • 支持自定义工具栏
  • 支持遮罩层点击关闭
  • 支持底部安全区域

注意

该组件在不同平台上的表现可能存在细微差异,请以实际效果为准。

平台差异说明

App(vue)App(nvue)H5小程序

基本使用

  • 通过 v-model 控制键盘的显示/隐藏。
  • 默认为数字键盘类型。
html
<template>
  <see-button @click="show = true">弹出键盘</see-button>
  <see-keyboard v-model="show" @on-input="handleInput" @on-delete="handleDelete" />
</template>

<script lang="ts" setup>
import { ref } from 'vue';
const show = ref(false);

const handleInput = (value) => {
  console.log('输入', value);
};

const handleDelete = () => {
  console.log('删除');
};
</script>

身份证键盘

  • type 设置为 idcard 可弹出身份证键盘,包含 X 键。
html
<template>
  <see-button @click="show = true">身份证键盘</see-button>
  <see-keyboard v-model="show" type="idcard" @on-input="handleInput" />
</template>

<script lang="ts" setup>
import { ref } from 'vue';
const show = ref(false);

const handleInput = (value) => {
  console.log('输入', value);
};
</script>

完整键盘

  • type 设置为 keyboard 可弹出完整键盘,包含字母和符号。
html
<template>
  <see-button @click="show = true">完整键盘</see-button>
  <see-keyboard v-model="show" type="keyboard" @on-input="handleInput" />
</template>

<script lang="ts" setup>
import { ref } from 'vue';
const show = ref(false);

const handleInput = (value) => {
  console.log('输入', value);
};
</script>

安全键盘

  • 通过设置 isRandomtrue 启用安全键盘,数字随机排列。
  • 适用于密码输入等安全场景。
html
<template>
  <see-button @click="show = true">安全键盘</see-button>
  <see-keyboard v-model="show" isRandom @on-input="handleInput" />
</template>

<script lang="ts" setup>
import { ref } from 'vue';
const show = ref(false);

const handleInput = (value) => {
  console.log('输入', value);
};
</script>

配合 Code 使用

  • 键盘组件通常与验证码输入框配合使用。
html
<template>
  <see-code v-model="code" :isFocus="false" @on-focus="showKeyboard = true" />
  <see-keyboard v-model="showKeyboard" @on-input="handleInput" @on-delete="handleDelete" />
</template>

<script lang="ts" setup>
import { ref } from 'vue';
const code = ref('');
const showKeyboard = ref(false);

const handleInput = (value) => {
  if (code.value.length < 6) {
    code.value += value;
  }
};

const handleDelete = () => {
  code.value = code.value.slice(0, -1);
};
</script>

自定义工具栏

  • 通过 isShowToolbar 控制是否显示工具栏。
  • 通过 title 设置工具栏标题。
  • 通过 confirmText 设置确认按钮文字。
  • 通过 toolbar 插槽自定义工具栏内容。
html
<template>
  <see-button @click="show = true">自定义工具栏</see-button>
  <see-keyboard
    v-model="show"
    title="安全键盘"
    confirm-text="确认"
    @on-confirm="handleConfirm"
    @on-close="show = false"
  />
</template>

<script lang="ts" setup>
import { ref } from 'vue';
const show = ref(false);

const handleConfirm = () => {
  show.value = false;
  console.log('确认');
};
</script>

API

Props

参数名说明类型默认值可选值
modelValue是否显示键盘booleanfalse-
type键盘类型'number' | 'idcard' | 'keyboard''number'-
isShowToolbar是否显示工具栏booleantrue-
confirmText确认按钮文字string'完成'-
isShowConfirm是否显示确认按钮booleantrue-
isShowDelete是否显示删除按钮booleantrue-
isRandom是否随机数字排列(安全键盘)booleanfalse-
isOverlay是否显示遮罩层booleantrue-
isCloseOnClickOverlay点击遮罩是否关闭booleantrue-
isSafeArea是否适配底部安全区域booleantrue-
title工具栏标题string''-

Events

事件名说明回调参数
onInput按键输入时触发value: string
onDelete点击删除键时触发-
onConfirm点击确认按钮时触发-
onClose键盘关闭时触发-
onOpen键盘打开时触发-

Slots

插槽名说明
toolbar自定义工具栏内容

辽 ICP 备 2025070134 号