cl-input-otp.uvue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <view class="cl-input-otp" :class="[pt.className]">
  3. <view class="cl-input-otp__inner">
  4. <cl-input
  5. v-model="value"
  6. ref="inputRef"
  7. :type="inputType"
  8. :pt="{
  9. className: '!h-full'
  10. }"
  11. :disabled="disabled"
  12. :autofocus="autofocus"
  13. :maxlength="length"
  14. :hold-keyboard="false"
  15. :clearable="false"
  16. @input="onInput"
  17. @focus="animateCursor(true)"
  18. @blur="animateCursor(true)"
  19. ></cl-input>
  20. </view>
  21. <view class="cl-input-otp__list" :class="[pt.list?.className]">
  22. <view
  23. v-for="(item, index) in list"
  24. :key="index"
  25. class="cl-input-otp__item"
  26. :class="[
  27. {
  28. 'is-disabled': disabled,
  29. 'is-dark': isDark,
  30. 'is-active': value.length == index && isFocus
  31. },
  32. pt.item?.className
  33. ]"
  34. >
  35. <text
  36. class="cl-input-otp__value"
  37. :class="[
  38. {
  39. 'is-disabled': disabled,
  40. 'is-dark': isDark
  41. },
  42. pt.value?.className
  43. ]"
  44. >{{ item }}</text
  45. >
  46. <view
  47. class="cl-input-otp__cursor"
  48. :class="[pt.cursor?.className]"
  49. :style="{
  50. opacity: cursorOpacity
  51. }"
  52. v-if="value.length == index && isFocus && item == ''"
  53. ></view>
  54. </view>
  55. </view>
  56. </view>
  57. </template>
  58. <script setup lang="ts">
  59. import { computed, ref, watch, type PropType, type Ref } from "vue";
  60. import type { ClInputType, PassThroughProps } from "../../types";
  61. import { isDark, parsePt } from "@/cool";
  62. defineOptions({
  63. name: "cl-input-otp"
  64. });
  65. // 定义组件属性
  66. const props = defineProps({
  67. // 透传样式
  68. pt: {
  69. type: Object,
  70. default: () => ({})
  71. },
  72. // 绑定值
  73. modelValue: {
  74. type: String,
  75. default: ""
  76. },
  77. // 是否自动聚焦
  78. autofocus: {
  79. type: Boolean,
  80. default: false
  81. },
  82. // 验证码位数
  83. length: {
  84. type: Number,
  85. default: 4
  86. },
  87. // 是否禁用
  88. disabled: {
  89. type: Boolean,
  90. default: false
  91. },
  92. // 输入框类型
  93. inputType: {
  94. type: String as PropType<ClInputType>,
  95. default: "number"
  96. }
  97. });
  98. /**
  99. * 事件定义
  100. * update:modelValue - 更新绑定值
  101. * done - 输入完成
  102. */
  103. const emit = defineEmits(["update:modelValue", "done"]);
  104. /**
  105. * 透传样式类型定义
  106. */
  107. type PassThrough = {
  108. className?: string;
  109. list?: PassThroughProps;
  110. item?: PassThroughProps;
  111. cursor?: PassThroughProps;
  112. value?: PassThroughProps;
  113. };
  114. // 解析透传样式
  115. const pt = computed(() => parsePt<PassThrough>(props.pt));
  116. // 输入框引用
  117. const inputRef = ref<ClInputComponentPublicInstance | null>(null);
  118. // 输入值
  119. const value = ref(props.modelValue) as Ref<string>;
  120. /**
  121. * 是否聚焦状态
  122. */
  123. const isFocus = computed<boolean>(() => {
  124. if (props.disabled) {
  125. return false;
  126. }
  127. if (inputRef.value == null) {
  128. return false;
  129. }
  130. return (inputRef.value as ClInputComponentPublicInstance).isFocus;
  131. });
  132. /**
  133. * 验证码数组
  134. * 根据长度生成空数组,每个位置填充对应的输入值
  135. */
  136. const list = computed<string[]>(() => {
  137. const arr = [] as string[];
  138. for (let i = 0; i < props.length; i++) {
  139. arr.push(value.value.charAt(i));
  140. }
  141. return arr;
  142. });
  143. /**
  144. * 监听绑定值变化
  145. * 同步更新内部值
  146. */
  147. watch(
  148. computed(() => props.modelValue),
  149. (val: string) => {
  150. value.value = val;
  151. }
  152. );
  153. /**
  154. * 输入事件处理
  155. * @param val 输入值
  156. */
  157. function onInput(val: string) {
  158. // 更新绑定值
  159. emit("update:modelValue", val);
  160. // 输入完成时触发done事件
  161. if (val.length == props.length) {
  162. uni.hideKeyboard();
  163. emit("done", val);
  164. }
  165. }
  166. /**
  167. * 光标闪烁透明度值
  168. * 范围: 0.3-1.0
  169. */
  170. const cursorOpacity = ref(0.3);
  171. /**
  172. * 光标闪烁动画帧ID
  173. */
  174. let cursorAnimationId = 0;
  175. /**
  176. * 控制光标闪烁动画
  177. * @param isIncreasing 透明度是否递增
  178. */
  179. function animateCursor(isIncreasing: boolean) {
  180. // #ifdef APP
  181. // 未获得焦点时不执行动画
  182. if (!isFocus.value) {
  183. return;
  184. }
  185. // 取消上一次动画
  186. if (cursorAnimationId != 0) {
  187. cancelAnimationFrame(cursorAnimationId);
  188. cursorAnimationId = 0;
  189. }
  190. // 执行动画帧
  191. cursorAnimationId = requestAnimationFrame(() => {
  192. // 根据方向调整透明度值
  193. cursorOpacity.value += isIncreasing ? 0.01 : -0.01;
  194. // 到达边界值时改变方向
  195. if (cursorOpacity.value > 1) {
  196. animateCursor(false);
  197. } else if (cursorOpacity.value <= 0.3) {
  198. animateCursor(true);
  199. } else {
  200. animateCursor(isIncreasing);
  201. }
  202. });
  203. // #endif
  204. }
  205. </script>
  206. <style lang="scss" scoped>
  207. .cl-input-otp {
  208. @apply relative;
  209. &__inner {
  210. @apply absolute top-0 h-full z-10;
  211. opacity: 0;
  212. left: -100%;
  213. width: 200%;
  214. }
  215. &__list {
  216. @apply flex flex-row relative;
  217. margin: 0 -10rpx;
  218. }
  219. &__item {
  220. @apply flex flex-row items-center justify-center;
  221. @apply border border-solid border-surface-200 rounded-lg bg-white;
  222. height: 80rpx;
  223. width: 80rpx;
  224. margin: 0 10rpx;
  225. &.is-disabled {
  226. @apply bg-surface-100 opacity-70;
  227. }
  228. &.is-dark {
  229. @apply bg-surface-800 border-surface-600;
  230. &.is-disabled {
  231. @apply bg-surface-700;
  232. }
  233. }
  234. &.is-active {
  235. @apply border-primary-500;
  236. }
  237. }
  238. &__value {
  239. @apply text-surface-700 text-md;
  240. &.is-dark {
  241. @apply text-white;
  242. }
  243. }
  244. &__cursor {
  245. @apply absolute;
  246. @apply bg-primary-500;
  247. width: 2rpx;
  248. height: 36rpx;
  249. }
  250. // #ifdef H5 || MP
  251. &__cursor {
  252. animation: flash 1s infinite ease;
  253. }
  254. @keyframes flash {
  255. 0% {
  256. opacity: 0.3;
  257. }
  258. 50% {
  259. opacity: 1;
  260. }
  261. 100% {
  262. opacity: 0.3;
  263. }
  264. }
  265. // #endif
  266. }
  267. </style>