You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

305 lines
7.6 KiB

  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. @import "../../libs/css/style.components.scss";
  177. @keyframes breathe {
  178. 0% {
  179. opacity: 0.3;
  180. }
  181. 50% {
  182. opacity: 1;
  183. }
  184. 100% {
  185. opacity: 0.3;
  186. }
  187. }
  188. .u-char-box {
  189. text-align: center;
  190. }
  191. .u-char-flex {
  192. display: flex;
  193. justify-content: center;
  194. flex-wrap: wrap;
  195. position: relative;
  196. }
  197. .u-input {
  198. position: absolute;
  199. top: 0;
  200. left: -100%;
  201. width: 200%;
  202. height: 100%;
  203. text-align: left;
  204. z-index: 9;
  205. opacity: 0;
  206. background: none;
  207. }
  208. .u-char-item {
  209. position: relative;
  210. width: 90rpx;
  211. height: 90rpx;
  212. margin: 10rpx 10rpx;
  213. font-size: 60rpx;
  214. font-weight: bold;
  215. color: $u-main-color;
  216. line-height: 90rpx;
  217. display: flex;
  218. justify-content: center;
  219. align-items: center;
  220. }
  221. .u-middle-line {
  222. border: none;
  223. }
  224. .u-box {
  225. box-sizing: border-box;
  226. border: 2rpx solid #cccccc;
  227. border-radius: 6rpx;
  228. }
  229. .u-box-active {
  230. overflow: hidden;
  231. animation-timing-function: ease-in-out;
  232. animation-duration: 1500ms;
  233. animation-iteration-count: infinite;
  234. animation-direction: alternate;
  235. border: 2rpx solid $u-type-primary;
  236. }
  237. .u-middle-line-active {
  238. background: $u-type-primary;
  239. }
  240. .u-breathe {
  241. animation: breathe 2s infinite ease;
  242. }
  243. .u-placeholder-line {
  244. display: none;
  245. position: absolute;
  246. left: 50%;
  247. top: 50%;
  248. transform: translate(-50%, -50%);
  249. width: 2rpx;
  250. height: 40rpx;
  251. background: #333333;
  252. animation: twinkling 1.5s infinite ease;
  253. }
  254. .u-animation-breathe {
  255. animation-name: breathe;
  256. }
  257. .u-dot {
  258. font-size: 34rpx;
  259. line-height: 34rpx;
  260. }
  261. .u-middle-line {
  262. height: 4px;
  263. background: #000000;
  264. width: 80%;
  265. position: absolute;
  266. border-radius: 2px;
  267. top: 50%;
  268. left: 50%;
  269. transform: translate(-50%, -50%);
  270. }
  271. .u-buttom-line-active {
  272. background: $u-type-primary;
  273. }
  274. .u-bottom-line {
  275. height: 4px;
  276. background: #000000;
  277. width: 80%;
  278. position: absolute;
  279. border-radius: 2px;
  280. bottom: 0;
  281. left: 50%;
  282. transform: translate(-50%);
  283. }
  284. </style>