u-message-input.vue 7.7 KB

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