tn-number-keyboard.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <view class="tn-number-keyboard-class tn-number-keyboard" @touchmove.stop.prevent="() => {}">
  3. <view class="tn-number-keyboard__grids">
  4. <view
  5. v-for="(item, index) in dataList"
  6. :key="index"
  7. class="tn-number-keyboard__grids__item"
  8. :class="{
  9. 'tn-bg-gray--light': showGaryBg(index),
  10. 'tn-border-solid-top': index <= 2,
  11. 'tn-border-solid-bottom': index < 9,
  12. 'tn-border-solid-right': (index + 1) % 3 != 0
  13. }"
  14. :hover-class="hoverClass(index)"
  15. :hover-stay-time="150"
  16. @tap="keyboardClick(item)"
  17. >
  18. <view class="tn-number-keyboard__grids__btn">{{ item }}</view>
  19. </view>
  20. <view
  21. class="tn-number-keyboard__grids__item tn-bg-gray--light"
  22. hover-class="tn-hover"
  23. :hover-stay-time="150"
  24. @touchstart.stop="backspaceClick"
  25. @touchend="clearTimer"
  26. >
  27. <view class="tn-number-keyboard__grids__btn tn-number-keyboard__back">
  28. <view class="tn-icon-left-arrow tn-number-keyboard__back__icon"></view>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. export default {
  36. name: 'tn-number-keyboard',
  37. props: {
  38. // 键盘类型
  39. // number -> 数字键盘 card -> 身份证键盘
  40. mode: {
  41. type: String,
  42. default: 'number'
  43. },
  44. // 是否显示键盘的'.'符号
  45. dotEnabled: {
  46. type: Boolean,
  47. default: true
  48. },
  49. // 是否为乱序键盘
  50. randomEnabled: {
  51. type: Boolean,
  52. default: false
  53. }
  54. },
  55. computed: {
  56. // 键盘显示的内容
  57. dataList() {
  58. let tmp = []
  59. if (!this.dotEnabled && this.mode === 'number') {
  60. if (!this.randomEnabled) {
  61. return [1, 2, 3, 4, 5, 6, 7, 8, 9, '', 0]
  62. } else {
  63. let data = this.$t.array.random([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
  64. data.splice(-1, 0, '')
  65. return data
  66. }
  67. } else if (this.dotEnabled && this.mode === 'number') {
  68. if (!this.randomEnabled) {
  69. return [1, 2, 3, 4, 5, 6, 7, 8, 9, this.dot, 0]
  70. } else {
  71. let data = this.$t.array.random([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
  72. data.splice(-1, 0, this.dot)
  73. return data
  74. }
  75. } else if (this.mode === 'card') {
  76. if (!this.randomEnabled) {
  77. return [1, 2, 3, 4, 5, 6, 7, 8, 9, this.cardX, 0]
  78. } else {
  79. let data = this.$t.array.random([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
  80. data.splice(-1, 0, this.cardX)
  81. return data
  82. }
  83. }
  84. },
  85. // 按键的样式
  86. keyStyle() {
  87. return index => {
  88. let style = {}
  89. if (this.mode === 'number' && !this.dotEnabled && index === 9) style.flex = '0 0 66.6666666666%'
  90. return style
  91. }
  92. },
  93. // 是否让按键显示灰色,只在数字键盘和非乱序且在点击时
  94. showGaryBg() {
  95. return index => {
  96. if (!this.randomEnabled && index === 9 && (this.mode !== 'number' || (this.mode === 'number' && this.dotEnabled))) return true
  97. else return false
  98. }
  99. },
  100. // 手指停留的class
  101. hoverClass() {
  102. return index => {
  103. if (this.mode === 'number' && !this.dotEnabled && index === 9) return ''
  104. if (!this.randomEnabled && index === 9 && (this.mode === 'number' && this.dotEnabled || this.mode === 'card')) return 'tn-hover'
  105. else return 'tn-number-keyboard--hover'
  106. }
  107. }
  108. },
  109. data() {
  110. return {
  111. // 退格键内容
  112. backspace: 'backspace',
  113. // 点内容
  114. dot: '.',
  115. // 长按多次删除事件监听
  116. longPressDeleteTimer: null,
  117. // 身份证的X符号
  118. cardX: 'X'
  119. }
  120. },
  121. methods: {
  122. // 点击退格键
  123. backspaceClick() {
  124. this.$emit('backspace')
  125. this.clearTimer()
  126. this.longPressDeleteTimer = setInterval(() => {
  127. this.$emit('backspace')
  128. }, 250)
  129. },
  130. // 获取键盘显示的内容
  131. keyboardClick(value) {
  132. if (this.mode === 'number' && !this.dotEnabled && value === '') return
  133. // 允许键盘显示点模式和触发非点按键时,将内容转换为数字类型
  134. if (this.dotEnabled && value != this.dot && value != this.cardX) value = Number(value)
  135. this.$emit('change', value)
  136. },
  137. // 清除定时器
  138. clearTimer() {
  139. if (this.longPressDeleteTimer) {
  140. clearInterval(this.longPressDeleteTimer)
  141. this.longPressDeleteTimer = null
  142. }
  143. }
  144. }
  145. }
  146. </script>
  147. <style lang="scss" scoped>
  148. .tn-number-keyboard {
  149. position: relative;
  150. &__grids {
  151. display: flex;
  152. flex-direction: row;
  153. flex-wrap: wrap;
  154. justify-content: flex-end;
  155. &__item {
  156. display: flex;
  157. flex-direction: row;
  158. flex: 0 0 33.3333333333%;
  159. align-items: center;
  160. justify-content: center;
  161. height: 110rpx;
  162. text-align: center;
  163. font-size: 50rpx;
  164. color: $tn-font-color;
  165. font-weight: 500;
  166. }
  167. }
  168. &__back {
  169. font-size: 38rpx;
  170. }
  171. &--hover {
  172. background-color: $tn-font-holder-color;
  173. }
  174. }
  175. </style>