kuan-ip-input.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <view class="kuan-ip-input">
  3. <view class="inputBox">
  4. <input class="input" type="number" :focus="focus[0]" v-model="a" @confirm="next(1)" confirm-hold maxlength="3"/>
  5. <input class="input" type="number" :focus="focus[1]" v-model="b" @confirm="next(2)" confirm-hold maxlength="3"/>
  6. <input class="input" type="number" :focus="focus[2]" v-model="c" @confirm="next(3)" confirm-hold maxlength="3"/>
  7. <input class="input" type="number" :focus="focus[3]" v-model="d" maxlength="3"/>
  8. </view>
  9. <view class="pointBox">
  10. <input class="point" type="text" value="." disabled/>
  11. <input class="point" type="text" value="." disabled/>
  12. <input class="point" type="text" value="." disabled/>
  13. <input class="point" type="text" value="" disabled/>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. let init = false
  19. export default {
  20. props:{
  21. value:String
  22. },
  23. data() {
  24. return {
  25. version:"1.0.0",
  26. a:"",
  27. b:"",
  28. c:"",
  29. d:"",
  30. focus:[false,false,false,false]
  31. };
  32. },
  33. watch:{
  34. value:function(e){
  35. this.ip = this.value
  36. },
  37. a:function(e){
  38. this.autoInput(e,1)
  39. },
  40. b:function(e){
  41. this.autoInput(e,2)
  42. },
  43. c:function(e){
  44. this.autoInput(e,3)
  45. }
  46. },
  47. created(){
  48. this.ip = this.value
  49. },
  50. methods:{
  51. next(n){
  52. let arr = [false,false,false,false]
  53. arr[n] = true
  54. this.focus = arr
  55. },
  56. autoInput(e,n){
  57. if(e.length===3){
  58. this.next(n)
  59. }
  60. }
  61. },
  62. computed:{
  63. ip:{
  64. get:function(){
  65. const {a,b,c,d} = this
  66. const ip = `${a}.${b}.${c}.${d}`
  67. if(init){
  68. this.$emit('input',ip);
  69. }
  70. init = true
  71. return ip
  72. },
  73. set:function(newValue){
  74. let arr = newValue.split(".")
  75. if(arr.length === 4){
  76. const [a,b,c,d] = arr
  77. Object.assign(this,{a,b,c,d})
  78. }else{
  79. if(newValue){
  80. throw Error("IP格式异常,赋值失败")
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. </script>
  88. <style lang="scss" scoped>
  89. .row-center{
  90. display: flex;
  91. flex-direction: row;
  92. align-items: center;
  93. }
  94. .kuan-ip-input{
  95. width: 100%;
  96. height: 100%;
  97. border: 1px solid #007AFF;
  98. border-radius: 3px;
  99. display: flex;
  100. flex-direction: row;
  101. position: relative;
  102. .inputBox{
  103. width: 100%;
  104. height: 100%;
  105. z-index: 10;
  106. @extend .row-center;
  107. input{
  108. text-align: center;
  109. }
  110. }
  111. .pointBox{
  112. z-index: 9;
  113. position: absolute;
  114. width: 100%;
  115. height: 100%;
  116. left: 0;
  117. top: 0;
  118. @extend .row-center;
  119. input{
  120. flex: 1;
  121. text-align: right;
  122. }
  123. }
  124. }
  125. </style>