You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
11 KiB

2 months ago
  1. <template>
  2. <view class="u-textarea" :class="textareaClass" :style="[textareaStyle]">
  3. <textarea
  4. class="u-textarea__field"
  5. :value="innerValue"
  6. :style="{ height: addUnit(height) }"
  7. :placeholder="placeholder"
  8. :placeholder-style="addStyle(placeholderStyle, 'string')"
  9. :placeholder-class="placeholderClass"
  10. :disabled="disabled"
  11. :focus="focus"
  12. :autoHeight="autoHeight"
  13. :fixed="fixed"
  14. :cursorSpacing="cursorSpacing"
  15. :cursor="cursor"
  16. :showConfirmBar="showConfirmBar"
  17. :selectionStart="selectionStart"
  18. :selectionEnd="selectionEnd"
  19. :adjustPosition="adjustPosition"
  20. :disableDefaultPadding="disableDefaultPadding"
  21. :holdKeyboard="holdKeyboard"
  22. :maxlength="maxlength"
  23. :confirm-type="confirmType"
  24. :ignoreCompositionEvent="ignoreCompositionEvent"
  25. @focus="onFocus"
  26. @blur="onBlur"
  27. @linechange="onLinechange"
  28. @input="onInput"
  29. @confirm="onConfirm"
  30. @keyboardheightchange="onKeyboardheightchange"
  31. ></textarea>
  32. <!-- #ifndef MP-ALIPAY -->
  33. <text
  34. class="u-textarea__count"
  35. :style="{
  36. 'background-color': disabled ? 'transparent' : '#fff',
  37. }"
  38. v-if="count"
  39. >{{ innerValue.length }}/{{ maxlength }}</text
  40. >
  41. <!-- #endif -->
  42. </view>
  43. </template>
  44. <script>
  45. import { props } from "./props.js";
  46. import { mpMixin } from '../../libs/mixin/mpMixin';
  47. import { mixin } from '../../libs/mixin/mixin';
  48. import { addStyle, addUnit, deepMerge, formValidate, os } from '../../libs/function/index';
  49. /**
  50. * Textarea 文本域
  51. * @description 文本域此组件满足了可能出现的表单信息补充编辑等实际逻辑的功能内置了字数校验等
  52. * @tutorial https://ijry.github.io/uview-plus/components/textarea.html
  53. *
  54. * @property {String | Number} value 输入框的内容
  55. * @property {String | Number} placeholder 输入框为空时占位符
  56. * @property {String} placeholderClass 指定placeholder的样式类注意页面或组件的style中写了scoped时需要在类名前写/deep/ 默认 'input-placeholder'
  57. * @property {String | Object} placeholderStyle 指定placeholder的样式字符串/对象形式"color: red;"
  58. * @property {String | Number} height 输入框高度默认 70
  59. * @property {String} confirmType 设置键盘右下角按钮的文字仅微信小程序App-vue和H5有效默认 'done'
  60. * @property {Boolean} disabled 是否禁用默认 false
  61. * @property {Boolean} count 是否显示统计字数默认 false
  62. * @property {Boolean} focus 是否自动获取焦点nvue不支持H5取决于浏览器的实现默认 false
  63. * @property {Boolean | Function} autoHeight 是否自动增加高度默认 false
  64. * @property {Boolean} fixed 如果textarea是在一个position:fixed的区域需要显示指定属性fixed为true默认 false
  65. * @property {Number} cursorSpacing 指定光标与键盘的距离默认 0
  66. * @property {String | Number} cursor 指定focus时的光标位置
  67. * @property {Function} formatter 内容式化函数
  68. * @property {Boolean} showConfirmBar 是否显示键盘上方带有完成按钮那一栏默认 true
  69. * @property {Number} selectionStart 光标起始位置自动聚焦时有效需与selection-end搭配使用默认 -1
  70. * @property {Number | Number} selectionEnd 光标结束位置自动聚焦时有效需与selection-start搭配使用默认 -1
  71. * @property {Boolean} adjustPosition 键盘弹起时是否自动上推页面默认 true
  72. * @property {Boolean | Number} disableDefaultPadding 是否去掉 iOS 下的默认内边距只微信小程序有效默认 false
  73. * @property {Boolean} holdKeyboard focus时点击页面的时候不收起键盘只微信小程序有效默认 false
  74. * @property {String | Number} maxlength 最大输入长度设置为 -1 的时候不限制最大长度默认 140
  75. * @property {String} border 边框类型surround-四周边框none-无边框bottom-底部边框默认 'surround'
  76. * @property {Boolean} ignoreCompositionEvent 是否忽略组件内对文本合成系统事件的处理
  77. *
  78. * @event {Function(e)} focus 输入框聚焦时触发event.detail = { value, height }height 为键盘高度
  79. * @event {Function(e)} blur 输入框失去焦点时触发event.detail = {value, cursor}
  80. * @event {Function(e)} linechange 输入框行数变化时调用event.detail = {height: 0, heightRpx: 0, lineCount: 0}
  81. * @event {Function(e)} input 当键盘输入时触发 input 事件
  82. * @event {Function(e)} confirm 点击完成时 触发 confirm 事件
  83. * @event {Function(e)} keyboardheightchange 键盘高度发生变化的时候触发此事件
  84. * @example <up-textarea v-model="value1" placeholder="请输入内容" ></up-textarea>
  85. */
  86. export default {
  87. name: "u-textarea",
  88. mixins: [mpMixin, mixin, props],
  89. data() {
  90. return {
  91. // 输入框的值
  92. innerValue: "",
  93. // 是否处于获得焦点状态
  94. focused: false,
  95. // value是否第一次变化,在watch中,由于加入immediate属性,会在第一次触发,此时不应该认为value发生了变化
  96. firstChange: true,
  97. // value绑定值的变化是由内部还是外部引起的
  98. changeFromInner: false,
  99. // 过滤处理方法
  100. innerFormatter: value => value
  101. }
  102. },
  103. created() {
  104. },
  105. watch: {
  106. // #ifdef VUE2
  107. value: {
  108. immediate: true,
  109. handler(newVal, oldVal) {
  110. this.innerValue = newVal;
  111. /* #ifdef H5 */
  112. // 在H5中,外部value变化后,修改input中的值,不会触发@input事件,此时手动调用值变化方法
  113. if (
  114. this.firstChange === false &&
  115. this.changeFromInner === false
  116. ) {
  117. this.valueChange();
  118. }
  119. /* #endif */
  120. this.firstChange = false;
  121. // 重置changeFromInner的值为false,标识下一次引起默认为外部引起的
  122. this.changeFromInner = false;
  123. },
  124. },
  125. // #endif
  126. // #ifdef VUE3
  127. modelValue: {
  128. immediate: true,
  129. handler(newVal, oldVal) {
  130. this.innerValue = newVal;
  131. /* #ifdef H5 */
  132. // 在H5中,外部value变化后,修改input中的值,不会触发@input事件,此时手动调用值变化方法
  133. if (
  134. this.firstChange === false &&
  135. this.changeFromInner === false
  136. ) {
  137. this.valueChange();
  138. }
  139. /* #endif */
  140. this.firstChange = false;
  141. // 重置changeFromInner的值为false,标识下一次引起默认为外部引起的
  142. this.changeFromInner = false;
  143. },
  144. }
  145. // #endif
  146. },
  147. computed: {
  148. // 组件的类名
  149. textareaClass() {
  150. let classes = [],
  151. { border, disabled } = this;
  152. border === "surround" &&
  153. (classes = classes.concat(["u-border", "u-textarea--radius"]));
  154. border === "bottom" &&
  155. (classes = classes.concat([
  156. "u-border-bottom",
  157. "u-textarea--no-radius",
  158. ]));
  159. disabled && classes.push("u-textarea--disabled");
  160. return classes.join(" ");
  161. },
  162. // 组件的样式
  163. textareaStyle() {
  164. const style = {};
  165. // #ifdef APP-NVUE
  166. // 由于textarea在安卓nvue上的差异性,需要额外再调整其内边距
  167. if (os() === "android") {
  168. style.paddingTop = "6px";
  169. style.paddingLeft = "9px";
  170. style.paddingBottom = "3px";
  171. style.paddingRight = "6px";
  172. }
  173. // #endif
  174. return deepMerge(style, addStyle(this.customStyle));
  175. },
  176. },
  177. // #ifdef VUE3
  178. emits: ['update:modelValue', 'linechange', 'focus', 'blur', 'change', 'confirm', 'keyboardheightchange'],
  179. // #endif
  180. methods: {
  181. addStyle,
  182. addUnit,
  183. // 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用
  184. setFormatter(e) {
  185. this.innerFormatter = e
  186. },
  187. onFocus(e) {
  188. this.$emit("focus", e);
  189. },
  190. onBlur(e) {
  191. this.$emit("blur", e);
  192. // 尝试调用u-form的验证方法
  193. formValidate(this, "blur");
  194. },
  195. onLinechange(e) {
  196. this.$emit("linechange", e);
  197. },
  198. onInput(e) {
  199. let { value = "" } = e.detail || {};
  200. // 格式化过滤方法
  201. const formatter = this.formatter || this.innerFormatter
  202. const formatValue = formatter(value)
  203. // 为了避免props的单向数据流特性,需要先将innerValue值设置为当前值,再在$nextTick中重新赋予设置后的值才有效
  204. this.innerValue = value
  205. this.$nextTick(() => {
  206. this.innerValue = formatValue;
  207. this.valueChange();
  208. })
  209. },
  210. // 内容发生变化,进行处理
  211. valueChange() {
  212. const value = this.innerValue;
  213. this.$nextTick(() => {
  214. // #ifdef VUE3
  215. this.$emit("update:modelValue", value);
  216. // #endif
  217. // #ifdef VUE2
  218. this.$emit("input", value);
  219. // #endif
  220. // 标识value值的变化是由内部引起的
  221. this.changeFromInner = true;
  222. this.$emit("change", value);
  223. // 尝试调用u-form的验证方法
  224. formValidate(this, "change");
  225. });
  226. },
  227. onConfirm(e) {
  228. this.$emit("confirm", e);
  229. },
  230. onKeyboardheightchange(e) {
  231. this.$emit("keyboardheightchange", e);
  232. },
  233. },
  234. };
  235. </script>
  236. <style lang="scss" scoped>
  237. @import "../../libs/css/components.scss";
  238. .u-textarea {
  239. border-radius: 4px;
  240. background-color: #fff;
  241. position: relative;
  242. @include flex;
  243. flex: 1;
  244. padding: 9px;
  245. &--radius {
  246. border-radius: 4px;
  247. }
  248. &--no-radius {
  249. border-radius: 0;
  250. }
  251. &--disabled {
  252. background-color: #f5f7fa;
  253. }
  254. &__field {
  255. flex: 1;
  256. font-size: 15px;
  257. color: $u-content-color;
  258. width: 100%;
  259. }
  260. &__count {
  261. position: absolute;
  262. right: 5px;
  263. bottom: 2px;
  264. font-size: 12px;
  265. color: $u-tips-color;
  266. background-color: #ffffff;
  267. padding: 1px 4px;
  268. }
  269. }
  270. </style>