0.1.0 • Published 10 months ago

react-native-simple-dropdown-select v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

react-native-simple-dropdown-select

A simple dropdown select picker with support for expo written in typescript. Supports search, one-level of nesting and customisations.

Installation

npm install react-native-simple-dropdown-select

Expo

Works with expo

npx expo install react-native-simple-dropdown-select

Demo Examples

Usage

import { DropDownSelect } from 'react-native-simple-dropdown-select';

export default function App() {
  const [open, setOpen] = useState(false);
  const [value, setValue] = useState<any>(null);

  return (
    <View style={{
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
    }}>
      <DropDownSelect
        toggle={() => setOpen(!open)}
        selectedData={value}
        open={open}
        data={[
          {
            id: 1,
            name: 'California',
            extraData: ['Apple', 'Banana', 'Orange', 'Mango'],
          },
          {
            id: 2,
            name: 'Texas',
            extraData: ['Tomato', 'Potato', 'Onion', 'Garlic'],
            disabled: true,
          },
          {
            id: 3,
            name: 'Florida',
          },
          {
            id: 4,
            name: 'New York',
            extraData: ['Strawberry', 'Blueberry', 'Raspberry', 'Blackberry'],
          },
          {
            id: 5,
            name: 'Washington',
            extraData: [
              {
                id: 1,
                name: 'Apple',
              },
              {
                id: 2,
                name: 'Banana',
              },
              {
                id: 3,
                name: 'Orange',
              },
              {
                id: 4,
                name: 'Mango',
              },
            ],
          },
          {
            id: 6,
            name: 'Oregon',
            extraData: ['Grapes', 'Cherry', 'Apple', 'Banana', 'Orange'],
          },
          {
            id: 7,
            name: 'Nevada',
            extraData: ['Peach', 'Plum', 'Pear', 'Apricot'],
          },
        ]}
        onSelect={(data) => {
          setValue(data);
          setOpen(false);
        }}
        dropDownContainerStyle={{
          maxHeight: 400,
          minWidth: 200,
        }}
        search
        subViewStyle={{
          backgroundColor: 'pink',
          borderWidth: 1,
        }}
      />
    </View>
  );
}

Props

data

The data prop can change the behaviour of the dropdown select depending on what values it contains.

The basic requirement is for it to have be an array of {id, name}. This will render a list with name displayed as title of items. Some optional fields are:

  • disabled?: the item will be visible but disabled in the drop down list.
  • extraData?: with this field, the dropdown item will have a nested select picker. The parent title will serve as an accordion that toggles the nested picker. extraData can either be an array of strings or numbers or an array of {id, name}.
  • any other extra fields. This will not affect the picker but will be included when the onSelect function is called.

search

The search prop will enable items in dropdown list to be searchable. A search box will appear. Note that nested items in extraData cannot be searched, only top level items.

PropDescriptionDefault
dataArray of objects of type {id, name, extraData?, disabled? ..any} to display in the dropdown. Can have one layer of nested data with extraData. extraData can be an array of strings or object of type {id, name, ...any}[]
selectedDataObject to display as selected. Has same object as a data item but with a value fieldnull
onSelectFunction to call when an item is selected. Gets passed a data item with a value fieldnull
labelLabel to display above the dropdownnull
placeholderPlaceholder text to display when no item is selected or as search input placeholdernull
containerStyleStyle object for the containernull
dropDownContainerStyleStyle object for the dropdown containernull
searchBoolean to enable searchfalse
searchInputStyleStyle object for the search inputnull
searchContainerStyleStyle object for the search containernull
EmptyListViewReact element to display when no items are foundnull
onChangeSearchTextFunction to call when search text changesnull
SearchIconLeftReact element to display on the left of the search inputsearch icon
SearchIconRightReact element to display on the right of the search inputclose icon
onEndReachedFunction to call when the end of the list is reachednull
labelStyleStyle object for the labelnull
showsVerticalScrollIndicatorBoolean to show the vertical scroll indicatorfalse
selectedBtnColorColor for the selected title button#E5E5E5
selectedSubBtnColorColor for the selected sub title button if nested with extraDatatransparent
btnColorDefault color for all title buttons not selected#fff
subBtnColorDefault color for all sub title button not selected if nested with extraDatatransparent
onSelectTitleFunction to call when a title is selectednull
titleStyleStyle object for the titlenull
titleButtonStyleStyle object for the title buttonnull
disabledButtonStyleStyle object for the disabled buttonnull
disabledTitleStyleStyle object for the disabled titlenull
subButtonStyleStyle object for the sub button if nested with extraDatanull
subTitleStyleStyle object for the sub title if nested with extraDatanull
SubCheckedIconReact element to display as the checked icon if nested with extraDatacheck-mark icon
TitleIconReact element to display as the title iconchevron updown icons
subViewStyleStyle object for the sub viewnull
selectedSubTitleStyleStyle object for the selected sub title if nested with extraDatanull
selectedSubBtnStyleStyle object for the selected sub button if nested with extraDatanull
titlePropsText props for the titlenull
subTitlePropsText props for the sub title if nested with extraDatanull
checkedIconPositionPosition for the checked icon. left or right of title'left'
SubSeparatorReact element to display as the sub separator if nested with extraDatanull

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library