index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/classNames'
  3. const defaults = {
  4. prefixCls: 'wux-keyboard',
  5. className: '',
  6. titleText: '安全键盘',
  7. cancelText: '取消',
  8. inputText: '输入数字密码',
  9. showCancel: true,
  10. disorder: false,
  11. password: true,
  12. maxlength: 6,
  13. onChange(value) {},
  14. callback(value) {},
  15. // onClose(value) {},
  16. }
  17. /**
  18. * 给指一位数组随机生成二维数组
  19. *
  20. * @param {boolean} [isRandom=false] 是否随机
  21. * @param {array} [arr=[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]] 默认数组
  22. * @returns
  23. */
  24. const upsetNums = (isRandom = false, arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) => {
  25. if (isRandom) {
  26. const floor = Math.floor
  27. const random = Math.random
  28. const len = arr.length
  29. let i, j, temp, n = floor(len / 2) + 1
  30. while (n--) {
  31. i = floor(random() * len)
  32. j = floor(random() * len)
  33. if (i !== j) {
  34. temp = arr[i]
  35. arr[i] = arr[j]
  36. arr[j] = temp
  37. }
  38. }
  39. }
  40. let nums = []
  41. for (let i = 0; i < 4; i++) {
  42. nums.push(arr.slice(i * 3, (i + 1) * 3))
  43. }
  44. return nums
  45. }
  46. baseComponent({
  47. useFunc: true,
  48. data: defaults,
  49. computed: {
  50. classes() {
  51. const { prefixCls } = this.data
  52. const wrap = classNames(prefixCls)
  53. const hd = `${prefixCls}__hd`
  54. const bd = `${prefixCls}__bd`
  55. const label = `${prefixCls}__label`
  56. const password = `${prefixCls}__password`
  57. const input = `${prefixCls}__input`
  58. const ft = `${prefixCls}__ft`
  59. const title = `${prefixCls}__title`
  60. const numbers = `${prefixCls}__numbers`
  61. const number = `${prefixCls}__number`
  62. const text = `${prefixCls}__text`
  63. const hover = `${prefixCls}__text--hover`
  64. return {
  65. wrap,
  66. hd,
  67. bd,
  68. label,
  69. password,
  70. input,
  71. ft,
  72. title,
  73. numbers,
  74. number,
  75. text,
  76. hover,
  77. }
  78. },
  79. },
  80. methods: {
  81. /**
  82. * 隐藏
  83. */
  84. hide() {
  85. this.$$setData({ in: false })
  86. },
  87. /**
  88. * 上拉键盘组件
  89. * @param {Object} opts 配置项
  90. * @param {String} opts.className 自定义类名
  91. * @param {String} opts.titleText 标题
  92. * @param {String} opts.cancelText 取消按钮的文字
  93. * @param {String} opts.inputText 提示文本
  94. * @param {Boolean} opts.showCancel 是否显示取消按钮
  95. * @param {Boolean} opts.disorder 是否打乱键盘
  96. * @param {Boolean} opts.password 是否密码类型
  97. * @param {Number} opts.maxlength 最大输入长度,设置为 -1 的时候不限制最大长度
  98. * @param {Function} opts.onChange change 事件触发的回调函数
  99. * @param {Function} opts.callback 输入完成后的回调函数
  100. * @param {Function} opts.onClose 输入完成后的回调函数,优先级高于 callback
  101. */
  102. show(opts = {}) {
  103. const nums = upsetNums(opts.disorder)
  104. const maxlength = opts.maxlength <= 0 ? -1 : opts.maxlength
  105. const keys = maxlength !== -1 ? [...new Array(maxlength || defaults.maxlength)].map(() => 1) : []
  106. const options = this.$$mergeOptionsAndBindMethods(Object.assign({ nums, keys, value: '' }, defaults, opts))
  107. this.$$setData({ in: true, ...options })
  108. return this.hide.bind(this)
  109. },
  110. /**
  111. * 增加
  112. */
  113. increase(e) {
  114. const dataset = e.currentTarget.dataset
  115. const nextValue = String(dataset.value)
  116. const { value, maxlength } = this.data
  117. if (value.length >= maxlength && maxlength !== -1) return
  118. this.updateValue(value + nextValue)
  119. },
  120. /**
  121. * 减少
  122. */
  123. decrease(e) {
  124. const { value } = this.data
  125. if (value.length === 0) return
  126. this.updateValue(value.substr(0, value.length - 1))
  127. },
  128. /**
  129. * 更新
  130. */
  131. updateValue(value = '') {
  132. this.$$setData({ value })
  133. // onChange
  134. if (typeof this.fns.onChange === 'function') {
  135. this.fns.onChange.call(this, value)
  136. }
  137. // onClose
  138. if (value.length === this.data.maxlength) {
  139. const preCloseCallback = this.fns.onClose || this.fns.callback
  140. const performCloseDialog = () => this.hide()
  141. if (preCloseCallback && typeof preCloseCallback === 'function') {
  142. const preCloseCallbackResult = preCloseCallback.call(this, value)
  143. if (typeof preCloseCallbackResult === 'object') {
  144. if (preCloseCallbackResult.closePromise) {
  145. preCloseCallbackResult.closePromise.then(performCloseDialog, performCloseDialog)
  146. } else {
  147. preCloseCallbackResult.then(performCloseDialog, performCloseDialog)
  148. }
  149. } else if (preCloseCallbackResult !== false) {
  150. performCloseDialog()
  151. }
  152. } else {
  153. performCloseDialog()
  154. }
  155. }
  156. },
  157. },
  158. })