1.0.24 • Published 4 years ago

@nextgencodecompany/react-native-dropdown-autocomplete v1.0.24

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

react-native-dropdown-autocomplete

npm npm

Based on https://bit.ly/2AFjUsj, the most significant advantage of this package among all others is that you can have multiple autocomplete's on your page after following instructions below. Works on Android and IOS.

Usage

Install the package
npm i react-native-dropdown-autocomplete
Wrap the page you want to have autocomplete on with withKeyboardAwareScrollView:
import React, {Component} from "react";
import {StyleSheet, View, SafeAreaView} from "react-native";
import {Ionicons} from "@expo/vector-icons";
import shortid from "shortid";
import {Autocomplete, withKeyboardAwareScrollView} from "react-native-dropdown-autocomplete";

class HomeScreen extends Component {
  handleSelectItem(item, index) {
    const {onDropdownClose} = this.props;
    onDropdownClose();
    console.log(item);
  }

  render() {
    const autocompletes = [...Array(10).keys()];

    const apiUrl = "https://5b927fd14c818e001456e967.mockapi.io/branches";

    const {scrollToInput, onDropdownClose, onDropdownShow} = this.props;

    return (
      <View style={styles.autocompletesContainer}>
        <SafeAreaView>
          {autocompletes.map(() => (
            <Autocomplete
              key={shortid.generate()}
              style={styles.input}
              scrollToInput={ev => scrollToInput(ev)}
              handleSelectItem={(item, id) => this.handleSelectItem(item, id)}
              onDropdownClose={() => onDropdownClose()}
              onDropdownShow={() => onDropdownShow()}
              renderIcon={() => (
                <Ionicons name="ios-add-circle-outline" size={20} color="#c7c6c1" style={styles.plus} />
              )}
              fetchDataUrl={apiUrl}
              minimumCharactersCount={2}
              highlightText
              valueExtractor={item => item.name}
              rightContent
              rightTextExtractor={item => item.properties}
            />
          ))}
        </SafeAreaView>
      </View>
    );
  }
}
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,
  },
});

export default withKeyboardAwareScrollView(HomeScreen);

Usage without data-driven content

You can also pass fixed array of items to the Autocomplete

const data = [
  "Apples",
  "Broccoli",
  "Chicken",
  "Duck",
  "Eggs",
  "Fish",
  "Granola",
  "Hash Browns",
];

Change valueExtractor and pass the data to Autocomplete without fetchDataUrl

<Autocomplete data={data} valueExtractor={item => item} />

Properties

namedescriptiontypedefault
refUsed to access component methods ("clearInput" for example)RefObject-
autoCorrectDisable auto-correctBooleantrue
keyboardTypeInput Keyboard TypeStringdefault
highlightTextHighlight search resultsBooleantrue
highLightColorHighlight text colorString#129a8d
rightContentRender additional text to the right of the itemBooleanfalse
resetOnSelectReset the input after choosing an itemBooleanfalse
minimumCharactersCountPerform API request after certain number of characters enteredNumber2
waitIntervalTimeout between user finished typing and new data fetchNumber400
placeholderAutocomplete input placeholder textStringAdd Item
placeholderColorInput placeholder colorString#acada9
spinnerSizeSize of activity indicatorStringsmall
spinnerColorActivity indicator colorString#129a8d
listHeaderText at the beginning of suggestionsString-
fetchDataUrlData source urlString-
noDataTextText to display when no resultsStringNo Results
initialValueText to display initiallyString-
inputContainerStyleStyles for autocomplete containerObject-
disableFullscreenUIdisable display inputfield in fullscreenBooleanfalse
inputStyleStyles for autocomplete inputObject-
spinnerStyleStyles for activity indicatorObject-
noDataTextStyleStyles for empty results textObject-
separatorStyleStyles for item dividersObject-
listFooterStyleStyles for list footerObject-
listHeaderStyleStyles for list headerObject-
rightContentStyleStyles for right contentObject-
rightContentItemStyleStyles for right content textObject-
listHeaderTextStyleStyles for list header textObject-
overlayStyleStyles for overlay viewObject-
pickerStyleStyles for item picker viewObject-
containerStyleStyles for dropdown container viewObject-
scrollStyleStyles for dropdown flatlistObject-
scrollToInputFocus on selected fieldFunction-
handleSelectItemSelection callback (args: item, index)Function-
onDropdownShowShow keyboardFunction-
onDropdownCloseHide keyboardFunction-
onChangeTextAutocomplete input text changesFunction-
renderIconRender icon near inputFunction-
valueExtractorExtract value from item (args: item, index)Function({ value }) => value
rightTextExtractorExtract value from item (args: item, index)Function({ value }) => value
fetchDataFetch data for autocompleteFunction-

Methods

You can use methods through ref property:

...
const componentRef = useRef()
<Autocomplete
  ref={componentRef}
  ...
/>
...
componentRef.current.clearInput()

Methods list

methoddescriptionreturn
clearInputClears input value-

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Authors:

Maksym Plotnikov

Maksym Kordiyak

While developing our react-native application I found out that there's no autocomplete library available which can satisfy a need for > 1 autocomplete on a page. Styles were overlapping and due to position: "absolute" we weren't able to actually trigger TouchableOpacity on an element from the list on android device. We used a library which provides dropdown with auto-adjusting position using measureInWindow on a ref, we tweaked it a bit and with autocomplete input and time-slicing implementation we managed to get what we need. Still we can't interact with TextInput component while dropdown is opened but this is the best result we got using React Native Modal.