u-code-input.vue 7.3 KB

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