cl-input-otp.uvue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. @change="onChange"
  25. ></cl-input>
  26. </view>
  27. <view class="cl-input-otp__list" :class="[pt.list?.className]">
  28. <view
  29. v-for="(item, index) in list"
  30. :key="index"
  31. class="cl-input-otp__item"
  32. :class="[
  33. {
  34. 'is-disabled': disabled,
  35. 'is-dark': isDark,
  36. 'is-active': value.length >= index && isFocus
  37. },
  38. pt.item?.className
  39. ]"
  40. >
  41. <cl-text
  42. :color="value.length >= index && isFocus ? 'primary' : ''"
  43. :pt="{
  44. className: pt.value?.className
  45. }"
  46. >{{ item }}</cl-text
  47. >
  48. <view
  49. class="cl-input-otp__cursor"
  50. :class="[pt.cursor?.className]"
  51. v-if="value.length == index && isFocus && item == ''"
  52. ></view>
  53. </view>
  54. </view>
  55. </view>
  56. </template>
  57. <script setup lang="ts">
  58. import { computed, ref, watch, type PropType, type Ref } from "vue";
  59. import type { ClInputType, PassThroughProps } from "../../types";
  60. import { isDark, parsePt } from "@/cool";
  61. defineOptions({
  62. name: "cl-input-otp"
  63. });
  64. // 定义组件属性
  65. const props = defineProps({
  66. // 透传样式
  67. pt: {
  68. type: Object,
  69. default: () => ({})
  70. },
  71. // 绑定值
  72. modelValue: {
  73. type: String,
  74. default: ""
  75. },
  76. // 是否自动聚焦
  77. autofocus: {
  78. type: Boolean,
  79. default: false
  80. },
  81. // 验证码位数
  82. length: {
  83. type: Number,
  84. default: 4
  85. },
  86. // 是否禁用
  87. disabled: {
  88. type: Boolean,
  89. default: false
  90. },
  91. // 输入框类型
  92. inputType: {
  93. type: String as PropType<ClInputType>,
  94. default: "number"
  95. }
  96. });
  97. /**
  98. * 事件定义
  99. * update:modelValue - 更新绑定值
  100. * done - 输入完成
  101. */
  102. const emit = defineEmits(["update:modelValue", "done"]);
  103. /**
  104. * 透传样式类型定义
  105. */
  106. type PassThrough = {
  107. className?: string;
  108. list?: PassThroughProps;
  109. item?: PassThroughProps;
  110. cursor?: PassThroughProps;
  111. value?: PassThroughProps;
  112. };
  113. // 解析透传样式
  114. const pt = computed(() => parsePt<PassThrough>(props.pt));
  115. // 输入框引用
  116. const inputRef = ref<ClInputComponentPublicInstance | null>(null);
  117. // 输入值
  118. const value = ref(props.modelValue) as Ref<string>;
  119. /**
  120. * 是否聚焦状态
  121. */
  122. const isFocus = computed<boolean>(() => {
  123. if (props.disabled) {
  124. return false;
  125. }
  126. if (inputRef.value == null) {
  127. return false;
  128. }
  129. return (inputRef.value as ClInputComponentPublicInstance).isFocus;
  130. });
  131. /**
  132. * 验证码数组
  133. * 根据长度生成空数组,每个位置填充对应的输入值
  134. */
  135. const list = computed<string[]>(() => {
  136. const arr = [] as string[];
  137. for (let i = 0; i < props.length; i++) {
  138. arr.push(value.value.charAt(i));
  139. }
  140. return arr;
  141. });
  142. /**
  143. * 监听绑定值变化
  144. * 同步更新内部值
  145. */
  146. watch(
  147. computed(() => props.modelValue),
  148. (val: string) => {
  149. value.value = val;
  150. }
  151. );
  152. /**
  153. * 输入事件处理
  154. * @param val 输入值
  155. */
  156. function onChange(val: string) {
  157. // 更新绑定值
  158. emit("update:modelValue", val);
  159. // 输入完成时触发done事件
  160. if (val.length == props.length) {
  161. uni.hideKeyboard();
  162. emit("done", val);
  163. }
  164. }
  165. </script>
  166. <style lang="scss" scoped>
  167. .cl-input-otp {
  168. @apply relative overflow-visible;
  169. &__inner {
  170. @apply absolute top-0 h-full z-10;
  171. opacity: 0;
  172. left: 0;
  173. width: 100%;
  174. }
  175. &__list {
  176. @apply flex flex-row relative;
  177. margin: 0 -10rpx;
  178. }
  179. &__item {
  180. @apply flex flex-row items-center justify-center duration-100;
  181. @apply border border-solid border-surface-200 rounded-lg bg-white;
  182. height: 80rpx;
  183. width: 80rpx;
  184. margin: 0 10rpx;
  185. &.is-disabled {
  186. @apply bg-surface-100 opacity-70;
  187. }
  188. &.is-dark {
  189. @apply bg-surface-800 border-surface-600;
  190. &.is-disabled {
  191. @apply bg-surface-700;
  192. }
  193. }
  194. &.is-active {
  195. @apply border-primary-500;
  196. }
  197. }
  198. &__cursor {
  199. @apply absolute duration-100;
  200. @apply bg-primary-500;
  201. width: 2rpx;
  202. height: 24rpx;
  203. }
  204. // #ifdef H5 || MP
  205. &__cursor {
  206. animation: flash 1s infinite ease;
  207. }
  208. @keyframes flash {
  209. 0% {
  210. opacity: 0;
  211. }
  212. 50% {
  213. opacity: 1;
  214. }
  215. 100% {
  216. opacity: 0;
  217. }
  218. }
  219. // #endif
  220. &--disabled {
  221. @apply opacity-50;
  222. }
  223. }
  224. </style>