cl-textarea.uvue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <template>
  2. <view
  3. class="cl-textarea"
  4. :class="[
  5. pt.className,
  6. {
  7. 'is-dark': isDark,
  8. 'cl-textarea--border': border,
  9. 'cl-textarea--focus': isFocus,
  10. 'cl-textarea--disabled': isDisabled,
  11. 'cl-textarea--error': isError
  12. }
  13. ]"
  14. >
  15. <textarea
  16. class="cl-textarea__inner"
  17. :class="[
  18. {
  19. 'is-disabled': isDisabled,
  20. 'is-dark': isDark
  21. },
  22. ptClassName
  23. ]"
  24. :style="textareaStyle"
  25. :value="value"
  26. :name="name"
  27. :disabled="readonly ?? isDisabled"
  28. :placeholder="placeholder"
  29. :placeholder-class="`text-surface-400 ${placeholderClass}`"
  30. :placeholder-style="placeholderStyle"
  31. :maxlength="maxlength"
  32. :focus="isFocusing"
  33. :cursor="cursor"
  34. :cursor-spacing="cursorSpacing"
  35. :cursor-color="cursorColor"
  36. :show-confirm-bar="showConfirmBar"
  37. :confirm-hold="confirmHold"
  38. :auto-height="autoHeight"
  39. :fixed="fixed"
  40. :adjust-position="adjustPosition"
  41. :hold-keyboard="holdKeyboard"
  42. :inputmode="inputmode"
  43. :disable-default-padding="disableDefaultPadding"
  44. :adjust-keyboard-to="adjustKeyboardTo"
  45. @confirm="onConfirm"
  46. @input="onInput"
  47. @linechange="onLineChange"
  48. @blur="onBlur"
  49. @focus="onFocus"
  50. @keyboardheightchange="onKeyboardheightchange"
  51. />
  52. <cl-text
  53. :pt="{ className: 'absolute right-2 bottom-2 text-xs text-surface-400' }"
  54. v-if="showWordLimit"
  55. >{{ value.length }} / {{ maxlength }}</cl-text
  56. >
  57. </view>
  58. </template>
  59. <script setup lang="ts">
  60. import { computed, nextTick, ref, watch, type PropType } from "vue";
  61. import type { PassThroughProps } from "../../types";
  62. import { useForm, useFormItem, useSize } from "../../hooks";
  63. import { isDark, parsePt, parseRpx } from "@/cool";
  64. import { t } from "@/locale";
  65. defineOptions({
  66. name: "cl-textarea"
  67. });
  68. // 组件属性定义
  69. const props = defineProps({
  70. // 透传样式
  71. pt: {
  72. type: Object,
  73. default: () => ({})
  74. },
  75. // 绑定值
  76. modelValue: {
  77. type: String,
  78. default: ""
  79. },
  80. // 是否显示边框
  81. border: {
  82. type: Boolean,
  83. default: true
  84. },
  85. // 是否禁用
  86. disabled: {
  87. type: Boolean,
  88. default: false
  89. },
  90. // 是否只读
  91. readonly: {
  92. type: Boolean,
  93. default: null
  94. },
  95. // 是否显示字数统计
  96. showWordLimit: {
  97. type: Boolean,
  98. default: true
  99. },
  100. // 名称
  101. name: {
  102. type: String,
  103. default: ""
  104. },
  105. // 占位符
  106. placeholder: {
  107. type: String,
  108. default: () => t("请输入")
  109. },
  110. // 占位符样式类
  111. placeholderClass: {
  112. type: String,
  113. default: ""
  114. },
  115. // 占位符样式
  116. placeholderStyle: {
  117. type: String,
  118. default: ""
  119. },
  120. // 最大输入长度
  121. maxlength: {
  122. type: Number,
  123. default: 140
  124. },
  125. // 是否自动聚焦
  126. autofocus: {
  127. type: Boolean,
  128. default: false
  129. },
  130. // 设置键盘右下角按钮的文字
  131. confirmType: {
  132. type: String as PropType<"done" | "go" | "next" | "search" | "send">,
  133. default: "done"
  134. },
  135. // 指定focus时的光标位置
  136. cursor: {
  137. type: Number,
  138. default: 0
  139. },
  140. // 点击键盘确认按钮时是否保持键盘不收起
  141. confirmHold: {
  142. type: Boolean,
  143. default: false
  144. },
  145. // 高度
  146. height: {
  147. type: [Number, String],
  148. default: 120
  149. },
  150. // 是否自动增高
  151. autoHeight: {
  152. type: Boolean,
  153. default: false
  154. },
  155. // 如果 textarea 是在一个 position:fixed 的区域,需要显示指定属性 fixed 为 true
  156. fixed: {
  157. type: Boolean,
  158. default: false
  159. },
  160. // 光标与键盘的距离
  161. cursorSpacing: {
  162. type: Number,
  163. default: 5
  164. },
  165. // 指定光标颜色
  166. cursorColor: {
  167. type: String,
  168. default: ""
  169. },
  170. // 是否显示键盘上方带有”完成“按钮那一栏
  171. showConfirmBar: {
  172. type: Boolean,
  173. default: true
  174. },
  175. // 光标起始位置
  176. selectionStart: {
  177. type: Number,
  178. default: -1
  179. },
  180. // 光标结束位置
  181. selectionEnd: {
  182. type: Number,
  183. default: -1
  184. },
  185. // 盘弹起时,是否自动上推页面
  186. adjustPosition: {
  187. type: Boolean,
  188. default: true
  189. },
  190. // 它提供了用户在编辑元素或其内容时可能输入的数据类型的提示。
  191. inputmode: {
  192. type: String as PropType<
  193. "none" | "text" | "decimal" | "numeric" | "tel" | "search" | "email" | "url"
  194. >,
  195. default: "text"
  196. },
  197. // focus时,点击页面的时候不收起键盘
  198. holdKeyboard: {
  199. type: Boolean,
  200. default: false
  201. },
  202. // 是否禁用默认内边距
  203. disableDefaultPadding: {
  204. type: Boolean,
  205. default: true
  206. },
  207. // 键盘对齐位置
  208. adjustKeyboardTo: {
  209. type: String as PropType<"cursor" | "bottom">,
  210. default: "cursor"
  211. }
  212. });
  213. // 事件定义
  214. const emit = defineEmits([
  215. "update:modelValue",
  216. "input",
  217. "change",
  218. "focus",
  219. "blur",
  220. "confirm",
  221. "linechange",
  222. "keyboardheightchange"
  223. ]);
  224. // cl-form 上下文
  225. const { disabled } = useForm();
  226. // cl-form-item 上下文
  227. const { isError } = useFormItem();
  228. // 是否禁用
  229. const isDisabled = computed(() => {
  230. return disabled.value || props.disabled;
  231. });
  232. // 透传样式类型定义
  233. type PassThrough = {
  234. className?: string;
  235. inner?: PassThroughProps;
  236. };
  237. // 解析透传样式
  238. const pt = computed(() => parsePt<PassThrough>(props.pt));
  239. // 字号
  240. const { ptClassName, getSize } = useSize(() => pt.value.inner?.className ?? "");
  241. // 文本框样式
  242. const textareaStyle = computed(() => {
  243. const style = {
  244. height: parseRpx(props.height)
  245. };
  246. // 字号
  247. const fontSize = getSize(null);
  248. if (fontSize != null) {
  249. style["fontSize"] = fontSize;
  250. }
  251. return style;
  252. });
  253. // 占位符样式
  254. const placeholderStyle = computed(() => {
  255. return `font-size: 26rpx; ${props.placeholderStyle}`;
  256. });
  257. // 绑定值
  258. const value = ref(props.modelValue);
  259. // 是否聚焦(样式作用)
  260. const isFocus = ref<boolean>(props.autofocus);
  261. // 是否聚焦(输入框作用)
  262. const isFocusing = ref<boolean>(props.autofocus);
  263. // 获取焦点事件
  264. function onFocus(e: UniTextareaFocusEvent) {
  265. isFocus.value = true;
  266. emit("focus", e);
  267. }
  268. // 失去焦点事件
  269. function onBlur(e: UniTextareaBlurEvent) {
  270. emit("blur", e);
  271. setTimeout(() => {
  272. isFocus.value = false;
  273. }, 0);
  274. }
  275. // 输入事件
  276. function onInput(e: UniInputEvent) {
  277. const v1 = e.detail.value;
  278. const v2 = value.value;
  279. value.value = v1;
  280. emit("update:modelValue", v1);
  281. emit("input", e);
  282. if (v1 != v2) {
  283. emit("change", v1);
  284. }
  285. }
  286. // 点击确认按钮事件
  287. function onConfirm(e: UniInputConfirmEvent) {
  288. emit("confirm", e);
  289. }
  290. // 键盘高度变化事件
  291. function onKeyboardheightchange(e: UniInputKeyboardHeightChangeEvent) {
  292. emit("keyboardheightchange", e);
  293. }
  294. // 行数变化事件
  295. function onLineChange(e: UniTextareaLineChangeEvent) {
  296. emit("linechange", e);
  297. }
  298. // 聚焦方法
  299. function focus() {
  300. setTimeout(() => {
  301. isFocusing.value = false;
  302. nextTick(() => {
  303. isFocusing.value = true;
  304. });
  305. }, 0);
  306. }
  307. watch(
  308. computed(() => props.modelValue),
  309. (val: string) => {
  310. value.value = val;
  311. }
  312. );
  313. defineExpose({
  314. isFocus,
  315. focus
  316. });
  317. </script>
  318. <style lang="scss" scoped>
  319. .cl-textarea {
  320. @apply flex flex-row items-center bg-white;
  321. @apply rounded-lg;
  322. padding: 16rpx 20rpx;
  323. transition-property: border-color, background-color;
  324. transition-duration: 0.2s;
  325. :deep(.uni-textarea-compute) {
  326. opacity: 0;
  327. }
  328. &__inner {
  329. @apply h-full text-surface-700;
  330. flex: 1;
  331. font-size: 28rpx;
  332. &.is-dark {
  333. @apply text-white;
  334. }
  335. }
  336. &__icon {
  337. @apply flex items-center justify-center h-full;
  338. padding-left: 20rpx;
  339. }
  340. &--border {
  341. @apply border border-solid border-surface-200;
  342. }
  343. &--focus {
  344. @apply border-primary-500;
  345. }
  346. &--disabled {
  347. @apply bg-surface-100 opacity-70;
  348. }
  349. &--error {
  350. @apply border-red-500;
  351. }
  352. &.is-dark {
  353. @apply bg-surface-800;
  354. &.cl-textarea--border {
  355. @apply border-surface-700;
  356. &.cl-textarea--focus {
  357. @apply border-primary-500;
  358. }
  359. }
  360. &.cl-textarea--disabled {
  361. @apply bg-surface-700;
  362. }
  363. }
  364. }
  365. </style>