cl-input.uvue 6.7 KB

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