AuthInputItem.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <view class="form-item">
  3. <!-- label -->
  4. <view v-if="label" class="form-label">
  5. {{ label }}
  6. </view>
  7. <!-- input 行 -->
  8. <view class="form-input">
  9. <input
  10. class="form-input-inner"
  11. v-model="modelValue"
  12. :type="inputType"
  13. :password="passwordToggle && isPassword"
  14. :maxlength="maxlength"
  15. :placeholder="placeholder"
  16. :disabled="disabled"
  17. @focus="onFocus"
  18. @blur="onBlur"
  19. />
  20. <!-- 清除 -->
  21. <view
  22. v-if="showClear"
  23. class="clear"
  24. :class="{ visible: isFocused }"
  25. @touchstart.stop.prevent="clear"
  26. >
  27. <wd-icon name="error-fill" size="28rpx" :color="CLEAR_ICON_COLOR" />
  28. </view>
  29. <!-- 密码显隐 -->
  30. <view v-if="passwordToggle" class="input-type-toggle" @click="togglePassword">
  31. <wd-icon :name="isPassword ? 'view' : 'eye-close'" size="28rpx" :color="CLEAR_ICON_COLOR" />
  32. </view>
  33. <!-- 右侧插槽(验证码按钮等) -->
  34. <view class="suffix">
  35. <slot name="suffix" />
  36. </view>
  37. </view>
  38. <!-- 错误提示 -->
  39. <text class="error-text" :class="{ visible: !!error }">
  40. {{ error }}
  41. </text>
  42. </view>
  43. </template>
  44. <script setup lang="ts">
  45. import { computed, ref, watch } from 'vue'
  46. /* ---------- props ---------- */
  47. interface Props {
  48. label?: string
  49. placeholder?: string
  50. type?: 'text' | 'number' | 'password'
  51. maxlength?: number
  52. error?: string
  53. passwordToggle?: boolean
  54. disabled?: boolean
  55. }
  56. const props = withDefaults(defineProps<Props>(), {
  57. type: 'text',
  58. placeholder: '',
  59. maxlength: 50,
  60. error: '',
  61. passwordToggle: false,
  62. disabled: false,
  63. })
  64. /* ---------- emits ---------- */
  65. const emit = defineEmits<{
  66. (e: 'clear'): void
  67. (e: 'validate'): void
  68. }>()
  69. /* ---------- v-model ---------- */
  70. const modelValue = defineModel<string>({
  71. default: '',
  72. })
  73. /* ---------- state ---------- */
  74. const CLEAR_ICON_COLOR = '#9CA3AF'
  75. const isFocused = ref(false)
  76. const isPassword = ref(true)
  77. /* ---------- 清除检测 ---------- */
  78. const hadValue = ref(false)
  79. watch(
  80. () => modelValue.value,
  81. (val) => {
  82. const hasValue = !!val
  83. if (hadValue.value && !hasValue) {
  84. emit('clear')
  85. }
  86. hadValue.value = hasValue
  87. },
  88. { immediate: true }
  89. )
  90. /* ---------- computed ---------- */
  91. const inputType = computed(() => {
  92. if (props.passwordToggle) {
  93. return isPassword.value ? 'password' : 'text'
  94. }
  95. return props.type
  96. })
  97. const showClear = computed(() => !!modelValue.value)
  98. /* ---------- handlers ---------- */
  99. const onFocus = () => {
  100. isFocused.value = true
  101. }
  102. const onBlur = () => {
  103. isFocused.value = false
  104. if (modelValue.value) {
  105. emit('validate')
  106. }
  107. }
  108. const clear = () => {
  109. modelValue.value = ''
  110. }
  111. const togglePassword = () => {
  112. isPassword.value = !isPassword.value
  113. }
  114. </script>
  115. <style scoped lang="scss">
  116. .form-item {
  117. display: flex;
  118. flex-direction: column;
  119. padding: 20rpx 0;
  120. border-bottom: 1rpx solid #e4e7ed;
  121. position: relative;
  122. .form-label {
  123. margin-bottom: 8rpx;
  124. color: var(--font-color-333);
  125. font-size: 16px;
  126. line-height: 40rpx;
  127. }
  128. .form-input {
  129. display: flex;
  130. align-items: center;
  131. min-width: 0; /* ⭐ 必须 */
  132. .form-input-inner {
  133. flex: 1;
  134. min-width: 0; /* ⭐ 小程序必需 */
  135. height: 70rpx;
  136. line-height: 70rpx;
  137. font-size: 28rpx;
  138. }
  139. .clear {
  140. margin-left: 20rpx;
  141. opacity: 0;
  142. pointer-events: none;
  143. }
  144. .clear.visible {
  145. opacity: 1;
  146. pointer-events: auto;
  147. }
  148. .input-type-toggle {
  149. flex-shrink: 0;
  150. margin-left: 20rpx;
  151. }
  152. .suffix {
  153. flex-shrink: 0;
  154. margin-left: 20rpx;
  155. }
  156. }
  157. .error-text {
  158. position: absolute;
  159. left: 0;
  160. bottom: 6rpx;
  161. font-size: 20rpx;
  162. line-height: 20rpx;
  163. color: #ff4d4f;
  164. opacity: 0;
  165. pointer-events: none; // 不影响点击
  166. }
  167. .error-text.visible {
  168. opacity: 1;
  169. }
  170. }
  171. </style>