cl-textarea.uvue 7.6 KB

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