guangchao.xu | 070005a | 2020-12-07 09:56:40 +0800 | [diff] [blame] | 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 | <view class="u-keyboard-grids-btn">{{ item }}</view> |
| 14 | </view> |
| 15 | <view class="u-keyboard-grids-item u-bg-gray" hover-class="u-hover-class" :hover-stay-time="100" @touchstart.stop="backspaceClick" |
| 16 | @touchend="clearTimer"> |
| 17 | <view class="u-keyboard-back u-keyboard-grids-btn"> |
| 18 | <u-icon name="backspace" :size="38" :bold="true"></u-icon> |
| 19 | </view> |
| 20 | </view> |
| 21 | </view> |
| 22 | </view> |
| 23 | </template> |
| 24 | |
| 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 | |
| 122 | <style lang="scss" scoped> |
| 123 | @import "../../libs/css/style.components.scss"; |
| 124 | |
| 125 | .u-keyboard { |
| 126 | position: relative; |
| 127 | z-index: 1003; |
| 128 | } |
| 129 | |
| 130 | .u-keyboard-grids { |
| 131 | @include vue-flex; |
| 132 | flex-wrap: wrap; |
| 133 | } |
| 134 | |
| 135 | .u-keyboard-grids-item { |
| 136 | flex: 0 0 33.3333333333%; |
| 137 | text-align: center; |
| 138 | font-size: 50rpx; |
| 139 | color: #333; |
| 140 | @include vue-flex; |
| 141 | align-items: center; |
| 142 | justify-content: center; |
| 143 | height: 110rpx; |
| 144 | font-weight: 500; |
| 145 | } |
| 146 | |
| 147 | .u-bg-gray { |
| 148 | background-color: $u-border-color; |
| 149 | } |
| 150 | |
| 151 | .u-keyboard-back { |
| 152 | font-size: 36rpx; |
| 153 | } |
| 154 | |
| 155 | .u-keyboard-hover { |
| 156 | background-color: #e7e6eb; |
| 157 | } |
| 158 | </style> |