u-car-keyboard.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <template>
  2. <view class="u-keyboard" @touchmove.stop.prevent>
  3. <view class="u-keyboard-grids">
  4. <block>
  5. <view class="u-keyboard-grids-item" v-for="(group, i) in isAbcMode ? EngKeyBoardList : areaList" :key="i">
  6. <view :hover-stay-time="100" @tap="carInputClick(i, j)" hover-class="u-carinput-hover" class="u-keyboard-grids-btn"
  7. :class="{'u-keyboard-grids-btn-last': i == 3 && !isAbcMode}"
  8. v-for="(item, j) in group" :key="j">
  9. {{ item }}
  10. </view>
  11. </view>
  12. <view @touchstart="backspaceClick" @touchend="clearTimer" :hover-stay-time="100" class="u-keyboard-back"
  13. hover-class="u-hover-class">
  14. <u-icon :size="36" name="backspace" :bold="true"></u-icon>
  15. </view>
  16. <view :hover-stay-time="100" class="u-keyboard-change" hover-class="u-carinput-hover" @tap="changeCarInputMode">
  17. <text class="zh" :class="[!isAbcMode ? 'active' : 'inactive']">中</text>
  18. /
  19. <text class="en" :class="[isAbcMode ? 'active' : 'inactive']">英</text>
  20. </view>
  21. </block>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. name: "u-keyboard",
  28. props: {
  29. // 是否打乱键盘按键的顺序
  30. random: {
  31. type: Boolean,
  32. default: false
  33. },
  34. // 车牌输入时,abc=true为输入车牌号码,bac=false为输入省份中文简称
  35. abc: {
  36. type: Boolean,
  37. default: false
  38. },
  39. },
  40. data() {
  41. return {
  42. isAbcMode: false,
  43. };
  44. },
  45. watch: {
  46. abc(val) {
  47. this.isAbcMode = val;
  48. }
  49. },
  50. computed: {
  51. areaList() {
  52. let data = [
  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. let tmp = [];
  93. // 打乱顺序
  94. if (this.random) data = this.$u.randomArray(data);
  95. // 切割成二维数组
  96. tmp[0] = data.slice(0, 10);
  97. tmp[1] = data.slice(10, 20);
  98. tmp[2] = data.slice(20, 30);
  99. tmp[3] = data.slice(30, 38);
  100. return tmp;
  101. },
  102. EngKeyBoardList() {
  103. let data = [
  104. 1,
  105. 2,
  106. 3,
  107. 4,
  108. 5,
  109. 6,
  110. 7,
  111. 8,
  112. 9,
  113. 0,
  114. 'Q',
  115. 'W',
  116. 'E',
  117. 'R',
  118. 'T',
  119. 'Y',
  120. 'U',
  121. 'I',
  122. 'O',
  123. 'P',
  124. 'A',
  125. 'S',
  126. 'D',
  127. 'F',
  128. 'G',
  129. 'H',
  130. 'J',
  131. 'K',
  132. 'L',
  133. 'Z',
  134. 'X',
  135. 'C',
  136. 'V',
  137. 'B',
  138. 'N',
  139. 'M'
  140. ];
  141. let tmp = [];
  142. if (this.random) data = this.$u.randomArray(data);
  143. tmp[0] = data.slice(0, 10);
  144. tmp[1] = data.slice(10, 20);
  145. tmp[2] = data.slice(20, 30);
  146. tmp[3] = data.slice(30, 36);
  147. return tmp;
  148. }
  149. },
  150. methods: {
  151. // 点击键盘按钮
  152. carInputClick(i, j) {
  153. let value = '';
  154. // 不同模式,获取不同数组的值
  155. if (this.isAbcMode) value = this.EngKeyBoardList[i][j];
  156. else value = this.areaList[i][j];
  157. this.$emit('change', value);
  158. },
  159. // 修改汽车牌键盘的输入模式,中文|英文
  160. changeCarInputMode() {
  161. this.isAbcMode = !this.isAbcMode;
  162. },
  163. // 点击退格键
  164. backspaceClick() {
  165. this.$emit('backspace');
  166. clearInterval(this.timer); //再次清空定时器,防止重复注册定时器
  167. this.timer = null;
  168. this.timer = setInterval(() => {
  169. this.$emit('backspace');
  170. }, 250);
  171. },
  172. clearTimer() {
  173. clearInterval(this.timer);
  174. this.timer = null;
  175. },
  176. }
  177. };
  178. </script>
  179. <style lang="scss" scoped>
  180. @import "../../libs/css/style.components.scss";
  181. .u-keyboard-grids {
  182. background: rgb(215, 215, 217);
  183. padding: 24rpx 0;
  184. position: relative;
  185. }
  186. .u-keyboard-grids-item {
  187. display: flex;
  188. align-items: center;
  189. justify-content: center;
  190. }
  191. .u-keyboard-grids-btn {
  192. text-decoration: none;
  193. width: 56rpx;
  194. flex: 0 0 58rpx;
  195. height: 80rpx;
  196. display: inline-block;
  197. font-size: 36rpx;
  198. text-align: center;
  199. line-height: 80rpx;
  200. background-color: #fff;
  201. margin: 8rpx 8rpx;
  202. border-radius: 8rpx;
  203. box-shadow: 0 2rpx 0rpx #888992;
  204. font-weight: 400;
  205. }
  206. .u-keyboard-grids-btn-last {
  207. width: 56rpx;
  208. flex: 0 0 58rpx;
  209. margin: 8rpx 5rpx;
  210. }
  211. .u-carinput-hover {
  212. background-color: rgb(185, 188, 195) !important;
  213. }
  214. .u-keyboard-back {
  215. position: absolute;
  216. width: 88rpx;
  217. right: 14rpx;
  218. bottom: 32rpx;
  219. height: 80rpx;
  220. background-color: rgb(185, 188, 195);
  221. display: flex;
  222. align-items: center;
  223. border-radius: 8rpx;
  224. justify-content: center;
  225. box-shadow: 0 2rpx 0rpx #888992;
  226. }
  227. .u-keyboard-change {
  228. font-size: 24rpx;
  229. box-shadow: 0 2rpx 0rpx #888992;
  230. position: absolute;
  231. width: 88rpx;
  232. left: 14rpx;
  233. line-height: 1;
  234. bottom: 32rpx;
  235. height: 80rpx;
  236. background-color: #ffffff;
  237. display: flex;
  238. align-items: center;
  239. border-radius: 8rpx;
  240. justify-content: center;
  241. }
  242. .u-keyboard-change .inactive.zh {
  243. transform: scale(0.85) translateY(-10rpx);
  244. }
  245. .u-keyboard-change .inactive.en {
  246. transform: scale(0.85) translateY(10rpx);
  247. }
  248. .u-keyboard-change .active {
  249. color: rgb(237, 112, 64);
  250. font-size: 30rpx;
  251. }
  252. .u-keyboard-change .zh {
  253. transform: translateY(-10rpx);
  254. }
  255. .u-keyboard-change .en {
  256. transform: translateY(10rpx);
  257. }
  258. /* DarkMode 下的样式 */
  259. @media (prefers-color-scheme: dark) {
  260. .u-keyboard-grids-btn{
  261. color: #000;
  262. }
  263. .u-keyboard-change{
  264. color: #000;
  265. }
  266. }
  267. </style>