u-textarea.vue 9.3 KB

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