Picker

A picker displays one or more scrollable lists of distinct values that people can choose from.

Installation

Before copy/pasting, add the following dependencies to your project:

Copy/paste the following code to the specified file path:

~/components/nativewindui/Picker.tsx
import { Picker as RNPicker } from '@react-native-picker/picker';
import { View } from 'react-native';

import { cn } from '~/lib/cn';
import { useColorScheme } from '~/lib/useColorScheme';

export function Picker<T>({
  mode = 'dropdown',
  style,
  dropdownIconColor,
  dropdownIconRippleColor,
  className,
  ...props
}: React.ComponentPropsWithoutRef<typeof RNPicker<T>>) {
  const { colors } = useColorScheme();

  return (
    <View
      className={cn(
        'ios:shadow-sm ios:shadow-black/5 rounded-md border border-background bg-background',
        className
      )}
    >
      <RNPicker
        mode={mode}
        style={
          style ?? {
            backgroundColor: colors.root,
            borderRadius: 8,
          }
        }
        dropdownIconColor={dropdownIconColor ?? colors.foreground}
        dropdownIconRippleColor={dropdownIconRippleColor ?? colors.foreground}
        {...props}
      />
    </View>
  );
}

export const PickerItem = RNPicker.Item;

Usage

index.tsx
import * as React from 'react';

import { useColorScheme } from '~/lib/useColorScheme';
import { Picker, PickerItem } from '~/components/nativewindui/Picker';

function PickerExample() {
  const { colors } = useColorScheme();
  const [picker, setPicker] = React.useState('blue');
  return (
    <Picker selectedValue={picker} onValueChange={(itemValue) => setPicker(itemValue)}>
      <PickerItem
        label="Red"
        value="red"
        color={colors.foreground}
        style={{
          backgroundColor: colors.root,
        }}
      />
      <PickerItem
        label="Blue"
        value="blue"
        color={colors.foreground}
        style={{
          backgroundColor: colors.root,
        }}
      />
      <PickerItem
        label="Green"
        value="green"
        color={colors.foreground}
        style={{
          backgroundColor: colors.root,
        }}
      />
    </Picker>
  );
}
© Ronin Technologies LLC 2024