-
Notifications
You must be signed in to change notification settings - Fork 384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat RN KeyboardAvoidingView #1857
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5034f0f
feat: add KeyboardAvoidingView
WX-DongXing 3094dea
fix: fix the issue of excessively long text display and inability to …
WX-DongXing 0fe77bc
feat: reduce judgment
WX-DongXing ea63620
feat: support input cursor-spacing
WX-DongXing d2d4901
feat: adapt to android adjustPan windowSoftInputMode
WX-DongXing a1ea528
fix: correct spelling errors
WX-DongXing b690903
feat: use in app layer
WX-DongXing 03ebf3e
feat: adapt auto focus
WX-DongXing 6e71c26
fix: fix the issue that Android device events cannot be triggered
WX-DongXing 6a56501
feat: switch back to use transform
WX-DongXing fb9b618
feat: set default style for textarea
WX-DongXing db0f47e
fix: calc above offset & rename vars
WX-DongXing 7ad599c
fix: element position correction after offset
WX-DongXing ed06883
feat: switch keyboardAvoidingView to Page layer
WX-DongXing c8276ba
feat: remove flexBasic on ios
WX-DongXing a2fe5a4
feat: keyboard avoid context use ref
WX-DongXing 7092548
fix: adjust ref
WX-DongXing b3bc2ce
Merge branch 'master' into feat-rn-keyboard-avoid-view
hiyuki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/webpack-plugin/lib/runtime/components/react/KeyboardAvoidingView.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import React, { ReactNode, useContext, useEffect } from 'react' | ||
import { DimensionValue, EmitterSubscription, Keyboard, Platform, View, ViewStyle } from 'react-native' | ||
import Animated, { useSharedValue, useAnimatedStyle, withTiming, Easing } from 'react-native-reanimated' | ||
import { KeyboardAvoidContext } from './context' | ||
import { extendObject } from './utils' | ||
|
||
type KeyboardAvoidViewProps = { | ||
children?: ReactNode | ||
style?: ViewStyle | ||
contentContainerStyle?: ViewStyle | ||
} | ||
|
||
const KeyboardAvoidingView = ({ children, style, contentContainerStyle }: KeyboardAvoidViewProps) => { | ||
const isIOS = Platform.OS === 'ios' | ||
const duration = isIOS ? 250 : 300 | ||
const easing = isIOS ? Easing.inOut(Easing.ease) : Easing.out(Easing.quad) | ||
|
||
const offset = useSharedValue(0) | ||
const basic = useSharedValue('auto') | ||
const keyboardAvoid = useContext(KeyboardAvoidContext) | ||
|
||
const animatedStyle = useAnimatedStyle(() => { | ||
return Object.assign( | ||
{ | ||
transform: [{ translateY: -offset.value }] | ||
}, | ||
isIOS ? {} : { flexBasis: basic.value as DimensionValue } | ||
) | ||
}) | ||
|
||
const resetKeyboard = () => { | ||
keyboardAvoid?.current && extendObject(keyboardAvoid.current, { | ||
cursorSpacing: 0, | ||
ref: null | ||
}) | ||
offset.value = withTiming(0, { duration, easing }) | ||
basic.value = 'auto' | ||
} | ||
|
||
useEffect(() => { | ||
let subscriptions: EmitterSubscription[] = [] | ||
|
||
if (isIOS) { | ||
subscriptions = [ | ||
Keyboard.addListener('keyboardWillShow', (evt: any) => { | ||
if (!keyboardAvoid?.current) return | ||
const { endCoordinates } = evt | ||
const { ref, cursorSpacing = 0 } = keyboardAvoid.current | ||
setTimeout(() => { | ||
ref?.current?.measure((x: number, y: number, width: number, height: number, pageX: number, pageY: number) => { | ||
const aboveOffset = offset.value + pageY + height - endCoordinates.screenY | ||
const aboveValue = -aboveOffset >= cursorSpacing ? 0 : aboveOffset + cursorSpacing | ||
const belowValue = Math.min(endCoordinates.height, aboveOffset + cursorSpacing) | ||
const value = aboveOffset > 0 ? belowValue : aboveValue | ||
offset.value = withTiming(value, { duration, easing }) | ||
}) | ||
}) | ||
}), | ||
Keyboard.addListener('keyboardWillHide', resetKeyboard) | ||
] | ||
} else { | ||
subscriptions = [ | ||
Keyboard.addListener('keyboardDidShow', (evt: any) => { | ||
if (!keyboardAvoid?.current) return | ||
const { endCoordinates } = evt | ||
const { ref, cursorSpacing = 0 } = keyboardAvoid.current | ||
ref?.current?.measure((x: number, y: number, width: number, height: number, pageX: number, pageY: number) => { | ||
const aboveOffset = pageY + height - endCoordinates.screenY | ||
const belowOffset = endCoordinates.height - aboveOffset | ||
const aboveValue = -aboveOffset >= cursorSpacing ? 0 : aboveOffset + cursorSpacing | ||
const belowValue = Math.min(belowOffset, cursorSpacing) | ||
const value = aboveOffset > 0 ? belowValue : aboveValue | ||
offset.value = withTiming(value, { duration, easing }, (finished) => { | ||
if (finished) { | ||
/** | ||
* In the Android environment, the layout information is not synchronized after the animation, | ||
* which results in the inability to correctly trigger element events. | ||
* Here, we utilize flexBasic to proactively trigger a re-layout | ||
*/ | ||
basic.value = '99.99%' | ||
} | ||
}) | ||
}) | ||
}), | ||
Keyboard.addListener('keyboardDidHide', resetKeyboard) | ||
] | ||
} | ||
|
||
return () => { | ||
subscriptions.forEach(subscription => subscription.remove()) | ||
} | ||
}, [keyboardAvoid]) | ||
|
||
return ( | ||
<View style={style}> | ||
<Animated.View | ||
style={[ | ||
contentContainerStyle, | ||
animatedStyle | ||
]} | ||
> | ||
{children} | ||
</Animated.View> | ||
</View> | ||
) | ||
} | ||
|
||
export default KeyboardAvoidingView |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
textarea也需要支持adjust-position