index.vue 8.7 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 8 9 10 11
      <div
        v-show="!(composition||valueSync.length)"
        ref="placeholder"
        :style="placeholderStyle"
        :class="placeholderClass"
12
        class="uni-textarea-placeholder"
fxy060608's avatar
fxy060608 已提交
13 14 15
      >
        {{ placeholder }}
      </div>
16 17
      <div
        ref="line"
fxy060608's avatar
fxy060608 已提交
18 19 20 21
        class="uni-textarea-line"
      >
&nbsp;
      </div>
22 23 24
      <div class="uni-textarea-compute">
        <div
          v-for="(item,index) in valueCompute"
fxy060608's avatar
fxy060608 已提交
25 26 27 28
          :key="index"
        >
          {{ item.trim() ? item : '.' }}
        </div>
29 30
        <v-uni-resize-sensor
          ref="sensor"
fxy060608's avatar
fxy060608 已提交
31 32
          @resize="_resize"
        />
33
      </div>
fxy060608's avatar
fxy060608 已提交
34 35 36 37 38 39
      <textarea
        ref="textarea"
        v-model="valueSync"
        :disabled="disabled"
        :maxlength="maxlengthNumber"
        :autofocus="autoFocus"
40
        :class="{'uni-textarea-textarea-fix-margin': fixMargin}"
Q
qiang 已提交
41
        :style="{'overflow-y': autoHeight? 'hidden':'auto'}"
fxy060608's avatar
fxy060608 已提交
42 43 44
        class="uni-textarea-textarea"
        @compositionstart="_compositionstart"
        @compositionend="_compositionend"
45
        @input.stop="_input"
fxy060608's avatar
fxy060608 已提交
46 47 48
        @focus="_focus"
        @blur="_blur"
        @touchstart.passive="_touchstart"
49
      />
fxy060608's avatar
fxy060608 已提交
50 51 52 53 54
    </div>
  </uni-textarea>
</template>
<script>
import {
55 56
  emitter,
  keyboard
fxy060608's avatar
fxy060608 已提交
57
} from 'uni-mixins'
58
const DARK_TEST_STRING = '(prefers-color-scheme: dark)'
fxy060608's avatar
fxy060608 已提交
59 60
export default {
  name: 'Textarea',
61
  mixins: [emitter, keyboard],
fxy060608's avatar
fxy060608 已提交
62 63 64 65 66 67 68 69 70 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
  model: {
    prop: 'value',
    event: 'update:value'
  },
  props: {
    name: {
      type: String,
      default: ''
    },
    value: {
      type: [String, Number],
      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,
97
      default: 'textarea-placeholder'
fxy060608's avatar
fxy060608 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    },
    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 {
Q
qiang 已提交
122
      valueSync: this._getValueString(this.value),
123
      valueComposition: '',
fxy060608's avatar
fxy060608 已提交
124 125 126
      composition: false,
      focusSync: this.focus,
      height: 0,
127
      focusChangeSource: '',
128 129
      // iOS 13 以下版本需要修正边距
      fixMargin: String(navigator.platform).indexOf('iP') === 0 && String(navigator.vendor).indexOf('Apple') === 0 && window.matchMedia(DARK_TEST_STRING).media !== DARK_TEST_STRING
fxy060608's avatar
fxy060608 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    }
  },
  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
148 149 150
    },
    valueCompute () {
      return (this.composition ? this.valueComposition : this.valueSync).split('\n')
fxy060608's avatar
fxy060608 已提交
151 152 153 154
    }
  },
  watch: {
    value (val) {
Q
qiang 已提交
155
      this.valueSync = this._getValueString(val)
fxy060608's avatar
fxy060608 已提交
156 157
    },
    valueSync (val) {
158 159
      if (val !== this._oldValue) {
        this._oldValue = val
fxy060608's avatar
fxy060608 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
        this.$trigger('input', {}, {
          value: val,
          cursor: this.$refs.textarea.selectionEnd
        })
        this.$emit('update:value', val)
      }
    },
    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) {
194 195 196 197
      let lineHeight = parseFloat(getComputedStyle(this.$el).lineHeight)
      if (isNaN(lineHeight)) {
        lineHeight = this.$refs.line.offsetHeight
      }
fxy060608's avatar
fxy060608 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
      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 () {
216
    this._oldValue = this.$refs.textarea.value = this.valueSync
217 218 219
    this._resize({
      height: this.$refs.sensor.$el.offsetHeight
    })
220 221 222 223 224 225 226 227 228 229

    let $vm = this
    while ($vm) {
      const scopeId = $vm.$options._scopeId
      if (scopeId) {
        this.$refs.placeholder.setAttribute(scopeId, '')
      }
      $vm = $vm.$parent
    }

230
    this.initKeyboard(this.$refs.textarea)
fxy060608's avatar
fxy060608 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
  },
  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
    },
    // 暂无完成按钮,此功能未实现
    _confirm ($event) {
      this.$trigger('confirm', $event, {
        value: this.valueSync
      })
    },
    _linechange ($event) {
      this.$trigger('linechange', $event, {
        value: this.valueSync
      })
    },
    _touchstart () {
      this.focusChangeSource = 'touch'
    },
283 284 285 286 287 288 289
    _resize ({ height }) {
      this.height = height
    },
    _input ($event) {
      if (this.composition) {
        this.valueComposition = $event.target.value
      }
fxy060608's avatar
fxy060608 已提交
290 291 292 293 294 295 296 297 298
    },
    _getFormData () {
      return {
        value: this.valueSync,
        key: this.name
      }
    },
    _resetFormData () {
      this.valueSync = ''
Q
qiang 已提交
299 300 301
    },
    _getValueString (value) {
      return value === null ? '' : String(value)
fxy060608's avatar
fxy060608 已提交
302 303 304 305 306 307 308 309 310 311 312 313
    }
  }
}
</script>

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