index.vue 7.2 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
    },
    placeholderStyle: {
      type: String,
      default: ''
    },
    autoHeight: {
      type: [Boolean, String],
      default: false
    },
103 104 105
    confirmType: {
      type: String,
      default: ''
fxy060608's avatar
fxy060608 已提交
106 107 108 109
    }
  },
  data () {
    return {
110
      valueComposition: '',
fxy060608's avatar
fxy060608 已提交
111
      height: 0,
112
      focusChangeSource: '',
113
      // iOS 13 以下版本需要修正边距
D
fix:  
DCloud_LXH 已提交
114
      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 已提交
115 116 117 118 119 120 121
    }
  },
  computed: {
    maxlengthNumber () {
      var maxlength = Number(this.maxlength)
      return isNaN(maxlength) ? 140 : maxlength
    },
122
    valueCompute () {
123
      return (this.composing ? this.valueComposition : this.valueSync).split('\n')
124 125 126
    },
    isDone () {
      return ['done', 'go', 'next', 'search', 'send'].includes(this.confirmType)
fxy060608's avatar
fxy060608 已提交
127 128 129 130 131 132 133 134 135
    }
  },
  watch: {
    focus (val) {
      if (val) {
        this.focusChangeSource = 'focus'
      }
    },
    height (height) {
136 137 138 139
      let lineHeight = parseFloat(getComputedStyle(this.$el).lineHeight)
      if (isNaN(lineHeight)) {
        lineHeight = this.$refs.line.offsetHeight
      }
fxy060608's avatar
fxy060608 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
      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 () {
158 159 160
    this._resize({
      height: this.$refs.sensor.$el.offsetHeight
    })
161 162 163 164 165 166 167 168 169

    let $vm = this
    while ($vm) {
      const scopeId = $vm.$options._scopeId
      if (scopeId) {
        this.$refs.placeholder.setAttribute(scopeId, '')
      }
      $vm = $vm.$parent
    }
fxy060608's avatar
fxy060608 已提交
170 171 172 173 174 175 176 177
  },
  beforeDestroy () {
    this.$dispatch('Form', 'uni-form-group-update', {
      type: 'remove',
      vm: this
    })
  },
  methods: {
178 179 180 181 182 183 184 185 186 187 188
    _onKeyDownEnter: function ($event) {
      if (this.isDone) {
        $event.preventDefault()
      }
    },
    _onKeyUpEnter: function ($event) {
      if (this.isDone) {
        this._confirm($event)
        this.$refs.textarea.blur()
      }
    },
189
    _onCompositionstart ($event) {
190
      this.composing = true
fxy060608's avatar
fxy060608 已提交
191
    },
192
    _onCompositionend ($event) {
193 194 195 196 197
      if (this.composing) {
        this.composing = false
        // 部分输入法 compositionend 事件可能晚于 input
        this._onInput($event)
      }
fxy060608's avatar
fxy060608 已提交
198 199 200 201 202 203 204 205 206 207 208 209
    },
    // 暂无完成按钮,此功能未实现
    _confirm ($event) {
      this.$trigger('confirm', $event, {
        value: this.valueSync
      })
    },
    _linechange ($event) {
      this.$trigger('linechange', $event, {
        value: this.valueSync
      })
    },
210
    _onTouchstart () {
fxy060608's avatar
fxy060608 已提交
211 212
      this.focusChangeSource = 'touch'
    },
213 214 215
    _resize ({ height }) {
      this.height = height
    },
216 217
    _onInput ($event, force) {
      if (this.composing) {
218
        this.valueComposition = $event.target.value
219
        return
220
      }
221 222 223
      this.$triggerInput($event, {
        value: this.valueSync,
        cursor: this.$refs.textarea.selectionEnd
224
      }, force)
fxy060608's avatar
fxy060608 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    },
    _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;
246
  line-height: normal;
247 248
  white-space: pre-wrap;
  word-break: break-all;
249
  box-sizing: content-box !important;
250 251 252 253
}
uni-textarea[hidden] {
  display: none;
}
Q
qiang 已提交
254 255
.uni-textarea-wrapper,
.uni-textarea-placeholder,
256
.uni-textarea-line,
Q
qiang 已提交
257 258 259 260 261 262 263 264
.uni-textarea-compute,
.uni-textarea-textarea {
  outline: none;
  border: none;
  padding: 0;
  margin: 0;
  text-decoration: inherit;
}
265
.uni-textarea-wrapper {
Q
qiang 已提交
266
  display: block;
fxy060608's avatar
fxy060608 已提交
267 268 269 270 271
  position: relative;
  width: 100%;
  height: 100%;
}
.uni-textarea-placeholder,
272
.uni-textarea-line,
273
.uni-textarea-compute,
fxy060608's avatar
fxy060608 已提交
274 275 276 277 278 279
.uni-textarea-textarea {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
280 281
  white-space: inherit;
  word-break: inherit;
fxy060608's avatar
fxy060608 已提交
282 283 284
}
.uni-textarea-placeholder {
  color: grey;
Q
qiang 已提交
285
  overflow: hidden;
fxy060608's avatar
fxy060608 已提交
286
}
287
.uni-textarea-line,
288 289 290 291
.uni-textarea-compute {
  visibility: hidden;
  height: auto;
}
292 293 294
.uni-textarea-line {
  width: 1em;
}
fxy060608's avatar
fxy060608 已提交
295 296
.uni-textarea-textarea {
  resize: none;
Q
qiang 已提交
297 298
  background: none;
  color: inherit;
299
  opacity: 1;
Q
qiang 已提交
300 301 302 303 304 305 306
  font: inherit;
  line-height: inherit;
  letter-spacing: inherit;
  text-align: inherit;
  text-indent: inherit;
  text-transform: inherit;
  text-shadow: inherit;
fxy060608's avatar
fxy060608 已提交
307
}
308
/* 用于解决 iOS textarea 内部默认边距 */
309
.uni-textarea-textarea-fix-margin {
310 311 312 313
  width: auto;
  right: 0;
  margin: 0 -3px;
}
314 315 316 317
.uni-textarea-textarea:disabled {
  /* 用于重置iOS14以下禁用状态文字颜色 */
  -webkit-text-fill-color: currentcolor;
}
fxy060608's avatar
fxy060608 已提交
318
</style>