index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/classNames'
  3. import styleToCssString from '../helpers/styleToCssString'
  4. baseComponent({
  5. properties: {
  6. prefixCls: {
  7. type: String,
  8. value: 'wux-textarea',
  9. },
  10. label: {
  11. type: String,
  12. value: '',
  13. },
  14. extra: {
  15. type: String,
  16. value: '',
  17. },
  18. defaultValue: {
  19. type: String,
  20. value: '',
  21. },
  22. value: {
  23. type: String,
  24. value: '',
  25. observer(newVal) {
  26. if (this.data.controlled) {
  27. this.updated(newVal)
  28. }
  29. },
  30. },
  31. controlled: {
  32. type: Boolean,
  33. value: false,
  34. },
  35. placeholder: {
  36. type: String,
  37. value: '',
  38. },
  39. placeholderStyle: {
  40. type: [String, Object],
  41. value: '',
  42. observer(newVal) {
  43. this.setData({
  44. extStyle: styleToCssString(newVal),
  45. })
  46. },
  47. },
  48. placeholderClass: {
  49. type: String,
  50. value: 'textarea-placeholder',
  51. },
  52. disabled: {
  53. type: Boolean,
  54. value: false,
  55. },
  56. maxlength: {
  57. type: Number,
  58. value: 140,
  59. },
  60. autoHeight: {
  61. type: Boolean,
  62. value: false,
  63. },
  64. cursorSpacing: {
  65. type: Number,
  66. value: 11,
  67. },
  68. focus: {
  69. type: Boolean,
  70. value: false,
  71. },
  72. cursor: {
  73. type: Number,
  74. value: -1,
  75. },
  76. showConfirmBar: {
  77. type: Boolean,
  78. value: true,
  79. },
  80. selectionStart: {
  81. type: Number,
  82. value: -1,
  83. },
  84. selectionEnd: {
  85. type: Number,
  86. value: -1,
  87. },
  88. adjustPosition: {
  89. type: Boolean,
  90. value: true,
  91. },
  92. rows: {
  93. type: Number,
  94. value: 1,
  95. observer: 'updateHeight',
  96. },
  97. hasCount: {
  98. type: Boolean,
  99. value: false,
  100. },
  101. clear: {
  102. type: Boolean,
  103. value: false,
  104. },
  105. error: {
  106. type: Boolean,
  107. value: false,
  108. },
  109. },
  110. data: {
  111. inputValue: '',
  112. inputFocus: false,
  113. inputRows: 1,
  114. inputHeight: '',
  115. extStyle: '',
  116. },
  117. computed: {
  118. classes() {
  119. const { prefixCls, disabled, inputFocus, error: hasError, hasCount } = this.data
  120. const wrap = classNames(prefixCls, {
  121. [`${prefixCls}--focus`]: inputFocus,
  122. [`${prefixCls}--disabled`]: disabled,
  123. [`${prefixCls}--error`]: hasError,
  124. [`${prefixCls}--has-count`]: hasCount,
  125. })
  126. const label = `${prefixCls}__label`
  127. const control = `${prefixCls}__control`
  128. const item = `${prefixCls}__item`
  129. const clear = `${prefixCls}__clear`
  130. const error = `${prefixCls}__error`
  131. const extra = `${prefixCls}__extra`
  132. const count = `${prefixCls}__count`
  133. const current = `${prefixCls}__current`
  134. return {
  135. wrap,
  136. label,
  137. control,
  138. item,
  139. clear,
  140. error,
  141. extra,
  142. count,
  143. current,
  144. }
  145. },
  146. },
  147. methods: {
  148. updateHeight(val = this.data.rows) {
  149. // rows 取值为大于或等于 1 的正整数
  150. const rows = Math.max(1, parseInt(val))
  151. const { prefixCls, inputRows } = this.data
  152. if (inputRows !== rows) {
  153. wx
  154. .createSelectorQuery()
  155. .in(this).select(`.${prefixCls}__item`)
  156. .boundingClientRect((rect) => {
  157. if (rect) {
  158. const lineHeight = inputRows > 1 ? rect.height / inputRows : rect.height
  159. const inputHeight = lineHeight * rows
  160. this.setData({
  161. inputRows: rows,
  162. inputHeight,
  163. })
  164. }
  165. })
  166. .exec()
  167. }
  168. },
  169. updated(inputValue) {
  170. if (this.data.inputValue !== inputValue) {
  171. this.setData({
  172. inputValue,
  173. })
  174. }
  175. },
  176. onChange(e) {
  177. if (!this.data.controlled) {
  178. this.updated(e.detail.value)
  179. }
  180. this.triggerEvent('change', e.detail)
  181. },
  182. onFocus(e) {
  183. this.clearTimer()
  184. this.setData({
  185. inputFocus: true,
  186. })
  187. this.triggerEvent('focus', e.detail)
  188. },
  189. onBlur(e) {
  190. this.setTimer()
  191. this.triggerEvent('blur', e.detail)
  192. },
  193. onConfirm(e) {
  194. this.triggerEvent('confirm', e.detail)
  195. },
  196. onClear() {
  197. const { controlled, inputValue } = this.data
  198. this.setData({
  199. inputValue: controlled ? inputValue : '',
  200. })
  201. this.triggerEvent('clear', { value: '' })
  202. },
  203. onError() {
  204. this.triggerEvent('error', { value: this.data.inputValue })
  205. },
  206. onLineChange(e) {
  207. this.triggerEvent('linechange', e.detail)
  208. },
  209. setTimer() {
  210. this.clearTimer()
  211. this.timeout = setTimeout(() => {
  212. this.setData({
  213. inputFocus: false,
  214. })
  215. }, 200)
  216. },
  217. clearTimer() {
  218. if (this.timeout) {
  219. clearTimeout(this.timeout)
  220. this.timeout = null
  221. }
  222. },
  223. },
  224. attached() {
  225. const { defaultValue, value, controlled } = this.data
  226. const inputValue = controlled ? value : defaultValue
  227. this.updated(inputValue)
  228. },
  229. ready() {
  230. this.updateHeight()
  231. },
  232. })