综合办公系统
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.

uv-textarea.vue 8.8KB

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