u-number-keyboard.vue 4.5 KB

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