1.0.1 • Published 3 years ago

@niku/react-native-dropdown-select v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

React Native Dropdown Select

A Dropdown select component for React Native. Easy to use with only one needed configuration.

Demo

Table of contents

Installation

With npm

npm install react-native-safe-area-context @niku/react-native-dropdown-select

With yarn

yarn add react-native-safe-area-context @niku/react-native-dropdown-select

Add SafeAreaProvider

You have to add SafeAreaProvider at your App's root (App.tsx/App.jsx).

import { SafeAreaProvider } from 'react-native-safe-area-context';

export function App() {
  return (
    <SafeAreaProvider>
    {/* ... */}
  </SafeAreaProvider>
  );
}

Usage

// ...
import DropdownSelect from 'react-native-dropdown-select';
import { SafeAreaProvider } from 'react-native-safe-area-context';
// ...

const options = [
  {
    label: "Select language or framework you love",
    value: null,
  },
  {
    label: "Languages",
    options: [
      {
        label: 'Javascript',
        value: 'js',
      },
      {
        label: 'Typescript',
        value: 'ts',
      },
      {
        label: 'Python',
        value: 'py',
      }
    ]
  },
  {
    label: "Frameworks - Libraies",
    options: [
      {
        label: 'Reactjs',
        value: 'reactjs',
      },
      {
        label: 'React Native',
        value: 'react-native',
      },
      {
        label: 'Vuejs',
        value: 'vuejs',
      },
      {
        label: "Laravel",
        value: "laravel"
      }
    ]
  },
  {
    label: "Others",
    value: "others"
  }
];

const defaultValue = 'js';

function App() {
  const [value, setValue] = React.useState(defaultValue);
  return (
    <SafeAreaProvider>
      <View style={styles.container}>
        <View>
          <DropdownSelect
            options={options}
            defaultValue={defaultValue}
            value={value}
            onSelectOption={(option) => {
              setValue(option.value);
            }}
            onHideDropdown={() => {
              console.log('hide');
            }}
            onShowDropdown={() => {
              console.log('show');
            }}
          />
        </View>
        <Text>{value}</Text>
        <Button title="Default" onPress={() => setValue(defaultValue)} />
      </View>
    </SafeAreaProvider>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    padding: 10,
  },
});

What's new

There are some new features in this verson.

  • Option group: You can group your option. Both option item and option group can be mixed in props options (See in Usage above).
  • withStatusBar: Deprecated. I caculate position of dropdown button with react-native-safe-area-context's frame and insets. So, we don't need this props anymore.

Props

NameDescriptionTypeDefaultRequired
optionsList of option for dropdownarrayYes
defaultValueDefault value to displayanyNo
valueCurrent value of dropdown selectanyNo
positionPosition of dropdown'top' | 'bottom''bottom'No
placeholderPlaceholder of dropdown select to display when no option selectedstring'Select an option...'No
loadingLoading state of dropdown selectbooleanfalseNo
withStatusBarDeprecatedbooleantrueNo
componentComponent to render dropdown buttonReact.ComponentType | React.ReactNodeNo
renderRender dropdown button via render function(props) => React.ReactNodeNo
childrenLoading state of dropdown select(props) => React.ReactElemnt | React.ReactNodeNo
loadingComponentLoading component to render loading iconReact.ComponentType | React.ReactNodeNo
renderLoadingRender loading icon via render function() => React.ReactNodeNo
optionComponentOption component to render option itemReact.ComponentType | React.ReactNodeNo
renderOptionRender option item via render function(props) => React.ReactNodeNo
optionGroupComponentOption group component to render option groupReact.ComponentType | React.ReactNodeNo
renderOptionGroupRender option group via render function(props) => React.ReactNodeNo
compareFuncCompare function to compare two option, return true if equal otherwise return false(option1, option2) => booleanNo
onShowDropdownCallback function is called when dropdown will be shown() => voidNo
onHideDropdownCallback function is called when dropdown will be hide() => voidNo
onSelectOptionCallback function is called when an option is selected(option1) => voidNo
buttonWrapperStyleAdditional styles for button's wrapperobjectNo
buttonContainerStyleAdditional styles for button's containerobjectNo
buttonLabelStyleAdditional styles for button's labelobjectNo
buttonIconStyleAdditional styles for button's iconobjectNo
dropdownStyleAdditional styles for dropdown's containerobjectNo
optionStyleAdditional styles for option's containerobjectNo
selectedOptionStyleAdditional styles for selected option's containerobjectNo
disabledOptionStyleAdditional styles for disabled option's containerobjectNo
optionLabelStyleAdditional styles for option's labelobjectNo
selectedOptionLabelStyleAdditional styles for selected option's labelobjectNo
disabledOptionLabelStyleAdditional styles for disabled option's labelobjectNo
optionGroupPaddingPadding left for nested option groupnumberNo

Contributing

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

License

MIT