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