cl-input.uvue 7.2 KB

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