Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autocomplete drop down not working when data get using the state #48

Open
kruz123 opened this issue Mar 19, 2020 · 6 comments
Open

autocomplete drop down not working when data get using the state #48

kruz123 opened this issue Mar 19, 2020 · 6 comments

Comments

@kruz123
Copy link

kruz123 commented Mar 19, 2020

hello, i am implement this library in my project and call my project api and get the data and set data in state and update the state but the problem is that when i give the data in the autocomplete in data attribute and when run the project then that time autocomplete not suggest the drop down. and not show the result but when i use the static array and after i apply then that time complete work. so please give me solutions.

here is my code----------------

import React, { Component } from 'react';
import { View, StyleSheet, SafeAreaView, AsyncStorage } from 'react-native';
import { Autocomplete, withKeyboardAwareScrollView } from "react-native-dropdown-autocomplete";

class demoDropDown extends Component {

constructor(props) {
    super(props);

    this.state = {
        stateList: []
    }

    this.container = React.createRef();
    this.handleChangeInput = this.handleChangeInput.bind(this);
}

componentDidMount = async () => {
    console.log("call componentDidMount");
    this.callCityApi();
}

callCityApi = async () => {
    console.log("call callCityApi");
    var apiUrl = 'MY API URL';

    var data = JSON.stringify({
        'authToken': 'UJTOBB'
    })

    // call ws
    fetch(apiUrl, {
        method: 'POST',
        headers: {
            // Accept: 'application/json',
            'Content-Type': 'application/json'
        },
        body: data,
    }).then((response) => {
        if (response.status === 200) {
            return response.json().then((responseData => {
                //  console.log("responsedata::" + JSON.stringify(responseData));   
                if (responseData.header.Status === 706) {
                    alert("706 ::+" + JSON.stringify(responseData));
                }
                else if (responseData.header.Status === 200) {
                    var stateListData = responseData.Data.CityList;
                    // console.log("stateListData::" + JSON.stringify(stateListData));   
                    console.log("stateListData ::" + stateListData.length);

                    AsyncStorage.setItem('stateListData', JSON.stringify(stateListData));

                    this.autoCompleteDropDown();
                }
                else {
                    alert("Other ::+" + JSON.stringify(responseData));
                }
            }))
        }
        else {
            alert("There is something wrong, please try again later, error code: " + response.status)
        }

    })
}

autoCompleteDropDown = () => {
    AsyncStorage.getItem('stateListData').then
        ((value) => {
            
            //console.log("Get value from async storage ::"+value);
            
            this.setState({
                stateList: value
            })
        })
}

handleChangeInput() {
  //console.log("on change text ::"+data);
}

handleSelectItem(item) {
    const { onDropdownClose } = this.props;
    onDropdownClose();
    console.log(item);
}

render() {
    console.log("call render");
    //console.log("stateList ::" + this.state.stateList);
    const { scrollToInput, onDropdownClose, onDropdownShow } = this.props;

    const data = this.state.stateList;
    
    return (
        <View style={styles.autocompletesContainer}>
            <SafeAreaView>
                <Autocomplete
                    ref={ref => {
                        this.container = ref;
                    }}
                    data={data}
                    inputContainerStyle={styles.inputContainer}
                    style={styles.input}
                    scrollToInput={ev => scrollToInput(ev)}
                    handleSelectItem={(item) => this.handleSelectItem(item)}
                    onDropdownClose={() => onDropdownClose()}
                    onDropdownShow={() => onDropdownShow()}
                    minimumCharactersCount={1}
                    onChangeText={() => this.handleChangeInput()}
                    highlightText
                    valueExtractor={item => item}
                />
            </SafeAreaView>
        </View>
    )
}

}

export default withKeyboardAwareScrollView(demoDropDown);

const styles = StyleSheet.create({
autocompletesContainer: {
paddingTop: 0,
zIndex: 1,
width: "100%",
paddingHorizontal: 8,
},
input: { maxHeight: 40 },
inputContainer: {
display: "flex",
flexShrink: 0,
flexGrow: 0,
flexDirection: "row",
flexWrap: "wrap",
alignItems: "center",
// borderBottomWidth: 1,
// borderColor: "#c7c6c1",
paddingVertical: 13,
paddingLeft: 12,
paddingRight: "5%",
width: "100%",
justifyContent: "flex-start",
},
container: {
flex: 1,
backgroundColor: "#ffffff",
},
plus: {
position: "absolute",
left: 15,
top: 10,
},
});

@pavelustenko
Copy link

try to rewrite class to use fetchData prop instead of data prop.

@Yariv-h
Copy link

Yariv-h commented Apr 13, 2020

Same issue here..

try to rewrite class to use fetchData prop instead of data prop.

is is possible to use with data? i would like to fetch the data only once

@lklima
Copy link

lklima commented May 13, 2020

try:
{data && ( <Autocomplete data={data} />)}

@OleksandrFomin
Copy link

TypeScript throws a warning that prop data does not exist on Autocomplete type.
Property 'data' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Autocomplete> & Readonly<AutocompleteProps> & Readonly<...>'.

@temuccio
Copy link

temuccio commented Oct 5, 2020

Hi,
I have the same problem.
I load the <Autocomplete data={data} /> when {data} is ready but does't works
{data.length > 0 ? ( <Autocomplete data={data} />) : (console.log('not data')) }

@temuccio
Copy link

temuccio commented Oct 5, 2020

Sorry, I have solved....The problem is equired props that I have omissed.
With
<Autocomplete data={data} valueExtractor={item => item.descr} />
works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants