在过滤器功能中键入错误

时间:2018-04-27 18:09:30

标签: react-native react-android

我正在尝试在React Native中创建一个自动完成文本框。我在过滤功能上遇到错误。当用户键入服务时,文本框应该使用服务的全名自动完成。服务名称来自我的json文件。我正在使用' react-native-autocomplete-input'为了实现这一目标。以下是错误的屏幕截图:

enter image description here

以下是我的App.js代码。

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */



import service from './services.json';


import Autocomplete from 'react-native-autocomplete-input';
import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  TouchableOpacity,
  View
} from 'react-native';



class Autocomp extends Component {
  static renderServices(coservice) {
    const { ser, Location, secondLoc} = coservice;


    return (
      <View>
        <Text style={styles.titleText}>{ser}</Text>
        <Text style={styles.openingText}>{secondLoc}</Text>
      </View>
    );
  }

  constructor(props) {
    super(props);
    this.state = {
       query: '',
       services:[]
    };
  }

  componentDidMount(){
      const {results: services} = service;
      this.setState({services});

  }




    findServices(query) {
      const inputValue = query.trim().toLowerCase();
      const inputLength = inputValue.length;

      const { services } = this.state;
      return inputLength === 0 ? [] : services.filter(ser =>ser.toLowerCase().slice(0, inputLength) === inputValue);
  }




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

    return (
      <View style={styles.container}>
        <Autocomplete
          autoCapitalize="none"
          autoCorrect={false}
          containerStyle={styles.autocompleteContainer}
          data={services.length === 1 && comp(query, services[0].ser) ? [] : services}
          defaultValue={query}
          onChangeText={text => this.setState({ query: text })}
          placeholder="Enter Services here"
          renderItem={({ ser, Phone }) => (
            <TouchableOpacity onPress={() => this.setState({ query: ser })}>
              <Text style={styles.itemText}>
                {ser} 
              </Text>
            </TouchableOpacity>
          )}
        />
        <View style={styles.descriptionContainer}>
          {services.length > 0 ? (
            Autocomp.renderServices(services[0])
          ) : (
            <Text style={styles.infoText}>
              Enter services
            </Text>
          )}
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    backgroundColor: '#F5FCFF',
    flex: 1,
    paddingTop: 25
  },
  autocompleteContainer: {
    flex: 1,
    left: 0,
    position: 'absolute',
    right: 0,
    top: 0,
    zIndex: 1
  },
  itemText: {
    fontSize: 15,
    margin: 2
  },
  descriptionContainer: {
    // `backgroundColor` needs to be set otherwise the
    // autocomplete input will disappear on text input.
    backgroundColor: '#F5FCFF',
    marginTop: 25
  },
  infoText: {
    textAlign: 'center'
  },
  titleText: {
    fontSize: 18,
    fontWeight: '500',
    marginBottom: 10,
    marginTop: 10,
    textAlign: 'center'
  },
  directorText: {
    color: 'grey',
    fontSize: 12,
    marginBottom: 10,
    textAlign: 'center'
  },
  openingText: {
    textAlign: 'center'
  }
});
export default Autocomp;

以下是我的services.json文件

{
    "id":1,
    "ser": "Service1",
    "Location": "TestLoc1",
    "Phone":"(999)-921-9292",
    "SecondLoc": "TestLoc",
    "email":"accrmail@test.com",
    "sourceLat":"33.977806",
    "sourceLong":"-117.373261",
    "destLatL1":"33.613355",
    "destLongL1":"-114.596569",
    "destLatL2":"33.761693",
    "destLongL2":"-116.971169",
    "destAddr1": "Test Drive, 99999",
    "destAddr2": "Test City, Test Drive, 92345"
  },

  {
    "id":1,
    "ser": "TestService",
    "Location": "TestLoc1",
    "Phone":"(999)-921-9292",
    "SecondLoc": "TestLoc",
    "email":"accrmail@test.com",
    "sourceLat":"33.977806",
    "sourceLong":"-117.373261",
    "destLatL1":"33.613355",
    "destLongL1":"-114.596569",
    "destLatL2":"33.761693",
    "destLongL2":"-116.971169",
    "destAddr1": "Test Drive, 99999",
    "destAddr2": "Test City, Test Drive, 92345"
  },
  ]

任何帮助都将受到高度赞赏。我检查了这个功能。一切看起来都很正确。

1 个答案:

答案 0 :(得分:1)

假设您的json文件如下所示,您的代码存在两个问题。

  • 解构是错误的。由于您直接从json文件导入名称为services的对象,尚未分配给任何命名的常量/变量,因此无法进行解构。< / LI>

因此,您必须将代码更改为

import services from './services.json';

componentDidMount(){
      this.setState({services});
  }
  • 您正在尝试在此处转换 service对象 toLowerCase ser =>ser.toLowerCase() 需要更改为

    services.filter(({ser}) => ser.toLowerCase().slice(0, inputLength) === inputValue);