haoxuan
2023-10-24 01cea4bc73210e43f748d82a02a432cce615be2d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
93
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
 
<template>
  <div class="input-box">
   
    <div
      class="input-content"
      @keydown="keydown"
      @keyup="keyup"
      @paste="paste"
      @mousewheel="mousewheel"
      @input="inputEvent"
      @compositionend="compositionend"
      v-for="(item, index) in codeList"
    >
      <input
        max="9"
        min="0"
        v-for="ele in item"
        maxlength="1"
        :data-index="ele"
        ref="firstinput"
        v-model.trim.number="input[ele]"
        :disabled="disabled"
      />
      <span
        style="
          height: 100%;
          display: inline-block;
          line-height: 22px;
          width: 22px;
          text-align: center;
        "
        class="el-icon-minus"
        v-if="index - (codeList.length - 1)"
      ></span>
      <!-- <input
        max="9"
        min="0"
        maxlength="1"
        data-index="1"
        v-model.trim.number="input[1]"
        type="number"
      />
     
      <input
        max="9"
        min="0"
        maxlength="1"
        data-index="5"
        v-model.trim.number="input[5]"
        type="number"
      /> -->
    </div>
  </div>
</template>
 
<script>
 
export default {
  data() {
    return {
      // 存放粘贴进来的数字
      pasteResult: [],
      inputList: [],
      codeList: [],
      // input: this.inputValue || [],
    };
  },
  props: ["code", "codenumer", "sum", "disabled", "del", "inputValue"],
  computed: {
    input() {
      // code 是父组件传进来的默认值,必须是6位长度的数组,这里就不再做容错判断处理
      // 最后空数组是默认值
      // return this.code || this.pasteResult.length === 6
      //   ? this.pasteResult
      //   : ["", "", "", "", "", ""];
      if(this.inputValue){
        return this.inputValue
      }else{
        return this.code || this.pasteResult.length === 6 ? this.pasteResult : [];
      }
      
    },
    // input: {
    //   get () {
    //     if (this.visible) {
    //       // 注册全局监听事件 [ 目前只考虑鼠标解除触发 ]
    //       window.addEventListener('mousedown', this.watchContextmenu)
    //     }
    //     return this.visible
    //   },
    //   set (newVal) {
    //     this.$emit('update:visible', newVal)
    //   }
    // },
  },
  methods: {
    compositionend(e){
      e.preventDefault()
      this.batchInsert(e.data,e.target)
    },
    // 解决一个输入框输入多个字符
    inputEvent(e) {
      var index = e.target.dataset.index * 1;
      var el = e.target;
      this.$set(this.input, index, el.value.slice(0, 1));
    },
    keydown(e) {
      var index = e.target.dataset.index * 1;
      var el = e.target;
      if (e.key === "Backspace") {
        if (this.input[index]&&this.input[index].length > 0) {
          this.$set(this.input, index, "");
        } else {
          if (el.previousElementSibling) {
            el.previousElementSibling.focus();
            this.$set(this.input, index - 1, "");
          }
        }
      } else if (e.key === "Delete") {
        if (this.input[index]&&this.input[index].length > 0) {
          this.$set(this.input, index, "");
        } else {
          if (el.nextElementSibling) {
            this.$set(this.input, (index = 1), "");
          }
        }
        if (el.nextElementSibling) {
          el.nextElementSibling.focus();
        }
      } else if (e.key === "Home") {
        el.parentElement.children[0] && el.parentElement.children[0].focus();
      } else if (e.key === "End") {
        el.parentElement.children[this.input.length - 1] &&
          el.parentElement.children[this.input.length - 1].focus();
      } else if (e.key === "ArrowLeft") {
        if (el.previousElementSibling) {
          el.previousElementSibling.focus();
        }
      } else if (e.key === "ArrowRight") {
        if (el.nextElementSibling) {
          el.nextElementSibling.focus();
        }
      } else if (e.key === "ArrowUp") {
        if (this.input[index] * 1 < 9) {
          this.$set(this.input, index, (this.input[index] * 1 + 1).toString());
        }
      } else if (e.key === "ArrowDown") {
        if (this.input[index] * 1 > 0) {
          this.$set(this.input, index, (this.input[index] * 1 - 1).toString());
        }
      }
    },
    keyup(e) {
      if (e.ctrlKey || e.shiftKey){
        // 忽略组合键时的字符输入
        return;
      }
      if (e.isComposing || e.keyCode === 229) {
        // 忽略输入法合成事件 忽略IME加工过的值
        return;
      }
      var index = e.target.dataset.index * 1;
      var el = e.target;
      // console.log(this.input);
      this.$emit("codeList", this.input);
      if (/Digit|Numpad|Key/i.test(e.code)) {
        // this.$set(this.input, index, e.code.replace(/Digit|Numpad|Key/i, ""));
        this.$set(this.input, index, e.key);
        el.nextElementSibling && el.nextElementSibling.focus();
        if (index === 5) {
          let number=0
          if(this.codenumer&&this.codenumer.length>0){
            for(let i in this.codenumer){
              number=number+Number(this.codenumer[i])
            }
          }
          console.log(this.pasteResult,'===keyup');
          if (this.input.join("").length === number) {
            document.activeElement.blur();
            this.$emit("complete", this.input);
          }
        }
      } else {
        if (this.input[index] === "") {
          this.$set(this.input, index, "");
        }
      }
    },
    mousewheel(e) {
      // 防止触发外部滚动条
      e.preventDefault()
      var index = e.target.dataset.index;
      if (e.wheelDelta > 0) {
        if (this.input[index] * 1 < 9) {
          this.$set(this.input, index, (this.input[index] * 1 + 1).toString());
        }
      } else if (e.wheelDelta < 0) {
        if (this.input[index] * 1 > 0) {
          this.$set(this.input, index, (this.input[index] * 1 - 1).toString());
        }
      } else if (e.key === "Enter") {
        if (this.input.join("").length === 6) {
          document.activeElement.blur();
          this.$emit("complete", this.input);
        }
      }
    },
    /**
     * 批量添加字符
     * @param str 字符串
     * @param currentInputElement 当前输入的input元素
     */
    batchInsert(str,currentInputElement){
      const charList = str.split('');
 
      let currentIndex = currentInputElement.dataset.index * 1;
 
      let activeInputElement =currentInputElement
      charList.forEach((ele,idx)=>{
        this.$set(this.input, currentIndex+idx, ele);
        activeInputElement = activeInputElement?.nextElementSibling
      })
      activeInputElement?.focus()
    },
    paste(e) {
      // 当进行粘贴时
      e.clipboardData.items[0].getAsString((str) => {
        // if (str.toString().length === 6) {
        //   this.pasteResult = str.split("");
        //   document.activeElement.blur();
        //   this.$emit("complete", this.input);
        // }
 
        this.batchInsert(str,e.target)
        this.$emit("complete", this.input);
      });
    },
    save() {
      this.inputList = []
      var arrlist = [];
      var list = [];
      if(this.codenumer&&this.codenumer.length>0){
        this.codenumer.forEach(item=>{
          this.inputList.push(item);
        })
        this.inputList.forEach((item, index) => {
          var arr = [];
          var x = list.length;
          var y = list.length + item;
          for (let index = x; index < y; index++) {
            arr.push(index);
            list.push(index);
          }
          arrlist.push(arr);
        });
      }
      this.codeList = arrlist;
      // console.log(this.codeList);
    },
    delcode(val) {
      this.codeList.splice(val, 1);
      this.inputList.splice(val, 1);
    },
  },
  watch: {
    codenumer(val) {
      console.log(val);
      this.save();
    },
    sum(val) {
      if (val == 0) {
        this.codeList.splice(this.del, 1);
        this.inputList.splice(this.del, 1);
      }
      //使用定时器防止删除的时候执行
      setTimeout(() => {
        // 防止连续输入相同个数时格子不增加;
        if (this.inputList.length < val) {
          console.log(val, "sum");
          this.save();
        }
      }, 200);
      // console.log(this.inputList.length, val);
      // console.log("数组长度", "输入次数");
    },
    del(val) {},
  },
  created() {
    this.save();
  },
 
  mounted() {
    // 等待dom渲染完成,在执行focus,否则无法获取到焦点
    // this.$nextTick(() => {
    //   this.$refs.firstinput.focus();
    // });
    // console.log(this.inputList);
  },
};
</script>
 
<style lang="scss" scoped>
.input-box {
  // display: table-cell;
  display: inline-block;
  .input-content {
    // width: 512px;
    // height: 32px;
    // display: flex;
    // align-items: center;
    // justify-content: space-between;
    display: inline-block;
    margin-right: 0px;
    line-height:28px;
    input {
      color: inherit;
      font-family: inherit;
      border: 0;
      outline: 0;
      border-bottom: 1px solid #919191;
      height: 22px;
      width: 22px;
      font-size:18px;
      text-align: center;
      border: #919191 1px solid;
      margin: 2px 3px;
      box-sizing: border-box;
    }
  }
  input::-webkit-outer-spin-button,
  input::-webkit-inner-spin-button {
    appearance: none;
    margin: 0;
  }
}
</style>