【问题标题】:react-native-autocomplete-input list not closing when selecting an option选择选项时反应本机自动完成输入列表未关闭
【发布时间】:2017-10-04 18:13:43
【问题描述】:

当我从列表中选择一个项目时,我的react-native-autocomplete-input 列表似乎没有关闭。

let Location = (props) => (

  <View style={styles.formItem}>

    <Autocomplete
      data={props.autocompleteResults.predictions}
      defaultValue={props.locationInput.toString()}
      onChangeText={
        text => {
          props.updateLocationInput(text)
          props.getAutocompleteResults(text)
        }
      }
      renderItem={place => (
        <TouchableOpacity
          style={{
            height: 44,
            flex: 1,
            justifyContent: 'center',
            ...styles.label,
            borderRadius: 8
          }}
          onPress={() => {
            console.log(place)
            props.updatePlace(place)
            props.updateLocationInput(place.description)
          }}>
          <Text numberOfLines={1}>{place.description}</Text>
        </TouchableOpacity>
      )}
      inputContainerStyle={{ borderWidth: 0 }}
      style={{
        height: 44,
        borderRadius: 8,
        backgroundColor: '#FFFFFF',
        alignSelf: 'stretch',
        paddingLeft: 10,
        position: 'relative',
        ...styles.label
      }}
    />
  </View>
)
Location.propTypes = {
  locationInput: React.PropTypes.string.isRequired,
  updateLocationInput: React.PropTypes.func.isRequired,
  getAutocompleteResults: React.PropTypes.func.isRequired,
  autocompleteResults: React.PropTypes.object.isRequired,
  updatePlace: React.PropTypes.func.isRequired
}

Location = connect(
  mapStateToProps,
  mapDispatchToProps
)(Location)

这就是Location 组件的使用方式。 Container 是一个原生组件:

<Container style={styles.container}>
        <ScrollView keyboardShouldPersistTaps={true}>
          <Categories />
          <View style={{ zIndex: 2, position: 'relative' }}>
            <Location />
          </View>
          <Keywords />
          <Button block style={styles.wideButton} onPress={() => props.toggleMenu()}>
            <Text>GO</Text>
          </Button>
        </ScrollView>
      </Container>

Location 组件位于ScrollView 内,但是当我取出滚动视图时问题仍然存在。我也完成了滚动视图修复&lt;ScrollView keyboardShouldPersistTaps={true}&gt; 是什么让列表永远不会关闭?

【问题讨论】:

  • react-native-autocomplete-input 的版本?
  • @Codesingh 版本 3.1.2
  • 把它降到 v1.1.2
  • 你在安卓上试试吗?
  • @Codesingh iOS 模拟器...刚刚想到...也许我应该在真实设备上尝试一下

标签: javascript css reactjs react-native jsx


【解决方案1】:

我们可以试试这个:-

//if data is not available then there would be nothing to show..
data={(!props.autocompleteResults.predictions.length || props.clicked)? [] : props.autocompleteResults.predictions }

onChangeText={
        text => {
          props.updateLocationInput(text)
          props.getAutocompleteResults(text)
          props.changeClick(false)
        }
      }

onPress={() => {
            console.log(place)
            props.updatePlace(place)
            props.updateLocationInput(place.description)
            props.changeClick(true) 
          }}

Inside the component:

constructor(props)
{
  super(props);
  this.state={ clicked : false }
}

<Location clicked = {this.state.clicked} changeClick={(isClicked) => this.setState({clicked: isClicked})} />

干杯:)

【讨论】:

  • 谢谢!对于不想阅读特定于我的案例的代码的其他人:只需从 onPress() 函数内部清空 &lt;Autocomplete&gt; 的数据属性即可。当没有要显示的列表项时,自动完成关闭。
  • 没错。:)
  • 我看到有一个可用于此功能的道具,hideResults={bool}.. :)
猜你喜欢
  • 2021-09-20
  • 1970-01-01
  • 2017-09-06
  • 2019-03-19
  • 2013-09-29
  • 1970-01-01
  • 1970-01-01
  • 2020-03-31
  • 2012-07-08
相关资源
最近更新 更多