Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-dropdown-picker

Package Overview
Dependencies
Maintainers
1
Versions
100
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-dropdown-picker

A picker (dropdown) for react native which supports both Android & iOS.

  • 2.0.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
47K
decreased by-43.97%
Maintainers
1
Weekly downloads
 
Created
Source

React native dropdown picker

A picker (dropdown) component for react native which supports both Android & iOS.

Getting Started

Screenshot Screenshot

Installation

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

Basic Usage

First of all import the package.

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

Render the component.

<DropDownPicker
    items={[
        {label: 'Item 1', value: 'item1'},
        {label: 'Item 2', value: 'item2'},
    ]}
    defaultIndex={0}
    containerStyle={{minWidth: 150}}
    onChangeItem={item => console.log(item.label, item.value)}
/>

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.

    items={[
        {label: 'Item 1', value: 'item1'},
        {label: 'Item 2', value: 'item2', selected: true},
    ]}
    
  2. The defaultIndex property.

    defaultIndex={1}
    
  3. The defaultValue property.

    defaultValue="item2"
    

Placeholder

You may want to have a placeholder while the default value is null.

Add the following properties to the component.

...
defaultNull
placeholder="Select an item"
...

Dynamic placeholder

In some cases you're going to create two or more pickers which are linked together.

Think of a country picker and city picker, whenever you're changing the country, the city picker should be reset and show the placeholder.

import React from 'react';
export default class MyComponent extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            country: null,
            city: null,
            cities: []
        };
    }

    changeCountry(item) {
        let city = null;
        let cities;
        switch (item.value) {
            case 'fr':
                cities = [
                    {label: 'Paris', value: 'paris'}
                ];
            break;
            case 'es':
                cities = [
                    {label: 'Madrid', value: 'madrid'}
                ];
            break;
        }

        this.setState({
            city,
            cities
        });
    }

    changeCity(item) {
        this.setState({
            city: item.value
        });
    }

    render() {
        return (
            <>
                <DropDownPicker
                    items={[
                        {label: 'France', value: 'fr'},
                        {label: 'Spain', value: 'es'},
                    ]}
                    defaultNull
                    placeholder="Select your country"
                    onChangeItem={item => this.changeCountry(item)}
                />
                <DropDownPicker
                    items={this.state.cities}
                    defaultNull={this.state.city === null}
                    placeholder="Select your city"
                    onChangeItem={item => this.changeCity(item)}
                />
            </>
        );
    }
}

Styling the component

You have 7 options to style the component.

  1. The style property.

    Use this to adjust the inner part of the picker.

    style={{paddingVertical: 10}}
    
  2. The containerStyle property.

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

    containerStyle={{width: 150, height: 70}}
    

    You don't have to set the width and height.

  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 activeItemStyle property.

    This property allows you to style the active item.

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

    This property allows you to style the active label.

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

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

    arrowStyle={{marginRight: 10}}
    

Props

NameDescriptionTypeDefaultRequired
itemsThe items for the component.arrayYes
defaultIndexThe index of the default item.number0No
defaultValueThe value of the default item.anyNo
defaultNullThis sets the choice to null which should be used with placeholderbooltrueNo
placeholderDefault text to be shown to the user which must be used with defaultNullstring'Select an item'No
dropDownMaxHeightHeight of the dropdown box.number150No
styleAdditional styles for the picker.object{}No
containerStyleAdditional styles for the container view.object{}No
itemStyleAdditional styles for the items.object{}No
labelStyleAdditional styles for the labels.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
showArrowAn option to show/hide the arrow.booltrueNo
arrowSizeThe size of the arrow.number15No
customArrowUpCustomize the arrow-up.jsxnullNo
customArrowDownCustomize the arrow-down.jsxnullNo
zIndexThis property specifies the stack order of the component.number5000No
disabledThis disables the component.boolfalseNo
onChangeItemCallback which returns item and index. The item is the selected object.functionNo

Keywords

FAQs

Package last updated on 19 Apr 2020

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc