cl-input.uvue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <view
  3. class="cl-input"
  4. :class="[
  5. pt.className,
  6. {
  7. 'is-dark': isDark,
  8. 'cl-input--border': border,
  9. 'cl-input--focus': isFocus,
  10. 'cl-input--disabled': disabled
  11. }
  12. ]"
  13. @tap="onTap"
  14. >
  15. <slot name="prepend"></slot>
  16. <view class="cl-input__icon !pl-0 pr-[12rpx]" v-if="prefixIcon">
  17. <cl-icon
  18. :name="prefixIcon"
  19. :size="pt.prefixIcon?.size ?? 32"
  20. :pt="{ className: parseClass([pt.prefixIcon?.className]) }"
  21. ></cl-icon>
  22. </view>
  23. <input
  24. class="cl-input__inner"
  25. :class="[
  26. {
  27. 'is-disabled': disabled,
  28. 'is-dark': isDark
  29. },
  30. pt.inner?.className
  31. ]"
  32. :value="value"
  33. :disabled="readonly ?? disabled"
  34. :type="type"
  35. :password="isPassword"
  36. :focus="isFocus"
  37. :placeholder="placeholder"
  38. :placeholder-class="`text-surface-400 ${placeholderClass}`"
  39. :maxlength="maxlength"
  40. :cursor-spacing="cursorSpacing"
  41. :confirm-type="confirmType"
  42. :confirm-hold="confirmHold"
  43. :adjust-position="adjustPosition"
  44. :hold-keyboard="holdKeyboard"
  45. @input="onInput"
  46. @focus="onFocus"
  47. @blur="onBlur"
  48. @confirm="onConfirm"
  49. @keyboardheightchange="onKeyboardheightchange"
  50. />
  51. <view class="cl-input__icon" v-if="suffixIcon">
  52. <cl-icon
  53. :name="suffixIcon"
  54. :size="pt.suffixIcon?.size ?? 32"
  55. :pt="{ className: parseClass([pt.prefixIcon?.className]) }"
  56. ></cl-icon>
  57. </view>
  58. <view class="cl-input__icon" @tap="clear" v-if="showClear">
  59. <cl-icon
  60. name="close-circle-fill"
  61. :size="32"
  62. :pt="{ className: '!text-surface-400' }"
  63. ></cl-icon>
  64. </view>
  65. <view class="cl-input__icon" @tap="showPassword" v-if="password">
  66. <cl-icon
  67. :name="isPassword ? 'eye-line' : 'eye-off-line'"
  68. :size="32"
  69. :pt="{ className: '!text-surface-300' }"
  70. ></cl-icon>
  71. </view>
  72. <slot name="append"></slot>
  73. </view>
  74. </template>
  75. <script setup lang="ts">
  76. import { computed, nextTick, ref, watch, type PropType } from "vue";
  77. import type { ClInputType, PassThroughProps } from "../../types";
  78. import { isDark, parseClass, parsePt } from "@/cool";
  79. import type { ClIconProps } from "../cl-icon/props";
  80. import { t } from "@/locale";
  81. defineOptions({
  82. name: "cl-input"
  83. });
  84. // 组件属性定义
  85. const props = defineProps({
  86. // 透传样式
  87. pt: {
  88. type: Object,
  89. default: () => ({})
  90. },
  91. // 绑定值
  92. modelValue: {
  93. type: String,
  94. default: ""
  95. },
  96. // 输入框类型
  97. type: {
  98. type: String as PropType<ClInputType>,
  99. default: "text"
  100. },
  101. // 前缀图标
  102. prefixIcon: {
  103. type: String,
  104. default: ""
  105. },
  106. // 后缀图标
  107. suffixIcon: {
  108. type: String,
  109. default: ""
  110. },
  111. // 是否密码框
  112. password: {
  113. type: Boolean,
  114. default: false
  115. },
  116. // 是否自动聚焦
  117. autofocus: {
  118. type: Boolean,
  119. default: false
  120. },
  121. // 是否禁用
  122. disabled: {
  123. type: Boolean,
  124. default: false
  125. },
  126. // 是否只读
  127. readonly: {
  128. type: Boolean,
  129. default: null
  130. },
  131. // 占位符
  132. placeholder: {
  133. type: String,
  134. default: () => t("请输入")
  135. },
  136. // 占位符样式类
  137. placeholderClass: {
  138. type: String,
  139. default: ""
  140. },
  141. // 是否显示边框
  142. border: {
  143. type: Boolean,
  144. default: true
  145. },
  146. // 是否可清除
  147. clearable: {
  148. type: Boolean,
  149. default: false
  150. },
  151. // 光标与键盘的距离
  152. cursorSpacing: {
  153. type: Number,
  154. default: 5
  155. },
  156. // 点击键盘确认按钮时是否保持键盘不收起
  157. confirmHold: {
  158. type: Boolean,
  159. default: false
  160. },
  161. // 设置键盘右下角按钮的文字
  162. confirmType: {
  163. type: String as PropType<"done" | "go" | "next" | "search" | "send">,
  164. default: "done"
  165. },
  166. // 键盘弹起时,是否自动上推页面
  167. adjustPosition: {
  168. type: Boolean,
  169. default: true
  170. },
  171. // 最大输入长度
  172. maxlength: {
  173. type: Number,
  174. default: 140
  175. },
  176. // 是否保持键盘不收起
  177. holdKeyboard: {
  178. type: Boolean,
  179. default: false
  180. }
  181. });
  182. // 事件定义
  183. const emit = defineEmits([
  184. "update:modelValue",
  185. "input",
  186. "change",
  187. "focus",
  188. "blur",
  189. "confirm",
  190. "clear",
  191. "keyboardheightchange"
  192. ]);
  193. // 透传样式类型定义
  194. type PassThrough = {
  195. className?: string;
  196. inner?: PassThroughProps;
  197. prefixIcon?: ClIconProps;
  198. suffixIcon?: ClIconProps;
  199. };
  200. // 解析透传样式
  201. const pt = computed(() => parsePt<PassThrough>(props.pt));
  202. // 绑定值
  203. const value = ref<string>("");
  204. // 是否聚焦
  205. const isFocus = ref<boolean>(props.autofocus);
  206. // 是否显示清除按钮
  207. const showClear = computed(() => {
  208. return isFocus.value && props.clearable && value.value != "";
  209. });
  210. // 是否显示密码
  211. const isPassword = ref(props.password);
  212. // 切换密码显示状态
  213. function showPassword() {
  214. isPassword.value = !isPassword.value;
  215. }
  216. // 获取焦点事件
  217. function onFocus() {
  218. isFocus.value = true;
  219. emit("focus");
  220. }
  221. // 失去焦点事件
  222. function onBlur() {
  223. emit("blur");
  224. setTimeout(() => {
  225. isFocus.value = false;
  226. }, 0);
  227. }
  228. // 输入事件
  229. function onInput(e: UniInputEvent) {
  230. const val = e.detail.value;
  231. value.value = val;
  232. emit("update:modelValue", val);
  233. emit("input", val);
  234. if (val != value.value) {
  235. emit("change", val);
  236. }
  237. }
  238. // 点击确认按钮事件
  239. function onConfirm(e: UniInputConfirmEvent) {
  240. emit("confirm", e);
  241. }
  242. // 键盘高度变化事件
  243. function onKeyboardheightchange(e: UniInputKeyboardHeightChangeEvent) {
  244. emit("keyboardheightchange", e);
  245. }
  246. // 点击事件
  247. function onTap() {
  248. if (props.disabled) {
  249. return;
  250. }
  251. isFocus.value = true;
  252. }
  253. // 聚焦方法
  254. function focus() {
  255. setTimeout(() => {
  256. isFocus.value = false;
  257. nextTick(() => {
  258. isFocus.value = true;
  259. });
  260. }, 0);
  261. }
  262. // 清除方法
  263. function clear() {
  264. value.value = "";
  265. emit("update:modelValue", "");
  266. emit("change", "");
  267. emit("clear");
  268. // #ifdef H5
  269. focus();
  270. // #endif
  271. }
  272. watch(
  273. computed(() => props.modelValue),
  274. (val: string) => {
  275. value.value = val;
  276. },
  277. {
  278. immediate: true
  279. }
  280. );
  281. defineExpose({
  282. isFocus,
  283. focus,
  284. clear
  285. });
  286. </script>
  287. <style lang="scss" scoped>
  288. .cl-input {
  289. @apply flex flex-row items-center bg-white duration-200;
  290. @apply rounded-lg;
  291. height: 66rpx;
  292. padding: 0 20rpx;
  293. transition-property: background-color, border-color;
  294. &__inner {
  295. @apply h-full text-surface-700;
  296. flex: 1;
  297. font-size: 28rpx;
  298. &.is-dark {
  299. @apply text-white;
  300. }
  301. }
  302. &__icon {
  303. @apply flex items-center justify-center h-full;
  304. padding-left: 20rpx;
  305. }
  306. &--border {
  307. @apply border border-solid border-surface-200;
  308. }
  309. &--disabled {
  310. @apply bg-surface-100 opacity-70;
  311. }
  312. &--focus {
  313. &.cl-input--border {
  314. @apply border-primary-500;
  315. }
  316. }
  317. &.is-dark {
  318. @apply bg-surface-800;
  319. &.cl-input--border {
  320. @apply border-surface-700;
  321. &.cl-input--focus {
  322. @apply border-primary-500;
  323. }
  324. }
  325. &.cl-input--disabled {
  326. @apply bg-surface-700;
  327. }
  328. }
  329. }
  330. </style>