cl-input.uvue 8.0 KB

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