cl-textarea.uvue 7.2 KB

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