u-message-input.vue 8.2 KB

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