u-message-input.vue 8.1 KB

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