cl-textarea.uvue 6.7 KB

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