myp-one.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <view class="code-box">
  3. <view class="flex-box">
  4. <input :value="inputValue" type="number" :focus="autoFocus" :maxlength="maxlength" class="hide-input" @input="getVal" />
  5. <block v-for="(item, index) in ranges" :key="index">
  6. <view :class="['item', { active: codeIndex === item, middle: type === 'middle', bottom: type === 'bottom', box: type === 'box' }]">
  7. <view class="line" v-if="type !== 'middle'"></view>
  8. <view v-if="type === 'middle' && codeIndex <= item" class="bottom-line"></view>
  9. <block v-if="isPwd && codeArr.length >= item"><text class="dot">●</text></block>
  10. <block v-else>
  11. <text class="number">{{ codeArr[index] ? codeArr[index] : '' }}</text>
  12. </block>
  13. </view>
  14. </block>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. // 支持使用 v-model
  20. // 支持使用refs
  21. export default {
  22. name: 'mypOneInput',
  23. props: {
  24. // 支持外部提供,支持使用v-model
  25. // 支持通过value来做清空
  26. value: {
  27. type: String,
  28. default: ''
  29. },
  30. // 4/6
  31. maxlength: {
  32. type: Number,
  33. default: 4
  34. },
  35. autoFocus: {
  36. type: Boolean,
  37. default: false
  38. },
  39. isPwd: {
  40. type: Boolean,
  41. default: false
  42. },
  43. // middle-middle line, bottom-bottom line, box-square box
  44. type: {
  45. type: String,
  46. default: 'bottom'
  47. }
  48. },
  49. watch: {
  50. maxlength: {
  51. immediate: true,
  52. handler: function(newV) {
  53. if (newV === 6) {
  54. this.ranges = [1, 2, 3, 4, 5, 6];
  55. } else {
  56. this.ranges = [1, 2, 3, 4];
  57. }
  58. }
  59. },
  60. value: {
  61. immediate: true,
  62. handler: function(newV) {
  63. if (newV !== this.inputValue) {
  64. this.inputValue = newV;
  65. this.toMakeAndCheck(newV);
  66. }
  67. }
  68. }
  69. },
  70. data() {
  71. return {
  72. inputValue: '',
  73. codeIndex: 1,
  74. codeArr: [],
  75. ranges: [1, 2, 3, 4]
  76. };
  77. },
  78. methods: {
  79. getVal(e) {
  80. const val = e.detail.value;
  81. this.inputValue = val;
  82. this.$emit('input', val);
  83. this.toMakeAndCheck(val);
  84. },
  85. toMakeAndCheck(val) {
  86. const arr = val.split('');
  87. this.codeIndex = arr.length + 1;
  88. this.codeArr = arr;
  89. if (this.codeIndex > Number(this.maxlength)) {
  90. this.$emit('finish', this.codeArr.join(''));
  91. }
  92. },
  93. // refs 时不再提供 v-model 支持
  94. // 支持使用refs来设置value
  95. // 没有提供数据保护与检测,自己在外面对数据进行检测保护
  96. set(val) {
  97. this.inputValue = val;
  98. this.toMakeAndCheck(val);
  99. },
  100. // 支持使用refs来清空
  101. clear() {
  102. this.inputValue = '';
  103. this.codeArr = [];
  104. this.codeIndex = 1;
  105. }
  106. }
  107. };
  108. </script>
  109. <style scoped>
  110. @keyframes twinkling {
  111. 0% {
  112. opacity: 0.2;
  113. }
  114. 50% {
  115. opacity: 0.5;
  116. }
  117. 100% {
  118. opacity: 0.2;
  119. }
  120. }
  121. .code-box {
  122. text-align: center;
  123. }
  124. .flex-box {
  125. display: flex;
  126. justify-content: center;
  127. flex-wrap: wrap;
  128. position: relative;
  129. }
  130. .flex-box .hide-input {
  131. position: absolute;
  132. top: 0;
  133. left: -100%;
  134. width: 200%;
  135. height: 100%;
  136. text-align: left;
  137. z-index: 9;
  138. opacity: 1;
  139. }
  140. .flex-box .item {
  141. position: relative;
  142. flex: 1;
  143. margin-right: 18rpx;
  144. font-size: 70rpx;
  145. font-weight: bold;
  146. color: #333333;
  147. line-height: 100rpx;
  148. }
  149. .flex-box .item::before {
  150. content: '';
  151. padding-top: 100%;
  152. display: block;
  153. }
  154. .flex-box .item:last-child {
  155. margin-right: 0;
  156. }
  157. .flex-box .middle {
  158. border: none;
  159. }
  160. .flex-box .box {
  161. box-sizing: border-box;
  162. border: 2rpx solid #cccccc;
  163. border-width: 2rpx 0 2rpx 2rpx;
  164. margin-right: 0;
  165. }
  166. .flex-box .box:first-of-type {
  167. border-top-left-radius: 8rpx;
  168. border-bottom-left-radius: 8rpx;
  169. }
  170. .flex-box .box:last-child {
  171. border-right: 2rpx solid #cccccc;
  172. border-top-right-radius: 8rpx;
  173. border-bottom-right-radius: 8rpx;
  174. }
  175. .flex-box .bottom {
  176. box-sizing: border-box;
  177. border-bottom: 1px solid #ddd;
  178. }
  179. .flex-box .active {
  180. border-color: #ddd;
  181. }
  182. .flex-box .active .line {
  183. display: block;
  184. }
  185. .flex-box .line {
  186. display: none;
  187. position: absolute;
  188. left: 50%;
  189. top: 50%;
  190. transform: translate(-50%, -50%);
  191. width: 2rpx;
  192. height: 40rpx;
  193. background: #333333;
  194. animation: twinkling 1s infinite ease;
  195. }
  196. .flex-box .dot,
  197. .flex-box .number {
  198. line-height: 40rpx;
  199. position: absolute;
  200. left: 50%;
  201. top: 50%;
  202. transform: translate(-50%, -50%);
  203. }
  204. .flex-box .bottom-line {
  205. height: 4px;
  206. background: #000000;
  207. width: 80%;
  208. position: absolute;
  209. border-radius: 2px;
  210. top: 50%;
  211. left: 50%;
  212. transform: translate(-50%, -50%);
  213. }
  214. </style>