index.vue 8.9 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
<template>
  <uni-input v-on="$listeners">
3 4
    <div
      ref="wrapper"
fxy060608's avatar
fxy060608 已提交
5
      :disabled="disabled">
6 7 8 9 10 11
      <input
        ref="input"
        v-model="inputValue"
        :disabled="disabled"
        :type="inputType"
        :maxlength="maxlength"
12 13
        :step="step"
        autocomplete="off"
14 15 16 17
        @focus="_onFocus"
        @blur="_onBlur"
        @input.stop="_onInput"
        @compositionstart="_onComposition"
fxy060608's avatar
fxy060608 已提交
18
        @compositionend="_onComposition"
19
        @keyup.stop="_onKeyup">
20 21 22 23
      <div
        v-show="!(composing || inputValue.length)"
        ref="placeholder"
        :style="placeholderStyle"
fxy060608's avatar
fxy060608 已提交
24
        :class="placeholderClass"
25
        class="input-placeholder">{{ placeholder }}</div>
fxy060608's avatar
fxy060608 已提交
26
    </div>
27 28
    <v-uni-resize-sensor
      ref="sensor"
29
      @resize="_resize" />
fxy060608's avatar
fxy060608 已提交
30 31 32 33 34 35
  </uni-input>
</template>
<script>
import {
  emitter
} from 'uni-mixins'
36
const INPUT_TYPES = ['text', 'number', 'idcard', 'digit', 'password']
37
const NUMBER_TYPES = ['number', 'digit']
fxy060608's avatar
fxy060608 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
export default {
  name: 'Input',
  mixins: [emitter],
  model: {
    prop: 'value',
    event: 'update:value'
  },
  props: {
    name: {
      type: String,
      default: ''
    },
    value: {
      type: [String, Number],
      default: ''
    },
    type: {
      type: String,
      default: 'text'
    },
    password: {
      type: [Boolean, String],
      default: false
    },
    placeholder: {
      type: String,
      default: ''
    },
    placeholderStyle: {
      type: String,
      default: ''
    },
    placeholderClass: {
      type: String,
      default: ''
    },
    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 {
      inputValue: this.value + '',
      composing: false,
95
      wrapperHeight: 0,
96 97
      cachedValue: '',
      isRendered: false
fxy060608's avatar
fxy060608 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    }
  },
  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:
115
          type = ~INPUT_TYPES.indexOf(this.type) ? this.type : 'text'
fxy060608's avatar
fxy060608 已提交
116 117 118 119 120 121
          break
      }
      return this.password ? 'password' : type
    },
    step () {
      // 处理部分设备中无法输入小数点的问题
122
      return ~NUMBER_TYPES.indexOf(this.type) ? '0.000000000000000001' : ''
fxy060608's avatar
fxy060608 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135
    }
  },
  watch: {
    focus (value) {
      value && this._focusInput()
    },
    value (value) {
      this.inputValue = value + ''
    },
    inputValue (value) {
      this.$emit('update:value', value)
    },
    maxlength (value) {
136
      const realValue = this.inputValue.slice(0, parseInt(value, 10))
fxy060608's avatar
fxy060608 已提交
137 138 139 140 141 142 143 144 145 146
      realValue !== this.inputValue && (this.inputValue = realValue)
    }
  },
  created () {
    this.$dispatch('Form', 'uni-form-group-update', {
      type: 'add',
      vm: this
    })
  },
  mounted () {
147
    this._initInputStyle()
fxy060608's avatar
fxy060608 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

    if (this.confirmType === 'search') {
      var formElem = document.createElement('form')
      formElem.action = ''
      formElem.onsubmit = function () {
        return false
      }
      formElem.appendChild(this.$refs.input)
      this.$refs.wrapper.appendChild(formElem)
    }

    this.focus && this._focusInput()
  },
  beforeDestroy () {
    this.$dispatch('Form', 'uni-form-group-update', {
      type: 'remove',
      vm: this
    })
  },
  methods: {
168
    _resize () {
169
      if (!this.isRendered) {
170 171 172
        this._initInputStyle()
      }
    },
fxy060608's avatar
fxy060608 已提交
173 174 175 176 177 178 179
    _initInputStyle () {
      const wrapper = this.$refs.wrapper
      const computedStyle = window.getComputedStyle(wrapper)
      const rectStyle = wrapper.getBoundingClientRect()
      const inputElem = this.$refs.input
      const placeholderElem = this.$refs.placeholder

180 181 182 183
      // 获取到的高度为 0 则认为组件未渲染,通常是使用 v-show 时会出现此情况。
      if (!rectStyle.height) {
        return
      } else {
184
        this.isRendered = true
185 186 187 188
      }
      // 渲染之后进行计算,设置实际的高度等样式。
      const realHeight = rectStyle.height - (parseFloat(computedStyle.borderTopWidth, 10) + parseFloat(computedStyle.borderBottomWidth,
        10))
fxy060608's avatar
fxy060608 已提交
189 190 191 192 193
      if (realHeight !== this.wrapperHeight) {
        inputElem.style.height = `${realHeight}px`
        inputElem.style.lineHeight = `${realHeight}px`
        this.wrapperHeight = realHeight
      }
194
      // inputElem.style.color = computedStyle.color
fxy060608's avatar
fxy060608 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

      placeholderElem.style.height = `${realHeight}px`
      placeholderElem.style.lineHeight = `${realHeight}px`
    },
    _onKeyup ($event) {
      if ($event.keyCode === 13) {
        this.$trigger('confirm', $event, {
          value: $event.target.value
        })
      }
    },
    _onInput ($event) {
      if (this.composing) {
        return
      }
210 211 212 213 214

      // 处理部分输入法可以输入其它字符的情况
      if (~NUMBER_TYPES.indexOf(this.type)) {
        if (this.$refs.input.validity && !this.$refs.input.validity.valid) {
          $event.target.value = this.cachedValue
215
          this.inputValue = $event.target.value
216 217
          // 输入非法字符不触发 input 事件
          return
218 219 220 221
        } else {
          this.cachedValue = this.inputValue
        }
      }
fxy060608's avatar
fxy060608 已提交
222

223 224 225 226 227 228
      // 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)
          this.inputValue = $event.target.value
229 230
          // 字符长度超出范围不触发 input 事件
          return
231 232 233
        }
      }

fxy060608's avatar
fxy060608 已提交
234
      this.$trigger('input', $event, {
235
        value: this.inputValue
fxy060608's avatar
fxy060608 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
      })
    },
    _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 () {
      this.inputValue = ''
    },
    _getFormData () {
      return this.name ? {
        value: this.inputValue,
        key: this.name
      } : {}
    }
  }
}
</script>
<style>
	uni-input {
		display: block;
		height: 1.4rem;
		text-overflow: clip;
		overflow: hidden;
		white-space: nowrap;
		font-family: UICTFontTextStyleBody;
		min-height: 1.4rem;
	}

	uni-input input {
		position: relative;
		min-height: 1.4rem;
		border: none;
		height: inherit;
		width: 100%;
		font-size: inherit;
		font-weight: inherit;
		font-family: UICTFontTextStyleBody;
		color: inherit;
		background: transparent;
		display: inherit;
		padding: 0;
		margin: 0;
		outline: none;
		vertical-align: middle;
		text-align: inherit;
		overflow: inherit;
		white-space: inherit;
		text-overflow: inherit;
		-webkit-tap-highlight-color: transparent;
		z-index: 2;
310
		opacity: inherit;
fxy060608's avatar
fxy060608 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	}

	uni-input[hidden] {
		display: none;
	}

	uni-input div {
		position: relative;
		min-height: 1.4rem;
		text-overflow: inherit;
		border: none;
		height: inherit;
		width: inherit;
		font-size: inherit;
		font-weight: inherit;
		font-family: UICTFontTextStyleBody;
		color: inherit;
		background: inherit;
		padding: 0;
		margin: 0;
		outline: none;
		text-align: inherit;
		-webkit-tap-highlight-color: transparent;
	}

	uni-input div[type=password] div {
		color: black;
	}

	uni-input div div {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: inherit;
X
xiaoyucoding 已提交
346
		line-height: 100%;
fxy060608's avatar
fxy060608 已提交
347 348 349 350 351 352 353 354 355 356
		min-height: 1.4rem;
		white-space: pre;
		text-align: inherit;
		overflow: hidden;
		vertical-align: middle;
		z-index: 1;
	}

	.input-placeholder {
		color: gray;
357 358
		height: inherit;
		line-height: inherit;
359
    background: none;
fxy060608's avatar
fxy060608 已提交
360 361 362 363 364 365 366
	}

	input[type="search"]::-webkit-search-cancel-button {
		display: none;
	}

	input::-webkit-outer-spin-button,
367
	input::-webkit-inner-spin-button {
fxy060608's avatar
fxy060608 已提交
368 369 370 371
		-webkit-appearance: none;
		margin: 0;
	}

372 373
	input[type="number"] {
		-moz-appearance: textfield;
fxy060608's avatar
fxy060608 已提交
374
	}
375
</style>