u-number-keyboard.vue 4.4 KB

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