u-textarea.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <template>
  2. <view class="u-textarea" :class="textareaClass" :style="[textareaStyle]">
  3. <textarea
  4. class="u-textarea__field"
  5. :value="innerValue"
  6. :style="fieldStyle"
  7. :placeholder="placeholder"
  8. :placeholder-style="addStyle(placeholderStyle, typeof placeholderStyle === 'string' ? 'string' : 'object')"
  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. fieldStyle() {
  149. let style = {};
  150. style['height'] = addUnit(this.height);
  151. if (this.autoHeight) {
  152. style['height'] = 'auto';
  153. style['minHeight'] = addUnit(this.height);
  154. }
  155. return style;
  156. },
  157. // 组件的类名
  158. textareaClass() {
  159. let classes = [],
  160. { border, disabled } = this;
  161. border === "surround" &&
  162. (classes = classes.concat(["u-border", "u-textarea--radius"]));
  163. border === "bottom" &&
  164. (classes = classes.concat([
  165. "u-border-bottom",
  166. "u-textarea--no-radius",
  167. ]));
  168. disabled && classes.push("u-textarea--disabled");
  169. return classes.join(" ");
  170. },
  171. // 组件的样式
  172. textareaStyle() {
  173. const style = {};
  174. // #ifdef APP-NVUE
  175. // 由于textarea在安卓nvue上的差异性,需要额外再调整其内边距
  176. if (os() === "android") {
  177. style.paddingTop = "6px";
  178. style.paddingLeft = "9px";
  179. style.paddingBottom = "3px";
  180. style.paddingRight = "6px";
  181. }
  182. // #endif
  183. return deepMerge(style, addStyle(this.customStyle));
  184. },
  185. },
  186. // #ifdef VUE3
  187. emits: ['update:modelValue', 'linechange', 'focus', 'blur', 'change', 'confirm', 'keyboardheightchange'],
  188. // #endif
  189. methods: {
  190. addStyle,
  191. addUnit,
  192. // 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用
  193. setFormatter(e) {
  194. this.innerFormatter = e
  195. },
  196. onFocus(e) {
  197. this.$emit("focus", e);
  198. },
  199. onBlur(e) {
  200. this.$emit("blur", e);
  201. // 尝试调用u-form的验证方法
  202. formValidate(this, "blur");
  203. },
  204. onLinechange(e) {
  205. this.$emit("linechange", e);
  206. },
  207. onInput(e) {
  208. let { value = "" } = e.detail || {};
  209. // 格式化过滤方法
  210. const formatter = this.formatter || this.innerFormatter
  211. const formatValue = formatter(value)
  212. // 为了避免props的单向数据流特性,需要先将innerValue值设置为当前值,再在$nextTick中重新赋予设置后的值才有效
  213. this.innerValue = value
  214. this.$nextTick(() => {
  215. this.innerValue = formatValue;
  216. this.valueChange();
  217. })
  218. },
  219. // 内容发生变化,进行处理
  220. valueChange() {
  221. const value = this.innerValue;
  222. this.$nextTick(() => {
  223. // #ifdef VUE3
  224. this.$emit("update:modelValue", value);
  225. // #endif
  226. // #ifdef VUE2
  227. this.$emit("input", value);
  228. // #endif
  229. // 标识value值的变化是由内部引起的
  230. this.changeFromInner = true;
  231. this.$emit("change", value);
  232. // 尝试调用u-form的验证方法
  233. formValidate(this, "change");
  234. });
  235. },
  236. onConfirm(e) {
  237. this.$emit("confirm", e);
  238. },
  239. onKeyboardheightchange(e) {
  240. this.$emit("keyboardheightchange", e);
  241. },
  242. },
  243. };
  244. </script>
  245. <style lang="scss" scoped>
  246. @import "../../libs/css/components.scss";
  247. .u-textarea {
  248. border-radius: 4px;
  249. background-color: #fff;
  250. position: relative;
  251. @include flex;
  252. flex: 1;
  253. padding: 9px;
  254. &--radius {
  255. border-radius: 4px;
  256. }
  257. &--no-radius {
  258. border-radius: 0;
  259. }
  260. &--disabled {
  261. background-color: #f5f7fa;
  262. }
  263. &__field {
  264. flex: 1;
  265. font-size: 15px;
  266. color: $u-content-color;
  267. width: 100%;
  268. }
  269. &__count {
  270. position: absolute;
  271. right: 5px;
  272. bottom: 2px;
  273. font-size: 12px;
  274. color: $u-tips-color;
  275. background-color: #ffffff;
  276. padding: 1px 4px;
  277. }
  278. }
  279. </style>