codecook-carnumber.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <view class="car-number">
  3. <view class="new-energy">新能源</view>
  4. <view class="wrap" @tap="focusHandler">
  5. <view
  6. :class="[
  7. 'cell',
  8. { last: index === length - 1 },
  9. { 'no-border': index === length - 1 || index === length - 2 },
  10. { active: index === current },
  11. ]"
  12. v-for="(val, index) in fill"
  13. :key="index"
  14. @tap.stop="focusHandler(index)"
  15. >
  16. <view class="val">{{ val }}</view>
  17. <view class="border"></view>
  18. </view>
  19. </view>
  20. <key-board
  21. v-if="focus"
  22. :type="kType"
  23. @on-delete="keyDeleteHandler"
  24. @on-input="keyInputHandler"
  25. @on-hide="keyHideHandler"
  26. ></key-board>
  27. </view>
  28. </template>
  29. <script>
  30. import KeyBoard from "../codecook-keyboard/codecook-keyboard.vue";
  31. export default {
  32. name: "CarNumber",
  33. components: {
  34. KeyBoard,
  35. },
  36. props: {
  37. value: {
  38. type: String,
  39. default: "",
  40. },
  41. length: {
  42. type: Number,
  43. default: 8,
  44. },
  45. },
  46. data() {
  47. return {
  48. focus: false,
  49. current: 0,
  50. fill: new Array(this.length).fill(""),
  51. };
  52. },
  53. computed: {
  54. kType() {
  55. return this.current === 0 ? "provinces" : "areas";
  56. },
  57. },
  58. watch: {
  59. // value(e) {
  60. // if (e.length == 7 || e.length == 8) {
  61. // this.fill = e.split("");
  62. // console.log(this.fill.length);
  63. // }
  64. // },
  65. fill(val) {
  66. console.log(val);
  67. this.$emit("input", val.join(""));
  68. this.$emit("change", val);
  69. },
  70. },
  71. methods: {
  72. setCarNumValue(e) {
  73. let fill = e.split("");
  74. // 如果是7位数的非新能源车辆则不足空位
  75. if (e.split("").length == 7) {
  76. fill.push("");
  77. }
  78. this.fill = fill;
  79. },
  80. focusHandler(index = 0) {
  81. this.focus = true;
  82. this.current = index;
  83. console.log(this.current);
  84. },
  85. keyDeleteHandler() {
  86. this.$set(this.fill, this.current, "");
  87. if (this.current <= 0) {
  88. return;
  89. }
  90. this.current -= 1;
  91. },
  92. keyInputHandler(key) {
  93. this.$set(this.fill, this.current, key);
  94. if (this.current >= this.length - 1) {
  95. return;
  96. }
  97. this.current += 1;
  98. },
  99. keyHideHandler() {
  100. this.focus = false;
  101. },
  102. },
  103. beforeMount() {
  104. if (this.value) {
  105. this.value.split("").forEach((key, index) => {
  106. if (index >= this.length) {
  107. return;
  108. }
  109. this.$set(this.fill, index, key);
  110. });
  111. this.current = Math.min(this.value.length, this.length - 1);
  112. }
  113. },
  114. mounted() {
  115. // this.focus = true;
  116. },
  117. };
  118. </script>
  119. <style scoped lang="less">
  120. .car-number {
  121. position: relative;
  122. width: 100%;
  123. .wrap {
  124. width: 100%;
  125. height: 100%;
  126. display: flex;
  127. justify-content: space-between;
  128. border: 1rpx solid #e6e6e6;
  129. box-sizing: border-box;
  130. border-radius: 8rpx;
  131. box-shadow: 0rpx 6rpx 6rpx 0rpx rgba(128, 128, 128, 0.1);
  132. padding: 15rpx 0;
  133. }
  134. .new-energy {
  135. font-size: 24rpx;
  136. color: #333232;
  137. margin-bottom: 13rpx;
  138. display: flex;
  139. flex-direction: row-reverse;
  140. padding-right: 5rpx;
  141. }
  142. .cell {
  143. box-sizing: border-box;
  144. padding: 14rpx 0;
  145. flex: 1;
  146. color: #666666;
  147. font-size: 34rpx;
  148. border-right: 1rpx solid #cccccc;
  149. text-align: center;
  150. border-bottom: 1rpx solid transparent;
  151. box-sizing: border-box;
  152. padding: 0 10rpx;
  153. display: flex;
  154. flex-direction: column;
  155. position: relative;
  156. height: 70rpx;
  157. .val {
  158. flex: 1;
  159. display: flex;
  160. align-items: center;
  161. justify-content: center;
  162. }
  163. .border {
  164. flex-shrink: 0;
  165. flex-grow: 0;
  166. height: 2rpx;
  167. background: transparent;
  168. width: 100%;
  169. }
  170. &.active {
  171. .border {
  172. background: #fe8525;
  173. }
  174. }
  175. &.no-border {
  176. border-right: none;
  177. }
  178. &.last:after {
  179. content: "";
  180. width: 100%;
  181. border: 2px solid #00ff00;
  182. border-radius: 8rpx;
  183. margin: -18rpx 0;
  184. position: absolute;
  185. top: 0;
  186. right: 0;
  187. bottom: 0;
  188. left: 0;
  189. box-sizing: border-box;
  190. }
  191. }
  192. }
  193. </style>