cl-input.uvue 8.3 KB

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