ip-input.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <view class="ip-input-content">
  3. <view class="ip-input-group">
  4. <view class="ip-input">
  5. <ip-fragment ref="focus0" :fontSize="fontSize" :validate="checkIpFragment" @returned="onReturned(0)"
  6. @complete="onComplete(0)" :value="IP.ip0" @updateValue="IP.ip0=$event" :max-length="3"
  7. :focus="focus.wrapper.focus0" @onFocus="focus.wrapper.focus0=$event" font-size="24px">
  8. </ip-fragment>
  9. </view>
  10. <view class="ip-dot">
  11. <text>.</text>
  12. </view>
  13. <view class="ip-input">
  14. <ip-fragment ref="focus1" :fontSize="fontSize" :validate="checkIpFragment" @returned="onReturned(1)"
  15. @complete="onComplete(1)" :value="IP.ip1" @updateValue="IP.ip1=$event" :max-length="3"
  16. :focus="focus.wrapper.focus1" @onFocus="focus.wrapper.focus1=$event" font-size="24px">
  17. </ip-fragment>
  18. </view>
  19. <view class="ip-dot">
  20. <text>.</text>
  21. </view>
  22. <view class="ip-input">
  23. <ip-fragment ref="focus2" :fontSize="fontSize" :validate="checkIpFragment" @returned="onReturned(2)"
  24. @complete="onComplete(2)" :value="IP.ip2" @updateValue="IP.ip2=$event" :max-length="3"
  25. :focus="focus.wrapper.focus2" @onFocus="focus.wrapper.focus2=$event" font-size="24px">
  26. </ip-fragment>
  27. </view>
  28. <view class="ip-dot">
  29. <text>.</text>
  30. </view>
  31. <view class="ip-input">
  32. <ip-fragment ref="focus3" :fontSize="fontSize" :validate="checkIpFragment" @returned="onReturned(3)"
  33. @complete="onComplete(3)" :value="IP.ip3" @updateValue="IP.ip3=$event" :max-length="3"
  34. :focus="focus.wrapper.focus3" @onFocus="focus.wrapper.focus3=$event" font-size="24px">
  35. </ip-fragment>
  36. </view>
  37. </view>
  38. <view class="keyborder-container" v-show="needKeyBoard" @click.stop>
  39. <view v-show="needKeyBoard">
  40. <key-board @typeCharacter="outLetCharacter" @typeComplete="onTypeComplete"></key-board>
  41. </view>
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. import KeyBoard from "./keyboard.vue";
  47. import Fragment from "./fragment.vue"
  48. export default {
  49. components: {
  50. "ip-fragment": Fragment,
  51. "key-board": KeyBoard
  52. },
  53. // #ifdef VUE3
  54. props: {
  55. "modelValue": {
  56. type: String,
  57. default: ""
  58. }
  59. },
  60. emits: {
  61. "update:modelValue": null
  62. },
  63. // #endif
  64. // #ifndef VUE3
  65. props: {
  66. "value": {
  67. type: String,
  68. default: ""
  69. },
  70. "fontSize": {
  71. type: String,
  72. default: "1rem"
  73. }
  74. },
  75. // #endif
  76. data() {
  77. return {
  78. IP: {
  79. ip0: "",
  80. ip1: "",
  81. ip2: "",
  82. ip3: "",
  83. },
  84. focus: {
  85. wrapper: {
  86. focus0: false,
  87. focus1: false,
  88. focus2: false,
  89. focus3: false,
  90. }
  91. }
  92. }
  93. },
  94. methods: {
  95. onInputFocus(e) {
  96. console.log("e", e);
  97. },
  98. checkIpFragment(str) {
  99. let r = parseInt(str);
  100. if (r > 255) {
  101. return false;
  102. }
  103. return true;
  104. },
  105. onComplete(index) {
  106. if (this.focus.wrapper[`focus${index+1}`] !== undefined)
  107. this.focus.wrapper[`focus${index+1}`] = true;
  108. },
  109. unFocusExcept(nodes, excludedKey) {
  110. let obj = {};
  111. for (let key of nodes) {
  112. if (key !== excludedKey) {
  113. obj[key] = false;
  114. }
  115. }
  116. obj[excludedKey] = true;
  117. return obj;
  118. },
  119. outLetCharacter(character) {
  120. const obj = this.focus.wrapper;
  121. const keys = Object.keys(obj);
  122. for (let key of keys) {
  123. if (this.focus.wrapper[key] === true) {
  124. this.$refs[key].inPut(character);
  125. break;
  126. }
  127. }
  128. },
  129. onTypeComplete() {
  130. const obj = this.focus.wrapper;
  131. const keys = Object.keys(obj);
  132. for (let key of keys) {
  133. if (this.focus.wrapper[key] === true) {
  134. this.$refs[key].onComplete();
  135. break;
  136. }
  137. }
  138. },
  139. onReturned(index) {
  140. if (this.focus.wrapper[`focus${index-1}`] !== undefined)
  141. this.focus.wrapper[`focus${index-1}`] = true;
  142. }
  143. },
  144. watch: {
  145. "focus.wrapper.focus0": {
  146. handler(n, o) {
  147. if (n === true) {
  148. let r = this.unFocusExcept(["focus0", "focus1", "focus2", "focus3"], "focus0");
  149. this.focus.wrapper = r;
  150. }
  151. }
  152. },
  153. "focus.wrapper.focus1": {
  154. handler(n, o) {
  155. if (n === true) {
  156. let r = this.unFocusExcept(["focus0", "focus1", "focus2", "focus3"], "focus1");
  157. this.focus.wrapper = r;
  158. }
  159. }
  160. },
  161. "focus.wrapper.focus2": {
  162. handler(n, o) {
  163. if (n === true) {
  164. let r = this.unFocusExcept(["focus0", "focus1", "focus2", "focus3"], "focus2");
  165. this.focus.wrapper = r;
  166. }
  167. }
  168. },
  169. "focus.wrapper.focus3": {
  170. handler(n, o) {
  171. if (n === true) {
  172. let r = this.unFocusExcept(["focus0", "focus1", "focus2", "focus3"], "focus3");
  173. this.focus.wrapper = r;
  174. }
  175. }
  176. },
  177. "IP": {
  178. handler(n, o) {
  179. if (this.IP.ip0 !== "" && this.IP.ip1 !== "" && this.IP.ip2 !== "" && this.IP.ip3 !== "") {
  180. const value = `${this.IP.ip0}.${this.IP.ip1}.${this.IP.ip2}.${this.IP.ip3}`
  181. // #ifdef VUE3
  182. this.$emit("update:modelValue", value);
  183. // #endif
  184. // #ifndef VUE3
  185. this.$emit("input", value);
  186. // #endif
  187. }
  188. },
  189. deep: true
  190. }
  191. },
  192. computed: {
  193. needKeyBoard() {
  194. return this.focus.wrapper.focus0 ||
  195. this.focus.wrapper.focus1 ||
  196. this.focus.wrapper.focus2 ||
  197. this.focus.wrapper.focus3;
  198. }
  199. }
  200. }
  201. </script>
  202. <style scoped lang="scss">
  203. .ip-input-content {
  204. width: 100%;
  205. height: 100%;
  206. .keyborder-container {
  207. position: fixed;
  208. bottom: 0px;
  209. left: 0px;
  210. right: 0px;
  211. z-index: 9999;
  212. border-top-left-radius: 16px;
  213. border-top-right-radius: 16px;
  214. overflow: hidden;
  215. }
  216. .ip-input-group {
  217. display: flex;
  218. justify-content: space-around;
  219. .ip-dot {
  220. width: 20rpx;
  221. display: flex;
  222. align-items: flex-end;
  223. }
  224. .ip-input {
  225. flex: 4;
  226. }
  227. }
  228. }
  229. </style>