react-native-dropdown-picker

repository·dev-5.x·Indexed 22 days ago

https://github.com/hossein-zare/react-native-dropdown-picker

A customizable dropdown component for React Native supporting Android and iOS. It features single and multiple selection, categorizable and searchable items, and parent/child item hierarchies. The library includes built-in support for localization, custom themes (Light and Dark), and various list modes such as FlatList, ScrollView, and Modal.

Tokens
4K
Snippets
18
Records
18
Agent score
77%

What's inside react-native-dropdown-picker

  1. Explore library examples in Expo

    dev-5.x

    The repository includes an examples subdirectory which is a working Expo project. This project demonstrates various implementation patterns, including:

    • Usage with Class Components
    • Usage with Function Components
    • Usage with TypeScript
    • Usage with JavaScript

    To run the examples locally:

    1. Navigate to the examples directory.
    2. Run npm install to install dependencies.
    3. Run npx expo start to launch the project.
    cd examples
    npm install
    npx expo start
  2. Basic usage of DropDownPicker

    dev-5.x

    To use react-native-dropdown-picker, you must manage the component's state (open status, selected value, and the items list) using React state hooks. The component requires several props to control its behavior and update its state:

    • open: Boolean indicating if the dropdown is open.
    • value: The currently selected item's value.
    • items: An array of objects, where each object has a label and a value.
    • setOpen: State setter function for the open state.
    • setValue: State setter function for the value state.
    • setItems: State setter function for the items state.
    • placeholder: String displayed when no value is selected.
    import React, {useState} from 'react';
    import {View, Text} from 'react-native';
    import DropDownPicker from 'react-native-dropdown-picker';
    
    export default function App() {
        const [open, setOpen] = useState(false);
        const [value, setValue] = useState(null);
        const [items, setItems] = useState([
            {label: 'Apple', value: 'apple'},
            {label: 'Banana', value: 'banana'},
            {label: 'Pear', value: 'pear'},
        ]);
    
        return (
            <View style={{flex: 1}}>
                <View
                    style={{
                        flex: 1,
                        alignItems: 'center',
                        justifyContent: 'center',
                        paddingHorizontal: 15,
                    }}>
                    <DropDownPicker
                        open={open}
                        value={value}
                        items={items}
                        setOpen={setOpen}
                        setValue={setValue}
                        setItems={setItems}
                        placeholder={'Choose a fruit.'}
                    />
                </View>
    
                <View style={{
                    flex: 1,
                    alignItems: 'center',
                    justifyContent: 'center'
                }}>
                    <Text>Chosen fruit: {value === null ? 'none' : value}</Text>
                </View>
            </View>
        );
    }
  3. Configure global Picker settings

    dev-5.x

    You can set global defaults for the picker's behavior using the following methods on the Picker object. These settings will apply to all instances of the picker unless overridden by local props.

    • setMode(mode): Sets the default MODE.
    • setListMode(mode): Sets the default LIST_MODE.
    • setDropDownDirection(direction): Sets the default DROPDOWN_DIRECTION.
    • setLanguage(language): Sets the default LANGUAGE.
    • setTheme(name): Sets the default theme from the available THEMES.
    import Picker from 'react-native-dropdown-picker';
    
    Picker.setMode('some_mode');
    Picker.setListMode('some_list_mode');
    Picker.setDropDownDirection('up');
    Picker.setLanguage('en');
    Picker.setTheme('dark');
  4. Manage custom themes

    dev-5.x

    You can extend the library's styling by adding custom themes. The available themes are stored in Picker.THEMES.

    • addTheme(name, theme): Registers a new theme object under a specific name.
    • setTheme(name): Sets the global default theme to the provided name.
    import Picker from 'react-native-dropdown-picker';
    
    const myCustomTheme = {
      container: { backgroundColor: 'red' },
      // ... other theme properties
    };
    
    Picker.addTheme('myTheme', myCustomTheme);
    Picker.setTheme('myTheme');
  5. Manage translations and languages

    dev-5.x

    The library allows you to add or modify translations for different languages to support internationalization.

    • addTranslation(language, translation): Adds a new translation object for a specific language.
    • modifyTranslation(language, translation): Merges new translation keys into an existing language's translation object.

    Available language constants can be accessed via Picker.LANGUAGE.

    import Picker from 'react-native-dropdown-picker';
    
    // Add a new language
    Picker.addTranslation('fr', { search: 'Rechercher' });
    
    // Modify existing translation
    Picker.modifyTranslation('fr', { select: 'Choisir' });
  6. Use the Picker component

    dev-5.x

    The Picker component is the main entry point for the library. It is used to render a dropdown picker in React Native applications. While the component itself is imported as the default export, it also serves as a namespace for configuring global settings like modes, directions, languages, and themes.

    import Picker from 'react-native-dropdown-picker';
  7. Apply RTL Styles with `RTL_STYLE`

    dev-5.x

    The RTL_STYLE function manually transforms a style object to support Right-to-Left (RTL) layouts when the device's I18nManager.isRTL is not already set to true. It swaps directional properties like marginLeft with marginRight, paddingStart with paddingEnd, etc.

    Parameters:

    • rtl: Boolean indicating if RTL mode should be applied.
    • style: The original style object.
    import { RTL_STYLE } from 'react-native-dropdown-picker';
    
    const myStyle = { marginLeft: 10, paddingStart: 5 };
    const rtlStyle = RTL_STYLE(true, myStyle); 
    // Result: { marginRight: 10, paddingEnd: 5 }
  8. Retrieve Translations with `GET_TRANSLATION`

    dev-5.x

    Use GET_TRANSLATION to fetch localized strings. It supports a fallback mechanism to the default language if the requested key is missing in the specified language.

    Parameters:

    • key: The translation key to retrieve.
    • language: The language code (from LANGUAGE constant). Defaults to LANGUAGE.DEFAULT ('EN').
    • customTranslation: An object containing custom key-value pairs to merge with the translations. This allows you to override or extend existing translations.
    import { GET_TRANSLATION, LANGUAGE } from 'react-native-dropdown-picker';
    
    const label = GET_TRANSLATION('some_key', LANGUAGE.ARABIC, { some_key: 'Custom Arabic Text' });
  9. Access available themes in react-native-dropdown-picker

    dev-5.x

    The library provides a set of predefined themes that can be used to style the dropdown component. You can access the theme objects via the default export of the themes module. The available themes are:

    • DEFAULT: A constant string 'LIGHT'.
    • LIGHT: The light theme configuration.
    • DARK: The dark theme configuration.
    import themes from './src/themes';
    
    // Accessing themes
    const lightTheme = themes.LIGHT;
    const darkTheme = themes.DARK;
    const defaultThemeName = themes.DEFAULT; // 'LIGHT'
  10. Reference dark theme styles

    dev-5.x

    The dark theme exports a StyleSheet containing the visual configuration for the dropdown in dark mode. This includes styles for the container, list items, search input, and badges. Note that these styles rely on a central Colors constant for color values (e.g., Colors.EBONY_CLAY, Colors.HEATHER, Colors.SHUTTLE_GREY).

    /* 
    Key style objects available in the dark theme StyleSheet:
    
    - container: The main wrapper style
    - style: The style for the collapsed dropdown component
    - dropDownContainer: The style for the expanded list container
    - listItemContainer: The style for individual items in the list
    - listItemLabel: The style for the text within list items
    - searchContainer: The style for the search bar area
    - searchTextInput: The style for the search input field
    - badgeStyle: The style for selection badges
    - modalContentContainer: The style for modal-based dropdowns
    - modalTitle: The style for modal titles
    */
  11. Reference light theme style properties

    dev-5.x

    The light theme exports a StyleSheet containing the default styling for the dropdown component in light mode. This includes styles for the container, items, search input, and badges. While these are internal to the library's default theme, they define the visual structure of the component.

    // Key style objects available in the light theme:
    
    // Main component container
    style: { ... },
    
    // The dropdown list container
    dropDownContainer: { ... },
    
    // Individual list items
    listItemContainer: { ... },
    listItemLabel: { ... },
    
    // Search input area
    searchContainer: { ... },
    searchTextInput: { ... },
    
    // Badge styling
    badgeStyle: { ... },
    badgeDotStyle: { ... },
    
    // Modal specific styles
    modalContentContainer: { ... },
    modalTitle: { ... }