Search code examples
react-nativeonclickdropdown

Dropdowns in React Native


I have a use case where I want to load a dropdown that will be scrollable on clicking the input field and then select a value from it.

I wanted to know how we can do this. Which library is preferred or can be used here to achieve this

I tried surfing over the internet but I couldn't find anything concrete, there are many libraries and am not sure which one should I use. I also tried using picker but I don't want to render the list as a popup in the center but from the input field.

Thanks in advance


Solution

  • You can try react-native-element-dropdown

          import React, { useState } from 'react';
          import { StyleSheet, Text, View } from 'react-native';
          import { Dropdown } from 'react-native-element-dropdown';
          import AntDesign from 'react-native-vector-icons/AntDesign';
        
          const data = [
            { label: 'Item 1', value: '1' },
            { label: 'Item 2', value: '2' },
            { label: 'Item 3', value: '3' },
            { label: 'Item 4', value: '4' },
            { label: 'Item 5', value: '5' },
            { label: 'Item 6', value: '6' },
            { label: 'Item 7', value: '7' },
            { label: 'Item 8', value: '8' },
          ];
        
          const DropdownComponent = () => {
            const [value, setValue] = useState(null);
            const [isFocus, setIsFocus] = useState(false);
        
            const renderLabel = () => {
              if (value || isFocus) {
                return (
                  <Text style={[styles.label, isFocus && { color: 'blue' }]}>
                    Dropdown label
                  </Text>
                );
              }
              return null;
            };
        
            return (
              <View style={styles.container}>
                {renderLabel()}
                <Dropdown
                  style={[styles.dropdown, isFocus && { borderColor: 'blue' }]}
                  placeholderStyle={styles.placeholderStyle}
                  selectedTextStyle={styles.selectedTextStyle}
                  inputSearchStyle={styles.inputSearchStyle}
                  iconStyle={styles.iconStyle}
                  data={data}
                  search
                  maxHeight={300}
                  labelField="label"
                  valueField="value"
                  placeholder={!isFocus ? 'Select item' : '...'}
                  searchPlaceholder="Search..."
                  value={value}
                  onFocus={() => setIsFocus(true)}
                  onBlur={() => setIsFocus(false)}
                  onChange={item => {
                    setValue(item.value);
                    setIsFocus(false);
                  }}
                  renderLeftIcon={() => (
                    <AntDesign
                      style={styles.icon}
                      color={isFocus ? 'blue' : 'black'}
                      name="Safety"
                      size={20}
                    />
                  )}
                />
              </View>
            );
          };
        
          export default DropdownComponent;
        
          const styles = StyleSheet.create({
            container: {
              backgroundColor: 'white',
              padding: 16,
            },
            dropdown: {
              height: 50,
              borderColor: 'gray',
              borderWidth: 0.5,
              borderRadius: 8,
              paddingHorizontal: 8,
            },
            icon: {
              marginRight: 5,
            },
            label: {
              position: 'absolute',
              backgroundColor: 'white',
              left: 22,
              top: 8,
              zIndex: 999,
              paddingHorizontal: 8,
              fontSize: 14,
            },
            placeholderStyle: {
              fontSize: 16,
            },
            selectedTextStyle: {
              fontSize: 16,
            },
            iconStyle: {
              width: 20,
              height: 20,
            },
            inputSearchStyle: {
              height: 40,
              fontSize: 16,
            },
          });