KeyBoard 数字键盘

用于展现数字键盘。

使用指南

在 page.json 中引入组件

  1. {
  2. "navigationBarTitleText": "KeyBoard",
  3. "usingComponents": {
  4. "wux-button": "../../dist/button/index",
  5. "wux-keyboard": "../../dist/keyboard/index"
  6. }
  7. }

示例

!> 该组件主要依靠 JavaScript 主动调用,所以一般只需在 wxml 中添加一个组件,并设置 id 为 #wux-keyboard 或其他,之后在 page.js 中调用 $wuxKeyBoard(id) 获取匹配到的第一个组件实例对象。

  1. <wux-keyboard id="wux-keyboard" />
  2. <view class="page">
  3. <view class="page__hd">
  4. <view class="page__title">KeyBoard</view>
  5. <view class="page__desc">数字键盘</view>
  6. </view>
  7. <view class="page__bd page__bd_spacing">
  8. <wux-button block type="light" bind:click="open">Open KeyBoard</wux-button>
  9. <wux-button block type="light" bind:click="openWitdhDisorder">Open a disorderly KeyBoard</wux-button>
  10. <wux-button block type="light" bind:click="openWithPromiseCallback">Open KeyBoard with promise callback</wux-button>
  11. <wux-button block type="light" bind:click="openPlain">Plain theme</wux-button>
  12. <wux-button block type="light" bind:click="openTimed">Open and close</wux-button>
  13. </view>
  14. </view>
  1. import { $wuxKeyBoard } from '../../dist/index'
  2. Page({
  3. data: {},
  4. onLoad() {},
  5. open() {
  6. $wuxKeyBoard().show({
  7. callback(value) {
  8. console.log(`输入的密码是:${value}`)
  9. return true
  10. },
  11. })
  12. },
  13. openWitdhDisorder() {
  14. $wuxKeyBoard().show({
  15. disorder: true,
  16. callback(value) {
  17. console.log(`输入的密码是:${value}`)
  18. return false
  19. },
  20. })
  21. },
  22. openWithPromiseCallback() {
  23. const http = (obj) => {
  24. return new Promise((resolve, reject) => {
  25. obj.success = (res) => resolve(res)
  26. obj.fail = (res) => reject(res)
  27. wx.request(obj)
  28. })
  29. }
  30. $wuxKeyBoard().show({
  31. callback(value) {
  32. console.log(`输入的密码是:${value}`)
  33. wx.showLoading({
  34. title: '验证支付密码'
  35. })
  36. return http({
  37. url: 'https://www.skyvow.cn/api/user/sign/in',
  38. method: 'POST',
  39. data: {
  40. username: 'admin',
  41. password: value
  42. }
  43. })
  44. .then(res => {
  45. const data = res.data
  46. console.log(data)
  47. wx.hideLoading()
  48. wx.showToast({
  49. title: data.meta.message,
  50. duration: 3000,
  51. })
  52. if (data.meta.code !== 0) {
  53. return Promise.reject(data.meta.message)
  54. }
  55. })
  56. },
  57. })
  58. },
  59. openPlain() {
  60. const fn = (title) => {
  61. wx.hideLoading()
  62. wx.showToast({
  63. title,
  64. duration: 3000,
  65. })
  66. }
  67. $wuxKeyBoard().show({
  68. className: 'className',
  69. titleText: '安全键盘',
  70. cancelText: '取消',
  71. inputText: '输入数字密码',
  72. showCancel: true,
  73. disorder: false,
  74. maxlength: 4,
  75. callback(value) {
  76. console.log(`输入的密码是:${value}`)
  77. wx.showLoading({
  78. title: '验证支付密码'
  79. })
  80. return new Promise((resolve, reject) => {
  81. setTimeout(() => Math.ceil(Math.random() * 10) >= 6 ? resolve(fn('密码正确')) : reject(fn('密码错误')), 3000)
  82. })
  83. },
  84. })
  85. },
  86. openTimed() {
  87. clearTimeout(this.timeout)
  88. const hide = $wuxKeyBoard().show({
  89. password: false,
  90. maxlength: -1,
  91. onChange(value) {
  92. console.log(`输入的密码是:${value}`)
  93. },
  94. onClose(value) {
  95. return false
  96. },
  97. })
  98. this.timeout = setTimeout(hide, 3000)
  99. },
  100. })

视频演示

KeyBoard

API

参数 类型 描述 默认值
options object 配置项 -
options.prefixCls string 自定义类名前缀 wux-keyboard
options.className string 自定义类名 -
options.titleText string 标题 安全键盘
options.cancelText string 取消按钮的文字 取消
options.inputText string 提示文本 输入数字密码
options.showCancel boolean 是否显示取消按钮 true
options.disorder boolean 是否打乱键盘 false
options.password boolean 是否密码类型 true
options.maxlength number,string 最大输入长度,设置为 -1 的时候不限制最大长度 6
options.onChange function change 事件触发的回调函数 -
options.callback function 输入完成后的回调函数 -
options.onClose function 输入完成后的回调函数,优先级高于 callback -