u-code-input.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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的键盘组件使用
  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. value: {
  85. immediate: true,
  86. handler(val) {
  87. // 转为字符串,超出部分截掉
  88. this.inputValue = String(val).substring(0, this.maxlength)
  89. }
  90. },
  91. },
  92. computed: {
  93. // 根据长度,循环输入框的个数,因为头条小程序数值不能用于v-for
  94. codeLength() {
  95. return new Array(Number(this.maxlength))
  96. },
  97. // 循环item的样式
  98. itemStyle() {
  99. return index => {
  100. const addUnit = uni.$u.addUnit
  101. const style = {
  102. width: addUnit(this.size),
  103. height: addUnit(this.size)
  104. }
  105. // 盒子模式下,需要额外进行处理
  106. if (this.mode === 'box') {
  107. // 设置盒子的边框,如果是细边框,则设置为0.5px宽度
  108. style.border = `${this.hairline ? 0.5 : 1}px solid ${this.borderColor}`
  109. // 如果盒子间距为0的话
  110. if (uni.$u.getPx(this.space) === 0) {
  111. // 给第一和最后一个盒子设置圆角
  112. if (index === 0) {
  113. style.borderTopLeftRadius = '3px'
  114. style.borderBottomLeftRadius = '3px'
  115. }
  116. if (index === this.codeLength.length - 1) {
  117. style.borderTopRightRadius = '3px'
  118. style.borderBottomRightRadius = '3px'
  119. }
  120. // 最后一个盒子的右边框需要保留
  121. if (index !== this.codeLength.length - 1) {
  122. style.borderRight = 'none'
  123. }
  124. }
  125. }
  126. if (index !== this.codeLength.length - 1) {
  127. // 设置验证码字符之间的距离,通过margin-right设置,最后一个字符,无需右边框
  128. style.marginRight = addUnit(this.space)
  129. } else {
  130. // 最后一个盒子的有边框需要保留
  131. style.marginRight = 0
  132. }
  133. return style
  134. }
  135. },
  136. // 将输入的值,转为数组,给item历遍时,根据当前的索引显示数组的元素
  137. codeArray() {
  138. return String(this.inputValue).split('')
  139. },
  140. // 下划线模式下,横线的样式
  141. lineStyle() {
  142. const style = {}
  143. style.height = this.hairline ? '2px' : '4px'
  144. style.width = uni.$u.addUnit(this.size)
  145. // 线条模式下,背景色即为边框颜色
  146. style.backgroundColor = this.borderColor
  147. return style
  148. }
  149. },
  150. methods: {
  151. // 监听输入框的值发生变化
  152. inputHandler(e) {
  153. const value = e.detail.value
  154. this.inputValue = value
  155. // 是否允许输入“.”符号
  156. if(this.disabledDot) {
  157. this.$nextTick(() => {
  158. this.inputValue = value.replace('.', '')
  159. })
  160. }
  161. // 未达到maxlength之前,发送change事件,达到后发送finish事件
  162. this.$emit('change', value)
  163. // 修改通过v-model双向绑定的值
  164. this.$emit('input', value)
  165. // 达到用户指定输入长度时,发出完成事件
  166. if (String(value).length >= Number(this.maxlength)) {
  167. this.$emit('finish', value)
  168. }
  169. }
  170. }
  171. }
  172. </script>
  173. <style lang="scss" scoped>
  174. @import "../../libs/css/components.scss";
  175. $u-code-input-cursor-width: 1px;
  176. $u-code-input-cursor-height: 40%;
  177. $u-code-input-cursor-animation-duration: 1s;
  178. $u-code-input-cursor-animation-name: u-cursor-flicker;
  179. .u-code-input {
  180. @include flex;
  181. position: relative;
  182. overflow: hidden;
  183. &__item {
  184. @include flex;
  185. justify-content: center;
  186. align-items: center;
  187. position: relative;
  188. &__text {
  189. font-size: 15px;
  190. color: $u-content-color;
  191. }
  192. &__dot {
  193. width: 7px;
  194. height: 7px;
  195. border-radius: 100px;
  196. background-color: $u-content-color;
  197. }
  198. &__line {
  199. position: absolute;
  200. bottom: 0;
  201. height: 4px;
  202. border-radius: 100px;
  203. width: 40px;
  204. background-color: $u-content-color;
  205. }
  206. /* #ifndef APP-PLUS */
  207. &__cursor {
  208. position: absolute;
  209. top: 50%;
  210. left: 50%;
  211. transform: translate(-50%,-50%);
  212. width: $u-code-input-cursor-width;
  213. height: $u-code-input-cursor-height;
  214. animation: $u-code-input-cursor-animation-duration u-cursor-flicker infinite;
  215. }
  216. /* #endif */
  217. }
  218. &__input {
  219. // 之所以需要input输入框,是因为有它才能唤起键盘
  220. // 这里将它设置为两倍的屏幕宽度,再将左边的一半移出屏幕,为了不让用户看到输入的内容
  221. position: absolute;
  222. left: -750rpx;
  223. width: 1500rpx;
  224. top: 0;
  225. background-color: transparent;
  226. text-align: left;
  227. }
  228. }
  229. /* #ifndef APP-PLUS */
  230. @keyframes u-cursor-flicker {
  231. 0% {
  232. opacity: 0;
  233. }
  234. 50% {
  235. opacity: 1;
  236. }
  237. 100% {
  238. opacity: 0;
  239. }
  240. }
  241. /* #endif */
  242. </style>