index.vue 7.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
<template>
2 3 4
  <uni-input
    @change.stop
    v-on="$listeners"
fxy060608's avatar
fxy060608 已提交
5
  >
6 7 8
    <div
      ref="wrapper"
      class="uni-input-wrapper"
fxy060608's avatar
fxy060608 已提交
9
    >
10
      <div
11
        v-show="!(composing || valueSync.length)"
12 13 14 15
        ref="placeholder"
        :style="placeholderStyle"
        :class="placeholderClass"
        class="uni-input-placeholder"
16 17
        v-text="placeholder"
      />
18
      <input
19
        v-if="!disabled || !fixColor"
20
        ref="input"
21
        v-model="valueSync"
22
        v-keyboard
23 24 25
        :disabled="disabled"
        :type="inputType"
        :maxlength="maxlength"
26
        :step="step"
27
        class="uni-input-input"
28
        autocomplete="off"
29 30 31 32
        @focus="_onFocus"
        @blur="_onBlur"
        @input.stop="_onInput"
        @compositionstart="_onComposition"
fxy060608's avatar
fxy060608 已提交
33
        @compositionend="_onComposition"
34 35
        @keyup.stop="_onKeyup"
      >
36 37 38 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"
        :step="step"
        class="uni-input-input"
      >
fxy060608's avatar
fxy060608 已提交
47 48 49 50 51
    </div>
  </uni-input>
</template>
<script>
import {
52
  field
fxy060608's avatar
fxy060608 已提交
53
} from 'uni-mixins'
54
const INPUT_TYPES = ['text', 'number', 'idcard', 'digit', 'password']
55
const NUMBER_TYPES = ['number', 'digit']
fxy060608's avatar
fxy060608 已提交
56 57
export default {
  name: 'Input',
58
  mixins: [field],
fxy060608's avatar
fxy060608 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  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,
82
      default: 'input-placeholder'
fxy060608's avatar
fxy060608 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    },
    disabled: {
      type: [Boolean, String],
      default: false
    },
    maxlength: {
      type: [Number, String],
      default: 140
    },
    confirmType: {
      type: String,
      default: 'done'
    }
  },
  data () {
    return {
      composing: false,
100
      wrapperHeight: 0,
101 102 103
      cachedValue: '',
      // Safari 14 以上修正禁用状态颜色
      fixColor: String(navigator.vendor).indexOf('Apple') === 0 && CSS.supports('image-orientation:from-image')
fxy060608's avatar
fxy060608 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    }
  },
  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:
121
          type = ~INPUT_TYPES.indexOf(this.type) ? this.type : 'text'
fxy060608's avatar
fxy060608 已提交
122 123 124 125 126 127
          break
      }
      return this.password ? 'password' : type
    },
    step () {
      // 处理部分设备中无法输入小数点的问题
128
      return ~NUMBER_TYPES.indexOf(this.type) ? '0.000000000000000001' : ''
fxy060608's avatar
fxy060608 已提交
129 130 131 132
    }
  },
  watch: {
    maxlength (value) {
133 134
      const realValue = this.valueSync.slice(0, parseInt(value, 10))
      realValue !== this.valueSync && (this.valueSync = realValue)
fxy060608's avatar
fxy060608 已提交
135 136 137 138 139 140 141 142 143 144
    }
  },
  created () {
    this.$dispatch('Form', 'uni-form-group-update', {
      type: 'add',
      vm: this
    })
  },
  mounted () {
    if (this.confirmType === 'search') {
145
      const formElem = document.createElement('form')
fxy060608's avatar
fxy060608 已提交
146 147 148 149
      formElem.action = ''
      formElem.onsubmit = function () {
        return false
      }
150
      formElem.className = 'uni-input-form'
fxy060608's avatar
fxy060608 已提交
151 152 153 154
      formElem.appendChild(this.$refs.input)
      this.$refs.wrapper.appendChild(formElem)
    }

155 156 157 158 159 160 161 162
    let $vm = this
    while ($vm) {
      const scopeId = $vm.$options._scopeId
      if (scopeId) {
        this.$refs.placeholder.setAttribute(scopeId, '')
      }
      $vm = $vm.$parent
    }
163 164

    this._initField('input')
fxy060608's avatar
fxy060608 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  },
  beforeDestroy () {
    this.$dispatch('Form', 'uni-form-group-update', {
      type: 'remove',
      vm: this
    })
  },
  methods: {
    _onKeyup ($event) {
      if ($event.keyCode === 13) {
        this.$trigger('confirm', $event, {
          value: $event.target.value
        })
      }
    },
    _onInput ($event) {
      if (this.composing) {
        return
      }
184 185 186 187 188

      // 处理部分输入法可以输入其它字符的情况
      if (~NUMBER_TYPES.indexOf(this.type)) {
        if (this.$refs.input.validity && !this.$refs.input.validity.valid) {
          $event.target.value = this.cachedValue
189
          this.valueSync = $event.target.value
190 191
          // 输入非法字符不触发 input 事件
          return
192
        } else {
193
          this.cachedValue = this.valueSync
194 195
        }
      }
fxy060608's avatar
fxy060608 已提交
196

197 198 199 200 201
      // type="number" 不支持 maxlength 属性,因此需要主动限制长度。
      if (this.inputType === 'number') {
        const maxlength = parseInt(this.maxlength, 10)
        if (maxlength > 0 && $event.target.value.length > maxlength) {
          $event.target.value = $event.target.value.slice(0, maxlength)
202
          this.valueSync = $event.target.value
203 204
          // 字符长度超出范围不触发 input 事件
          return
205 206
        }
      }
207 208
      this.$triggerInput($event, {
        value: this.valueSync
fxy060608's avatar
fxy060608 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
      })
    },
    _onFocus ($event) {
      this.$trigger('focus', $event, {
        value: $event.target.value
      })
    },
    _onBlur ($event) {
      this.$trigger('blur', $event, {
        value: $event.target.value
      })
    },
    _onComposition ($event) {
      if ($event.type === 'compositionstart') {
        this.composing = true
      } else {
        this.composing = false
226 227
        // 部分输入法 compositionend 事件可能晚于 input
        this._onInput($event)
fxy060608's avatar
fxy060608 已提交
228 229 230
      }
    },
    _resetFormData () {
231
      this.valueSync = ''
fxy060608's avatar
fxy060608 已提交
232 233 234
    },
    _getFormData () {
      return this.name ? {
235
        value: this.valueSync,
fxy060608's avatar
fxy060608 已提交
236 237 238 239 240 241 242
        key: this.name
      } : {}
    }
  }
}
</script>
<style>
243 244 245 246 247 248
uni-input {
  display: block;
  font-size: 16px;
  line-height: 1.4em;
  height: 1.4em;
  min-height: 1.4em;
249
  overflow: hidden;
250
}
fxy060608's avatar
fxy060608 已提交
251

252 253 254
uni-input[hidden] {
  display: none;
}
fxy060608's avatar
fxy060608 已提交
255

256 257 258 259 260 261 262 263 264 265
.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 已提交
266

267 268
.uni-input-wrapper,
.uni-input-form {
269
  display: flex;
270 271 272
  position: relative;
  width: 100%;
  height: 100%;
273 274
  flex-direction: column;
  justify-content: center;
275
}
fxy060608's avatar
fxy060608 已提交
276

277
.uni-input-placeholder,
Q
qiang 已提交
278
.uni-input-input {
279 280
  width: 100%;
}
fxy060608's avatar
fxy060608 已提交
281

282
.uni-input-placeholder {
283
  position: absolute;
284
  top: auto !important;
285
  left: 0;
286 287 288 289 290
  color: gray;
  overflow: hidden;
  text-overflow: clip;
  white-space: pre;
  word-break: keep-all;
291
  pointer-events: none;
292
  line-height: inherit;
293
}
fxy060608's avatar
fxy060608 已提交
294

295
.uni-input-input {
296
  position: relative;
297 298
  display: block;
  height: 100%;
299 300
  background: none;
  color: inherit;
301
  opacity: 1;
302 303 304 305 306 307 308 309
  font: inherit;
  line-height: inherit;
  letter-spacing: inherit;
  text-align: inherit;
  text-indent: inherit;
  text-transform: inherit;
  text-shadow: inherit;
}
fxy060608's avatar
fxy060608 已提交
310

311 312 313
.uni-input-input[type="search"]::-webkit-search-cancel-button {
  display: none;
}
fxy060608's avatar
fxy060608 已提交
314

315 316 317 318 319
.uni-input-input::-webkit-outer-spin-button,
.uni-input-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
fxy060608's avatar
fxy060608 已提交
320

321 322 323
.uni-input-input[type="number"] {
  -moz-appearance: textfield;
}
324 325 326 327 328

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