UmessageInput.vue 8.5 KB

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