【发布时间】:2017-10-05 10:04:48
【问题描述】:
问题:我有一个带有 TextInput 和 SectionList 的页面。当我专注于 TextInput 然后单击 SectionList 中的 ListItem 时 - 它只隐藏键盘但不导航到 SecondPage,它应该在 onPress 上执行,如下所示。如果 TextInput 没有焦点 - 一切正常。
期望的行为:在关注 TextInput 时,如果我单击 ListItem - 它应该将我导航到 SecondPage。
问题:
1. 我的代码有问题还是 SectionList 的正常行为?
2. 有没有办法实现期望的行为?
代码:
SectionList + TextInput 组件
const ds = [
{ data: [ {word: 'BMW'}, {word: 'Mercedez'}, {word: 'Tesla'}], title: 'Cars' },
{ data: [{word: 'Finland'}, {word: 'USA'}, {word: 'Somalia'}], title: 'Countries' },
{ data: [{word: 'Water'}, {word: 'Coke'}, {word: 'Tea'}], title: 'Liquids' },
];
class FirstPage extends React.Component {
render() {
return (
<View>
<View style={{ flexDirection: 'row' }}>
<TextInput
onChangeText={(v) => this.setState({ text: v})}
style={{ borderWidth: 1, flex: 1 }}
value={this.state.text}
/>
</View>
<SectionList
renderItem={({ item }) => <ListItem word={item.word} navigation={this.props.navigation} />}
renderSectionHeader={({ section }) => <ListItem word={section.title} section/>}
sections={ds}
keyExtractor={(item) => item.word}
/>
</View>
);
}
}
ListItem 组件
render() {
const { navigation, section, word } = this.props;
const { letterStyle, noteStyle, sectionStyle, termStyle } = styles;
if (section)
{
return (
<TouchableOpacity>
<View>
<CardSection style={sectionStyle}>
<Text style={letterStyle}>{word}</Text>
</CardSection>
</View>
</TouchableOpacity>
);
}
return (
<TouchableOpacity
onPress={() => navigation.navigate('SecondPage')}
>
<View>
<CardSection style={sectionStyle}>
<Text numberOfLines={1} style={termStyle}>{word}</Text>
</CardSection>
</View>
</TouchableOpacity>
);
}
【问题讨论】:
-
此解决方案也适用于 SectionLists:stackoverflow.com/questions/36307853/… 修改了此链接处的选定答案,以反映该解决方案对 SectionLists 的成功应用。
标签: javascript reactjs react-native react-navigation