index.vue 9.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
<template>
2
  <uni-textarea v-on="$listeners">
3
    <div class="uni-textarea-wrapper">
fxy060608's avatar
fxy060608 已提交
4
      <div
5
        v-show="!(composing || valueSync.length)"
fxy060608's avatar
fxy060608 已提交
6 7 8
        ref="placeholder"
        :style="placeholderStyle"
        :class="placeholderClass"
9
        class="uni-textarea-placeholder"
10 11
        v-text="placeholder"
      />
12 13
      <div
        ref="line"
fxy060608's avatar
fxy060608 已提交
14
        class="uni-textarea-line"
15 16
        v-text="' '"
      />
17 18
      <div class="uni-textarea-compute">
        <div
19
          v-for="(item, index) in valueCompute"
fxy060608's avatar
fxy060608 已提交
20
          :key="index"
21 22
          v-text="item.trim() ? item : '.'"
        />
23 24
        <v-uni-resize-sensor
          ref="sensor"
fxy060608's avatar
fxy060608 已提交
25 26
          @resize="_resize"
        />
27
      </div>
fxy060608's avatar
fxy060608 已提交
28
      <textarea
29
        v-if="!disabled || !fixColor"
fxy060608's avatar
fxy060608 已提交
30 31
        ref="textarea"
        v-model="valueSync"
32
        v-keyboard
33
        v-field
fxy060608's avatar
fxy060608 已提交
34 35
        :disabled="disabled"
        :maxlength="maxlengthNumber"
36 37
        :class="{ 'uni-textarea-textarea-fix-margin': fixMargin }"
        :style="{ 'overflow-y': autoHeight ? 'hidden' : 'auto' }"
38
        :enterkeyhint="confirmType"
fxy060608's avatar
fxy060608 已提交
39
        class="uni-textarea-textarea"
40 41 42
        @change.stop
        @compositionstart.stop="_onCompositionstart"
        @compositionend.stop="_onCompositionend"
43 44 45 46
        @input.stop="_onInput"
        @focus="_onFocus"
        @blur="_onBlur"
        @touchstart.passive="_onTouchstart"
47 48
        @keyup.enter="_onKeyUpEnter"
        @keydown.enter="_onKeyDownEnter"
49
      />
50
      <!-- fix: 禁止 readonly 状态获取焦点 -->
51 52 53 54 55 56 57 58 59 60
      <textarea
        v-if="disabled && fixColor"
        ref="textarea"
        :value="valueSync"
        tabindex="-1"
        :readonly="disabled"
        :maxlength="maxlengthNumber"
        :class="{ 'uni-textarea-textarea-fix-margin': fixMargin }"
        :style="{ 'overflow-y': autoHeight ? 'hidden' : 'auto' }"
        class="uni-textarea-textarea"
61
        @focus="($event) => $event.target.blur()"
62
      />
fxy060608's avatar
fxy060608 已提交
63 64 65 66 67
    </div>
  </uni-textarea>
</template>
<script>
import {
68
  field
fxy060608's avatar
fxy060608 已提交
69
} from 'uni-mixins'
70
const DARK_TEST_STRING = '(prefers-color-scheme: dark)'
fxy060608's avatar
fxy060608 已提交
71 72
export default {
  name: 'Textarea',
73
  mixins: [field],
fxy060608's avatar
fxy060608 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  props: {
    name: {
      type: String,
      default: ''
    },
    maxlength: {
      type: [Number, String],
      default: 140
    },
    placeholder: {
      type: String,
      default: ''
    },
    disabled: {
      type: [Boolean, String],
      default: false
    },
    placeholderClass: {
      type: String,
93
      default: 'textarea-placeholder'
fxy060608's avatar
fxy060608 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    },
    placeholderStyle: {
      type: String,
      default: ''
    },
    autoHeight: {
      type: [Boolean, String],
      default: false
    },
    cursor: {
      type: [Number, String],
      default: -1
    },
    selectionStart: {
      type: [Number, String],
      default: -1
    },
    selectionEnd: {
      type: [Number, String],
      default: -1
114 115 116 117
    },
    confirmType: {
      type: String,
      default: ''
fxy060608's avatar
fxy060608 已提交
118 119 120 121
    }
  },
  data () {
    return {
122
      valueComposition: '',
123
      composing: false,
fxy060608's avatar
fxy060608 已提交
124 125
      focusSync: this.focus,
      height: 0,
126
      focusChangeSource: '',
127
      // iOS 13 以下版本需要修正边距
128 129 130
      fixMargin: String(navigator.platform).indexOf('iP') === 0 && String(navigator.vendor).indexOf('Apple') === 0 && window.matchMedia(DARK_TEST_STRING).media !== DARK_TEST_STRING,
      // Safari 14 以上修正禁用状态颜色
      fixColor: String(navigator.vendor).indexOf('Apple') === 0 && CSS.supports('image-orientation:from-image')
fxy060608's avatar
fxy060608 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    }
  },
  computed: {
    maxlengthNumber () {
      var maxlength = Number(this.maxlength)
      return isNaN(maxlength) ? 140 : maxlength
    },
    cursorNumber () {
      var cursor = Number(this.cursor)
      return isNaN(cursor) ? -1 : cursor
    },
    selectionStartNumber () {
      var selectionStart = Number(this.selectionStart)
      return isNaN(selectionStart) ? -1 : selectionStart
    },
    selectionEndNumber () {
      var selectionEnd = Number(this.selectionEnd)
      return isNaN(selectionEnd) ? -1 : selectionEnd
149 150
    },
    valueCompute () {
151
      return (this.composing ? this.valueComposition : this.valueSync).split('\n')
152 153 154
    },
    isDone () {
      return ['done', 'go', 'next', 'search', 'send'].includes(this.confirmType)
fxy060608's avatar
fxy060608 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    }
  },
  watch: {
    focus (val) {
      if (val) {
        this.focusChangeSource = 'focus'
      }
    },
    focusSync (val) {
      this.$emit('update:focus', val)
      this._checkSelection()
      this._checkCursor()
    },
    cursorNumber () {
      this._checkCursor()
    },
    selectionStartNumber () {
      this._checkSelection()
    },
    selectionEndNumber () {
      this._checkSelection()
    },
    height (height) {
178 179 180 181
      let lineHeight = parseFloat(getComputedStyle(this.$el).lineHeight)
      if (isNaN(lineHeight)) {
        lineHeight = this.$refs.line.offsetHeight
      }
fxy060608's avatar
fxy060608 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
      var lineCount = Math.round(height / lineHeight)
      this.$trigger('linechange', {}, {
        height,
        heightRpx: 750 / window.innerWidth * height,
        lineCount
      })
      if (this.autoHeight) {
        this.$el.style.height = this.height + 'px'
      }
    }
  },
  created () {
    this.$dispatch('Form', 'uni-form-group-update', {
      type: 'add',
      vm: this
    })
  },
  mounted () {
200 201 202
    this._resize({
      height: this.$refs.sensor.$el.offsetHeight
    })
203 204 205 206 207 208 209 210 211

    let $vm = this
    while ($vm) {
      const scopeId = $vm.$options._scopeId
      if (scopeId) {
        this.$refs.placeholder.setAttribute(scopeId, '')
      }
      $vm = $vm.$parent
    }
fxy060608's avatar
fxy060608 已提交
212 213 214 215 216 217 218 219
  },
  beforeDestroy () {
    this.$dispatch('Form', 'uni-form-group-update', {
      type: 'remove',
      vm: this
    })
  },
  methods: {
220 221 222 223 224 225 226 227 228 229 230
    _onKeyDownEnter: function ($event) {
      if (this.isDone) {
        $event.preventDefault()
      }
    },
    _onKeyUpEnter: function ($event) {
      if (this.isDone) {
        this._confirm($event)
        this.$refs.textarea.blur()
      }
    },
231
    _onFocus: function ($event) {
fxy060608's avatar
fxy060608 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
      this.focusSync = true
      this.$trigger('focus', $event, {
        value: this.valueSync
      })
    },
    _checkSelection () {
      if (this.focusSync && (!this.focusChangeSource) && this.selectionStartNumber > -1 && this.selectionEndNumber > -1) {
        this.$refs.textarea.selectionStart = this.selectionStartNumber
        this.$refs.textarea.selectionEnd = this.selectionEndNumber
      }
    },
    _checkCursor () {
      if (this.focusSync && (this.focusChangeSource === 'focus' || ((!this.focusChangeSource) && this.selectionStartNumber < 0 && this.selectionEndNumber < 0)) && this.cursorNumber > -1) {
        this.$refs.textarea.selectionEnd = this.$refs.textarea.selectionStart = this.cursorNumber
      }
    },
248
    _onBlur: function ($event) {
249 250 251 252 253
      // iOS 输入法 compositionend 事件可能晚于 blur
      if (this.composing) {
        this.composing = false
        this._onInput($event, true)
      }
fxy060608's avatar
fxy060608 已提交
254 255 256 257 258 259
      this.focusSync = false
      this.$trigger('blur', $event, {
        value: this.valueSync,
        cursor: this.$refs.textarea.selectionEnd
      })
    },
260
    _onCompositionstart ($event) {
261
      this.composing = true
fxy060608's avatar
fxy060608 已提交
262
    },
263
    _onCompositionend ($event) {
264 265 266 267 268
      if (this.composing) {
        this.composing = false
        // 部分输入法 compositionend 事件可能晚于 input
        this._onInput($event)
      }
fxy060608's avatar
fxy060608 已提交
269 270 271 272 273 274 275 276 277 278 279 280
    },
    // 暂无完成按钮,此功能未实现
    _confirm ($event) {
      this.$trigger('confirm', $event, {
        value: this.valueSync
      })
    },
    _linechange ($event) {
      this.$trigger('linechange', $event, {
        value: this.valueSync
      })
    },
281
    _onTouchstart () {
fxy060608's avatar
fxy060608 已提交
282 283
      this.focusChangeSource = 'touch'
    },
284 285 286
    _resize ({ height }) {
      this.height = height
    },
287 288
    _onInput ($event, force) {
      if (this.composing) {
289
        this.valueComposition = $event.target.value
290
        return
291
      }
292 293 294
      this.$triggerInput($event, {
        value: this.valueSync,
        cursor: this.$refs.textarea.selectionEnd
295
      }, force)
fxy060608's avatar
fxy060608 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    },
    _getFormData () {
      return {
        value: this.valueSync,
        key: this.name
      }
    },
    _resetFormData () {
      this.valueSync = ''
    }
  }
}
</script>

<style>
uni-textarea {
  width: 300px;
  height: 150px;
  display: block;
  position: relative;
  font-size: 16px;
317
  line-height: normal;
318 319
  white-space: pre-wrap;
  word-break: break-all;
320
  box-sizing: content-box !important;
321 322 323 324
}
uni-textarea[hidden] {
  display: none;
}
Q
qiang 已提交
325 326
.uni-textarea-wrapper,
.uni-textarea-placeholder,
327
.uni-textarea-line,
Q
qiang 已提交
328 329 330 331 332 333 334 335
.uni-textarea-compute,
.uni-textarea-textarea {
  outline: none;
  border: none;
  padding: 0;
  margin: 0;
  text-decoration: inherit;
}
336
.uni-textarea-wrapper {
Q
qiang 已提交
337
  display: block;
fxy060608's avatar
fxy060608 已提交
338 339 340 341 342
  position: relative;
  width: 100%;
  height: 100%;
}
.uni-textarea-placeholder,
343
.uni-textarea-line,
344
.uni-textarea-compute,
fxy060608's avatar
fxy060608 已提交
345 346 347 348 349 350
.uni-textarea-textarea {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
351 352
  white-space: inherit;
  word-break: inherit;
fxy060608's avatar
fxy060608 已提交
353 354 355
}
.uni-textarea-placeholder {
  color: grey;
Q
qiang 已提交
356
  overflow: hidden;
fxy060608's avatar
fxy060608 已提交
357
}
358
.uni-textarea-line,
359 360 361 362
.uni-textarea-compute {
  visibility: hidden;
  height: auto;
}
363 364 365
.uni-textarea-line {
  width: 1em;
}
fxy060608's avatar
fxy060608 已提交
366 367
.uni-textarea-textarea {
  resize: none;
Q
qiang 已提交
368 369
  background: none;
  color: inherit;
370
  opacity: 1;
Q
qiang 已提交
371 372 373 374 375 376 377
  font: inherit;
  line-height: inherit;
  letter-spacing: inherit;
  text-align: inherit;
  text-indent: inherit;
  text-transform: inherit;
  text-shadow: inherit;
fxy060608's avatar
fxy060608 已提交
378
}
379
/* 用于解决 iOS textarea 内部默认边距 */
380
.uni-textarea-textarea-fix-margin {
381 382 383 384
  width: auto;
  right: 0;
  margin: 0 -3px;
}
385 386 387 388
.uni-textarea-textarea:disabled {
  /* 用于重置iOS14以下禁用状态文字颜色 */
  -webkit-text-fill-color: currentcolor;
}
fxy060608's avatar
fxy060608 已提交
389
</style>