u-textarea.vue 11 KB

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