codecook-carnumber.vue 4.2 KB

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