u-textarea.vue 9.2 KB

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