Last active
January 13, 2017 16:52
-
-
Save Lepozepo/e6c3a544f5c35bf296df1b27de875746 to your computer and use it in GitHub Desktop.
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
import { Component, PropTypes } from 'react'; | |
import { ScrollView, TextInput } from 'react-native'; | |
// Parent Component | |
class Card extends Component { | |
static childContextTypes = { | |
handleFocus: PropTypes.func, | |
} | |
getChildContext = () => ({ | |
handleFocus: this.handleFocus, | |
}) | |
handleFocus = (y) => { | |
this.scrollView.scrollTo({ y }); | |
} | |
render() { | |
const {children} = this.props; | |
return ( | |
<ScrollView ref={(r) => {this.scrollView = r}}> | |
{children} | |
</ScrollView> | |
); | |
} | |
} | |
// Child Component | |
class CardTextInput extends Component { | |
static contextTypes = { | |
handleFocus: PropTypes.func, | |
} | |
handleFocus = () => { | |
if (!this.context.handleFocus) return; | |
this.context.handleFocus(this.yPosition); | |
} | |
handleLayout = ({nativeEvent:{layout:{ y }}}) => { | |
this.yPosition = y; | |
} | |
render() { | |
return ( | |
<TextInput | |
onLayout={this.handleLayout} | |
onFocus={this.handleFocus} | |
/> | |
); | |
} | |
} | |
// A developer using your awesome it-just-works component | |
class Cards extends Component { | |
render() { | |
return ( | |
<Card> | |
<CardTextInput /> | |
</Card> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment