cl-input-otp.uvue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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" @tap="onCursor()">
  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. ref="cursorRef"
  50. class="cl-input-otp__cursor"
  51. :class="[pt.cursor?.className]"
  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, nextTick, onMounted, ref, watch, type PropType } from "vue";
  60. import type { ClInputType, PassThroughProps } from "../../types";
  61. import { AnimationEngine, createAnimation, isEmpty, last, parsePt, isDark } 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 cursorRef = ref<UniElement[]>([]);
  120. // 输入值
  121. const value = ref(props.modelValue);
  122. /**
  123. * 是否聚焦状态
  124. */
  125. const isFocus = computed<boolean>(() => {
  126. if (props.disabled) {
  127. return false;
  128. }
  129. if (inputRef.value == null) {
  130. return false;
  131. }
  132. return (inputRef.value as ClInputComponentPublicInstance).isFocus;
  133. });
  134. /**
  135. * 验证码数组
  136. * 根据长度生成空数组,每个位置填充对应的输入值
  137. */
  138. const list = computed<string[]>(() => {
  139. const arr = [] as string[];
  140. for (let i = 0; i < props.length; i++) {
  141. arr.push(value.value.charAt(i));
  142. }
  143. return arr;
  144. });
  145. // 光标动画引擎
  146. let animationEngine: AnimationEngine | null = null;
  147. /**
  148. * 光标动画
  149. */
  150. async function onCursor() {
  151. await nextTick();
  152. if (isEmpty(cursorRef.value)) {
  153. return;
  154. }
  155. // 开始动画
  156. // #ifdef APP
  157. if (animationEngine != null) {
  158. animationEngine!.stop();
  159. }
  160. animationEngine = createAnimation(last(cursorRef.value), {
  161. duration: 600,
  162. loop: -1,
  163. alternate: true
  164. })
  165. .opacity("0", "1")
  166. .play();
  167. // #endif
  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;
  200. &__inner {
  201. @apply absolute top-0 h-full z-10;
  202. opacity: 0;
  203. // 小程序隐藏 placeholder
  204. left: -100%;
  205. width: 200%;
  206. }
  207. &__list {
  208. @apply flex flex-row relative;
  209. margin: 0 -5px;
  210. // #ifdef APP-IOS
  211. padding-bottom: 0.05px;
  212. // #endif
  213. }
  214. &__item {
  215. @apply flex flex-row items-center justify-center duration-100;
  216. @apply border border-solid border-surface-200 rounded-lg;
  217. background-color: #fff;
  218. height: 40px;
  219. width: 40px;
  220. margin: 0 5px;
  221. &.is-disabled {
  222. @apply bg-surface-100 opacity-70;
  223. }
  224. &.is-dark {
  225. @apply bg-surface-800 border-surface-600;
  226. &.is-disabled {
  227. @apply bg-surface-700;
  228. }
  229. }
  230. &.is-active {
  231. @apply border-primary-500;
  232. }
  233. // #ifdef APP-HARMONY
  234. @apply border-none bg-surface-50;
  235. &.is-active {
  236. @apply bg-surface-100;
  237. }
  238. &.is-dark {
  239. @apply bg-surface-700;
  240. &.is-active {
  241. @apply bg-surface-600;
  242. }
  243. &.is-disabled {
  244. @apply opacity-70;
  245. }
  246. }
  247. // #endif
  248. }
  249. &__cursor {
  250. @apply absolute bg-primary-500;
  251. width: 1px;
  252. height: 12px;
  253. // #ifndef APP
  254. animation: blink 1s infinite;
  255. @keyframes blink {
  256. 0% {
  257. opacity: 1;
  258. }
  259. 50% {
  260. opacity: 0;
  261. }
  262. }
  263. // #endif
  264. }
  265. &--disabled {
  266. @apply opacity-50;
  267. }
  268. }
  269. </style>