代码拉取完成,页面将自动刷新
同步操作将从 张翔/react-native-yusha-customKeyboard 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
//@flow
import React, {Component, PureComponent} from 'react'
import {
ScrollView,
findNodeHandle,
Dimensions,
TextInput,
View,
Platform,
Keyboard,
UIManager,
} from 'react-native'
import * as CustomKeyboard from './customKeyboard'
export default class AwareCusKeyBoardScrollView extends PureComponent {
state: Object
//防止自定义键盘切换之间的闪跳
showKeyBoard: boolean
resetTimeout: number
//监听系统键盘
showKeyBoardSub: any
hideKeyboardSub: any
//监听自定义键盘
showCustomKeyBoardSub: any
hideCustomKeyboardSub: any
//区分showSys->hideSys和showSys->showCus->hideSys之间的区别
flag: number
constructor() {
super(...arguments)
this.flag = 0
this.state = {
showKeyBoard: false,
}
}
componentDidMount() {
if (Platform.OS === 'android') {
this.showCustomKeyBoardSub = CustomKeyboard.addKeyBoardShowListener(this._onFocus)
this.hideCustomKeyboardSub = CustomKeyboard.addKeyBoardHideListener(this._onReset)
this.showKeyBoardSub = Keyboard.addListener('keyboardDidShow', this._showSysKeyborad)
this.hideKeyboardSub = Keyboard.addListener('keyboardDidHide', this._hideSysKeyboard)
}
}
componentDidUpdate(preProps: Object, preState: Object,) {
if (preState.showKeyBoard != this.state.showKeyBoard) {
this._updateScrollTo()
}
}
componentWillUnmount() {
this.showCustomKeyBoardSub && CustomKeyboard.removeKeyBoardListener(this.showCustomKeyBoardSub)
this.hideCustomKeyboardSub && CustomKeyboard.removeKeyBoardListener(this.hideCustomKeyboardSub)
this.showKeyBoardSub && this.showKeyBoardSub.remove()
this.hideKeyboardSub && this.hideKeyboardSub.remove()
this.scrollTimeout && clearTimeout(this.scrollTimeout)
this.resetTimeout && clearTimeout(this.resetTimeout)
}
_showSysKeyborad = (frames: Object) => {
console.log('show system keyboard')
//防止自定义键盘跳到系统键盘出bug
this.resetTimeout && clearTimeout(this.resetTimeout)
this.flag++
}
_hideSysKeyboard = () => {
this.flag--
if(this.flag === 0) {
//之间切换状态到键盘不显示
this.showKeyBoard = false
this._changeKeyBoardState()
}
}
_onFocus = () => {
this.showKeyBoard = true
this.flag += 2
this.resetTimeout && clearTimeout(this.resetTimeout)
this._changeKeyBoardState()
}
_onReset = () => {
this.showKeyBoard = false
this.flag -= 2
this.resetTimeout && clearTimeout(this.resetTimeout)
this.resetTimeout = setTimeout(()=>{
this._changeKeyBoardState()
}, 200)
}
_changeKeyBoardState = () => {
this.setState((preState) => {
if (preState.showKeyBoard === this.showKeyBoard) {
this._updateScrollTo()
return preState
}
return {showKeyBoard: this.showKeyBoard}
})
}
_onError = () => {}
_updateScrollTo = () => {
if(TextInput.State.currentlyFocusedField() == null) {
return
}
const currentlyTfNode = TextInput.State.currentlyFocusedField()
const scrollViewNode = findNodeHandle(this.refs.scrollView)
//显示和隐藏键盘
if (this.state.showKeyBoard) {
UIManager.measureInWindow(scrollViewNode, (x, y, width, height) => {
UIManager.measureLayout(
currentlyTfNode,
scrollViewNode,
this._onError,
(left, top, width, height) => {
const windowHeight = Dimensions.get('window').height
const subHeight = windowHeight - CustomKeyboard.currentHeight
const currentHeight = top + height + y + 30 //上下padding高度
if (subHeight < currentHeight) {
this.refs.scrollView.scrollTo({y: currentHeight - subHeight})
}
}
)
})
} else {
this.refs.scrollView.scrollTo({y: 0})
}
}
render() {
const {children, otherProps} = this.props
return (
<ScrollView
ref="scrollView"
key="scrollView"
keyboardShouldPersistTaps="handled"
{...otherProps}
>
{this.props.children}
<View style={{height: this.state.showKeyBoard ? CustomKeyboard.currentHeight : 0}}/>
</ScrollView>
)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。