cl-input-otp.uvue 5.2 KB

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