u-number-keyboard.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <view class="u-keyboard" @touchmove.stop.prevent>
  3. <view class="u-keyboard-grids">
  4. <view
  5. class="u-keyboard-grids-item"
  6. :class="[btnBgGray(index) ? 'u-bg-gray' : '', index <= 2 ? 'u-border-top' : '', index < 9 ? 'u-border-bottom' : '', (index + 1) % 3 != 0 ? 'u-border-right' : '']"
  7. :style="[itemStyle(index)]"
  8. v-for="(item, index) in numList"
  9. :key="index"
  10. :hover-class="hoverClass(index)"
  11. :hover-stay-time="100"
  12. @tap="keyboardClick(item)"
  13. >
  14. <view class="u-keyboard-grids-btn">{{ item }}</view>
  15. </view>
  16. <view class="u-keyboard-grids-item u-bg-gray" hover-class="u-hover-class" :hover-stay-time="100" @touchstart.stop="backspaceClick" @touchend="clearTimer">
  17. <view class="u-keyboard-back u-keyboard-grids-btn"><u-icon name="backspace" :size="38" :bold="true"></u-icon></view>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. props: {
  25. // 键盘的类型,number-数字键盘,card-身份证键盘
  26. mode: {
  27. type: String,
  28. default: 'number'
  29. },
  30. // 是否显示键盘的"."符号
  31. dotEnabled: {
  32. type: Boolean,
  33. default: true
  34. },
  35. // 是否打乱键盘按键的顺序
  36. random: {
  37. type: Boolean,
  38. default: false
  39. }
  40. },
  41. data() {
  42. return {
  43. backspace: 'backspace', // 退格键内容
  44. dot: '.', // 点
  45. timer: null, // 长按多次删除的事件监听
  46. cardX: 'X' // 身份证的X符号
  47. };
  48. },
  49. computed: {
  50. // 键盘需要显示的内容
  51. numList() {
  52. let tmp = [];
  53. if (!this.dotEnabled && this.mode == 'number') {
  54. if (!this.random) {
  55. return [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
  56. } else {
  57. return this.$u.randomArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
  58. }
  59. } else if (this.dotEnabled && this.mode == 'number') {
  60. if (!this.random) {
  61. return [1, 2, 3, 4, 5, 6, 7, 8, 9, this.dot, 0];
  62. } else {
  63. return this.$u.randomArray([1, 2, 3, 4, 5, 6, 7, 8, 9, this.dot, 0]);
  64. }
  65. } else if (this.mode == 'card') {
  66. if (!this.random) {
  67. return [1, 2, 3, 4, 5, 6, 7, 8, 9, this.cardX, 0];
  68. } else {
  69. return this.$u.randomArray([1, 2, 3, 4, 5, 6, 7, 8, 9, this.cardX, 0]);
  70. }
  71. }
  72. },
  73. // 按键的样式,在非乱序&&数字键盘&&不显示点按钮时,index为9时,按键占位两个空间
  74. itemStyle() {
  75. return index => {
  76. let style = {};
  77. if (this.mode == 'number' && !this.dotEnabled && index == 9) style.flex = '0 0 66.6666666666%';
  78. return style;
  79. };
  80. },
  81. // 是否让按键显示灰色,只在非乱序&&数字键盘&&且允许点按键的时候
  82. btnBgGray() {
  83. return index => {
  84. if (!this.random && index == 9 && (this.mode != 'number' || (this.mode == 'number' && this.dotEnabled))) return true;
  85. else return false;
  86. };
  87. },
  88. hoverClass() {
  89. return index => {
  90. if(!this.random && index == 9 && (this.mode == 'number' && this.dotEnabled || this.mode == 'card')) return 'u-hover-class';
  91. else return 'u-keyboard-hover';
  92. }
  93. }
  94. },
  95. methods: {
  96. // 点击退格键
  97. backspaceClick() {
  98. this.$emit('backspace');
  99.  clearInterval(this.timer); //再次清空定时器,防止重复注册定时器
  100.       this.timer = setInterval(() => {
  101.     this.$emit('backspace');
  102.       }, 250);
  103. },
  104. clearTimer() {
  105. clearInterval(this.timer);
  106. },
  107. // 获取键盘显示的内容
  108. keyboardClick(val) {
  109. // 允许键盘显示点模式和触发非点按键时,将内容转为数字类型
  110. if (this.dotEnabled && val != this.dot && val != this.cardX) val = Number(val);
  111. this.$emit('change', val);
  112. }
  113. }
  114. };
  115. </script>
  116. <style lang="scss" scoped>
  117. .u-keyboard {
  118. position: relative;
  119. z-index: 1003;
  120. }
  121. .u-keyboard-grids {
  122. display: flex;
  123. flex-wrap: wrap;
  124. }
  125. .u-keyboard-grids-item {
  126. flex: 0 0 33.3333333333%;
  127. text-align: center;
  128. font-size: 50rpx;
  129. color: #333;
  130. display: flex;
  131. align-items: center;
  132. justify-content: center;
  133. height: 110rpx;
  134. font-weight: 500;
  135. }
  136. .u-bg-gray {
  137. background-color: #e7e6eb;
  138. }
  139. .u-keyboard-back {
  140. font-size: 36rpx;
  141. }
  142. .u-keyboard-hover {
  143. background-color: #e7e6eb;
  144. }
  145. </style>