-
Notifications
You must be signed in to change notification settings - Fork 3k
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
migrated ScrollViewWithContext to a functional component #23879
Changes from 1 commit
4c4faa3
fef0ab6
31bcc5e
11e7c46
3fd0ead
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,54 @@ | ||
import React from 'react'; | ||
import {ScrollView} from 'react-native'; | ||
import React, { useState } from 'react'; | ||
import { ScrollView } from 'react-native'; | ||
|
||
const MIN_SMOOTH_SCROLL_EVENT_THROTTLE = 16; | ||
|
||
const ScrollContext = React.createContext(); | ||
const defaultScrollViewRef = React.createRef(); | ||
|
||
// eslint-disable-next-line react/forbid-foreign-prop-types | ||
const propTypes = ScrollView.propTypes; | ||
|
||
/* | ||
* <ScrollViewWithContext /> is a wrapper around <ScrollView /> that provides a ref to the <ScrollView />. | ||
* <ScrollViewWithContext /> can be used as a direct replacement for <ScrollView /> | ||
* if it contains one or more <Picker /> / <RNPickerSelect /> components. | ||
* Using this wrapper will automatically handle scrolling to the picker's <TextInput /> | ||
* when the picker modal is opened | ||
*/ | ||
class ScrollViewWithContext extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
contentOffsetY: 0, | ||
}; | ||
this.scrollViewRef = this.props.innerRef || React.createRef(null); | ||
|
||
this.setContextScrollPosition = this.setContextScrollPosition.bind(this); | ||
} | ||
|
||
setContextScrollPosition(event) { | ||
if (this.props.onScroll) { | ||
this.props.onScroll(event); | ||
} | ||
this.setState({contentOffsetY: event.nativeEvent.contentOffset.y}); | ||
} | ||
|
||
render() { | ||
return ( | ||
<ScrollView | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...this.props} | ||
ref={this.scrollViewRef} | ||
onScroll={this.setContextScrollPosition} | ||
scrollEventThrottle={this.props.scrollEventThrottle || MIN_SMOOTH_SCROLL_EVENT_THROTTLE} | ||
> | ||
<ScrollContext.Provider | ||
value={{ | ||
scrollViewRef: this.scrollViewRef, | ||
contentOffsetY: this.state.contentOffsetY, | ||
}} | ||
> | ||
{this.props.children} | ||
</ScrollContext.Provider> | ||
</ScrollView> | ||
); | ||
} | ||
} | ||
ScrollViewWithContext.propTypes = propTypes; | ||
const ScrollViewWithContext = React.forwardRef( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Write this as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
({ onScroll, scrollEventThrottle, children, innerRef, ...restProps }, ref) => { | ||
const [contentOffsetY, setContentOffsetY] = useState(0); | ||
const scrollViewRef = innerRef || ref || defaultScrollViewRef; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the fallback ref we should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. can't add directly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we still have innerRef and ref? Shouldn't it be just innerRef? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed extra ref |
||
|
||
const setContextScrollPosition = (event) => { | ||
if (onScroll) { | ||
onScroll(event); | ||
} | ||
setContentOffsetY(event.nativeEvent.contentOffset.y); | ||
}; | ||
|
||
return ( | ||
<ScrollView | ||
ref={scrollViewRef} | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...restProps} | ||
onScroll={setContextScrollPosition} | ||
scrollEventThrottle={scrollEventThrottle || MIN_SMOOTH_SCROLL_EVENT_THROTTLE} | ||
> | ||
<ScrollContext.Provider | ||
value={{ | ||
scrollViewRef, | ||
contentOffsetY, | ||
}} | ||
> | ||
{children} | ||
</ScrollContext.Provider> | ||
</ScrollView> | ||
); | ||
} | ||
); | ||
|
||
ScrollViewWithContext.propTypes = propTypes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
|
||
export default React.forwardRef((props, ref) => ( | ||
<ScrollViewWithContext | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
innerRef={ref} | ||
/> | ||
<ScrollViewWithContext | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
innerRef={ref} | ||
/> | ||
)); | ||
|
||
export {ScrollContext}; | ||
export { ScrollContext }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing new line. Please run prettier ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
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.
Can you please add the comment back?
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.
done