u-message-input.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <view class="u-char-box">
  3. <view class="u-char-flex">
  4. <input :disabled="disabledKeyboard" :value="valueModel" type="number" :focus="focus" :maxlength="maxlength" class="u-input" @input="getVal"/>
  5. <view v-for="(item, index) in maxlength" :key="index">
  6. <view :class="[breathe && charArrLength == index ? 'u-breathe' : '', 'u-char-item',
  7. charArrLength === index && mode == 'box' ? 'u-box-active' : '',
  8. mode === 'box' ? 'u-box' : '']" :style="{
  9. fontWeight: bold ? 'bold' : 'normal',
  10. fontSize: fontSize + 'rpx',
  11. width: width + 'rpx',
  12. height: width + 'rpx',
  13. color: inactiveColor
  14. }">
  15. <view class="u-placeholder-line" :style="{
  16. display: charArrLength === index ? 'block' : 'none',
  17. height: width * 0.5 +'rpx'
  18. }"
  19. v-if="mode !== 'middleLine'"
  20. ></view>
  21. <view v-if="mode === 'middleLine' && charArrLength <= index" :class="[breathe && charArrLength == index ? 'u-breathe' : '', charArrLength === index ? 'u-middle-line-active' : '']"
  22. class="u-middle-line" :style="{height: bold ? '4px' : '2px', background: charArrLength === index ? activeColor : inactiveColor}"></view>
  23. <view v-if="mode === 'bottomLine'" :class="[breathe && charArrLength == index ? 'u-breathe' : '', charArrLength === index ? 'u-buttom-line-active' : '']"
  24. class="u-bottom-line" :style="{height: bold ? '4px' : '2px', background: charArrLength === index ? activeColor : inactiveColor}"></view>
  25. <block v-if="!dotFill"> {{ charArr[index] ? charArr[index] : ''}}</block>
  26. <block v-else>
  27. <text class="u-dot">{{ charArr[index] ? '●' : ''}}</text>
  28. </block>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. /**
  36. * messageInput 验证码输入框
  37. * @description 该组件一般用于验证用户短信验证码的场景,也可以结合uView的键盘组件使用
  38. * @tutorial https://www.uviewui.com/components/messageInput.html
  39. * @property {String Number} maxlength 输入字符个数(默认4)
  40. * @property {Boolean} dot-fill 是否用圆点填充(默认false)
  41. * @property {String} mode 模式选择,见上方"基本使用"说明(默认box)
  42. * @property {String Number} value 预置值
  43. * @property {Boolean} breathe 是否开启呼吸效果,见上方说明(默认true)
  44. * @property {Boolean} focus 是否自动获取焦点(默认false)
  45. * @property {Boolean} bold 字体和输入横线是否加粗(默认true)
  46. * @property {String Number} font-size 字体大小,单位rpx(默认60)
  47. * @property {String} active-color 当前激活输入框的样式(默认#2979ff)
  48. * @property {String} focus 非激活输入框的样式,文字颜色同此值(默认#606266)
  49. * @property {String | Number} width 输入框宽度,单位rpx,高等于宽(默认80)
  50. * @property {Boolean} disabled-keyboard 禁止点击输入框唤起系统键盘(默认false)
  51. * @event {Function} change 输入内容发生改变时触发,具体见官网说明
  52. * @event {Function} finish 输入字符个数达maxlength值时触发,见官网说明
  53. * @example <u-message-input mode="bottomLine"></u-message-input>
  54. */
  55. export default {
  56. name: "u-message-input",
  57. props: {
  58. // 最大输入长度
  59. maxlength: {
  60. type: [Number, String],
  61. default: 4
  62. },
  63. // 是否用圆点填充
  64. dotFill: {
  65. type: Boolean,
  66. default: false
  67. },
  68. // 显示模式,box-盒子模式,bottomLine-横线在底部模式,middleLine-横线在中部模式
  69. mode: {
  70. type: String,
  71. default: "box"
  72. },
  73. // 预置值
  74. value: {
  75. type: [String, Number],
  76. default: ''
  77. },
  78. // 当前激活输入item,是否带有呼吸效果
  79. breathe: {
  80. type: Boolean,
  81. default: true
  82. },
  83. // 是否自动获取焦点
  84. focus: {
  85. type: Boolean,
  86. default: false
  87. },
  88. // 字体是否加粗
  89. bold: {
  90. type: Boolean,
  91. default: false
  92. },
  93. // 字体大小
  94. fontSize: {
  95. type: [String, Number],
  96. default: 60
  97. },
  98. // 激活样式
  99. activeColor: {
  100. type: String,
  101. default: '#2979ff'
  102. },
  103. // 未激活的样式
  104. inactiveColor: {
  105. type: String,
  106. default: '#606266'
  107. },
  108. // 输入框的大小,单位rpx,宽等于高
  109. width: {
  110. type: [Number, String],
  111. default: '80'
  112. },
  113. // 是否隐藏原生键盘,如果想用自定义键盘的话,需设置此参数为true
  114. disabledKeyboard: {
  115. type: Boolean,
  116. default: false
  117. }
  118. },
  119. watch: {
  120. maxlength: {
  121. // 此值设置为true,会在组件加载后无需maxlength变化就会执行一次本监听函数,无需再created生命周期中处理
  122. immediate: true,
  123. handler(val) {
  124. this.maxlength = Number(val);
  125. }
  126. },
  127. value: {
  128. immediate: true,
  129. handler(val) {
  130. // 转为字符串
  131. val = String(val);
  132. // 超出部分截掉
  133. this.valueModel = val.substring(0, this.maxlength);
  134. }
  135. },
  136. },
  137. data() {
  138. return {
  139. valueModel: ""
  140. }
  141. },
  142. computed: {
  143. // 是否显示呼吸灯效果
  144. animationClass() {
  145. return (index) => {
  146. if (this.breathe && this.charArr.length == index) return 'u-breathe';
  147. else return '';
  148. }
  149. },
  150. // 用于显示字符
  151. charArr() {
  152. return this.valueModel.split('');
  153. },
  154. charArrLength() {
  155. return this.charArr.length;
  156. }
  157. },
  158. methods: {
  159. getVal(e) {
  160. let {
  161. value
  162. } = e.detail
  163. this.valueModel = value;
  164. // 判断长度是否超出了maxlength值,理论上不会发生,因为input组件设置了maxlength属性值
  165. if (String(value).length > this.maxlength) return;
  166. // 未达到maxlength之前,发送change事件,达到后发送finish事件
  167. this.$emit('change', value);
  168. if (String(value).length == this.maxlength) {
  169. this.$emit('finish', value);
  170. }
  171. }
  172. }
  173. }
  174. </script>
  175. <style scoped lang="scss">
  176. @keyframes breathe {
  177. 0% {
  178. opacity: 0.3;
  179. }
  180. 50% {
  181. opacity: 1;
  182. }
  183. 100% {
  184. opacity: 0.3;
  185. }
  186. }
  187. .u-char-box {
  188. text-align: center;
  189. }
  190. .u-char-flex {
  191. display: flex;
  192. justify-content: center;
  193. flex-wrap: wrap;
  194. position: relative;
  195. }
  196. .u-input {
  197. position: absolute;
  198. top: 0;
  199. left: -100%;
  200. width: 200%;
  201. height: 100%;
  202. text-align: left;
  203. z-index: 9;
  204. opacity: 0;
  205. background: none;
  206. }
  207. .u-char-item {
  208. position: relative;
  209. width: 90rpx;
  210. height: 90rpx;
  211. margin: 10rpx 10rpx;
  212. font-size: 60rpx;
  213. font-weight: bold;
  214. color: $u-main-color;
  215. line-height: 90rpx;
  216. display: flex;
  217. justify-content: center;
  218. align-items: center;
  219. }
  220. .u-middle-line {
  221. border: none;
  222. }
  223. .u-box {
  224. box-sizing: border-box;
  225. border: 2rpx solid #cccccc;
  226. border-radius: 6rpx;
  227. }
  228. .u-box-active {
  229. overflow: hidden;
  230. animation-timing-function: ease-in-out;
  231. animation-duration: 1500ms;
  232. animation-iteration-count: infinite;
  233. animation-direction: alternate;
  234. border: 2rpx solid $u-type-primary;
  235. }
  236. .u-middle-line-active {
  237. background: $u-type-primary;
  238. }
  239. .u-breathe {
  240. animation: breathe 2s infinite ease;
  241. }
  242. .u-placeholder-line {
  243. display: none;
  244. position: absolute;
  245. left: 50%;
  246. top: 50%;
  247. transform: translate(-50%, -50%);
  248. width: 2rpx;
  249. height: 40rpx;
  250. background: #333333;
  251. animation: twinkling 1.5s infinite ease;
  252. }
  253. .u-animation-breathe {
  254. animation-name: breathe;
  255. }
  256. .u-dot {
  257. font-size: 34rpx;
  258. line-height: 34rpx;
  259. }
  260. .u-middle-line {
  261. height: 4px;
  262. background: #000000;
  263. width: 80%;
  264. position: absolute;
  265. border-radius: 2px;
  266. top: 50%;
  267. left: 50%;
  268. transform: translate(-50%, -50%);
  269. }
  270. .u-buttom-line-active {
  271. background: $u-type-primary;
  272. }
  273. .u-bottom-line {
  274. height: 4px;
  275. background: #000000;
  276. width: 80%;
  277. position: absolute;
  278. border-radius: 2px;
  279. bottom: 0;
  280. left: 50%;
  281. transform: translate(-50%);
  282. }
  283. </style>