index.vue 9.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
<template>
2
  <uni-input v-on="$listeners">
3 4 5
    <div
      ref="wrapper"
      class="uni-input-wrapper"
fxy060608's avatar
fxy060608 已提交
6
    >
7
      <div
8
        v-show="!(composing || valueSync.length || cachedValue === '-')"
9 10 11 12
        ref="placeholder"
        :style="placeholderStyle"
        :class="placeholderClass"
        class="uni-input-placeholder"
13 14
        v-text="placeholder"
      />
15
      <input
16
        v-if="!disabled || !fixColor"
17
        ref="input"
18
        v-model="valueSync"
19
        v-keyboard
20
        v-field
21 22 23
        :disabled="disabled"
        :type="inputType"
        :maxlength="maxlength"
24
        :step="_step"
25
        :enterkeyhint="confirmType"
26
        :pattern="type === 'number' ? '[0-9]*' : null"
27
        class="uni-input-input"
28
        :autocomplete="autocomplete"
29
        @change.stop
30 31 32
        @focus="_onFocus"
        @blur="_onBlur"
        @input.stop="_onInput"
33 34
        @compositionstart.stop="_onComposition"
        @compositionend.stop="_onComposition"
D
DCloud_LXH 已提交
35
        @compositionupdate.stop="_onComposition"
36
        @keyup.enter.stop="_onKeyup"
37
      >
38
      <!-- fix: 禁止 readonly 状态获取焦点 -->
39 40 41 42 43 44 45 46
      <input
        v-if="disabled && fixColor"
        ref="input"
        :value="valueSync"
        tabindex="-1"
        :readonly="disabled"
        :type="inputType"
        :maxlength="maxlength"
47
        :step="_step"
48
        class="uni-input-input"
49
        @focus="($event) => $event.target.blur()"
50
      >
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55
    </div>
  </uni-input>
</template>
<script>
import {
56
  field
fxy060608's avatar
fxy060608 已提交
57
} from 'uni-mixins'
58 59
import { kebabCase } from 'uni-shared'
const INPUT_TYPES = ['text', 'number', 'idcard', 'digit', 'password', 'tel']
60
const NUMBER_TYPES = ['number', 'digit']
61
const AUTOCOMPLETES = ['off', 'one-time-code']
fxy060608's avatar
fxy060608 已提交
62 63
export default {
  name: 'Input',
64
  mixins: [field],
fxy060608's avatar
fxy060608 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  props: {
    name: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: 'text'
    },
    password: {
      type: [Boolean, String],
      default: false
    },
    placeholder: {
      type: String,
      default: ''
    },
    placeholderStyle: {
      type: String,
      default: ''
    },
    placeholderClass: {
      type: String,
88
      default: 'input-placeholder'
fxy060608's avatar
fxy060608 已提交
89 90 91 92 93 94 95 96 97 98 99 100
    },
    disabled: {
      type: [Boolean, String],
      default: false
    },
    maxlength: {
      type: [Number, String],
      default: 140
    },
    confirmType: {
      type: String,
      default: 'done'
101 102 103 104
    },
    textContentType: {
      type: String,
      default: ''
105 106 107 108
    },
    step: {
      type: String,
      default: '0.000000000000000001'
fxy060608's avatar
fxy060608 已提交
109 110 111 112
    }
  },
  data () {
    return {
113
      wrapperHeight: 0,
D
fix:  
DCloud_LXH 已提交
114
      cachedValue: ''
fxy060608's avatar
fxy060608 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    }
  },
  computed: {
    inputType: function () {
      let type = ''
      switch (this.type) {
        case 'text':
          this.confirmType === 'search' && (type = 'search')
          break
        case 'idcard':
          // TODO 可能要根据不同平台进行区分处理
          type = 'text'
          break
        case 'digit':
          type = 'number'
          break
        default:
132
          type = ~INPUT_TYPES.indexOf(this.type) ? this.type : 'text'
fxy060608's avatar
fxy060608 已提交
133 134 135 136
          break
      }
      return this.password ? 'password' : type
    },
137
    _step () {
fxy060608's avatar
fxy060608 已提交
138
      // 处理部分设备中无法输入小数点的问题
139
      return ~NUMBER_TYPES.indexOf(this.type) ? this.step : ''
140 141 142 143 144 145 146 147 148 149
    },
    autocomplete () {
      const camelizeIndex = AUTOCOMPLETES.indexOf(this.textContentType)
      const kebabCaseIndex = AUTOCOMPLETES.indexOf(kebabCase(this.textContentType))
      const index = camelizeIndex !== -1
        ? camelizeIndex
        : kebabCaseIndex !== -1
          ? kebabCaseIndex
          : 0
      return AUTOCOMPLETES[index]
fxy060608's avatar
fxy060608 已提交
150 151 152 153
    }
  },
  watch: {
    maxlength (value) {
154 155
      const realValue = this.valueSync.slice(0, parseInt(value, 10))
      realValue !== this.valueSync && (this.valueSync = realValue)
156 157 158 159 160
    },
    valueSync (value) {
      if (this.type === 'number' && !(this.cachedValue === '-' && value === '')) {
        this.cachedValue = value
      }
fxy060608's avatar
fxy060608 已提交
161 162 163 164 165 166 167 168 169 170
    }
  },
  created () {
    this.$dispatch('Form', 'uni-form-group-update', {
      type: 'add',
      vm: this
    })
  },
  mounted () {
    if (this.confirmType === 'search') {
171
      const formElem = document.createElement('form')
fxy060608's avatar
fxy060608 已提交
172 173 174 175
      formElem.action = ''
      formElem.onsubmit = function () {
        return false
      }
176
      formElem.className = 'uni-input-form'
fxy060608's avatar
fxy060608 已提交
177 178 179 180
      formElem.appendChild(this.$refs.input)
      this.$refs.wrapper.appendChild(formElem)
    }

181 182 183 184 185 186 187 188
    let $vm = this
    while ($vm) {
      const scopeId = $vm.$options._scopeId
      if (scopeId) {
        this.$refs.placeholder.setAttribute(scopeId, '')
      }
      $vm = $vm.$parent
    }
fxy060608's avatar
fxy060608 已提交
189 190 191 192 193 194 195 196 197
  },
  beforeDestroy () {
    this.$dispatch('Form', 'uni-form-group-update', {
      type: 'remove',
      vm: this
    })
  },
  methods: {
    _onKeyup ($event) {
198
      const input = $event.target
199
      this.$trigger('confirm', $event, {
200
        value: input.value
201
      })
202 203 204
      if (!this.confirmHold) {
        input.blur()
      }
fxy060608's avatar
fxy060608 已提交
205
    },
206
    _onInput ($event, force) {
207 208
      let outOfMaxlength = false

D
DCloud_LXH 已提交
209
      if (this.composing && this.ignoreCompositionEvent) {
fxy060608's avatar
fxy060608 已提交
210 211
        return
      }
212

213
      if (this.inputType === 'number') {
214
        // type="number" 不支持 maxlength 属性,因此需要主动限制长度。
215 216
        const maxlength = parseInt(this.maxlength, 10)
        if (maxlength > 0 && $event.target.value.length > maxlength) {
217 218 219 220 221 222 223 224 225
          // 输入前字符长度超出范围,则不触发input,且将值还原
          // 否则截取一定长度且触发input
          if (this.cachedValue.length === maxlength) {
            this.valueSync = this.cachedValue
            outOfMaxlength = true
          } else {
            $event.target.value = $event.target.value.slice(0, maxlength)
            this.valueSync = $event.target.value
          }
226 227
        }

228 229 230 231 232 233 234 235 236
        // 数字类型输入错误时无法获取具体的值,自定义校验和纠正。
        this.__clearCachedValue && $event.target.removeEventListener('blur', this.__clearCachedValue)
        if ($event.target.validity && !$event.target.validity.valid) {
          if ((!this.cachedValue && $event.data === '-') || (this.cachedValue[0] === '-' && $event.inputType === 'deleteContentBackward')) {
            this.cachedValue = '-'
            const clearCachedValue = this.__clearCachedValue = () => {
              this.cachedValue = ''
            }
            $event.target.addEventListener('blur', clearCachedValue)
237 238
            return
          }
239 240 241 242 243
          this.cachedValue = this.valueSync = $event.target.value = this.cachedValue === '-' ? '' : this.cachedValue
          // 输入非法字符不触发 input 事件
          return
        } else {
          this.cachedValue = this.valueSync
244
        }
245
      }
fxy060608's avatar
fxy060608 已提交
246

247
      if (outOfMaxlength) return
248

D
DCloud_LXH 已提交
249 250
      if (!this.ignoreCompositionEvent) this.valueSync = this.$refs.input.value

251
      this.$triggerInput($event, {
252
        value: this.valueSync
253
      }, force)
fxy060608's avatar
fxy060608 已提交
254 255
    },
    _onComposition ($event) {
D
DCloud_LXH 已提交
256 257 258 259 260 261 262 263 264 265 266
      switch ($event.type) {
        case 'compositionstart':
          this.composing = true
          break
        case 'compositionend':
          if (this.composing) {
            this.composing = false
            // 部分输入法 compositionend 事件可能晚于 input
            this._onInput($event)
          }
          break
fxy060608's avatar
fxy060608 已提交
267
      }
D
DCloud_LXH 已提交
268 269 270

      !this.ignoreCompositionEvent &&
        this.$trigger($event.type, $event, { data: $event.data })
fxy060608's avatar
fxy060608 已提交
271 272
    },
    _resetFormData () {
273
      this.valueSync = ''
fxy060608's avatar
fxy060608 已提交
274 275 276
    },
    _getFormData () {
      return this.name ? {
277
        value: this.valueSync,
fxy060608's avatar
fxy060608 已提交
278 279 280 281 282 283 284
        key: this.name
      } : {}
    }
  }
}
</script>
<style>
285 286 287 288 289 290
uni-input {
  display: block;
  font-size: 16px;
  line-height: 1.4em;
  height: 1.4em;
  min-height: 1.4em;
291
  overflow: hidden;
292
}
fxy060608's avatar
fxy060608 已提交
293

294 295 296
uni-input[hidden] {
  display: none;
}
fxy060608's avatar
fxy060608 已提交
297

298 299 300 301 302 303 304 305 306 307
.uni-input-wrapper,
.uni-input-placeholder,
.uni-input-form,
.uni-input-input {
  outline: none;
  border: none;
  padding: 0;
  margin: 0;
  text-decoration: inherit;
}
fxy060608's avatar
fxy060608 已提交
308

309 310
.uni-input-wrapper,
.uni-input-form {
311
  display: flex;
312 313 314
  position: relative;
  width: 100%;
  height: 100%;
315 316
  flex-direction: column;
  justify-content: center;
317
}
fxy060608's avatar
fxy060608 已提交
318

319
.uni-input-placeholder,
Q
qiang 已提交
320
.uni-input-input {
321 322
  width: 100%;
}
fxy060608's avatar
fxy060608 已提交
323

324
.uni-input-placeholder {
325
  position: absolute;
326
  top: auto !important;
327
  left: 0;
328 329 330 331 332
  color: gray;
  overflow: hidden;
  text-overflow: clip;
  white-space: pre;
  word-break: keep-all;
333
  pointer-events: none;
334
  line-height: inherit;
335
}
fxy060608's avatar
fxy060608 已提交
336

337
.uni-input-input {
338
  position: relative;
339 340
  display: block;
  height: 100%;
341 342
  background: none;
  color: inherit;
343
  opacity: 1;
344 345 346 347 348 349 350 351
  font: inherit;
  line-height: inherit;
  letter-spacing: inherit;
  text-align: inherit;
  text-indent: inherit;
  text-transform: inherit;
  text-shadow: inherit;
}
fxy060608's avatar
fxy060608 已提交
352

Q
qiang 已提交
353 354
.uni-input-input[type="search"]::-webkit-search-cancel-button,
.uni-input-input[type="search"]::-webkit-search-decoration {
355 356
  display: none;
}
fxy060608's avatar
fxy060608 已提交
357

358 359 360 361 362
.uni-input-input::-webkit-outer-spin-button,
.uni-input-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
fxy060608's avatar
fxy060608 已提交
363

364 365 366
.uni-input-input[type="number"] {
  -moz-appearance: textfield;
}
367 368 369 370 371

.uni-input-input:disabled {
  /* 用于重置iOS14以下禁用状态文字颜色 */
  -webkit-text-fill-color: currentcolor;
}
372
</style>