IPInputBox.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <div class="ip-box" :class="{ disabled: props.disabled }">
  3. <el-input
  4. ref="input1Ref"
  5. v-model="ip1"
  6. :disabled="oDisabled"
  7. maxlength="3"
  8. name="ip"
  9. onkeyup="value=value.replace(/[^\d]/g,'')"
  10. @blur="submitIp"
  11. @input="(val) => checkVal(val, 'ip1')"
  12. />
  13. <div class="ip-dot" />
  14. <el-input
  15. ref="input2Ref"
  16. v-model="ip2"
  17. :disabled="oDisabled"
  18. maxlength="3"
  19. name="ip"
  20. onkeyup="value=value.replace(/[^\d]/g,'')"
  21. @blur="submitIp"
  22. @input="(val) => checkVal(val, 'ip2')"
  23. />
  24. <div class="ip-dot" />
  25. <el-input
  26. ref="input3Ref"
  27. v-model="ip3"
  28. :disabled="oDisabled"
  29. maxlength="3"
  30. name="ip"
  31. onkeyup="value=value.replace(/[^\d]/g,'')"
  32. @blur="submitIp"
  33. @input="(val) => checkVal(val, 'ip3')"
  34. />
  35. <div class="ip-dot" />
  36. <el-input
  37. ref="input4Ref"
  38. v-model="ip4"
  39. :disabled="oDisabled"
  40. maxlength="3"
  41. name="ip"
  42. onkeyup="value=value.replace(/[^\d]/g,'')"
  43. @blur="submitIp"
  44. @input="(val) => checkVal(val, 'ip4')"
  45. />
  46. </div>
  47. </template>
  48. <script lang="ts" setup>
  49. import { ref } from 'vue'
  50. import { ElInput } from 'element-plus'
  51. interface Props {
  52. ipVal: string | undefined
  53. disabled: number | undefined
  54. }
  55. const props = defineProps<Props>()
  56. const emit = defineEmits<{
  57. 'update:ipVal': [string]
  58. }>()
  59. const oDisabled = ref()
  60. watch(
  61. () => props.disabled,
  62. (newValue) => {
  63. oDisabled.value = Number(newValue)
  64. }
  65. )
  66. const ip1 = ref()
  67. const ip2 = ref()
  68. const ip3 = ref()
  69. const ip4 = ref()
  70. watch(
  71. () => props.ipVal,
  72. (newValue) => {
  73. const ip = newValue?.match(/\d+/g)
  74. if (ip) {
  75. ip1.value = ip[0]
  76. ip2.value = ip[1]
  77. ip3.value = ip[2]
  78. ip4.value = ip[3]
  79. }
  80. }
  81. )
  82. const input1Ref = ref<InstanceType<typeof ElInput>>()
  83. const input2Ref = ref<InstanceType<typeof ElInput>>()
  84. const input3Ref = ref<InstanceType<typeof ElInput>>()
  85. const input4Ref = ref<InstanceType<typeof ElInput>>()
  86. const submitIp = () => {
  87. if (ip1.value && ip2.value && ip3.value && ip4.value) {
  88. let ipVal = ip1.value + '.' + ip2.value + '.' + ip3.value + '.' + ip4.value
  89. emit('update:ipVal', ipVal)
  90. } else {
  91. emit('update:ipVal', '')
  92. }
  93. }
  94. const checkVal = (val: string, key: string) => {
  95. if (parseInt(val) > 255) {
  96. switch (key) {
  97. case 'ip1':
  98. ip1.value = 255
  99. focusInput(2)
  100. break
  101. case 'ip2':
  102. ip2.value = 255
  103. focusInput(3)
  104. break
  105. case 'ip3':
  106. ip3.value = 255
  107. focusInput(4)
  108. break
  109. case 'ip4':
  110. ip4.value = 255
  111. break
  112. default:
  113. break
  114. }
  115. } else if (parseInt(val) <= 255 && parseInt(val) >= 100) {
  116. //输入三位数自动跳到下一个输入值
  117. switch (key) {
  118. case 'ip1':
  119. focusInput(2)
  120. break
  121. case 'ip2':
  122. focusInput(3)
  123. break
  124. case 'ip3':
  125. focusInput(4)
  126. break
  127. default:
  128. break
  129. }
  130. }
  131. }
  132. const focusInput = (key: number) => {
  133. switch (key) {
  134. case 2:
  135. input2Ref.value?.focus()
  136. break
  137. case 3:
  138. input3Ref.value?.focus()
  139. break
  140. case 4:
  141. input4Ref.value?.focus()
  142. break
  143. default:
  144. break
  145. }
  146. }
  147. </script>
  148. <style lang="scss" scoped>
  149. .ip-box {
  150. width: 210px;
  151. border: 1px solid #d9d9d9;
  152. border-radius: 20px;
  153. height: 32px;
  154. display: flex;
  155. :deep(.el-input) {
  156. width: 50px;
  157. }
  158. :deep(.el-input__inner) {
  159. border: 0 !important;
  160. }
  161. :deep(.el-input__wrapper) {
  162. box-shadow: none;
  163. background-color: unset;
  164. }
  165. :deep(.el-form-item.is-error) {
  166. box-shadow: none;
  167. }
  168. }
  169. .ip-dot {
  170. display: inline-block;
  171. width: 4px;
  172. height: 4px;
  173. border-radius: 50%;
  174. background-color: #000000;
  175. display: flex;
  176. place-self: center;
  177. }
  178. .disabled {
  179. background-color: #f5f7fa;
  180. }
  181. </style>