提交 89d45a4e 编写于 作者: X xiaoyucoding

perf: H5 优化 input 组件的高度渲染

上级 f8b0d34c
...@@ -15,16 +15,17 @@ ...@@ -15,16 +15,17 @@
@input.stop="_onInput" @input.stop="_onInput"
@compositionstart="_onComposition" @compositionstart="_onComposition"
@compositionend="_onComposition" @compositionend="_onComposition"
@keyup.stop="_onKeyup" @keyup.stop="_onKeyup">
>
<div <div
v-show="!(composing || inputValue.length)" v-show="!(composing || inputValue.length)"
ref="placeholder" ref="placeholder"
:style="placeholderStyle" :style="placeholderStyle"
:class="placeholderClass" :class="placeholderClass"
class="input-placeholder" class="input-placeholder">{{ placeholder }}</div>
>{{ placeholder }}</div>
</div> </div>
<v-uni-resize-sensor
ref="sensor"
@resize="_resize" />
</uni-input> </uni-input>
</template> </template>
<script> <script>
...@@ -33,6 +34,7 @@ import { ...@@ -33,6 +34,7 @@ import {
} from 'uni-mixins' } from 'uni-mixins'
const INPUT_TYPES = ['text', 'number', 'idcard', 'digit', 'password'] const INPUT_TYPES = ['text', 'number', 'idcard', 'digit', 'password']
const NUMBER_TYPES = ['number', 'digit'] const NUMBER_TYPES = ['number', 'digit']
let isRendered = false
export default { export default {
name: 'Input', name: 'Input',
mixins: [emitter], mixins: [emitter],
...@@ -141,7 +143,7 @@ export default { ...@@ -141,7 +143,7 @@ export default {
}) })
}, },
mounted () { mounted () {
// this._initInputStyle() this._initInputStyle()
if (this.confirmType === 'search') { if (this.confirmType === 'search') {
var formElem = document.createElement('form') var formElem = document.createElement('form')
...@@ -162,6 +164,11 @@ export default { ...@@ -162,6 +164,11 @@ export default {
}) })
}, },
methods: { methods: {
_resize () {
if (!isRendered) {
this._initInputStyle()
}
},
_initInputStyle () { _initInputStyle () {
const wrapper = this.$refs.wrapper const wrapper = this.$refs.wrapper
const computedStyle = window.getComputedStyle(wrapper) const computedStyle = window.getComputedStyle(wrapper)
...@@ -169,7 +176,15 @@ export default { ...@@ -169,7 +176,15 @@ export default {
const inputElem = this.$refs.input const inputElem = this.$refs.input
const placeholderElem = this.$refs.placeholder const placeholderElem = this.$refs.placeholder
const realHeight = rectStyle.height - (parseFloat(computedStyle.borderTopWidth, 10) + parseFloat(computedStyle.borderBottomWidth, 10)) // 获取到的高度为 0 则认为组件未渲染,通常是使用 v-show 时会出现此情况。
if (!rectStyle.height) {
return
} else {
isRendered = true
}
// 渲染之后进行计算,设置实际的高度等样式。
const realHeight = rectStyle.height - (parseFloat(computedStyle.borderTopWidth, 10) + parseFloat(computedStyle.borderBottomWidth,
10))
if (realHeight !== this.wrapperHeight) { if (realHeight !== this.wrapperHeight) {
inputElem.style.height = `${realHeight}px` inputElem.style.height = `${realHeight}px`
inputElem.style.lineHeight = `${realHeight}px` inputElem.style.lineHeight = `${realHeight}px`
...@@ -397,12 +412,12 @@ export default { ...@@ -397,12 +412,12 @@ export default {
} }
input::-webkit-outer-spin-button, input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button{ input::-webkit-inner-spin-button {
-webkit-appearance: none; -webkit-appearance: none;
margin: 0; margin: 0;
} }
input[type="number"]{ input[type="number"] {
-moz-appearance:textfield; -moz-appearance: textfield;
} }
</style> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册