cl-input.uvue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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() {
  241. isFocus.value = true;
  242. emit("focus");
  243. }
  244. // 失去焦点事件
  245. function onBlur() {
  246. emit("blur");
  247. setTimeout(() => {
  248. isFocus.value = false;
  249. }, 0);
  250. }
  251. // 输入事件
  252. function onInput(e: UniInputEvent) {
  253. const val = e.detail.value;
  254. value.value = val;
  255. emit("update:modelValue", val);
  256. emit("input", val);
  257. if (val != value.value) {
  258. emit("change", val);
  259. }
  260. }
  261. // 点击确认按钮事件
  262. function onConfirm(e: UniInputConfirmEvent) {
  263. emit("confirm", e);
  264. }
  265. // 键盘高度变化事件
  266. function onKeyboardheightchange(e: UniInputKeyboardHeightChangeEvent) {
  267. emit("keyboardheightchange", e);
  268. }
  269. // 点击事件
  270. function onTap() {
  271. if (isDisabled.value) {
  272. return;
  273. }
  274. isFocus.value = true;
  275. }
  276. // 聚焦方法
  277. function focus() {
  278. setTimeout(() => {
  279. isFocus.value = false;
  280. nextTick(() => {
  281. isFocus.value = true;
  282. });
  283. }, 0);
  284. }
  285. // 清除方法
  286. function clear() {
  287. value.value = "";
  288. emit("update:modelValue", "");
  289. emit("change", "");
  290. emit("clear");
  291. // #ifdef H5
  292. focus();
  293. // #endif
  294. }
  295. watch(
  296. computed(() => props.modelValue),
  297. (val: string) => {
  298. value.value = val;
  299. },
  300. {
  301. immediate: true
  302. }
  303. );
  304. defineExpose({
  305. isFocus,
  306. focus,
  307. clear
  308. });
  309. </script>
  310. <style lang="scss" scoped>
  311. .cl-input {
  312. @apply flex flex-row items-center bg-white duration-200;
  313. @apply rounded-lg;
  314. height: 66rpx;
  315. padding: 0 20rpx;
  316. transition-property: background-color, border-color;
  317. &__inner {
  318. @apply h-full text-surface-700;
  319. flex: 1;
  320. font-size: 28rpx;
  321. &.is-dark {
  322. @apply text-white;
  323. }
  324. }
  325. &__icon {
  326. @apply flex items-center justify-center h-full;
  327. padding-left: 20rpx;
  328. }
  329. &--border {
  330. @apply border border-solid border-surface-200;
  331. }
  332. &--disabled {
  333. @apply bg-surface-100 opacity-70;
  334. }
  335. &--focus {
  336. &.cl-input--border {
  337. @apply border-primary-500;
  338. }
  339. }
  340. &--error {
  341. @apply border-red-500;
  342. }
  343. &.is-dark {
  344. @apply bg-surface-800;
  345. &.cl-input--border {
  346. @apply border-surface-700;
  347. &.cl-input--focus {
  348. @apply border-primary-500;
  349. }
  350. }
  351. &.cl-input--disabled {
  352. @apply bg-surface-700;
  353. }
  354. }
  355. }
  356. </style>