Невозможно щелкнуть ListItem в SectionList, имея фокус в TextInput

Проблема: у меня есть страница с TextInput и SectionList. Когда я фокусируюсь на TextInput, а затем нажимаю на ListItem в SectionList, он только скрывает клавиатуру, но не переходит на SecondPage, что он должен делать при нажатии, как вы можете видеть ниже. Если 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>
            );
    }

person Eduard    schedule 05.10.2017    source источник
comment
Это решение работает и для SectionList: «> stackoverflow.com/questions/36307853/ Изменен выбранный ответ по этой ссылке, чтобы отразить успешное применение решения для SectionLists.   -  person Eduard    schedule 05.10.2017


Ответы (1)


Вы пробовали это:

onMenuPress(item){
   console.log(item);
   this.props.navigation.navigate("some_route");
}

render() {
.....some code
    <ListItem onPress={(item) => this.onMenuPress(item)}>
....more code
}

Помните, что привязывайте метод onMenuPress к конструктору, например:

constructor(props){
   super(props);
   this.onMenuPress = this.onMenuPress.bind(this);
}

Работает на меня.

person Julian Corrêa    schedule 24.01.2018