u-code-input.vue 7.1 KB

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