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.
 
 
 

351 rivejä
14 KiB

  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="placeholderStyleInner"
  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="countStyle"
  36. v-if="count"
  37. >{{ valueLength }}/{{ maxlength }}</text
  38. >
  39. <!-- #endif -->
  40. </view>
  41. </template>
  42. <script>
  43. import { props } from "./props.js";
  44. import { mpMixin } from '../../libs/mixin/mpMixin';
  45. import { mixin } from '../../libs/mixin/mixin';
  46. import { addStyle, addUnit, deepMerge, formValidate, os } from '../../libs/function/index';
  47. /**
  48. * Textarea 文本域
  49. * @description 文本域此组件满足了可能出现的表单信息补充,编辑等实际逻辑的功能,内置了字数校验等
  50. * @tutorial https://uview-plus.jiangruyi.com/components/textarea.html
  51. *
  52. * @property {String | Number} value 输入框的内容
  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. *
  76. * @event {Function(e)} focus 输入框聚焦时触发,event.detail = { value, height },height 为键盘高度
  77. * @event {Function(e)} blur 输入框失去焦点时触发,event.detail = {value, cursor}
  78. * @event {Function(e)} linechange 输入框行数变化时调用,event.detail = {height: 0, heightRpx: 0, lineCount: 0}
  79. * @event {Function(e)} input 当键盘输入时,触发 input 事件
  80. * @event {Function(e)} confirm 点击完成时, 触发 confirm 事件
  81. * @event {Function(e)} keyboardheightchange 键盘高度发生变化的时候触发此事件
  82. * @example <up-textarea v-model="value1" placeholder="请输入内容" ></up-textarea>
  83. */
  84. export default {
  85. name: "u-textarea",
  86. mixins: [mpMixin, mixin, props],
  87. data() {
  88. return {
  89. // 输入框的值
  90. innerValue: "",
  91. // 是否处于获得焦点状态
  92. focused: false,
  93. // value是否第一次变化,在watch中,由于加入immediate属性,会在第一次触发,此时不应该认为value发生了变化
  94. firstChange: true,
  95. // value绑定值的变化是由内部还是外部引起的
  96. changeFromInner: false,
  97. // 过滤处理方法
  98. innerFormatter: value => value
  99. }
  100. },
  101. created() {
  102. },
  103. watch: {
  104. // #ifdef VUE2
  105. value: {
  106. immediate: true,
  107. handler(newVal, oldVal) {
  108. this.innerValue = this.normalizeValue(newVal);
  109. /* #ifdef H5 */
  110. // 在H5中,外部value变化后,修改input中的值,不会触发@input事件,此时手动调用值变化方法
  111. if (
  112. this.firstChange === false &&
  113. this.changeFromInner === false
  114. ) {
  115. this.valueChange();
  116. }
  117. /* #endif */
  118. this.firstChange = false;
  119. // 重置changeFromInner的值为false,标识下一次引起默认为外部引起的
  120. this.changeFromInner = false;
  121. },
  122. },
  123. // #endif
  124. // #ifdef VUE3
  125. modelValue: {
  126. immediate: true,
  127. handler(newVal, oldVal) {
  128. this.innerValue = this.normalizeValue(newVal);
  129. /* #ifdef H5 */
  130. // 在H5中,外部value变化后,修改input中的值,不会触发@input事件,此时手动调用值变化方法
  131. if (
  132. this.firstChange === false &&
  133. this.changeFromInner === false
  134. ) {
  135. this.valueChange();
  136. }
  137. /* #endif */
  138. this.firstChange = false;
  139. // 重置changeFromInner的值为false,标识下一次引起默认为外部引起的
  140. this.changeFromInner = false;
  141. },
  142. }
  143. // #endif
  144. },
  145. computed: {
  146. // 字数统计安全取值,避免 v-model 为 null/undefined/number 时访问 length 报错
  147. valueLength() {
  148. return String(this.innerValue ?? '').length
  149. },
  150. placeholderStyleInner() {
  151. if (this.placeholderStyle) {
  152. return addStyle(this.placeholderStyle, typeof this.placeholderStyle === 'string' ? 'string' : 'object')
  153. }
  154. return `color: ${this.upThemeVar('--up-tips-color', this.$u?.color?.tipsColor || '#909399')}`
  155. },
  156. countStyle() {
  157. if (this.disabled) return { backgroundColor: 'transparent' }
  158. return {
  159. backgroundColor: this.upThemeVar('--up-card-bg-color', '#ffffff'),
  160. color: this.upThemeVar('--up-tips-color', '#909193')
  161. }
  162. },
  163. fieldStyle() {
  164. const style = {
  165. height: addUnit(this.height),
  166. backgroundColor: 'transparent',
  167. color: this.upThemeVar('--up-content-color', '#606266'),
  168. caretColor: this.upThemeVar('--up-main-color', '#303133')
  169. };
  170. if (this.autoHeight) {
  171. style['height'] = 'auto';
  172. style['minHeight'] = addUnit(this.height);
  173. }
  174. return style;
  175. },
  176. // 组件的类名
  177. textareaClass() {
  178. let classes = [],
  179. { border, disabled } = this;
  180. border === "surround" &&
  181. (classes = classes.concat(["u-textarea--radius"]));
  182. border === "bottom" &&
  183. (classes = classes.concat([
  184. "u-textarea--no-radius",
  185. ]));
  186. disabled && classes.push("u-textarea--disabled");
  187. return classes.join(" ");
  188. },
  189. textareaBorderColor() {
  190. const lightBorder = this.upThemeVar('--up-border-color', '#dadbde');
  191. return this.upThemeVar(
  192. '--up-input-border-color',
  193. this.upThemeIsDark ? 'rgba(255, 255, 255, 0.08)' : lightBorder
  194. );
  195. },
  196. // 组件的样式
  197. textareaStyle() {
  198. const style = {};
  199. style.backgroundColor = this.disabled
  200. ? this.upThemeVar('--up-bg-color', '#f5f7fa')
  201. : this.upThemeVar('--up-card-bg-color', '#ffffff');
  202. style.color = this.upThemeVar('--up-content-color', '#606266');
  203. if (this.border === "surround") {
  204. style.borderWidth = "0.5px";
  205. style.borderStyle = "solid";
  206. style.borderColor = this.textareaBorderColor;
  207. }
  208. if (this.border === "bottom") {
  209. style.borderBottomWidth = "0.5px";
  210. style.borderBottomStyle = "solid";
  211. style.borderBottomColor = this.textareaBorderColor;
  212. }
  213. // #ifdef APP-NVUE
  214. // 由于textarea在安卓nvue上的差异性,需要额外再调整其内边距
  215. if (os() === "android") {
  216. style.paddingTop = "6px";
  217. style.paddingLeft = "9px";
  218. style.paddingBottom = "3px";
  219. style.paddingRight = "6px";
  220. }
  221. // #endif
  222. return deepMerge(style, addStyle(this.customStyle));
  223. },
  224. },
  225. // #ifdef VUE3
  226. emits: ['update:modelValue', 'linechange', 'focus', 'blur', 'change', 'confirm', 'keyboardheightchange'],
  227. // #endif
  228. methods: {
  229. addStyle,
  230. addUnit,
  231. // 统一将外部值归一为可安全展示/统计的内容
  232. normalizeValue(value) {
  233. if (value === null || value === undefined) return ''
  234. // textarea 展示与统计均按字符串处理
  235. return typeof value === 'number' ? String(value) : value
  236. },
  237. // 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用
  238. setFormatter(e) {
  239. this.innerFormatter = e
  240. },
  241. onFocus(e) {
  242. this.$emit("focus", e);
  243. },
  244. onBlur(e) {
  245. this.$emit("blur", e);
  246. // 尝试调用u-form的验证方法
  247. formValidate(this, "blur");
  248. },
  249. onLinechange(e) {
  250. this.$emit("linechange", e);
  251. },
  252. onInput(e) {
  253. let { value } = e.detail || {};
  254. value = this.normalizeValue(value)
  255. // 格式化过滤方法
  256. const formatter = this.formatter || this.innerFormatter
  257. const formatValue = this.normalizeValue(formatter(value))
  258. // 为了避免props的单向数据流特性,需要先将innerValue值设置为当前值,再在$nextTick中重新赋予设置后的值才有效
  259. this.innerValue = value
  260. this.$nextTick(() => {
  261. this.innerValue = formatValue;
  262. this.valueChange();
  263. })
  264. },
  265. // 内容发生变化,进行处理
  266. valueChange() {
  267. const value = this.innerValue;
  268. this.$nextTick(() => {
  269. // #ifdef VUE3
  270. this.$emit("update:modelValue", value);
  271. // #endif
  272. // #ifdef VUE2
  273. this.$emit("input", value);
  274. // #endif
  275. // 标识value值的变化是由内部引起的
  276. this.changeFromInner = true;
  277. this.$emit("change", value);
  278. // 尝试调用u-form的验证方法
  279. formValidate(this, "change");
  280. });
  281. },
  282. onConfirm(e) {
  283. this.$emit("confirm", e);
  284. },
  285. onKeyboardheightchange(e) {
  286. this.$emit("keyboardheightchange", e);
  287. },
  288. },
  289. };
  290. </script>
  291. <style lang="scss" scoped>
  292. .u-textarea {
  293. border-radius: 4px;
  294. background-color: var(--up-card-bg-color, #fff);
  295. position: relative;
  296. @include flex;
  297. flex: 1;
  298. padding: 9px;
  299. &--radius {
  300. border-radius: 4px;
  301. }
  302. &--no-radius {
  303. border-radius: 0;
  304. }
  305. &--disabled {
  306. background-color: var(--up-bg-color, #f5f7fa);
  307. }
  308. &__field {
  309. flex: 1;
  310. font-size: 15px;
  311. color: $u-content-color;
  312. background-color: transparent;
  313. border: none;
  314. outline: none;
  315. width: 100%;
  316. /* #ifdef H5 */
  317. :deep(.uni-textarea-wrapper),
  318. :deep(.uni-textarea-textarea) {
  319. background-color: transparent;
  320. }
  321. :deep(.uni-textarea-textarea) {
  322. color: inherit;
  323. border: none;
  324. outline: none;
  325. }
  326. :deep(.uni-textarea-placeholder) {
  327. color: var(--up-tips-color, #909193);
  328. }
  329. /* #endif */
  330. }
  331. &__count {
  332. position: absolute;
  333. right: 5px;
  334. bottom: 2px;
  335. font-size: 12px;
  336. color: $u-tips-color;
  337. background-color: var(--up-card-bg-color, #ffffff);
  338. padding: 1px 4px;
  339. }
  340. }
  341. </style>