cl-input.uvue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. >
  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. <!-- @vue-ignore -->
  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="isFocusing"
  40. :placeholder="placeholder"
  41. :placeholder-class="`text-surface-400 ${placeholderClass}`"
  42. :placeholder-style="placeholderStyle"
  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="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 type { ClIconProps } from "../cl-icon/props";
  83. import { useForm, useFormItem, useSize } from "../../hooks";
  84. import { isDark, parseClass, parsePt } from "@/cool";
  85. import { t } from "@/locale";
  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, getSize } = useSize(() => pt.value.inner?.className ?? "");
  227. // 输入框样式
  228. const inputStyle = computed(() => {
  229. const style = {};
  230. // 字号
  231. const fontSize = getSize(null);
  232. if (fontSize != null) {
  233. style["fontSize"] = fontSize;
  234. }
  235. return style;
  236. });
  237. // 占位符样式
  238. const placeholderStyle = computed(() => {
  239. return `font-size: 26rpx; ${props.placeholderStyle}`;
  240. });
  241. // 绑定值
  242. const value = ref<string>("");
  243. // 是否聚焦(样式作用)
  244. const isFocus = ref<boolean>(props.autofocus);
  245. // 是否聚焦(输入框作用)
  246. const isFocusing = ref<boolean>(props.autofocus);
  247. // 是否显示清除按钮
  248. const showClear = computed(() => {
  249. return props.clearable && value.value != "";
  250. });
  251. // 是否显示密码
  252. const isPassword = ref(props.password);
  253. // 是否超出限制
  254. const isExceed = computed(() => {
  255. // 检查数字精度是否超出限制
  256. if (props.type == "digit" && props.precision >= 0 && value.value != "") {
  257. const parts = value.value.split(".");
  258. return parts.length > 1 && parts[1].length > props.precision;
  259. } else {
  260. return false;
  261. }
  262. });
  263. // 切换密码显示状态
  264. function showPassword() {
  265. isPassword.value = !isPassword.value;
  266. }
  267. // 获取焦点事件
  268. function onFocus(e: UniInputFocusEvent) {
  269. isFocus.value = true;
  270. emit("focus", e);
  271. }
  272. // 失去焦点事件
  273. function onBlur(e: UniInputBlurEvent) {
  274. emit("blur", e);
  275. // 处理数字精度
  276. if (props.type == "digit" && props.precision > 0 && value.value != "") {
  277. const numValue = parseFloat(value.value);
  278. if (!isNaN(numValue)) {
  279. const formattedValue = numValue.toFixed(props.precision);
  280. value.value = formattedValue;
  281. emit("update:modelValue", formattedValue);
  282. emit("change", formattedValue);
  283. }
  284. }
  285. setTimeout(() => {
  286. isFocus.value = false;
  287. }, 0);
  288. }
  289. // 输入事件
  290. function onInput(e: UniInputEvent) {
  291. const v1 = e.detail.value;
  292. const v2 = value.value;
  293. value.value = v1;
  294. emit("update:modelValue", v1);
  295. emit("input", e);
  296. if (v1 != v2) {
  297. emit("change", v1);
  298. }
  299. }
  300. // 点击确认按钮事件
  301. function onConfirm(e: UniInputConfirmEvent) {
  302. emit("confirm", e);
  303. }
  304. // 键盘高度变化事件
  305. function onKeyboardheightchange(e: UniInputKeyboardHeightChangeEvent) {
  306. emit("keyboardheightchange", e);
  307. }
  308. // 聚焦方法
  309. function focus() {
  310. setTimeout(() => {
  311. isFocusing.value = false;
  312. nextTick(() => {
  313. isFocusing.value = true;
  314. });
  315. }, 0);
  316. }
  317. // 清除方法
  318. function clear() {
  319. value.value = "";
  320. emit("update:modelValue", "");
  321. emit("change", "");
  322. emit("clear");
  323. // #ifdef H5
  324. focus();
  325. // #endif
  326. }
  327. watch(
  328. computed(() => props.modelValue),
  329. (val: string) => {
  330. value.value = val;
  331. },
  332. {
  333. immediate: true
  334. }
  335. );
  336. defineExpose({
  337. isFocus,
  338. focus,
  339. clear
  340. });
  341. </script>
  342. <style lang="scss" scoped>
  343. .cl-input {
  344. @apply flex flex-row items-center bg-white duration-200;
  345. @apply rounded-lg;
  346. height: 66rpx;
  347. padding: 0 20rpx;
  348. transition-property: background-color, border-color;
  349. &__inner {
  350. @apply h-full text-surface-700;
  351. flex: 1;
  352. font-size: 28rpx;
  353. &.is-dark {
  354. @apply text-white;
  355. }
  356. &.is-exceed {
  357. @apply text-red-500;
  358. }
  359. &.is-exceed.is-dark {
  360. @apply text-red-400;
  361. }
  362. }
  363. &__icon {
  364. @apply flex items-center justify-center h-full;
  365. padding-left: 20rpx;
  366. }
  367. &--border {
  368. @apply border border-solid border-surface-200;
  369. }
  370. &--disabled {
  371. @apply bg-surface-100 opacity-70;
  372. }
  373. &--focus {
  374. &.cl-input--border {
  375. @apply border-primary-500;
  376. }
  377. }
  378. &--error {
  379. @apply border-red-500;
  380. }
  381. &.is-dark {
  382. @apply bg-surface-800;
  383. &.cl-input--border {
  384. @apply border-surface-600;
  385. &.cl-input--focus {
  386. @apply border-primary-500;
  387. }
  388. }
  389. &.cl-input--disabled {
  390. @apply bg-surface-700;
  391. }
  392. }
  393. }
  394. </style>