cl-input-otp.uvue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. @focus="onFocus"
  25. @change="onChange"
  26. ></cl-input>
  27. </view>
  28. <view class="cl-input-otp__list" :class="[pt.list?.className]">
  29. <view
  30. v-for="(item, index) in list"
  31. :key="index"
  32. class="cl-input-otp__item"
  33. :class="[
  34. {
  35. 'is-disabled': disabled,
  36. 'is-dark': isDark,
  37. 'is-active': value.length >= index && isFocus
  38. },
  39. pt.item?.className
  40. ]"
  41. >
  42. <cl-text
  43. :color="value.length >= index && isFocus ? 'primary' : ''"
  44. :pt="{
  45. className: pt.value?.className
  46. }"
  47. >{{ item }}</cl-text
  48. >
  49. <view
  50. ref="cursorRef"
  51. class="cl-input-otp__cursor"
  52. :class="[pt.cursor?.className]"
  53. v-if="value.length == index && isFocus && item == ''"
  54. ></view>
  55. </view>
  56. </view>
  57. </view>
  58. </template>
  59. <script setup lang="ts">
  60. import { computed, nextTick, onMounted, ref, watch, type PropType, type Ref } from "vue";
  61. import type { ClInputType, PassThroughProps } from "../../types";
  62. import { createAnimation, isDark, isEmpty, parsePt } from "@/cool";
  63. defineOptions({
  64. name: "cl-input-otp"
  65. });
  66. // 定义组件属性
  67. const props = defineProps({
  68. // 透传样式
  69. pt: {
  70. type: Object,
  71. default: () => ({})
  72. },
  73. // 绑定值
  74. modelValue: {
  75. type: String,
  76. default: ""
  77. },
  78. // 是否自动聚焦
  79. autofocus: {
  80. type: Boolean,
  81. default: false
  82. },
  83. // 验证码位数
  84. length: {
  85. type: Number,
  86. default: 4
  87. },
  88. // 是否禁用
  89. disabled: {
  90. type: Boolean,
  91. default: false
  92. },
  93. // 输入框类型
  94. inputType: {
  95. type: String as PropType<ClInputType>,
  96. default: "number"
  97. }
  98. });
  99. /**
  100. * 事件定义
  101. * update:modelValue - 更新绑定值
  102. * done - 输入完成
  103. */
  104. const emit = defineEmits(["update:modelValue", "done"]);
  105. /**
  106. * 透传样式类型定义
  107. */
  108. type PassThrough = {
  109. className?: string;
  110. list?: PassThroughProps;
  111. item?: PassThroughProps;
  112. cursor?: PassThroughProps;
  113. value?: PassThroughProps;
  114. };
  115. // 解析透传样式
  116. const pt = computed(() => parsePt<PassThrough>(props.pt));
  117. // 输入框引用
  118. const inputRef = ref<ClInputComponentPublicInstance | null>(null);
  119. // 光标引用
  120. const cursorRef = ref<UniElement[]>([]);
  121. // 输入值
  122. const value = ref(props.modelValue);
  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. async function onCursor() {
  150. await nextTick();
  151. if (isEmpty(cursorRef.value)) {
  152. return;
  153. }
  154. // 开始动画
  155. createAnimation(cursorRef.value[0], {
  156. duration: 600,
  157. loop: -1,
  158. alternate: true
  159. })
  160. .opacity("0", "1")
  161. .play();
  162. }
  163. /**
  164. * 输入框聚焦
  165. */
  166. function onFocus() {
  167. onCursor();
  168. }
  169. /**
  170. * 输入事件处理
  171. * @param val 输入值
  172. */
  173. function onChange(val: string) {
  174. // 更新绑定值
  175. emit("update:modelValue", val);
  176. // 输入完成时触发done事件
  177. if (val.length == props.length) {
  178. uni.hideKeyboard();
  179. emit("done", val);
  180. }
  181. // 更新光标动画
  182. onCursor();
  183. }
  184. onMounted(() => {
  185. watch(
  186. computed(() => props.modelValue),
  187. (val: string) => {
  188. value.value = val;
  189. onCursor();
  190. },
  191. {
  192. immediate: true
  193. }
  194. );
  195. });
  196. </script>
  197. <style lang="scss" scoped>
  198. .cl-input-otp {
  199. @apply relative overflow-visible;
  200. &__inner {
  201. @apply absolute top-0 h-full z-10;
  202. opacity: 0;
  203. left: 0;
  204. width: 100%;
  205. }
  206. &__list {
  207. @apply flex flex-row relative;
  208. margin: 0 -10rpx;
  209. }
  210. &__item {
  211. @apply flex flex-row items-center justify-center duration-100;
  212. @apply border border-solid border-surface-200 rounded-lg bg-white;
  213. height: 80rpx;
  214. width: 80rpx;
  215. margin: 0 10rpx;
  216. &.is-disabled {
  217. @apply bg-surface-100 opacity-70;
  218. }
  219. &.is-dark {
  220. @apply bg-surface-800 border-surface-600;
  221. &.is-disabled {
  222. @apply bg-surface-700;
  223. }
  224. }
  225. &.is-active {
  226. @apply border-primary-500;
  227. }
  228. }
  229. &__cursor {
  230. @apply absolute bg-primary-500;
  231. width: 2rpx;
  232. height: 24rpx;
  233. }
  234. &--disabled {
  235. @apply opacity-50;
  236. }
  237. }
  238. </style>