【问题标题】:Making react-native-autocomplete-input visible when keyboard is opened打开键盘时使 react-native-autocomplete-input 可见
【发布时间】:2023-03-22 11:15:01
【问题描述】:

我使用react-native-autocomplete-input 组件,它位于屏幕底部,但我不知道如何在打开键盘时显示结果列表。我尝试使用ScrollViewreact-native-keyboard-spacer 组件,但结果列表在键盘后面。

import React, { Component } from 'react';
import {
  View,
  ScrollView,
  Platform,
  TouchableOpacity,
  Text,
} from 'react-native';
import styles from './styles';
import KeyboardSpacer from 'react-native-keyboard-spacer';
import Autocomplete from 'react-native-autocomplete-input';

const API = 'https://swapi.co/api';

class Container extends Component {
  constructor(props) {
    super(props);

    this.state = {
      query: '',
    };
  }

  componentDidMount() {
    fetch(`${API}/films/`)
      .then(res => res.json())
      .then((json) => {
        const { results: films } = json;
        this.setState({ films });
      });
  }

  findFilm(query) {
    if (query === '') {
      return [];
    }

    const { films } = this.state;
    const regex = new RegExp(`${query.trim()}`, 'i');
    return films.filter(film => film.title.search(regex) >= 0);
  }

  render() {
    const { query } = this.state;
    const films = this.findFilm(query);
    const comp = (a, b) => a.toLowerCase().trim() === b.toLowerCase().trim();

    return (
      <View style={styles.container}>
        <ScrollView style={styles.scrollView}>
          <Autocomplete
            autoCapitalize="none"
            autoCorrect={false}
            containerStyle={styles.autocompleteContainer}
            data={films.length === 1 && comp(query, films[0].title) ? [] : films}
            defaultValue={query}
            onChangeText={text => this.setState({ query: text })}
            placeholder="Enter Star Wars film title"
            renderItem={({ title, release_date }) => (
              <TouchableOpacity onPress={() => this.setState({ query: title })}>
                <Text style={styles.itemText}>
                  {title} ({release_date.split('-')[0]})
                </Text>
              </TouchableOpacity>
            )}
          />
        </ScrollView>
        {Platform.OS === 'ios' ? <KeyboardSpacer /> : null}
      </View>
    );
  }
}

export default Container;

export default styles = StyleSheet.create({
  container: {
    flex: 1,
    fontFamily: 'proxima_nova_regular',
    alignSelf: 'stretch',
  },
  scrollView: {
    alignSelf: 'stretch',
    flex: 1,
  },
  autocompleteContainer: {
    marginLeft: 10,
    marginRight: 10,
    marginTop: 400,
  },
  itemText: {
    fontSize: 15,
    margin: 2,
  },
});

【问题讨论】:

  • 从 autocompleteContainer 样式类中移除 marginTop。
  • 我需要底部的自动完成组件
  • 然后在自动完成文本字段的焦点上添加marginBottom。
  • 没用,你只是在结果列表和文本输入之间留了一个差距
  • 如果您的自动完成是固定高度或有最大高度,您可以在键盘上addListener 并使用自动完成的填充/边距动态放置输入。

标签: react-native


【解决方案1】:

考虑到您的情况,您需要react-native-keyboard-aware-scroll-view

虽然 React Native 在实现 KeyboardAvoidingView 方面做得很好,但它有很多问题,正如 here 所提到的,特别是在 ScrollView 中,它在底部造成了额外的松弛

而且由于列表项的内容可以是动态的,因此有必要将它们包装在 ScrollView 中。

您可以在代码中进行以下更改。

import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'

<KeyboardAwareScrollView
    innerRef={ref => this.scrollView = ref} //... Access the ref for any other functions here
    contentContainerStyle={{flex: 1}}>
    <Autocomplete
        onFocus={() => {this.scrollView.props.scrollToEnd({animated: true})}} // ... Scroll To End on TextInput focus
        autoCapitalize="none"
        autoCorrect={false}
        containerStyle={styles.autocompleteContainer}
        data={films.length === 1 && comp(query, films[0].title) ? [] : films}
        defaultValue={query}
        onChangeText={text => this.setState({ query: text })}
        placeholder="Enter Star Wars film title"
        renderItem={({ title, release_date }) => (
            <TouchableOpacity onPress={() => this.setState({ query: title })}>
                <Text style={styles.itemText}>
                    {title} ({release_date.split('-')[0]})
                </Text>
            </TouchableOpacity>
        )}
    />    
</KeyboardAwareScrollView>

【讨论】:

  • 谢谢,效果很好。明天我会奖励你的答案。系统不允许我早点这样做。
猜你喜欢
  • 2020-10-05
  • 2016-11-11
  • 1970-01-01
  • 2020-12-13
  • 2019-02-08
  • 1970-01-01
  • 2018-03-25
  • 2021-05-15
  • 1970-01-01
相关资源
最近更新 更多