index.vue 8.9 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
<template>
2
  <uni-textarea
3
    @change.stop
fxy060608's avatar
fxy060608 已提交
4 5
    v-on="$listeners"
  >
6
    <div class="uni-textarea-wrapper">
fxy060608's avatar
fxy060608 已提交
7
      <div
8
        v-show="!(composition || valueSync.length)"
fxy060608's avatar
fxy060608 已提交
9 10 11
        ref="placeholder"
        :style="placeholderStyle"
        :class="placeholderClass"
12
        class="uni-textarea-placeholder"
13 14
        v-text="placeholder"
      />
15 16
      <div
        ref="line"
fxy060608's avatar
fxy060608 已提交
17
        class="uni-textarea-line"
18 19
        v-text="' '"
      />
20 21
      <div class="uni-textarea-compute">
        <div
22
          v-for="(item, index) in valueCompute"
fxy060608's avatar
fxy060608 已提交
23
          :key="index"
24 25
          v-text="item.trim() ? item : '.'"
        />
26 27
        <v-uni-resize-sensor
          ref="sensor"
fxy060608's avatar
fxy060608 已提交
28 29
          @resize="_resize"
        />
30
      </div>
fxy060608's avatar
fxy060608 已提交
31
      <textarea
32
        v-if="!disabled || !fixColor"
fxy060608's avatar
fxy060608 已提交
33 34
        ref="textarea"
        v-model="valueSync"
35
        v-keyboard
fxy060608's avatar
fxy060608 已提交
36 37
        :disabled="disabled"
        :maxlength="maxlengthNumber"
38
        :autofocus="autoFocus || focus"
39 40
        :class="{ 'uni-textarea-textarea-fix-margin': fixMargin }"
        :style="{ 'overflow-y': autoHeight ? 'hidden' : 'auto' }"
fxy060608's avatar
fxy060608 已提交
41 42 43
        class="uni-textarea-textarea"
        @compositionstart="_compositionstart"
        @compositionend="_compositionend"
44
        @input.stop="_input"
fxy060608's avatar
fxy060608 已提交
45 46 47
        @focus="_focus"
        @blur="_blur"
        @touchstart.passive="_touchstart"
48
      />
49 50 51 52 53 54 55 56 57 58 59
      <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"
      />
fxy060608's avatar
fxy060608 已提交
60 61 62 63 64
    </div>
  </uni-textarea>
</template>
<script>
import {
65
  baseInput
fxy060608's avatar
fxy060608 已提交
66
} from 'uni-mixins'
67
const DARK_TEST_STRING = '(prefers-color-scheme: dark)'
fxy060608's avatar
fxy060608 已提交
68 69
export default {
  name: 'Textarea',
70
  mixins: [baseInput],
fxy060608's avatar
fxy060608 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  props: {
    name: {
      type: String,
      default: ''
    },
    maxlength: {
      type: [Number, String],
      default: 140
    },
    placeholder: {
      type: String,
      default: ''
    },
    disabled: {
      type: [Boolean, String],
      default: false
    },
    focus: {
      type: [Boolean, String],
      default: false
    },
    autoFocus: {
      type: [Boolean, String],
      default: false
    },
    placeholderClass: {
      type: String,
98
      default: 'textarea-placeholder'
fxy060608's avatar
fxy060608 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    },
    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
    }
  },
  data () {
    return {
123
      valueComposition: '',
fxy060608's avatar
fxy060608 已提交
124 125 126
      composition: false,
      focusSync: this.focus,
      height: 0,
127
      focusChangeSource: '',
128
      // iOS 13 以下版本需要修正边距
129 130 131
      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 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    }
  },
  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
150 151 152
    },
    valueCompute () {
      return (this.composition ? this.valueComposition : this.valueSync).split('\n')
fxy060608's avatar
fxy060608 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    }
  },
  watch: {
    focus (val) {
      if (val) {
        this.focusChangeSource = 'focus'
        if (this.$refs.textarea) {
          this.$refs.textarea.focus()
        }
      } else {
        if (this.$refs.textarea) {
          this.$refs.textarea.blur()
        }
      }
    },
    focusSync (val) {
      this.$emit('update:focus', val)
      this._checkSelection()
      this._checkCursor()
    },
    cursorNumber () {
      this._checkCursor()
    },
    selectionStartNumber () {
      this._checkSelection()
    },
    selectionEndNumber () {
      this._checkSelection()
    },
    height (height) {
183 184 185 186
      let lineHeight = parseFloat(getComputedStyle(this.$el).lineHeight)
      if (isNaN(lineHeight)) {
        lineHeight = this.$refs.line.offsetHeight
      }
fxy060608's avatar
fxy060608 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
      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 () {
205 206 207
    this._resize({
      height: this.$refs.sensor.$el.offsetHeight
    })
208 209 210 211 212 213 214 215 216

    let $vm = this
    while ($vm) {
      const scopeId = $vm.$options._scopeId
      if (scopeId) {
        this.$refs.placeholder.setAttribute(scopeId, '')
      }
      $vm = $vm.$parent
    }
fxy060608's avatar
fxy060608 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  },
  beforeDestroy () {
    this.$dispatch('Form', 'uni-form-group-update', {
      type: 'remove',
      vm: this
    })
  },
  methods: {
    _focus: function ($event) {
      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
      }
    },
    _blur: function ($event) {
      this.focusSync = false
      this.$trigger('blur', $event, {
        value: this.valueSync,
        cursor: this.$refs.textarea.selectionEnd
      })
    },
    _compositionstart ($event) {
      this.composition = true
    },
    _compositionend ($event) {
      this.composition = false
254 255
      // 部分输入法 compositionend 事件可能晚于 input
      this._input($event)
fxy060608's avatar
fxy060608 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    },
    // 暂无完成按钮,此功能未实现
    _confirm ($event) {
      this.$trigger('confirm', $event, {
        value: this.valueSync
      })
    },
    _linechange ($event) {
      this.$trigger('linechange', $event, {
        value: this.valueSync
      })
    },
    _touchstart () {
      this.focusChangeSource = 'touch'
    },
271 272 273 274 275 276
    _resize ({ height }) {
      this.height = height
    },
    _input ($event) {
      if (this.composition) {
        this.valueComposition = $event.target.value
277
        return
278
      }
279 280 281 282
      this.$triggerInput($event, {
        value: this.valueSync,
        cursor: this.$refs.textarea.selectionEnd
      })
fxy060608's avatar
fxy060608 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    },
    _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;
304
  line-height: normal;
305 306
  white-space: pre-wrap;
  word-break: break-all;
307 308 309 310
}
uni-textarea[hidden] {
  display: none;
}
Q
qiang 已提交
311 312
.uni-textarea-wrapper,
.uni-textarea-placeholder,
313
.uni-textarea-line,
Q
qiang 已提交
314 315 316 317 318 319 320 321
.uni-textarea-compute,
.uni-textarea-textarea {
  outline: none;
  border: none;
  padding: 0;
  margin: 0;
  text-decoration: inherit;
}
322
.uni-textarea-wrapper {
Q
qiang 已提交
323
  display: block;
fxy060608's avatar
fxy060608 已提交
324 325 326 327 328
  position: relative;
  width: 100%;
  height: 100%;
}
.uni-textarea-placeholder,
329
.uni-textarea-line,
330
.uni-textarea-compute,
fxy060608's avatar
fxy060608 已提交
331 332 333 334 335 336
.uni-textarea-textarea {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
337 338
  white-space: inherit;
  word-break: inherit;
fxy060608's avatar
fxy060608 已提交
339 340 341
}
.uni-textarea-placeholder {
  color: grey;
Q
qiang 已提交
342
  overflow: hidden;
fxy060608's avatar
fxy060608 已提交
343
}
344
.uni-textarea-line,
345 346 347 348
.uni-textarea-compute {
  visibility: hidden;
  height: auto;
}
349 350 351
.uni-textarea-line {
  width: 1em;
}
fxy060608's avatar
fxy060608 已提交
352 353
.uni-textarea-textarea {
  resize: none;
Q
qiang 已提交
354 355
  background: none;
  color: inherit;
356
  opacity: 1;
Q
qiang 已提交
357 358 359 360 361 362 363
  font: inherit;
  line-height: inherit;
  letter-spacing: inherit;
  text-align: inherit;
  text-indent: inherit;
  text-transform: inherit;
  text-shadow: inherit;
fxy060608's avatar
fxy060608 已提交
364
}
365
/* 用于解决 iOS textarea 内部默认边距 */
366
.uni-textarea-textarea-fix-margin {
367 368 369 370
  width: auto;
  right: 0;
  margin: 0 -3px;
}
371 372 373 374
.uni-textarea-textarea:disabled {
  /* 用于重置iOS14以下禁用状态文字颜色 */
  -webkit-text-fill-color: currentcolor;
}
fxy060608's avatar
fxy060608 已提交
375
</style>