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

147 148 149 150 151 152 153 154 155
    let $vm = this
    while ($vm) {
      const scopeId = $vm.$options._scopeId
      if (scopeId) {
        this.$refs.placeholder.setAttribute(scopeId, '')
      }
      $vm = $vm.$parent
    }

156 157
    this.initKeyboard(this.$refs.input)

fxy060608's avatar
fxy060608 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    this.focus && this._focusInput()
  },
  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
      }
178 179 180 181 182

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

191 192 193 194 195
      // 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)
196
          this.valueSync = $event.target.value
197 198
          // 字符长度超出范围不触发 input 事件
          return
199 200
        }
      }
201 202
      this.$triggerInput($event, {
        value: this.valueSync
fxy060608's avatar
fxy060608 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
      })
    },
    _onFocus ($event) {
      this.$trigger('focus', $event, {
        value: $event.target.value
      })
    },
    _onBlur ($event) {
      this.$trigger('blur', $event, {
        value: $event.target.value
      })
    },
    _focusInput () {
      setTimeout(() => {
        this.$refs.input.focus()
      }, 350)
    },
    _blurInput () {
      setTimeout(() => {
        this.$refs.input.blur()
      }, 350)
    },
    _onComposition ($event) {
      if ($event.type === 'compositionstart') {
        this.composing = true
      } else {
        this.composing = false
      }
    },
    _resetFormData () {
233
      this.valueSync = ''
fxy060608's avatar
fxy060608 已提交
234 235 236
    },
    _getFormData () {
      return this.name ? {
237
        value: this.valueSync,
fxy060608's avatar
fxy060608 已提交
238 239 240 241 242 243 244
        key: this.name
      } : {}
    }
  }
}
</script>
<style>
245 246 247 248 249 250
uni-input {
  display: block;
  font-size: 16px;
  line-height: 1.4em;
  height: 1.4em;
  min-height: 1.4em;
251
  overflow: hidden;
252
}
fxy060608's avatar
fxy060608 已提交
253

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

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

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

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

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

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

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

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

323 324 325
.uni-input-input[type="number"] {
  -moz-appearance: textfield;
}
326
</style>