index.vue 7.9 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 || !valid)"
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
        class="uni-input-input"
27
        autocomplete="off"
28
        @change.stop
29 30 31
        @focus="_onFocus"
        @blur="_onBlur"
        @input.stop="_onInput"
32 33
        @compositionstart.stop="_onComposition"
        @compositionend.stop="_onComposition"
34
        @keyup.enter.stop="_onKeyup"
35
      >
36
      <!-- fix: 禁止 readonly 状态获取焦点 -->
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"
47
        @focus="($event) => $event.target.blur()"
48
      >
fxy060608's avatar
fxy060608 已提交
49 50 51 52 53
    </div>
  </uni-input>
</template>
<script>
import {
54
  field
fxy060608's avatar
fxy060608 已提交
55
} from 'uni-mixins'
56
const INPUT_TYPES = ['text', 'number', 'idcard', 'digit', 'password']
57
const NUMBER_TYPES = ['number', 'digit']
fxy060608's avatar
fxy060608 已提交
58 59
export default {
  name: 'Input',
60
  mixins: [field],
fxy060608's avatar
fxy060608 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  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,
84
      default: 'input-placeholder'
fxy060608's avatar
fxy060608 已提交
85 86 87 88 89 90 91 92 93 94 95 96
    },
    disabled: {
      type: [Boolean, String],
      default: false
    },
    maxlength: {
      type: [Number, String],
      default: 140
    },
    confirmType: {
      type: String,
      default: 'done'
97 98 99 100
    },
    verifyNumber: {
      type: Boolean,
      default: false
fxy060608's avatar
fxy060608 已提交
101 102 103 104
    }
  },
  data () {
    return {
105
      valid: true,
106
      wrapperHeight: 0,
D
fix:  
DCloud_LXH 已提交
107
      cachedValue: ''
fxy060608's avatar
fxy060608 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    }
  },
  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:
125
          type = ~INPUT_TYPES.indexOf(this.type) ? this.type : 'text'
fxy060608's avatar
fxy060608 已提交
126 127 128 129 130 131
          break
      }
      return this.password ? 'password' : type
    },
    step () {
      // 处理部分设备中无法输入小数点的问题
132
      return ~NUMBER_TYPES.indexOf(this.type) ? '0.000000000000000001' : ''
fxy060608's avatar
fxy060608 已提交
133 134 135 136
    }
  },
  watch: {
    maxlength (value) {
137 138
      const realValue = this.valueSync.slice(0, parseInt(value, 10))
      realValue !== this.valueSync && (this.valueSync = realValue)
fxy060608's avatar
fxy060608 已提交
139 140 141 142 143 144 145 146 147 148
    }
  },
  created () {
    this.$dispatch('Form', 'uni-form-group-update', {
      type: 'add',
      vm: this
    })
  },
  mounted () {
    if (this.confirmType === 'search') {
149
      const formElem = document.createElement('form')
fxy060608's avatar
fxy060608 已提交
150 151 152 153
      formElem.action = ''
      formElem.onsubmit = function () {
        return false
      }
154
      formElem.className = 'uni-input-form'
fxy060608's avatar
fxy060608 已提交
155 156 157 158
      formElem.appendChild(this.$refs.input)
      this.$refs.wrapper.appendChild(formElem)
    }

159 160 161 162 163 164 165 166
    let $vm = this
    while ($vm) {
      const scopeId = $vm.$options._scopeId
      if (scopeId) {
        this.$refs.placeholder.setAttribute(scopeId, '')
      }
      $vm = $vm.$parent
    }
fxy060608's avatar
fxy060608 已提交
167 168 169 170 171 172 173 174 175
  },
  beforeDestroy () {
    this.$dispatch('Form', 'uni-form-group-update', {
      type: 'remove',
      vm: this
    })
  },
  methods: {
    _onKeyup ($event) {
176 177 178
      this.$trigger('confirm', $event, {
        value: $event.target.value
      })
fxy060608's avatar
fxy060608 已提交
179
    },
180
    _onInput ($event, force) {
fxy060608's avatar
fxy060608 已提交
181 182 183
      if (this.composing) {
        return
      }
184 185

      if (~NUMBER_TYPES.indexOf(this.type)) {
186
        // 在输入 - 负号 的情况下,event.target.value没有值,但是会触发校验 false
187
        this.valid = this.$refs.input.validity && this.$refs.input.validity.valid
188
        if (!this.verifyNumber) {
189
          this.cachedValue = this.valueSync
190 191 192 193 194 195 196 197 198 199 200
        } else {
          // 处理部分输入法可以输入其它字符的情况,此处理无法先输入 -(负号),只能输入数字再移动光标输入负号
          if (this.$refs.input.validity && !this.valid) {
            $event.target.value = this.cachedValue
            this.valueSync = $event.target.value
            // 输入非法字符不触发 input 事件
            return
          } else {
            this.cachedValue = this.valueSync
          }
        }
201
      }
fxy060608's avatar
fxy060608 已提交
202

203 204 205 206 207
      // 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)
208
          this.valueSync = $event.target.value
209 210
          // 字符长度超出范围不触发 input 事件
          return
211 212
        }
      }
213 214

      this.$triggerInput($event, Object.assign({
215
        value: this.valueSync
216 217 218 219 220 221 222
      }, (() => {
        if (!this.verifyNumber) {
          return {
            valid: this.valid
          }
        }
      })()), force)
fxy060608's avatar
fxy060608 已提交
223 224 225 226
    },
    _onComposition ($event) {
      if ($event.type === 'compositionstart') {
        this.composing = true
227
      } else if (this.composing) {
fxy060608's avatar
fxy060608 已提交
228
        this.composing = false
229 230
        // 部分输入法 compositionend 事件可能晚于 input
        this._onInput($event)
fxy060608's avatar
fxy060608 已提交
231 232 233
      }
    },
    _resetFormData () {
234
      this.valueSync = ''
fxy060608's avatar
fxy060608 已提交
235 236 237
    },
    _getFormData () {
      return this.name ? {
238
        value: this.valueSync,
fxy060608's avatar
fxy060608 已提交
239 240 241 242 243 244 245
        key: this.name
      } : {}
    }
  }
}
</script>
<style>
246 247 248 249 250 251
uni-input {
  display: block;
  font-size: 16px;
  line-height: 1.4em;
  height: 1.4em;
  min-height: 1.4em;
252
  overflow: hidden;
253
}
fxy060608's avatar
fxy060608 已提交
254

255 256 257
uni-input[hidden] {
  display: none;
}
fxy060608's avatar
fxy060608 已提交
258

259 260 261 262 263 264 265 266 267 268
.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 已提交
269

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

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

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

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

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

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

324 325 326
.uni-input-input[type="number"] {
  -moz-appearance: textfield;
}
327 328 329 330 331

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