<template>
<view class="u-keyboard" @touchmove.stop.prevent>
<view class="u-keyboard-grids">
<view
class="u-keyboard-grids-item"
:class="[btnBgGray(index) ? 'u-bg-gray' : '', index <= 2 ? 'u-border-top' : '', index < 9 ? 'u-border-bottom' : '', (index + 1) % 3 != 0 ? 'u-border-right' : '']"
:style="[itemStyle(index)]"
v-for="(item, index) in numList"
:key="index"
:hover-class="hoverClass(index)"
:hover-stay-time="100"
@tap="keyboardClick(item)"
>
<view class="u-keyboard-grids-btn">{{ item }}</view>
</view>
<view
class="u-keyboard-grids-item u-bg-gray"
hover-class="u-hover-class"
:hover-stay-time="100"
@touchstart.stop="backspaceClick"
@touchend="clearTimer"
>
<view class="u-keyboard-back u-keyboard-grids-btn">
<u-icon name="backspace" :size="38" :bold="true"></u-icon>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
props: {
mode: {
type: String,
default: 'number'
},
dotEnabled: {
type: Boolean,
default: true
},
random: {
type: Boolean,
default: false
}
},
data() {
return {
backspace: 'backspace',
dot: '.',
timer: null,
cardX: 'X'
}
},
computed: {
numList() {
let tmp = []
if (!this.dotEnabled && this.mode == 'number') {
if (!this.random) {
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
} else {
return this.$u.randomArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
}
} else if (this.dotEnabled && this.mode == 'number') {
if (!this.random) {
return [1, 2, 3, 4, 5, 6, 7, 8, 9, this.dot, 0]
} else {
return this.$u.randomArray([1, 2, 3, 4, 5, 6, 7, 8, 9, this.dot, 0])
}
} else if (this.mode == 'card') {
if (!this.random) {
return [1, 2, 3, 4, 5, 6, 7, 8, 9, this.cardX, 0]
} else {
return this.$u.randomArray([1, 2, 3, 4, 5, 6, 7, 8, 9, this.cardX, 0])
}
}
},
itemStyle() {
return index => {
let style = {}
if (this.mode == 'number' && !this.dotEnabled && index == 9)
style.flex = '0 0 66.6666666666%'
return style
}
},
btnBgGray() {
return index => {
if (
!this.random &&
index == 9 &&
(this.mode != 'number' || (this.mode == 'number' && this.dotEnabled))
)
return true
else return false
}
},
hoverClass() {
return index => {
if (
!this.random &&
index == 9 &&
((this.mode == 'number' && this.dotEnabled) || this.mode == 'card')
)
return 'u-hover-class'
else return 'u-keyboard-hover'
}
}
},
methods: {
backspaceClick() {
this.$emit('backspace')
clearInterval(this.timer)
this.timer = null
this.timer = setInterval(() => {
this.$emit('backspace')
}, 250)
},
clearTimer() {
clearInterval(this.timer)
this.timer = null
},
keyboardClick(val) {
if (this.dotEnabled && val != this.dot && val != this.cardX)
val = Number(val)
this.$emit('change', val)
}
}
}
</script>
<style lang="scss" scoped>
@import '../../libs/css/style.components.scss';
.u-keyboard {
position: relative;
z-index: 1003;
}
.u-keyboard-grids {
display: flex;
flex-wrap: wrap;
}
.u-keyboard-grids-item {
flex: 0 0 33.3333333333%;
text-align: center;
font-size: 25rpx;
color: #333;
display: flex;
align-items: center;
justify-content: center;
height: 55rpx;
font-weight: 500;
}
.u-bg-gray {
background-color: #e7e6eb;
}
.u-keyboard-back {
font-size: 18rpx;
}
.u-keyboard-hover {
background-color: #e7e6eb;
}
</style>