cl-input.uvue 8.0 KB

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