Skip to content
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

[RNMobile] Update KeyboardAwareFlatList iOS implementation #33170

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 56 additions & 51 deletions packages/components/src/mobile/keyboard-aware-flat-list/index.ios.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
/**
* External dependencies
*/
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
import { FlatList } from 'react-native';
import { isEqual } from 'lodash';
import { KeyboardAwareFlatList as RNKeyboardAwareFlatList } from 'react-native-keyboard-aware-scroll-view';

/**
* WordPress dependencies
*/
import { memo } from '@wordpress/element';

const List = memo( FlatList, isEqual );
import { useRef } from '@wordpress/element';

export const KeyboardAwareFlatList = ( {
extraScrollHeight,
Expand All @@ -19,53 +15,62 @@ export const KeyboardAwareFlatList = ( {
autoScroll,
scrollViewStyle,
inputAccessoryViewHeight,
scrollEnabled,
...listProps
} ) => (
<KeyboardAwareScrollView
style={ [ { flex: 1 }, scrollViewStyle ] }
keyboardDismissMode="none"
enableResetScrollToCoords={ false }
keyboardShouldPersistTaps="handled"
extraScrollHeight={ extraScrollHeight }
extraHeight={ 0 }
inputAccessoryViewHeight={ inputAccessoryViewHeight }
enableAutomaticScroll={ autoScroll === undefined ? false : autoScroll }
ref={ ( ref ) => {
this.scrollViewRef = ref;
innerRef( ref );
} }
onKeyboardWillHide={ () => {
this.keyboardWillShowIndicator = false;
} }
onKeyboardDidHide={ () => {
setTimeout( () => {
if (
! this.keyboardWillShowIndicator &&
this.latestContentOffsetY !== undefined &&
! shouldPreventAutomaticScroll()
) {
// Reset the content position if keyboard is still closed
if ( this.scrollViewRef ) {
this.scrollViewRef.scrollToPosition(
0,
this.latestContentOffsetY,
true
);
} ) => {
const scrollViewRef = useRef();
const keyboardWillShowIndicator = useRef();
const latestContentOffsetY = useRef();
return (
<RNKeyboardAwareFlatList
style={ [ { flex: 1 }, scrollViewStyle ] }
contentContainerStyle={ { flexGrow: 1 } }
keyboardDismissMode="none"
enableResetScrollToCoords={ false }
keyboardShouldPersistTaps="handled"
extraScrollHeight={ extraScrollHeight }
extraHeight={ 0 }
inputAccessoryViewHeight={ inputAccessoryViewHeight }
enableAutomaticScroll={
autoScroll === undefined ? false : autoScroll
}
ref={ ( ref ) => {
scrollViewRef.current = ref;
innerRef( ref );
} }
onKeyboardWillHide={ () => {
keyboardWillShowIndicator.current = false;
} }
onKeyboardDidHide={ () => {
setTimeout( () => {
if (
! keyboardWillShowIndicator.current &&
latestContentOffsetY.current !== undefined &&
! shouldPreventAutomaticScroll()
) {
// Reset the content position if keyboard is still closed
if ( scrollViewRef.current ) {
scrollViewRef.current.scrollToPosition(
0,
latestContentOffsetY.current,
true
);
}
}
}
}, 50 );
} }
onKeyboardWillShow={ () => {
this.keyboardWillShowIndicator = true;
} }
scrollEnabled={ listProps.scrollEnabled }
onScroll={ ( event ) => {
this.latestContentOffsetY = event.nativeEvent.contentOffset.y;
} }
>
<List { ...listProps } />
</KeyboardAwareScrollView>
);
}, 50 );
} }
onKeyboardWillShow={ () => {
keyboardWillShowIndicator.current = true;
} }
scrollEnabled={ scrollEnabled }
onScroll={ ( event ) => {
latestContentOffsetY.current =
event.nativeEvent.contentOffset.y;
} }
{ ...listProps }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this relocated prop spread is unintentionally overriding some of the props set above, or some of the locally set props are irrelevant. There appears to be only one usage of this component and it sets several style properties itself. We may need to merge the props or remove the locally set props.

  • keyboardShouldPersistTaps
  • style
  • scrollViewStyle
  • contentContainerStyle

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good point, we should review what was the purpose of the different style ones (style, scrollViewStyle and contentContainerStyle) in the original component and decide in each case the best way to proceed, probably some of the local ones are no longer necessary.

/>
);
};

KeyboardAwareFlatList.handleCaretVerticalPositionChange = (
scrollView,
Expand Down