Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. <template>
  2. <div class="input-group">
  3.  
  4. <input
  5. v-for="(item, index) in countInput" :key="item.toString()"
  6. type="text"
  7. :maxlength="maxlength"
  8. @click="onClick(index)"
  9. @input="onInput($event)"
  10. @keyup="keymonitor"
  11. @blur="onBlur"
  12. class="input"
  13. >
  14.  
  15. </div>
  16. </template>
  17.  
  18. <script>
  19. export default {
  20. name: "InputGroup",
  21. props: {
  22. countInput: {
  23. type: Number,
  24. default: 2,
  25. },
  26. maxlength: {
  27. type: Number,
  28. default: 2,
  29. },
  30. },
  31. data() {
  32. return {
  33. number: [],
  34. activeNum: '',
  35. elem: [],
  36. count: '',
  37. text: '',
  38. }
  39. },
  40. methods: {
  41. init() {
  42. window.addEventListener('mouseout', e => {
  43. if (this.activeNumTrue) {
  44. if (!e.toElement) {
  45. this.elem[this.activeNum].blur();
  46. this.activeNum = '';
  47. }
  48. }
  49. });
  50. this.elem = this.$el.querySelectorAll('.input');
  51. this.number = Array.apply(null, {length: this.countInput}).map(Number.call, Number);
  52. },
  53. onBlur() {
  54. this.activeNum = '';
  55. },
  56. keymonitor(e) {
  57. const key = e.key;
  58. const target = e.target;
  59. const val = target.value;
  60. const countNegative = this.activeNum - 1;
  61. if (key === 'Backspace') {
  62. if (this.number.indexOf(countNegative) !== -1) {
  63. if (target.selectionStart === 0) {
  64. val.length === 0 && this.onLeft();
  65. }
  66. }
  67. }
  68. },
  69. onClick(val) {
  70. this.activeNum = val;
  71. if (this.activeNumTrue) {
  72. this.elem[this.activeNum].select();
  73. }
  74. },
  75. onInput($event) {
  76. const target = $event.target;
  77. target.value.length === this.maxlength && this.onRight();
  78. },
  79. onLeft() {
  80. if (this.activeNumTrue) {
  81. this.count = this.activeNum -1;
  82. this.counter();
  83. }
  84. },
  85. onRight() {
  86. if (this.activeNumTrue) {
  87. this.count = this.activeNum +1;
  88. this.counter();
  89. }
  90. },
  91. counter() {
  92. const elem = this.elem;
  93. const count = this.count;
  94. if (this.number.indexOf(count) !== -1) {
  95. elem[this.activeNum].blur();
  96. elem[count].focus();
  97. elem[count].value.length && elem[count].select();
  98. this.activeNum = count;
  99. }
  100. }
  101. },
  102. computed: {
  103. activeNumTrue() {
  104. return typeof this.activeNum !== 'string'
  105. }
  106. },
  107. mounted() {
  108. this.init();
  109. }
  110. }
  111. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement