HomeOur TeamContact

Local List Auto Complete

By Utkarsh Lubal
Published in Others
May 06, 2023
1 min read
Local List Auto Complete

Table Of Contents

01
Introduction
02
Installation
03
Example
04
Tutorial

Introduction

React Native Autocomplete is a library that allows users to easily create an autocomplete input field in their React Native app. Autocomplete is a feature that can be useful for many types of applications, especially those that rely heavily on user input, such as search engines or e-commerce apps.

With the React Native Autocomplete library, developers can create an input field that will display a list of suggested values based on the user’s input. This can be done by simply passing an array of options to the Autocomplete component. The user’s input is then matched against the options, and a list of matches is displayed.

The Autocomplete component also allows for customization of the suggestion list, including custom rendering of individual options and the ability to limit the number of options displayed at once. Overall, React Native Autocomplete is a powerful and flexible library that can help developers create more user-friendly and efficient input fields in their React Native apps.

Events

eventInfo
onTypingText is entered. The callback can be delayed with option autoCompleteFetchRequestDelay.
onSelectA cell in the suggestions list is selected.
onFocusText input get focus.
onBlurText input lost focus.

Other events from Text Input are avalaible.

Global options

optiontypeInfo
cellComponentstringName of a React Native component used to render cells. If null, use the default rendering.
suggestionsarrayIf using default cell rendering specify an Array of string, otherwise any object.
autoCompleteFetchRequestDelaynumberDelay in milliseconds before retrieving suggestions.
maximumNumberOfAutoCompleteRowsnumberNumber of suggestions displayed.
showTextFieldDropShadowWhenAutoCompleteTableIsOpenboolDisplay a drop shadow around the text field.
autoCompleteTableViewHiddenboolIf true, the suggestions list will be hidden.
autoCompleteTableBorderColorcolorSet suggestions list border color.
autoCompleteTableBorderWidthnumberSet suggestions list border color.
autoCompleteTableBackgroundColorcolorSet suggestions list border size.
autoCompleteTableCornerRadiusnumberSet suggestions list background color.
autoCompleteTableTopOffsetnumberSet the distance between the text field and the suggestions list.
autoCompleteTableLeftOffsetnumberSet the left offset between the container and the suggestions list.
autoCompleteTableSizeOffsetnumberSet the offset of the suggestions list size. Combined with autoCompleteTableLeftOffset, you can reduce the width of the suggestions list and to center it. Exemple: autoCompleteTableLeftOffset=20 autoCompleteTableSizeOffset=40
autoCompleteRowHeightnumberHeight of cells in the suggestions list.

Default cell rendering options

optiontypeInfo
autoCompleteFontSizenumberFont Size used to display text.
autoCompleteRegularFontNamestringFont used to display text.
autoCompleteBoldFontNamestringFont used to display suggestion text.
autoCompleteTableCellTextColorcolorText Color used to display text.
autoCompleteTableCellBackgroundColorcolorBackground color of cells.
applyBoldEffectToAutoCompleteSuggestionsboolIf false, disable bold effect on suggestion text.
reverseAutoCompleteSuggestionsBoldEffectboolReverse the bold effect.

Installation

npm install react-native-autocomplete

Example

import React, { memo, useState } from 'react'
import { Text, View, SafeAreaView,
  ScrollView,  StatusBar,  StyleSheet,  useColorScheme,  Platform, KeyboardAvoidingView,} from 'react-native'
import { AutocompleteDropdown } from 'react-native-autocomplete-dropdown'

export const LocalDataSetExample = memo(() => {
  const [selectedItem, setSelectedItem] = useState(null)

  return (
    <SafeAreaView style={{ flex: 1 }}>
      <StatusBar barStyle={'light-content'} />
      <KeyboardAvoidingView
        style={{ flex: 1 }}
        behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
        enabled>

        <ScrollView
          nestedScrollEnabled
          keyboardDismissMode="on-drag"
          keyboardShouldPersistTaps="handled"
          contentInsetAdjustmentBehavior="automatic"
          contentContainerStyle={{ paddingBottom: 200 }}
          style={styles.scrollContainer}>
          <View style={[styles.container]}>
          <Text style={styles.sectionTitle}>Local list</Text>
            <AutocompleteDropdown
              clearOnFocus={false}
              closeOnBlur={true}
              initialValue={{ id: '2' }} // or just '2'
              onSelectItem={setSelectedItem}
              dataSet={[
                { id: '1', title: 'Alpha' },
                { id: '2', title: 'Beta' },
                { id: '3', title: 'Gamma' },
              ]}
            />
              <Text style={{ color: '#668', fontSize: 13 }}>Selected item: {JSON.stringify(selectedItem)}</Text>
          </View>
        </ScrollView>
      </KeyboardAvoidingView>
    </SafeAreaView>

  )
})

const styles = StyleSheet.create({
  scrollContainer: {
    flex: 1,
  },
  container: {
    padding: 20,
  },
  title: {
    textAlign: 'center',
    fontSize: 25,
    marginBottom: 50,
  },
  section: {
    marginBottom: 40,
  },
  sectionTitle: {
    fontWeight: 'bold',
    marginBottom: 3,
  },
})

export default LocalDataSetExample;

Tutorial

Coming Soon…


Tags

autocomplete
Previous Article
Local List customization Auto Complete
Utkarsh Lubal

Utkarsh Lubal

Full Stack Developer

Related Posts

Exploring Alternative Emulators for React Native Development
Exploring Alternative Emulators for React Native Development
January 20, 2024
1 min

Quick Links

Advertise with usAbout UsContact Us

Social Media