Socket
Socket
Sign inDemoInstall

react-native-autocomplete-dropdown

Package Overview
Dependencies
530
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-native-autocomplete-dropdown

Dropdown Item picker with search and autocomplete (typeahead) functionality for react native


Version published
Weekly downloads
8.8K
decreased by-9%
Maintainers
1
Install size
453 kB
Created
Weekly downloads
 

Readme

Source

react-native-autocomplete-dropdown

Dropdown Item picker with search and autocomplete (typeahead) functionality for react native

license npm npm

This is documentation for version 3.x, if you are looking docs for version 2.x, you can find it here

Demo

Run expo snack demo @onmotion/react-native-autocomplete-dropdown

Nav

Installation

Run:

npm install --save react-native-autocomplete-dropdown

or

yarn add react-native-autocomplete-dropdown

Post-install Steps

Make sure react-native-svg is installed. Follow the guide https://github.com/software-mansion/react-native-svg

yarn add react-native-svg

iOS

Run: npx pod-install for install react-native-svg dependency (if not installed yet).

Android

No additional steps are necessary

Usage

Wrap your root component in AutocompleteDropdownContextProvider from react-native-autocomplete-dropdown as you can see in example

<AutocompleteDropdownContextProvider>
    <AppRootOrWhatever/>
</AutocompleteDropdownContextProvider>

If you have a header component, you can pass an offset. For example with react navigation

//...
import { useHeaderHeight } from '@react-navigation/elements';
//...
const headerHeight = useHeaderHeight();
//...

<AutocompleteDropdownContextProvider headerOffset={headerHeight} >
    <AppRootOrWhatever/>
</AutocompleteDropdownContextProvider>

import the package

import { AutocompleteDropdown } from 'react-native-autocomplete-dropdown';

Dataset item format

dataSet property must be an array of objects or null. Object required keys are:

{
    id: 'some uniq string id',
    title: 'list item title'
}

Example with local Dataset

const [selectedItem, setSelectedItem] = useState(null);

<AutocompleteDropdown
  clearOnFocus={false}
  closeOnBlur={true}
  closeOnSubmit={false}
  initialValue={{ id: '2' }} // or just '2'
  onSelectItem={setSelectedItem}
  dataSet={[
    { id: '1', title: 'Alpha' },
    { id: '2', title: 'Beta' },
    { id: '3', title: 'Gamma' },
  ]}
/>;

Example with remote requested Dataset

import React, { memo, useCallback, useRef, useState } from 'react'
import { Button, Dimensions, Text, View, Platform } from 'react-native'
import { AutocompleteDropdown } from 'react-native-autocomplete-dropdown'

export const RemoteDataSetExample2 = memo(() => {
  const [loading, setLoading] = useState(false)
  const [suggestionsList, setSuggestionsList] = useState(null)
  const [selectedItem, setSelectedItem] = useState(null)
  const dropdownController = useRef(null)

  const searchRef = useRef(null)

  const getSuggestions = useCallback(async q => {
    const filterToken = q.toLowerCase()
    console.log('getSuggestions', q)
    if (typeof q !== 'string' || q.length < 3) {
      setSuggestionsList(null)
      return
    }
    setLoading(true)
    const response = await fetch('https://jsonplaceholder.typicode.com/posts')
    const items = await response.json()
    const suggestions = items
      .filter(item => item.title.toLowerCase().includes(filterToken))
      .map(item => ({
        id: item.id,
        title: item.title,
      }))
    setSuggestionsList(suggestions)
    setLoading(false)
  }, [])

  const onClearPress = useCallback(() => {
    setSuggestionsList(null)
  }, [])

  const onOpenSuggestionsList = useCallback(isOpened => {}, [])

  return (
    <>
      <View
        style={[
          { flex: 1, flexDirection: 'row', alignItems: 'center' },
          Platform.select({ ios: { zIndex: 1 } }),
        ]}>
        <AutocompleteDropdown
          ref={searchRef}
          controller={controller => {
            dropdownController.current = controller
          }}
          // initialValue={'1'}
          direction={Platform.select({ ios: 'down' })}
          dataSet={suggestionsList}
          onChangeText={getSuggestions}
          onSelectItem={item => {
            item && setSelectedItem(item.id)
          }}
          debounce={600}
          suggestionsListMaxHeight={Dimensions.get('window').height * 0.4}
          onClear={onClearPress}
          //  onSubmit={(e) => onSubmitSearch(e.nativeEvent.text)}
          onOpenSuggestionsList={onOpenSuggestionsList}
          loading={loading}
          useFilter={false} // set false to prevent rerender twice
          textInputProps={{
            placeholder: 'Type 3+ letters (dolo...)',
            autoCorrect: false,
            autoCapitalize: 'none',
            style: {
              borderRadius: 25,
              backgroundColor: '#383b42',
              color: '#fff',
              paddingLeft: 18,
            },
          }}
          rightButtonsContainerStyle={{
            right: 8,
            height: 30,

            alignSelf: 'center',
          }}
          inputContainerStyle={{
            backgroundColor: '#383b42',
            borderRadius: 25,
          }}
          suggestionsListContainerStyle={{
            backgroundColor: '#383b42',
          }}
          containerStyle={{ flexGrow: 1, flexShrink: 1 }}
          renderItem={(item, text) => <Text style={{ color: '#fff', padding: 15 }}>{item.title}</Text>}
        //   ChevronIconComponent={<Feather name="chevron-down" size={20} color="#fff" />}
        //   ClearIconComponent={<Feather name="x-circle" size={18} color="#fff" />}
          inputHeight={50}
          showChevron={false}
          closeOnBlur={false}
          //  showClear={false}
        />
        <View style={{ width: 10 }} />
        <Button style={{ flexGrow: 0 }} title="Toggle" onPress={() => dropdownController.current.toggle()} />
      </View>
      <Text style={{ color: '#668', fontSize: 13 }}>Selected item id: {JSON.stringify(selectedItem)}</Text>
    </>
  )
})

More examples see at https://github.com/onmotion/react-native-autocomplete-dropdown/tree/main/example

Playground

cd example
yarn install
npx pod-install
yarn ios

Options

OptionDescriptionTypeDefault
dataSetset of list itemsarraynull
initialValuestring (id) or object that contain idstring | objectnull
loadingloading stateboolfalse
useFilterwhether use local filter by dataSet (useful set to false for remote filtering to prevent rerender twice)booltrue
showClearshow clear buttonbooltrue
showChevronshow chevron (open/close) buttonbooltrue
closeOnBlurwhether to close dropdown on blurboolfalse
closeOnSubmitwhether to close dropdown on submitboolfalse
clearOnFocuswhether to clear typed text on focusbooltrue
ignoreAccentsignore diacriticsbooltrue
trimSearchTexttrim the searched textbooltrue
debouncewait ms before call onChangeTextnumber0
suggestionsListMaxHeightmax height of dropdownnumber200
direction"up" or "down"stringdown + auto calculate
matchFromwhether match suggestions from start of titles or anywhere in the title. Possible values are "any" or "start"stringany
bottomOffsetfor calculate dropdown direction (e.g. tabbar)number0
onChangeTextevent textInput onChangeTextfunction
onSelectItemevent onSelectItemfunction
onOpenSuggestionsListevent onOpenSuggestionsListfunction
onChevronPressevent onChevronPressfunction
onClearevent on clear button pressfunction
onSubmitevent on submit KB button pressfunction
onBlurevent fired on text input blurfunction
onFocusevent on focus text inputfunction
renderItemJSX for render item (item, searchText) => JSX | null if return null then the element will not be displayedfunctionitem.title
controllerreturn reference to module controller with methods close, open, toggle, clear, setInputText, setItemfunction
containerStyleViewStyle
rightButtonsContainerStyleViewStyle
suggestionsListContainerStyleViewStyle
suggestionsListTextStyleTextStylestyles of suggestions list text items
ChevronIconComponentReact.ComponentFeather chevron icon
ClearIconComponentReact.ComponentFeather x icon
ScrollViewComponentremoved in 2.0.0 based on FlatListReact.Component nameScrollView that provide suggestions content
EmptyResultComponentreplace the default `` Component on empty resultReact.Component
InputComponentinput element componentReact.ComponentTypeTextInput
emptyResultTextreplace the default "Nothing found" text on empty resultstring"Nothing found"
textInputPropstext input propsTextInputProps
flatListPropsprops for \ componentFlatListProps\

Keywords

FAQs

Last updated on 17 Mar 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc