u-car-keyboard.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <template>
  2. <view
  3. class="u-keyboard"
  4. @touchmove.stop.prevent="noop"
  5. >
  6. <view
  7. v-for="(group, i) in abc ? engKeyBoardList : areaList"
  8. :key="i"
  9. class="u-keyboard__button"
  10. :index="i"
  11. :class="[i + 1 === 4 && 'u-keyboard__button--center']"
  12. >
  13. <view
  14. v-if="i === 3"
  15. class="u-keyboard__button__inner-wrapper"
  16. >
  17. <view
  18. class="u-keyboard__button__inner-wrapper__left"
  19. hover-class="u-hover-class"
  20. :hover-stay-time="200"
  21. @tap="changeCarInputMode"
  22. >
  23. <text
  24. class="u-keyboard__button__inner-wrapper__left__lang"
  25. :class="[!abc && 'u-keyboard__button__inner-wrapper__left__lang--active']"
  26. >中
  27. </text>
  28. <text class="u-keyboard__button__inner-wrapper__left__line">/</text>
  29. <text
  30. class="u-keyboard__button__inner-wrapper__left__lang"
  31. :class="[abc && 'u-keyboard__button__inner-wrapper__left__lang--active']"
  32. >英
  33. </text>
  34. </view>
  35. </view>
  36. <view
  37. class="u-keyboard__button__inner-wrapper"
  38. v-for="(item, j) in group"
  39. :key="j"
  40. >
  41. <view
  42. class="u-keyboard__button__inner-wrapper__inner"
  43. :hover-stay-time="200"
  44. @tap="carInputClick(i, j)"
  45. hover-class="u-hover-class"
  46. >
  47. <text class="u-keyboard__button__inner-wrapper__inner__text">{{ item }}</text>
  48. </view>
  49. </view>
  50. <view
  51. v-if="i === 3"
  52. @touchstart="backspaceClick"
  53. @touchend="clearTimer"
  54. class="u-keyboard__button__inner-wrapper"
  55. >
  56. <view
  57. class="u-keyboard__button__inner-wrapper__right"
  58. hover-class="u-hover-class"
  59. :hover-stay-time="200"
  60. >
  61. <u-icon
  62. size="28"
  63. name="backspace"
  64. color="#303133"
  65. ></u-icon>
  66. </view>
  67. </view>
  68. </view>
  69. </view>
  70. </template>
  71. <script>
  72. import props from './props.js';
  73. /**
  74. * keyboard 键盘组件
  75. * @description 此为uView自定义的键盘面板,内含了数字键盘,车牌号键,身份证号键盘3种模式,都有可以打乱按键顺序的选项。
  76. * @tutorial https://uviewui.com/components/keyboard.html
  77. * @property {Boolean} random 是否打乱键盘的顺序
  78. * @event {Function} change 点击键盘触发
  79. * @event {Function} backspace 点击退格键触发
  80. * @example <u-keyboard ref="uKeyboard" mode="car" v-model="show"></u-keyboard>
  81. */
  82. export default {
  83. name: "u-keyboard",
  84. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  85. data() {
  86. return {
  87. // 车牌输入时,abc=true为输入车牌号码,bac=false为输入省份中文简称
  88. abc: false
  89. };
  90. },
  91. computed: {
  92. areaList() {
  93. let data = [
  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. let tmp = [];
  132. // 打乱顺序
  133. if (this.random) data = uni.$u.randomArray(data);
  134. // 切割成二维数组
  135. tmp[0] = data.slice(0, 10);
  136. tmp[1] = data.slice(10, 20);
  137. tmp[2] = data.slice(20, 30);
  138. tmp[3] = data.slice(30, 36);
  139. return tmp;
  140. },
  141. engKeyBoardList() {
  142. let data = [
  143. 1,
  144. 2,
  145. 3,
  146. 4,
  147. 5,
  148. 6,
  149. 7,
  150. 8,
  151. 9,
  152. 0,
  153. 'Q',
  154. 'W',
  155. 'E',
  156. 'R',
  157. 'T',
  158. 'Y',
  159. 'U',
  160. 'I',
  161. 'O',
  162. 'P',
  163. 'A',
  164. 'S',
  165. 'D',
  166. 'F',
  167. 'G',
  168. 'H',
  169. 'J',
  170. 'K',
  171. 'L',
  172. 'Z',
  173. 'X',
  174. 'C',
  175. 'V',
  176. 'B',
  177. 'N',
  178. 'M'
  179. ];
  180. let tmp = [];
  181. if (this.random) data = uni.$u.randomArray(data);
  182. tmp[0] = data.slice(0, 10);
  183. tmp[1] = data.slice(10, 20);
  184. tmp[2] = data.slice(20, 30);
  185. tmp[3] = data.slice(30, 36);
  186. return tmp;
  187. }
  188. },
  189. methods: {
  190. // 点击键盘按钮
  191. carInputClick(i, j) {
  192. let value = '';
  193. // 不同模式,获取不同数组的值
  194. if (this.abc) value = this.engKeyBoardList[i][j];
  195. else value = this.areaList[i][j];
  196. // 如果允许自动切换,则将中文状态切换为英文
  197. if (!this.abc && this.autoChange) uni.$u.sleep(200).then(() => this.abc = true)
  198. this.$emit('change', value);
  199. },
  200. // 修改汽车牌键盘的输入模式,中文|英文
  201. changeCarInputMode() {
  202. this.abc = !this.abc;
  203. },
  204. // 点击退格键
  205. backspaceClick() {
  206. this.$emit('backspace');
  207. clearInterval(this.timer); //再次清空定时器,防止重复注册定时器
  208. this.timer = null;
  209. this.timer = setInterval(() => {
  210. this.$emit('backspace');
  211. }, 250);
  212. },
  213. clearTimer() {
  214. clearInterval(this.timer);
  215. this.timer = null;
  216. },
  217. }
  218. };
  219. </script>
  220. <style lang="scss" scoped>
  221. @import "../../libs/css/components.scss";
  222. $u-car-keyboard-background-color: rgb(224, 228, 230) !default;
  223. $u-car-keyboard-padding: 6px 0 6px !default;
  224. $u-car-keyboard-button-inner-width: 64 rpx !default;
  225. $u-car-keyboard-button-inner-background-color: #FFFFFF !default;
  226. $u-car-keyboard-button-height: 80 rpx !default;
  227. $u-car-keyboard-button-inner-box-shadow: 0 1px 0px #999992 !default;
  228. $u-car-keyboard-button-border-radius: 4px !default;
  229. $u-car-keyboard-button-inner-margin: 8 rpx 5 rpx !default;
  230. $u-car-keyboard-button-text-font-size: 16px !default;
  231. $u-car-keyboard-button-text-color: $u-main-color !default;
  232. $u-car-keyboard-center-inner-margin: 0 4 rpx !default;
  233. $u-car-keyboard-special-button-width: 134 rpx !default;
  234. $u-car-keyboard-lang-font-size: 16px !default;
  235. $u-car-keyboard-lang-color: $u-main-color !default;
  236. $u-car-keyboard-active-color: $u-primary !default;
  237. $u-car-keyboard-line-font-size: 15px !default;
  238. $u-car-keyboard-line-color: $u-main-color !default;
  239. $u-car-keyboard-line-margin: 0 1px !default;
  240. $u-car-keyboard-u-hover-class-background-color: #BBBCC6 !default;
  241. .u-keyboard {
  242. @include flex(column);
  243. justify-content: space-around;
  244. background-color: $u-car-keyboard-background-color;
  245. align-items: stretch;
  246. padding: $u-car-keyboard-padding;
  247. &__button {
  248. @include flex;
  249. justify-content: center;
  250. flex: 1;
  251. /* #ifndef APP-NVUE */
  252. /* #endif */
  253. &__inner-wrapper {
  254. box-shadow: $u-car-keyboard-button-inner-box-shadow;
  255. margin: $u-car-keyboard-button-inner-margin;
  256. border-radius: $u-car-keyboard-button-border-radius;
  257. &__inner {
  258. @include flex;
  259. justify-content: center;
  260. align-items: center;
  261. width: $u-car-keyboard-button-inner-width;
  262. background-color: $u-car-keyboard-button-inner-background-color;
  263. height: $u-car-keyboard-button-height;
  264. border-radius: $u-car-keyboard-button-border-radius;
  265. &__text {
  266. font-size: $u-car-keyboard-button-text-font-size;
  267. color: $u-car-keyboard-button-text-color;
  268. }
  269. }
  270. &__left,
  271. &__right {
  272. border-radius: $u-car-keyboard-button-border-radius;
  273. width: $u-car-keyboard-special-button-width;
  274. height: $u-car-keyboard-button-height;
  275. background-color: $u-car-keyboard-u-hover-class-background-color;
  276. @include flex;
  277. justify-content: center;
  278. align-items: center;
  279. box-shadow: $u-car-keyboard-button-inner-box-shadow;
  280. }
  281. &__left {
  282. &__line {
  283. font-size: $u-car-keyboard-line-font-size;
  284. color: $u-car-keyboard-line-color;
  285. margin: $u-car-keyboard-line-margin;
  286. }
  287. &__lang {
  288. font-size: $u-car-keyboard-lang-font-size;
  289. color: $u-car-keyboard-lang-color;
  290. &--active {
  291. color: $u-car-keyboard-active-color;
  292. }
  293. }
  294. }
  295. }
  296. }
  297. }
  298. .u-hover-class {
  299. background-color: $u-car-keyboard-u-hover-class-background-color;
  300. }
  301. </style>