GithubHelp home page GithubHelp logo

react-native-searchable-dropdown's Introduction

npm license

React Native Searchable Dropdown

Searchable Dropdown to help you search with in the list (FlatList), and you can pick single item and multiple items.

example example

Installation

npm install --save react-native-searchable-dropdown

Properties

Props Description
items dropdown items
defaultIndex Default selected index of items. (optional)
onTextChange on text change you can passs onTextChange and catch the input text. (optional)
onItemSelect on item selection you can passs onItemSelect and catch the input item.
containerStyle component container style
textInputStyle TextInput style
itemStyle items on dropdown
itemTextStyle item text
resetValue reset textInput Value with true and false state
placeholder textInput placeholder
placeholderTextColor textInput placeholderTextColor
itemsContainerStyle items container style you can pass maxHeight to restrict the items dropdown hieght
underlineColorAndroid TextInput underline height
listProps all supported (FlatList) props example: listProps={ nestedScrollEnabled: true }
textInputProps all supported (TextInput) props example: textInputProps={ underlineColorAndroid: 'transparent' }
setSort filter data on text changing example: setSort={(item, searchedText)=> item.name.toLowerCase().startsWith(searchedText.toLowerCase())}
multi boolean toggle multi selection
selectedItems selectedItems of multi selection note: work when if multi prop is true
chip boolean toggle chip display mode note: work when if multi prop is true
onRemoveItem { (item, index) => { } } note: work when if multi prop is true

Example

import React, { Component, Fragment } from 'react';
import SearchableDropdown from 'react-native-searchable-dropdown';

var items = [
  {
    id: 1,
    name: 'JavaScript',
  },
  {
    id: 2,
    name: 'Java',
  },
  {
    id: 3,
    name: 'Ruby',
  },
  {
    id: 4,
    name: 'React Native',
  },
  {
    id: 5,
    name: 'PHP',
  },
  {
    id: 6,
    name: 'Python',
  },
  {
    id: 7,
    name: 'Go',
  },
  {
    id: 8,
    name: 'Swift',
  },
];
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedItems: [
        {
          id: 7,
          name: 'Go',
        },
        {
          id: 8,
          name: 'Swift',
        }
      ]
    }
  }
  render() {
  return (
        <Fragment>
          {/* Multi */}
          <SearchableDropdown
            multi={true}
            selectedItems={this.state.selectedItems}
            onItemSelect={(item) => {
              const items = this.state.selectedItems;
              items.push(item)
              this.setState({ selectedItems: items });
            }}
            containerStyle={{ padding: 5 }}
            onRemoveItem={(item, index) => {
              const items = this.state.selectedItems.filter((sitem) => sitem.id !== item.id);
              this.setState({ selectedItems: items });
            }}
            itemStyle={{
              padding: 10,
              marginTop: 2,
              backgroundColor: '#ddd',
              borderColor: '#bbb',
              borderWidth: 1,
              borderRadius: 5,
            }}
            itemTextStyle={{ color: '#222' }}
            itemsContainerStyle={{ maxHeight: 140 }}
            items={items}
            defaultIndex={2}
            chip={true}
            resetValue={false}
            textInputProps={
              {
                placeholder: "placeholder",
                underlineColorAndroid: "transparent",
                style: {
                    padding: 12,
                    borderWidth: 1,
                    borderColor: '#ccc',
                    borderRadius: 5,
                },
                onTextChange: text => alert(text)
              }
            }
            listProps={
              {
                nestedScrollEnabled: true,
              }
            }
          />
          {/* Single */}
          <SearchableDropdown
            onItemSelect={(item) => {
              const items = this.state.selectedItems;
              items.push(item)
              this.setState({ selectedItems: items });
            }}
            containerStyle={{ padding: 5 }}
            onRemoveItem={(item, index) => {
              const items = this.state.selectedItems.filter((sitem) => sitem.id !== item.id);
              this.setState({ selectedItems: items });
            }}
            itemStyle={{
              padding: 10,
              marginTop: 2,
              backgroundColor: '#ddd',
              borderColor: '#bbb',
              borderWidth: 1,
              borderRadius: 5,
            }}
            itemTextStyle={{ color: '#222' }}
            itemsContainerStyle={{ maxHeight: 140 }}
            items={items}
            defaultIndex={2}
            resetValue={false}
            textInputProps={
              {
                placeholder: "placeholder",
                underlineColorAndroid: "transparent",
                style: {
                    padding: 12,
                    borderWidth: 1,
                    borderColor: '#ccc',
                    borderRadius: 5,
                },
                onTextChange: text => alert(text)
              }
            }
            listProps={
              {
                nestedScrollEnabled: true,
              }
            }
        />
      </Fragment>
  );
  }
}

react-native-searchable-dropdown's People

Contributors

agilewarealok avatar arjayosma avatar gianpaj avatar prateekgoel avatar techieyann avatar zubair-ibrahim-ibex avatar zubairpaizer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-native-searchable-dropdown's Issues

onItemSelect Does not update the textbox

Platform = "iOS"

When I select a particular record form the dropdown, the selected item is not showing on the textbox ( Displaying blank field ). At the same time I triggered console.log statement from onItemSelect and I am getting the exact selected value from the dropdown. But I am sure why the selected value is not showing on the textbox rather it is showing empty textbox.

Can anyone please help me with this issue. Thanks.

Dropdown Item not Scrolling

I use this lib to render the searchable dropdown inside flatlist. All works fine but dropdown items are not scrolling. While scrolling the dropdown parent flatlist scrolls instead.

When Item is selected in single select dropdown, it stores the selected item details but does not show in dropdown?

`state = {
    visnames: [
        {
            id: 1,
            name: 'JavaScript',
        },
        {
            id: 2,
            name: 'Java',
        },
        {
            id: 3,
            name: 'Ruby',
        },
        {
            id: 4,
            name: 'React Native',
        },
        {
            id: 5,
            name: 'PHP',
        },
        {
            id: 6,
            name: 'Python',
        },
        {
            id: 7,
            name: 'Go',
        },
        {
            id: 8,
            name: 'Swift',
        },
    ],
    selectedStore: '',
    selectedStoreId: ''
};
<View style={{ margin: 20 }}>
    <SearchableDropdown
        onItemSelect={(item, index) => {
            console.log(item)
            this.setState({
                selectedStore: item.name,
                selectedStoreId: item.id
            })
        }}
        itemStyle={styles.dropdowninput}
        itemTextStyle={styles.searchdropdowncolor}
        itemsContainerStyle={styles.searchdropdown}
        items={visnames}
        defaultIndex={''}
        resetValue={false}
        textInputProps={
            {
                placeholder: "Select Store",
                underlineColorAndroid: "transparent",
                style: {
                    padding: 12,
                    borderWidth: 1,
                    borderColor: '#ccc',
                    borderRadius: 5,
                },
            }
        }
        listProps={{ nestedScrollEnabled: true, }}
    />
</View>`

Issue when placed inside ScrollView

if I put this inside ScrollView there's a warning and the alert when selecting the item is not working.
all working fine when not using ScrollView

#Request nestedScrollEnabled

Hello,
if the component inside the ScrollView or something like that, dropdown list not able to scroll. Right now I fixing that issue by adding nestedScrollEnabled={true} to node_modules folder.

It always be reset if I'm doing npm install
can you add nestedScrollEnabled={true} props to the main package?

thanks

Custom Text Input

I wants to use native-base floating text-input in searchable drop down. Is there any possibility?

Search not working in 1.1.0

I am using the example code provided in this repo, in 1.1.0 I am not able to search in the dropdown since the input immediately gets overwritten.

However if i downgrade to 1.0.6, searching works all fine.

It is enough to reproduce this error by putting a , get focus of it and start typing (in addition, example code in the readme also reproduces it).

The datasets for the searchable dropdown list to be dynamic

The dataset provided to the component in the form of props.items is kind of static in nature. In usecase like where the data is dynamic, there should be something that reacts to the updated data and update the component props values like using getDerivedStateFromProps

Dropdown Fails to Collapse Upon Hiding the Keyboard

Hi, as the title says, the dropdown menu does not collapse when the keyboard is hidden, or when the screen is turned off.

This can cause two issues.

Issue 1) - Depending on the styling rules you're using, hitting the hide keyboard button on the keyboard will show how the searchable drop-down has pushed the other flex boxes out of the way. Some people simply hide this issue by letting the keyboard cover up the messed up layout of the flexboxes below the searchable dropdown.

Issue 2) - Strange layout issues can occur if a searchable dropdown is left open when the screen of the device is turned off, as when you turn your screen back on, everything gets squished and mis aligned (Although it depends on the styling rules you're using.) I'm not exactly sure what's causing that aspect of it, but I'm lead to believe it's deleterious behavior and has something to do with the way React sets up flex boxes based off of how many components are taking up real estate on the screen. My pet theory is that your dropdown takes up space that wasn't there upon the initial render and upon a complete rerender (i.e. turning the device back on), I think the dimensions are altered from what they where when the page was rendered without the dropdown having been opened, that is, prior to having the device's screen turned off.

I have confirmed this on Android and have fixed the issue in my own code by importing Keyboard from react and calling Keyboard.dismiss to force the Dropdown to collapse by responding to the dismissal event.

I did this by doing the following:

_keyboardDidHide() {
Keyboard.dismiss();
}

componentDidMount() {
this.keyboardDidHideListener = Keyboard.addListener(
'keyboardDidHide',
this._keyboardDidHide,
);
}
componentWillUnmount() {
this.keyboardDidHideListener.remove();
}

Can't implement onBlur event.

SearchableDropdown's TextInputProps accepts React Native TextInput props, however; if you define a custom onBlur event component won't close the flatList. As I looked up the code I saw that onBlur event, change the focus state of SearchableDropdown component. If you define a custom onBlur, it can't handle focus state of the component. Do I miss something? Is there any way to write custom onBlur?

how to put dynamic data to items prop

how to put dynamic data to items prop without using {"id":1,"name":React} as key

I got different JSON as it is from Backed Response it doesnt have key as id and name as items are not visible in the list

Chip container position

Hi,

I followed the example and when I select items from the list, they are put in chips that are positioned above the search text box. Is it possible to put this container with chips under the search textbox?

Thank you

clear inputfield

resetValue doesn't seem to work for reseting the inputfield, after fetching new data the inputfield still have the old data in it, is there a possible solution for this?

Issue with react-native version

Hi,

I've upgraded to the latest react-native version 0.60.5. After doing that I am getting issues with the react-native-searchable-dropdown package and get the following warning. I have looked a bit and believe it is due to the version of react-native used in the package. When copying the code in a seperate js file using the project react-native dependency the issue is gone.

Screenshot 2019-08-19 at 23 53 53

Should be a quick fix I imagine, thanks :) great package!

Issue with zIndex

While using it inside a flatlist item, its not possible to touch or select the elements from the drop down. There seems to be some issue with zIndex.

Unable to set default index for dynamic option list?

componentDidMount() {
fetch('https://aboutreact.herokuapp.com/demosearchables.php')
.then(response => response.json())
.then(responseJson => {
//Successful response from the API Call
this.setState({
serverData: [...this.state.serverData, ...responseJson.results],
//adding the new data in Data Source of the SearchableDropdown
});
})
.catch(error => {
console.error(error);
});
}

I want to autoselect 4th option from serverData.
items={this.state.serverData}
//mapping of item array
defaultIndex={this.state.searchIndex} //my search index

Selection of option does not work in certain case

Hi,

Thanks for the library. We have a form which is formed using multiple cards. There exists atleast 1 searchable dropdown being implemented in each card. Options are shown properly as expected. However those options which hangs outside the card (since we have number of options more than 5 for each card) as most of the searchable dropdown are the last element in each card. If we move it above we get the selection captured since the options mostly lay inside the card (parent).

Any pointers to fix it up ?

Cant select option in nested scrollview

anyone experiencing the issue? the issue with scrolling on the nested scrollview seems to be fixed, but when i pick the option, nothing happens and the onItemSelect function is not running.
thanks

onItemSelect does not fire

Hi....I must be doing some stupid but I cannot get onItemSelect to fire....even with a simple alert in it...Any help much appreciated...neat component!

<SearchableDropdown

        onItemSelect={item => {
          alert(item);
        }}
        containerStyle={{ padding: 5 }}
        onRemoveItem={(item, index) => {
          alert(item);
        }}
        itemStyle={{
          padding: 10,
          marginTop: 2,
          backgroundColor: "#FFFFFF",
          borderColor: "#bbb",
          borderWidth: 1,
          borderRadius: 5
        }}
        itemTextStyle={{ color: "#FF0000" }}
        itemsContainerStyle={{ maxHeight: 300 }}
        items={this.state.manufacturers}
        resetValue={false}
        textInputProps={{
          placeholder: "Start typing the manufacturer...",
          underlineColorAndroid: "transparent",
          style: {
            padding: 12,
            borderWidth: 1,
            borderColor: "#ccc",
            borderRadius: 5
          }
          //onTextChange: text => alert(text)
        }}
        listProps={{
          nestedScrollEnabled: true
        }}
      />

Search on Basis of Alphabetical Order

Need to have search on basis of Alphabetical Order. For Example it shouldn't be filtering on basis of LIKE but only searching on basis of alphabets in order from left to right.
Like in case of countries if i search P i should all the names of countries starting from P not the entries which just contain P.
Please update what can be done in such a scenario.

Call focus method by reference

Hi, could someone help me with this problem?

I'm having trouble accessing the component reference and calling the focus method.
I am trying as follows:

returnKeyType="next"
onSubmitEditing={() => inputRef.current.focus()}

defaultIndex property doesn't change after re-render cycle

Hello!

I'm trying to use Searchable-Dropdown in pretty simple components like that:

import React from 'react';
import {View} from 'react-native';
import SearchableDropdown from 'react-native-searchable-dropdown';

const DropDownList = props => {

return (

<SearchableDropdown
onItemSelect={(item) => {
props.onChoosingItem(item);
}}
defaultIndex = {props.defaultIndex}
containerStyle={styles.containerDDL}
itemStyle={{...styles.itemDDL,...props.itemStyle}}
itemTextStyle={{...styles.itemDDLText, ...props.textStyle}}
itemsContainerStyle={styles.itemContainerDDL}
items={props.ddlItems}
resetValue={false}
textInputStyle={{...styles.ddlTextInput,...props.textInputStyle}}
onTextChange = { text => console.log(text)}
listProps={
{
nestedScrollEnabled: true,
}
}
/>

);
};

const styles = StyleSheet.create({
itemDDL: {
padding: 2,
marginTop: 2,
backgroundColor: '#ddd',
borderColor: '#bbb',
borderWidth: 1,
},
itemDDLText: {
color: '#222'
},
ddlTextInput: {
padding: 5,
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 5,
},
itemContainerDDL: {
maxHeight: 120
},
containerDDL: {
padding: 1
},
ddlView: {
margin: 10,
flexDirection: 'row'
},
label: {
textAlign: 'center',
padding: 5,
fontSize: 14,
fontWeight: "bold",
},
alignRules: {
justifyContent: 'flex-start',
alignItems: 'stretch'
}
});

export default DropDownList;

But for some reason, props.defaultIndex property is read very only first render cycle, after "DropDownList" 's rendered first time, SearchableDropdown doesn't try to read this value though the whole component ("DropDownList") is rendered every time when the props change, can you please explain why it works this way?

defaultIndex is not workng

I have pass items using API and after fill items i want to select default index but i am not able to Please provide me solution

Support image props

Can you add a feature that supports images like FlatList render Item prop?

ListView is deprecated

Hi! I am currently getting this warning: "Warning: ListView is deprecated and will be removed in a future release. See https://fb.me/nolistview for more information". Though I can still use the component, but it may become unusable in the next iteration of react-native.

Persist previous selected value if no one else is selected

Currently I'm trying to use it to display a value that the user picked:

Screenshot from 2020-06-12 08-12-17

Is there a way to mantain the previous value when the user taps outside the dropdown without picking a new one?

Current setup:

<SearchableDropdown
        multi={multiSelect}
        onItemSelect={(item) => {
          const items = selectedMethods;
          items.push(item);
          setSelectedMethods(items);
        }}
        containerStyle={styles.dropDown}
        itemStyle={styles.itemStyle}
        itemTextStyle={styles.itemTextStyle}
        itemsContainerStyle={styles.itemsContainerStyle}
        items={data}
        defaultIndex={selectedMethodIndex}
        resetValue={false}
        textInputProps={
          {
            placeholder: placeholder,
            underlineColorAndroid: 'transparent',
            style: styles.textInputStyle,
          }
        }
        listProps={
          {
            nestedScrollEnabled: true,
          }
        }
      />

textInputProps fails to pass props to textInput

Hi, after playing around with this for a half an hour I have decided the passing of props to the internal textInput component is broken.

Please check the way the textInputProps prop is being distributed to the props of the textInput component inside of the SearchableDropdown.

Currently the following code fails to produce any change to my SearchableDropdown (notice the max length prop, it does not restrict the textInput field to 35 characters):

              <SearchableDropdown
                onTextChange={text => this.state.building = text}
                onItemSelect={item => this.state.building = item.name}
                // AlignSelf breaks wrapping, as wrapping and flex is controlled by parent component's calculated dimensions.
                containerStyle={{
                  alignSelf: 'stretch',
                  marginBottom: 10,
                  backgroundColor: 'white',
                  borderBottomWidth: 3,
                  borderBottomColor: '#CCCCCC'
                }
                }
                textInputStyle={{
                  alignSelf: 'stretch',
                  padding: 0,
                  marginLeft: 10
                }}
                itemStyle={{
                  alignSelf: 'stretch',
                  marginHorizontal: 20,
                  padding: 10,
                  marginVertical: 2,
                  backgroundColor: '#CFB87C',
                  borderColor: '#E6E6E6',
                  borderWidth: 1,
                  borderRadius: 5,
                }}
                itemTextStyle={{ color: '#222' }}
                itemsContainerStyle={{ alignSelf: 'stretch', maxHeight: 150 }}
                items={items}
                defaultIndex={null}
                placeholder="Enter building"
                resetValue={false}
                textInputProps={{maxLength: 35}}
                underlineColorAndroid="transparent"
              />

onBlur event

It is possible to intercept onBlur event on inputField?

Listdata auto close when glide twice.

I nest searchable-dropdown in KeyboardAwareScrollView and add props keyboardShouldPersistTaps="handled" for it.
I take 2 issue.

  1. Listdata auto close when glide twice.
  2. Before pick first item, must pick twice with respect to the remaining items . They just show up

Can someone fix this error. Tks

defaultIndex error

defaultIndex={0} not view element(0) in array which only appears blank in the input box

Require more values for defaultItemValue for API call

I'm using a custom API call to fetch values for the drop-down list.
The dummy endpoint I'm using is https://aboutreact.com/demo/demosearchables.php which works fine since it uses id and name.

However, when I'm trying to hit my endpoint, I want to use more values rather than just id and name.

Whenever I try to do that, it gives me the following error:
typeError: undefined is not an object (evaluating 'item.name').

Since defaultItemValue only takes in name and id, it seems to me that this is a restriction with the library and it can only use a JSON object with those two values in it.

Is anyone else experiencing the same issue and any thoughts on how to resolve it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.