提交 cd1298ab 编写于 作者: Q qiang

feat: input、textarea 组件支持 keyboardheightchange 事件

上级 0bc71b7d
import { import {
plusReady plusReady
} from 'uni-shared' } from 'uni-shared'
import emitter from './emitter'
/** /**
* 保证iOS点击输入框外隐藏键盘 * 保证iOS点击输入框外隐藏键盘
...@@ -65,15 +66,22 @@ function resetSoftinputNavBar (vm) { ...@@ -65,15 +66,22 @@ function resetSoftinputNavBar (vm) {
let resetTimer let resetTimer
let isAndroid let isAndroid
let osVersion let osVersion
let keyboardHeight
let keyboardChangeCallback
if (__PLATFORM__ === 'app-plus') { if (__PLATFORM__ === 'app-plus') {
plusReady(() => { plusReady(() => {
isAndroid = plus.os.name.toLowerCase() === 'android' isAndroid = plus.os.name.toLowerCase() === 'android'
osVersion = plus.os.version osVersion = plus.os.version
}) })
document.addEventListener('keyboardchange', function (event) {
keyboardHeight = event.height
keyboardChangeCallback && keyboardChangeCallback()
}, false)
} }
export default { export default {
name: 'Keyboard', name: 'Keyboard',
mixins: [emitter],
props: { props: {
cursorSpacing: { cursorSpacing: {
type: [Number, String], type: [Number, String],
...@@ -102,10 +110,12 @@ export default { ...@@ -102,10 +110,12 @@ export default {
methods: { methods: {
initKeyboard (el) { initKeyboard (el) {
let focus let focus
let keyboardHeight
const keyboardChange = (event) => { const keyboardChange = () => {
keyboardHeight = event.height this.$trigger('keyboardheightchange', {}, {
height: keyboardHeight,
duration: 0.25
})
// 安卓切换不同键盘类型时会导致键盘收回,需重新设置 // 安卓切换不同键盘类型时会导致键盘收回,需重新设置
if (focus && keyboardHeight === 0) { if (focus && keyboardHeight === 0) {
setSoftinputTemporary(this) setSoftinputTemporary(this)
...@@ -122,7 +132,13 @@ export default { ...@@ -122,7 +132,13 @@ export default {
document.addEventListener('click', iosHideKeyboard, false) document.addEventListener('click', iosHideKeyboard, false)
if (__PLATFORM__ === 'app-plus') { if (__PLATFORM__ === 'app-plus') {
document.addEventListener('keyboardchange', keyboardChange, false) keyboardChangeCallback = keyboardChange
if (keyboardHeight) {
this.$trigger('keyboardheightchange', {}, {
height: keyboardHeight,
duration: 0
})
}
setSoftinputNavBar(this) setSoftinputNavBar(this)
setSoftinputTemporary(this) setSoftinputTemporary(this)
} }
...@@ -149,7 +165,13 @@ export default { ...@@ -149,7 +165,13 @@ export default {
document.removeEventListener('click', iosHideKeyboard, false) document.removeEventListener('click', iosHideKeyboard, false)
if (__PLATFORM__ === 'app-plus') { if (__PLATFORM__ === 'app-plus') {
document.removeEventListener('keyboardchange', keyboardChange, false) keyboardChangeCallback = null
if (keyboardHeight) {
this.$trigger('keyboardheightchange', {}, {
height: 0,
duration: 0
})
}
resetSoftinputNavBar(this) resetSoftinputNavBar(this)
if (isAndroid) { if (isAndroid) {
// 还原安卓软键盘配置,避免影响 web-view 组件 // 还原安卓软键盘配置,避免影响 web-view 组件
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册