5.4.6 • Published 1 year ago

react-native-dropdown-picker v5.4.6

Weekly downloads
12,107
License
MIT
Repository
github
Last release
1 year ago

React native dropdown picker v3

A single or multiple, searchable item picker (dropdown) component for react native which supports both Android & iOS.

Caution (incompatibility)

x < 3.0.0 Versions are incompatible with the current version.

It's required to follow the docs in order to upgrade the package to v3.x

Changelog

  • Added multiple items feature.
  • Added searchable items feature.
  • Removed defaultIndex property.
  • Removed defaultNull property.
  • The defaultValue is state-friendly.

Getting Started

Screenshot Screenshot

Installation

via NPM
npm install react-native-dropdown-picker --save
via Yarn
yarn add react-native-dropdown-picker

Basic Usage

The first step is to import the package.

import DropDownPicker from 'react-native-dropdown-picker';

Single

Select a single item.

this.state = {
    country: 'uk'
}

<DropDownPicker
    items={[
        {label: 'UK', value: 'uk'},
        {label: 'France', value: 'france'},
    ]}
    defaultValue={this.state.country}
    containerStyle={{height: 40}}
    style={{backgroundColor: '#fafafa'}}
    dropDownStyle={{backgroundColor: '#fafafa'}}
    onChangeItem={item => this.setState({
        country: item.value
    })}
/>

Multiple

Select multiple items.

this.state = {
    countries: ['uk']
}

<DropDownPicker
    items={[
        {label: 'UK', value: 'uk'},
        {label: 'France', value: 'france'},
    ]}

    multiple={true}
    multipleText="%d items have been selected."
    min={0}
    max={10}

    defaultValue={this.state.countries}
    containerStyle={{height: 40}}
    onChangeItem={item => this.setState({
        countries: item // an array of the selected items
    })}
/>

Searchable items

Search for specific items.

searchable={true}
searchablePlaceholder="Search..."
searchableError="Not Found"

Default item

You may want to select one of the items as default.

Use one of these ways: 1. Add selected: true to the object. (This method is not state-friendly!)

```javascript
items={[
    {label: 'Item 1', value: 'item1'},
    {label: 'Item 2', value: 'item2', selected: true, disabled: true},
]}
```
  1. The defaultValue property.
    defaultValue="uk" // Single
    defaultValue=["uk"] // Multiple

Placeholder

You may want to have a placeholder while the default value is null or an empty array.

Add the following properties to the component.

this.state = {
    data: null, // Single
    data: [] // Multiple
}

...
defaultValue={this.state.data}
placeholder="Select an item"
...

Styling the component

You have 10 options to style the component. 1. The style property.

Use this to adjust the inner part of the picker.
```javacript
style={{paddingVertical: 10}}
```
  1. The dropDownStyle property.

    Additional styles for the dropdown box.

    dropDownStyle={{backgroundColor: '#fafafa'}}
  2. The containerStyle property.

    Use this to adjust the outer part of the picker such as margin, width, height, flex, ...

    containerStyle={{width: 150, height: 70}}
  3. The itemStyle property.

    If you want the labels on the left and right side or to centerize them:

    itemStyle={{alignItems: 'flex-start|flex-end|center'}}
  4. The labelStyle property.

    This property gives full control over the label.

    labelStyle={{fontSize: 14, color: '#000'}}
  5. The placeholderStyle property.

    It is possible to style the placeholder text with this property.

    placeholderStyle={{fontWeight: 'bold'}}
  6. The activeItemStyle property.

    This property allows you to style the active item.

    activeItemStyle={{alignItems: 'center'}}
  7. The activeLabelStyle property.

    This property allows you to style the active label.

    activeLabelStyle={{color: 'red'}}
  8. The arrowStyle property.

    Adds your additional styles to the View element of the arrow.

    arrowStyle={{marginRight: 10}}
  9. The searchableStyle property.

    Additional styles for the TextInput

    searchableStyle={{backgroundColor: '#dfdfdf'}}

FAQ

Multiple pickers and the open dropdown issue

Clicking on another picker doesn't close the other pickers? This can be fixed with the help of state.

this.state = {
    itemA: null,
    isVisibleA: false,

    itemB: null,
    isVisibleB: false
}

changeVisibility(state) {
    this.setState({
        isVisibleA: false,
        isVisibleB: false,
        ...state
    });
}

// Picker A
<DropDownPicker
    items={[
        {label: 'UK', value: 'uk'},
        {label: 'France', value: 'france'},
    ]}
    defaultValue={this.state.itemA}
    containerStyle={{height: 40}}

    isVisible={this.state.isVisibleA}
    onOpen={() => this.changeVisibility({
        isVisibleA: true
    })}
    onClose={() => this.setState({
        isVisibleA: false
    })}
    onChangeItem={item => this.setState({
        itemA: item.value
    })}
/>

// Picker B
<DropDownPicker
    items={[
        {label: 'UK', value: 'uk'},
        {label: 'France', value: 'france'},
    ]}
    defaultValue={this.state.itemB}
    containerStyle={{height: 40}}

    isVisible={this.state.isVisibleB}
    onOpen={() => this.changeVisibility({
        isVisibleB: true
    })}
    onClose={() => this.setState({
        isVisibleB: false
    })}
    onChangeItem={item => this.setState({
        itemB: item.value
    })}
/>

borderRadius

The only thing you have to avoid is borderRadius. All the corners must be set separately.

style={{
    borderTopLeftRadius: 10, borderTopRightRadius: 10,
    borderBottomLeftRadius: 10, borderBottomRightRadius: 10
}}
dropDownStyle={{
    borderBottomLeftRadius: 20, borderBottomRightRadius: 20
}}

zIndex conflicts

A lot of users use the containerStyle property to style the picker which results in unexpected behaviors like untouchable scrollviews.

The style and dropDownStyle properties must be used instead.

Use the containerStyle prop to adjust the outer part of the picker such as margin, width, height, flex, ...

Dropdown Overflow

Adding borders to the component will separate or overflow elements. to solve this issue you just need to add marginTop to the dropDownStyle and specify the value which fits your component well.

dropDownStyle={{marginTop: 2}}

Props

NameDescriptionTypeDefaultRequired
itemsThe items for the component.arrayYes
defaultValueThe value of the default item. (If multiple={true}, it takes an array of pre-selected values: ['uk'])anyNo
placeholderDefault text to be shown to the user when defaultValue={null} or defaultValue={[]}string'Select an item'No
dropDownMaxHeightHeight of the dropdown box.number150No
styleAdditional styles for the picker.object{}No
dropDownStyleAdditional styles for the dropdown box.object{}No
containerStyleAdditional styles for the container view.object{}No
itemStyleAdditional styles for the items.object{}No
labelStyleAdditional styles for the labels.object{}No
placeholderStyleAdditional styles for the placeholder text.object{}No
activeItemStyleAdditional styles for the active item.object{}No
activeLabelStyleAdditional styles for the active label.object{}No
arrowStyleAdditional styles for the arrow.object{}No
arrowColorThe color of arrow iconsstring#000No
arrowSizeThe size of the arrow.number15No
showArrowAn option to show/hide the arrow.booltrueNo
customArrowUpCustomize the arrow-up.jsx(size, color) => ...No
customArrowDownCustomize the arrow-down.jsx(size, color) => ...No
customTickIconCustomize the tick icon for multiple item picker.jsx() => ...No
zIndexThis property specifies the stack order of the component.number5000No
disabledDisables the component.boolfalseNo
isVisibleOpen or close the dropdown box.boolfalseNo
multipleIf set to true selecting multiple items is possible.boolfalseNo
multipleTexta Text to inform the user how many items have been selected.string%d items have been selectedNo
minminimum number of items.number0No
maxmaximum number of items.number10000000No
searchableShows a TextInput to search for specific items.boolfalseNo
searchableStyleAdditional styles for the TextInput.object{}No
searchableErrorShows a message when nothing found.stringNot FoundNo
onOpenFires when you open the picker.func() => {}No
onCloseFires when you close the picker.func() => {}No
onChangeItemCallback which returns item and index. The item is the selected object or an array of the selected values.funcNo
5.4.6

1 year ago

5.4.5

1 year ago

5.4.7-beta.0

1 year ago

5.4.7-beta.1

1 year ago

5.4.4

1 year ago

5.4.3

1 year ago

5.4.2

2 years ago

5.4.1

2 years ago

5.4.0

2 years ago

5.4.0-beta.0

2 years ago

5.3.0

2 years ago

5.2.5-beta.0

2 years ago

5.2.2-beta.1

2 years ago

5.2.2-beta.0

2 years ago

5.2.2-beta.3

2 years ago

5.2.2-beta.2

2 years ago

5.2.3

2 years ago

5.2.2

2 years ago

5.2.0

2 years ago

5.1.28

3 years ago

5.1.27

3 years ago

5.1.25

3 years ago

5.1.23

3 years ago

5.1.21

3 years ago

5.1.20

3 years ago

5.1.19

3 years ago

5.1.18

3 years ago

5.0.0

3 years ago

5.1.0-beta.0

3 years ago

5.1.0-beta.1

3 years ago

5.1.9

3 years ago

5.1.8

3 years ago

5.1.7

3 years ago

5.1.5

3 years ago

5.1.4

3 years ago

5.1.3

3 years ago

5.1.2

3 years ago

5.1.0

3 years ago

5.1.17

3 years ago

5.1.16

3 years ago

5.1.15

3 years ago

5.1.14

3 years ago

5.1.13

3 years ago

5.1.12

3 years ago

5.1.11

3 years ago

5.1.10

3 years ago

4.0.9

3 years ago

4.0.7

3 years ago

4.0.8

3 years ago

4.0.6

3 years ago

4.0.5

3 years ago

4.0.4

3 years ago

4.0.3-beta.0

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.8.3

3 years ago

3.8.2

3 years ago

3.8.0

3 years ago

3.7.8

3 years ago

3.7.6

3 years ago

3.7.5

3 years ago

3.7.1

3 years ago

3.7.0

4 years ago

3.6.8

4 years ago

3.6.7

4 years ago

3.6.6

4 years ago

3.6.2

4 years ago

3.6.1

4 years ago

3.6.0

4 years ago

3.6.5

4 years ago

3.6.4

4 years ago

3.6.3

4 years ago

3.5.1

4 years ago

3.5.0

4 years ago

3.1.12-beta.4

4 years ago

3.1.12-beta.5

4 years ago

3.1.12-beta.3

4 years ago

3.1.12-beta.2

4 years ago

3.1.12-beta.1

4 years ago

3.1.12-beta.0

4 years ago

3.1.11

4 years ago

3.1.10

4 years ago

3.1.9

4 years ago

3.1.8

4 years ago

3.1.7

4 years ago

3.1.6

4 years ago

3.1.5

4 years ago

3.1.4

4 years ago

3.1.3

4 years ago

3.1.2

4 years ago

3.0.2-beta.0

4 years ago

3.0.3-beta.0

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.1-beta.0

4 years ago

3.0.0

4 years ago

2.1.8

4 years ago

2.1.6

4 years ago

2.1.5

4 years ago

2.1.7

4 years ago

2.1.4

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.9

4 years ago

2.0.10

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.0

4 years ago

1.0.1

4 years ago