u-code-input.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <view class="u-code-input">
  3. <view
  4. class="u-code-input__item"
  5. :style="[itemStyle(index)]"
  6. v-for="(item, index) in codeLength"
  7. :key="index"
  8. >
  9. <view
  10. class="u-code-input__item__dot"
  11. v-if="dot && codeArray.length > index"
  12. ></view>
  13. <text
  14. v-else
  15. :style="{
  16. fontSize: $u.addUnit(fontSize),
  17. fontWeight: bold ? 'bold' : 'normal',
  18. color: color
  19. }"
  20. >{{codeArray[index]}}</text>
  21. <view
  22. class="u-code-input__item__line"
  23. v-if="mode === 'line'"
  24. :style="[lineStyle]"
  25. ></view>
  26. <!-- #ifndef APP-PLUS -->
  27. <view v-if="isFocus && codeArray.length === index" :style="{backgroundColor: color}" class="u-code-input__item__cursor"></view>
  28. <!-- #endif -->
  29. </view>
  30. <input
  31. :disabled="disabledKeyboard"
  32. type="number"
  33. :focus="focus"
  34. :value="inputValue"
  35. :maxlength="maxlength"
  36. :adjustPosition="adjustPosition"
  37. class="u-code-input__input"
  38. @input="inputHandler"
  39. :style="{
  40. height: $u.addUnit(size)
  41. }"
  42. @focus="isFocus = true"
  43. @blur="isFocus = false"
  44. />
  45. </view>
  46. </template>
  47. <script>
  48. import props from './props.js';
  49. import mpMixin from '../../libs/mixin/mpMixin.js';
  50. import mixin from '../../libs/mixin/mixin.js';
  51. /**
  52. * CodeInput 验证码输入
  53. * @description 该组件一般用于验证用户短信验证码的场景,也可以结合uview-plus的键盘组件使用
  54. * @tutorial https://ijry.github.io/uview-plus/components/codeInput.html
  55. * @property {String | Number} maxlength 最大输入长度 (默认 6 )
  56. * @property {Boolean} dot 是否用圆点填充 (默认 false )
  57. * @property {String} mode 显示模式,box-盒子模式,line-底部横线模式 (默认 'box' )
  58. * @property {Boolean} hairline 是否细边框 (默认 false )
  59. * @property {String | Number} space 字符间的距离 (默认 10 )
  60. * @property {String | Number} value 预置值
  61. * @property {Boolean} focus 是否自动获取焦点 (默认 false )
  62. * @property {Boolean} bold 字体和输入横线是否加粗 (默认 false )
  63. * @property {String} color 字体颜色 (默认 '#606266' )
  64. * @property {String | Number} fontSize 字体大小,单位px (默认 18 )
  65. * @property {String | Number} size 输入框的大小,宽等于高 (默认 35 )
  66. * @property {Boolean} disabledKeyboard 是否隐藏原生键盘,如果想用自定义键盘的话,需设置此参数为true (默认 false )
  67. * @property {String} borderColor 边框和线条颜色 (默认 '#c9cacc' )
  68. * @property {Boolean} disabledDot 是否禁止输入"."符号 (默认 true )
  69. *
  70. * @event {Function} change 输入内容发生改变时触发,具体见上方说明 value:当前输入的值
  71. * @event {Function} finish 输入字符个数达maxlength值时触发,见上方说明 value:当前输入的值
  72. * @example <u-code-input v-model="value4" :focus="true"></u-code-input>
  73. */
  74. export default {
  75. name: 'u-code-input',
  76. mixins: [mpMixin, mixin, props],
  77. data() {
  78. return {
  79. inputValue: '',
  80. isFocus: this.focus
  81. }
  82. },
  83. watch: {
  84. // #ifdef VUE2
  85. value: {
  86. // #endif
  87. // #ifdef VUE3
  88. modelValue: {
  89. // #endif
  90. immediate: true,
  91. handler(val) {
  92. // 转为字符串,超出部分截掉
  93. this.inputValue = String(val).substring(0, this.maxlength)
  94. }
  95. },
  96. },
  97. computed: {
  98. // 根据长度,循环输入框的个数,因为头条小程序数值不能用于v-for
  99. codeLength() {
  100. return new Array(Number(this.maxlength))
  101. },
  102. // 循环item的样式
  103. itemStyle() {
  104. return index => {
  105. const addUnit = uni.$u.addUnit
  106. const style = {
  107. width: addUnit(this.size),
  108. height: addUnit(this.size)
  109. }
  110. // 盒子模式下,需要额外进行处理
  111. if (this.mode === 'box') {
  112. // 设置盒子的边框,如果是细边框,则设置为0.5px宽度
  113. style.border = `${this.hairline ? 0.5 : 1}px solid ${this.borderColor}`
  114. // 如果盒子间距为0的话
  115. if (uni.$u.getPx(this.space) === 0) {
  116. // 给第一和最后一个盒子设置圆角
  117. if (index === 0) {
  118. style.borderTopLeftRadius = '3px'
  119. style.borderBottomLeftRadius = '3px'
  120. }
  121. if (index === this.codeLength.length - 1) {
  122. style.borderTopRightRadius = '3px'
  123. style.borderBottomRightRadius = '3px'
  124. }
  125. // 最后一个盒子的右边框需要保留
  126. if (index !== this.codeLength.length - 1) {
  127. style.borderRight = 'none'
  128. }
  129. }
  130. }
  131. if (index !== this.codeLength.length - 1) {
  132. // 设置验证码字符之间的距离,通过margin-right设置,最后一个字符,无需右边框
  133. style.marginRight = addUnit(this.space)
  134. } else {
  135. // 最后一个盒子的有边框需要保留
  136. style.marginRight = 0
  137. }
  138. return style
  139. }
  140. },
  141. // 将输入的值,转为数组,给item历遍时,根据当前的索引显示数组的元素
  142. codeArray() {
  143. return String(this.inputValue).split('')
  144. },
  145. // 下划线模式下,横线的样式
  146. lineStyle() {
  147. const style = {}
  148. style.height = this.hairline ? '2px' : '4px'
  149. style.width = uni.$u.addUnit(this.size)
  150. // 线条模式下,背景色即为边框颜色
  151. style.backgroundColor = this.borderColor
  152. return style
  153. }
  154. },
  155. emits: ["change", 'finish', "update:modelValue"],
  156. methods: {
  157. // 监听输入框的值发生变化
  158. inputHandler(e) {
  159. const value = e.detail.value
  160. this.inputValue = value
  161. // 是否允许输入“.”符号
  162. if(this.disabledDot) {
  163. this.$nextTick(() => {
  164. this.inputValue = value.replace('.', '')
  165. })
  166. }
  167. // 未达到maxlength之前,发送change事件,达到后发送finish事件
  168. this.$emit('change', value)
  169. // 修改通过v-model双向绑定的值
  170. // #ifdef VUE3
  171. this.$emit("update:modelValue", value);
  172. // #endif
  173. // #ifdef VUE2
  174. this.$emit("input", value);
  175. // #endif
  176. // 达到用户指定输入长度时,发出完成事件
  177. if (String(value).length >= Number(this.maxlength)) {
  178. this.$emit('finish', value)
  179. }
  180. }
  181. }
  182. }
  183. </script>
  184. <style lang="scss" scoped>
  185. @import "../../libs/css/components.scss";
  186. $u-code-input-cursor-width: 1px;
  187. $u-code-input-cursor-height: 40%;
  188. $u-code-input-cursor-animation-duration: 1s;
  189. $u-code-input-cursor-animation-name: u-cursor-flicker;
  190. .u-code-input {
  191. @include flex;
  192. position: relative;
  193. overflow: hidden;
  194. &__item {
  195. @include flex;
  196. justify-content: center;
  197. align-items: center;
  198. position: relative;
  199. &__text {
  200. font-size: 15px;
  201. color: $u-content-color;
  202. }
  203. &__dot {
  204. width: 7px;
  205. height: 7px;
  206. border-radius: 100px;
  207. background-color: $u-content-color;
  208. }
  209. &__line {
  210. position: absolute;
  211. bottom: 0;
  212. height: 4px;
  213. border-radius: 100px;
  214. width: 40px;
  215. background-color: $u-content-color;
  216. }
  217. /* #ifndef APP-PLUS */
  218. &__cursor {
  219. position: absolute;
  220. top: 50%;
  221. left: 50%;
  222. transform: translate(-50%,-50%);
  223. width: $u-code-input-cursor-width;
  224. height: $u-code-input-cursor-height;
  225. animation: $u-code-input-cursor-animation-duration u-cursor-flicker infinite;
  226. }
  227. /* #endif */
  228. }
  229. &__input {
  230. // 之所以需要input输入框,是因为有它才能唤起键盘
  231. // 这里将它设置为两倍的屏幕宽度,再将左边的一半移出屏幕,为了不让用户看到输入的内容
  232. position: absolute;
  233. left: -750rpx;
  234. width: 1500rpx;
  235. top: 0;
  236. background-color: transparent;
  237. text-align: left;
  238. }
  239. }
  240. /* #ifndef APP-PLUS */
  241. @keyframes u-cursor-flicker {
  242. 0% {
  243. opacity: 0;
  244. }
  245. 50% {
  246. opacity: 1;
  247. }
  248. 100% {
  249. opacity: 0;
  250. }
  251. }
  252. /* #endif */
  253. </style>