u-car-keyboard.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. >中</text>
  27. <text class="u-keyboard__button__inner-wrapper__left__line">/</text>
  28. <text
  29. class="u-keyboard__button__inner-wrapper__left__lang"
  30. :class="[abc && 'u-keyboard__button__inner-wrapper__left__lang--active']"
  31. >英</text>
  32. </view>
  33. </view>
  34. <view
  35. class="u-keyboard__button__inner-wrapper"
  36. v-for="(item, j) in group"
  37. :key="j"
  38. >
  39. <view
  40. class="u-keyboard__button__inner-wrapper__inner"
  41. :hover-stay-time="200"
  42. @tap="carInputClick(i, j)"
  43. hover-class="u-hover-class"
  44. >
  45. <text class="u-keyboard__button__inner-wrapper__inner__text">{{ item }}</text>
  46. </view>
  47. </view>
  48. <view
  49. v-if="i === 3"
  50. @touchstart="backspaceClick"
  51. @touchend="clearTimer"
  52. class="u-keyboard__button__inner-wrapper"
  53. >
  54. <view
  55. class="u-keyboard__button__inner-wrapper__right"
  56. hover-class="u-hover-class"
  57. :hover-stay-time="200"
  58. >
  59. <u-icon
  60. size="28"
  61. name="backspace"
  62. color="#303133"
  63. ></u-icon>
  64. </view>
  65. </view>
  66. </view>
  67. </view>
  68. </template>
  69. <script>
  70. import props from './props.js';
  71. import mpMixin from '../../libs/mixin/mpMixin.js';
  72. import mixin from '../../libs/mixin/mixin.js';
  73. /**
  74. * keyboard 键盘组件
  75. * @description 此为uView自定义的键盘面板,内含了数字键盘,车牌号键,身份证号键盘3种模式,都有可以打乱按键顺序的选项。
  76. * @tutorial https://uiadmin.net/uview-plus/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: [mpMixin, 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. emits: ["change", "backspace"],
  190. methods: {
  191. // 点击键盘按钮
  192. carInputClick(i, j) {
  193. let value = '';
  194. // 不同模式,获取不同数组的值
  195. if (this.abc) value = this.engKeyBoardList[i][j];
  196. else value = this.areaList[i][j];
  197. // 如果允许自动切换,则将中文状态切换为英文
  198. if (!this.abc && this.autoChange) uni.$u.sleep(200).then(() => this.abc = true)
  199. this.$emit('change', value);
  200. },
  201. // 修改汽车牌键盘的输入模式,中文|英文
  202. changeCarInputMode() {
  203. this.abc = !this.abc;
  204. },
  205. // 点击退格键
  206. backspaceClick() {
  207. this.$emit('backspace');
  208. clearInterval(this.timer); //再次清空定时器,防止重复注册定时器
  209. this.timer = null;
  210. this.timer = setInterval(() => {
  211. this.$emit('backspace');
  212. }, 250);
  213. },
  214. clearTimer() {
  215. clearInterval(this.timer);
  216. this.timer = null;
  217. },
  218. }
  219. };
  220. </script>
  221. <style lang="scss" scoped>
  222. @import "../../libs/css/components.scss";
  223. $u-car-keyboard-background-color: rgb(224, 228, 230) !default;
  224. $u-car-keyboard-padding:6px 0 6px !default;
  225. $u-car-keyboard-button-inner-width:64rpx !default;
  226. $u-car-keyboard-button-inner-background-color:#FFFFFF !default;
  227. $u-car-keyboard-button-height:80rpx !default;
  228. $u-car-keyboard-button-inner-box-shadow:0 1px 0px #999992 !default;
  229. $u-car-keyboard-button-border-radius:4px !default;
  230. $u-car-keyboard-button-inner-margin:8rpx 5rpx !default;
  231. $u-car-keyboard-button-text-font-size:16px !default;
  232. $u-car-keyboard-button-text-color:$u-main-color !default;
  233. $u-car-keyboard-center-inner-margin: 0 4rpx !default;
  234. $u-car-keyboard-special-button-width:134rpx !default;
  235. $u-car-keyboard-lang-font-size:16px !default;
  236. $u-car-keyboard-lang-color:$u-main-color !default;
  237. $u-car-keyboard-active-color:$u-primary !default;
  238. $u-car-keyboard-line-font-size:15px !default;
  239. $u-car-keyboard-line-color:$u-main-color !default;
  240. $u-car-keyboard-line-margin:0 1px !default;
  241. $u-car-keyboard-u-hover-class-background-color:#BBBCC6 !default;
  242. .u-keyboard {
  243. @include flex(column);
  244. justify-content: space-around;
  245. background-color: $u-car-keyboard-background-color;
  246. align-items: stretch;
  247. padding: $u-car-keyboard-padding;
  248. &__button {
  249. @include flex;
  250. justify-content: center;
  251. flex: 1;
  252. /* #ifndef APP-NVUE */
  253. /* #endif */
  254. &__inner-wrapper {
  255. box-shadow: $u-car-keyboard-button-inner-box-shadow;
  256. margin: $u-car-keyboard-button-inner-margin;
  257. border-radius: $u-car-keyboard-button-border-radius;
  258. &__inner {
  259. @include flex;
  260. justify-content: center;
  261. align-items: center;
  262. width: $u-car-keyboard-button-inner-width;
  263. background-color: $u-car-keyboard-button-inner-background-color;
  264. height: $u-car-keyboard-button-height;
  265. border-radius: $u-car-keyboard-button-border-radius;
  266. &__text {
  267. font-size: $u-car-keyboard-button-text-font-size;
  268. color: $u-car-keyboard-button-text-color;
  269. }
  270. }
  271. &__left,
  272. &__right {
  273. border-radius: $u-car-keyboard-button-border-radius;
  274. width: $u-car-keyboard-special-button-width;
  275. height: $u-car-keyboard-button-height;
  276. background-color: $u-car-keyboard-u-hover-class-background-color;
  277. @include flex;
  278. justify-content: center;
  279. align-items: center;
  280. box-shadow: $u-car-keyboard-button-inner-box-shadow;
  281. }
  282. &__left {
  283. &__line {
  284. font-size: $u-car-keyboard-line-font-size;
  285. color: $u-car-keyboard-line-color;
  286. margin: $u-car-keyboard-line-margin;
  287. }
  288. &__lang {
  289. font-size: $u-car-keyboard-lang-font-size;
  290. color: $u-car-keyboard-lang-color;
  291. &--active {
  292. color: $u-car-keyboard-active-color;
  293. }
  294. }
  295. }
  296. }
  297. }
  298. }
  299. .u-hover-class {
  300. background-color: $u-car-keyboard-u-hover-class-background-color;
  301. }
  302. </style>