uv-code-input.vue 8.7 KB

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