提交 a67363f1 编写于 作者: Q qiang

fix: App 端延迟获取焦点 question/107820

上级 88ef44bb
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
:type="inputType" :type="inputType"
:maxlength="maxlength" :maxlength="maxlength"
:step="step" :step="step"
:autofocus="focus"
class="uni-input-input" class="uni-input-input"
autocomplete="off" autocomplete="off"
@focus="_onFocus" @focus="_onFocus"
...@@ -50,13 +49,13 @@ ...@@ -50,13 +49,13 @@
</template> </template>
<script> <script>
import { import {
baseInput field
} 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']
export default { export default {
name: 'Input', name: 'Input',
mixins: [baseInput], mixins: [field],
props: { props: {
name: { name: {
type: String, type: String,
...@@ -90,10 +89,6 @@ export default { ...@@ -90,10 +89,6 @@ export default {
type: [Number, String], type: [Number, String],
default: 140 default: 140
}, },
focus: {
type: [Boolean, String],
default: false
},
confirmType: { confirmType: {
type: String, type: String,
default: 'done' default: 'done'
...@@ -134,9 +129,6 @@ export default { ...@@ -134,9 +129,6 @@ export default {
} }
}, },
watch: { watch: {
focus (val) {
this.$refs.input && this.$refs.input[val ? 'focus' : 'blur']()
},
maxlength (value) { maxlength (value) {
const realValue = this.valueSync.slice(0, parseInt(value, 10)) const realValue = this.valueSync.slice(0, parseInt(value, 10))
realValue !== this.valueSync && (this.valueSync = realValue) realValue !== this.valueSync && (this.valueSync = realValue)
...@@ -168,6 +160,8 @@ export default { ...@@ -168,6 +160,8 @@ export default {
} }
$vm = $vm.$parent $vm = $vm.$parent
} }
this._initField('input')
}, },
beforeDestroy () { beforeDestroy () {
this.$dispatch('Form', 'uni-form-group-update', { this.$dispatch('Form', 'uni-form-group-update', {
......
...@@ -35,16 +35,15 @@ ...@@ -35,16 +35,15 @@
v-keyboard v-keyboard
:disabled="disabled" :disabled="disabled"
:maxlength="maxlengthNumber" :maxlength="maxlengthNumber"
:autofocus="autoFocus || focus"
:class="{ 'uni-textarea-textarea-fix-margin': fixMargin }" :class="{ 'uni-textarea-textarea-fix-margin': fixMargin }"
:style="{ 'overflow-y': autoHeight ? 'hidden' : 'auto' }" :style="{ 'overflow-y': autoHeight ? 'hidden' : 'auto' }"
class="uni-textarea-textarea" class="uni-textarea-textarea"
@compositionstart="_compositionstart" @compositionstart="_onCompositionstart"
@compositionend="_compositionend" @compositionend="_onCompositionend"
@input.stop="_input" @input.stop="_onInput"
@focus="_focus" @focus="_onFocus"
@blur="_blur" @blur="_onBlur"
@touchstart.passive="_touchstart" @touchstart.passive="_onTouchstart"
/> />
<textarea <textarea
v-if="disabled && fixColor" v-if="disabled && fixColor"
...@@ -62,12 +61,12 @@ ...@@ -62,12 +61,12 @@
</template> </template>
<script> <script>
import { import {
baseInput field
} from 'uni-mixins' } from 'uni-mixins'
const DARK_TEST_STRING = '(prefers-color-scheme: dark)' const DARK_TEST_STRING = '(prefers-color-scheme: dark)'
export default { export default {
name: 'Textarea', name: 'Textarea',
mixins: [baseInput], mixins: [field],
props: { props: {
name: { name: {
type: String, type: String,
...@@ -85,14 +84,6 @@ export default { ...@@ -85,14 +84,6 @@ export default {
type: [Boolean, String], type: [Boolean, String],
default: false default: false
}, },
focus: {
type: [Boolean, String],
default: false
},
autoFocus: {
type: [Boolean, String],
default: false
},
placeholderClass: { placeholderClass: {
type: String, type: String,
default: 'textarea-placeholder' default: 'textarea-placeholder'
...@@ -156,13 +147,6 @@ export default { ...@@ -156,13 +147,6 @@ export default {
focus (val) { focus (val) {
if (val) { if (val) {
this.focusChangeSource = 'focus' this.focusChangeSource = 'focus'
if (this.$refs.textarea) {
this.$refs.textarea.focus()
}
} else {
if (this.$refs.textarea) {
this.$refs.textarea.blur()
}
} }
}, },
focusSync (val) { focusSync (val) {
...@@ -214,6 +198,8 @@ export default { ...@@ -214,6 +198,8 @@ export default {
} }
$vm = $vm.$parent $vm = $vm.$parent
} }
this._initField('textarea')
}, },
beforeDestroy () { beforeDestroy () {
this.$dispatch('Form', 'uni-form-group-update', { this.$dispatch('Form', 'uni-form-group-update', {
...@@ -222,7 +208,7 @@ export default { ...@@ -222,7 +208,7 @@ export default {
}) })
}, },
methods: { methods: {
_focus: function ($event) { _onFocus: function ($event) {
this.focusSync = true this.focusSync = true
this.$trigger('focus', $event, { this.$trigger('focus', $event, {
value: this.valueSync value: this.valueSync
...@@ -239,20 +225,20 @@ export default { ...@@ -239,20 +225,20 @@ export default {
this.$refs.textarea.selectionEnd = this.$refs.textarea.selectionStart = this.cursorNumber this.$refs.textarea.selectionEnd = this.$refs.textarea.selectionStart = this.cursorNumber
} }
}, },
_blur: function ($event) { _onBlur: function ($event) {
this.focusSync = false this.focusSync = false
this.$trigger('blur', $event, { this.$trigger('blur', $event, {
value: this.valueSync, value: this.valueSync,
cursor: this.$refs.textarea.selectionEnd cursor: this.$refs.textarea.selectionEnd
}) })
}, },
_compositionstart ($event) { _onCompositionstart ($event) {
this.composition = true this.composition = true
}, },
_compositionend ($event) { _onCompositionend ($event) {
this.composition = false this.composition = false
// 部分输入法 compositionend 事件可能晚于 input // 部分输入法 compositionend 事件可能晚于 input
this._input($event) this._onInput($event)
}, },
// 暂无完成按钮,此功能未实现 // 暂无完成按钮,此功能未实现
_confirm ($event) { _confirm ($event) {
...@@ -265,13 +251,13 @@ export default { ...@@ -265,13 +251,13 @@ export default {
value: this.valueSync value: this.valueSync
}) })
}, },
_touchstart () { _onTouchstart () {
this.focusChangeSource = 'touch' this.focusChangeSource = 'touch'
}, },
_resize ({ height }) { _resize ({ height }) {
this.height = height this.height = height
}, },
_input ($event) { _onInput ($event) {
if (this.composition) { if (this.composition) {
this.valueComposition = $event.target.value this.valueComposition = $event.target.value
return return
......
...@@ -23,6 +23,10 @@ UniViewJSBridge.subscribe('getSelectedTextRange', function ({ pageId, callbackId ...@@ -23,6 +23,10 @@ UniViewJSBridge.subscribe('getSelectedTextRange', function ({ pageId, callbackId
}, pageId) }, pageId)
}) })
// App 延迟获取焦点
const FOCUS_DELAY = 200
let startTime
export default { export default {
name: 'BaseInput', name: 'BaseInput',
mixins: [emitter, keyboard], mixins: [emitter, keyboard],
...@@ -34,6 +38,17 @@ export default { ...@@ -34,6 +38,17 @@ export default {
value: { value: {
type: [String, Number], type: [String, Number],
default: '' default: ''
},
/**
* 已废弃属性,用于历史兼容
*/
autoFocus: {
type: [Boolean, String],
default: false
},
focus: {
type: [Boolean, String],
default: false
} }
}, },
data () { data () {
...@@ -41,6 +56,20 @@ export default { ...@@ -41,6 +56,20 @@ export default {
valueSync: this._getValueString(this.value) valueSync: this._getValueString(this.value)
} }
}, },
watch: {
focus (val) {
if (val) {
this._focus()
} else {
this._blur()
}
}
},
computed: {
needFocus () {
return this.autoFocus || this.focus
}
},
created () { created () {
const valueChange = this.__valueChange = debounce((val) => { const valueChange = this.__valueChange = debounce((val) => {
this.valueSync = this._getValueString(val) this.valueSync = this._getValueString(val)
...@@ -62,6 +91,38 @@ export default { ...@@ -62,6 +91,38 @@ export default {
methods: { methods: {
_getValueString (value) { _getValueString (value) {
return value === null ? '' : String(value) return value === null ? '' : String(value)
},
_initField (ref) {
this._fieldRef = ref
startTime = startTime || Date.now()
if (this.needFocus) {
this._focus()
}
},
_focus () {
if (!this.needFocus) {
return
}
const field = this.$refs[this._fieldRef]
if (!field || (__PLATFORM__ === 'app-plus' && !window.plus)) {
setTimeout(this._focus.bind(this), 100)
return
}
if (__PLATFORM__ === 'h5') {
field.focus()
} else {
const timeout = FOCUS_DELAY - (Date.now() - startTime)
if (timeout > 0) {
setTimeout(this._focus.bind(this), timeout)
return
}
field.focus()
plus.key.showSoftKeybord()
}
},
_blur () {
const field = this.$refs[this._fieldRef]
field && field.blur()
} }
} }
} }
...@@ -24,9 +24,9 @@ export { ...@@ -24,9 +24,9 @@ export {
from './keyboard' from './keyboard'
export { export {
default as baseInput default as field
} }
from './base-input' from './field'
export { export {
default as interact default as interact
......
...@@ -7,12 +7,6 @@ import { ...@@ -7,12 +7,6 @@ import {
*/ */
function iosHideKeyboard () { } function iosHideKeyboard () { }
function showSoftKeybord () {
plusReady(() => {
plus.key.showSoftKeybord()
})
}
function setSoftinputTemporary (vm, reset) { function setSoftinputTemporary (vm, reset) {
plusReady(() => { plusReady(() => {
const MODE_ADJUSTRESIZE = 'adjustResize' const MODE_ADJUSTRESIZE = 'adjustResize'
...@@ -98,13 +92,6 @@ export default { ...@@ -98,13 +92,6 @@ export default {
default: true default: true
} }
}, },
watch: {
focus (val) {
if (val && __PLATFORM__ === 'app-plus') {
showSoftKeybord()
}
}
},
directives: { directives: {
keyboard: { keyboard: {
inserted (el, binding, vnode) { inserted (el, binding, vnode) {
...@@ -112,11 +99,6 @@ export default { ...@@ -112,11 +99,6 @@ export default {
} }
} }
}, },
mounted () {
if ((this.autoFocus || this.focus) && __PLATFORM__ === 'app-plus') {
showSoftKeybord()
}
},
methods: { methods: {
initKeyboard (el) { initKeyboard (el) {
let focus let focus
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册