Picker

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

Installation

1
Run the following command:
npx nwui-cli@latest add picker
🚀
Ship.
1
Add the following dependencies to your project:
npx expo install @react-native-picker/picker
2
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';

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>
  );
}

const PickerItem = RNPicker.Item;

export { Picker, PickerItem };
🚢
Ship.

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