Picker
System wheel/dialog picker with composed choices, typed string/number values, and cross-platform disabled handling.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add pickerSource files
Expand a file to read the source the installer copies into your app.
components/ui/picker/Picker.tsx
import * as React from 'react'
import { Platform, StyleSheet, View } from 'react-native'
import {
Picker as NativePicker,
type PickerProps as NativePickerProps,
type PickerItemProps as NativePickerItemProps,
} from '@react-native-picker/picker'
import { useTheme, useThemeColors } from '@/lib/theme'
import { cn } from '@/lib/utils'
export type PickerValue = string | number
export type PickerRef = NativePicker<PickerValue>
export interface PickerItemProps extends Omit<NativePickerItemProps<PickerValue>, 'label' | 'value'> {
label: string
value: PickerValue
}
export interface PickerProps extends Omit<NativePickerProps<PickerValue>, 'children' | 'onValueChange'> {
className?: string
children?: React.ReactNode
defaultValue?: PickerValue
onValueChange?: (value: PickerValue, itemIndex: number) => void
}
export const PickerItem = NativePicker.Item as React.ComponentType<PickerItemProps>
function pickerChildren(children: React.ReactNode): React.ReactElement<PickerItemProps>[] {
return React.Children.toArray(children).flatMap((child) => {
if (!React.isValidElement<PickerItemProps>(child)) return []
if (child.type === React.Fragment) return pickerChildren((child.props as { children?: React.ReactNode }).children)
return [child]
})
}
/**
* Android RN 0.81 builds require Picker 2.11.3+ for the upstream CMake fix.
* A JavaScript dependency update cannot replace Expo Go 54's bundled native picker.
*/
export const Picker = React.forwardRef<PickerRef, PickerProps>(
(
{
className,
children,
selectedValue,
defaultValue,
onValueChange,
enabled = true,
style,
itemStyle,
dropdownIconColor,
accessibilityState,
...props
},
ref,
) => {
const colors = useThemeColors()
const { resolvedTheme } = useTheme()
const [internal, setInternal] = React.useState(defaultValue)
const options = pickerChildren(children)
const resolved = selectedValue ?? internal ?? options[0]?.props.value
const selectedIndex = options.findIndex((option) => option.props.value === resolved)
return (
<View
pointerEvents={enabled ? 'auto' : 'none'}
className={cn(
'overflow-hidden rounded-md border border-input bg-background',
!enabled && 'opacity-50',
className,
)}
>
<NativePicker
ref={ref}
{...props}
enabled={enabled}
selectedValue={Platform.OS === 'web' ? String(selectedIndex) : resolved}
accessibilityState={{ ...accessibilityState, disabled: !enabled }}
dropdownIconColor={dropdownIconColor ?? colors.foreground}
style={[
{
minHeight: 48,
width: '100%',
backgroundColor: colors.background,
color: colors.foreground,
fontSize: Platform.OS === 'ios' ? 17 : 16,
...(Platform.OS === 'web' ? { colorScheme: resolvedTheme } : {}),
},
style,
]}
itemStyle={[{ color: colors.foreground, fontSize: 17 }, itemStyle]}
onValueChange={(_nativeValue, index) => {
const option = options[index]
if (!enabled || !option || option.props.enabled === false) return
const next = option.props.value
if (selectedValue === undefined) setInternal(next)
if (next !== resolved) onValueChange?.(next, index)
}}
>
{options.map((option, index) => {
const androidStyle = Platform.OS === 'android' ? StyleSheet.flatten(option.props.style) : undefined
const defaultColor = option.props.enabled === false ? colors.mutedForeground : colors.foreground
return React.cloneElement(option, {
key: option.key ?? `${typeof option.props.value}-${option.props.value}`,
value: Platform.OS === 'web' ? String(index) : option.props.value,
color: option.props.color ?? (androidStyle?.color ? undefined : defaultColor),
// Android paints popup rows separately from the closed picker surface.
style:
Platform.OS === 'android'
? { backgroundColor: colors.background, ...androidStyle }
: option.props.style,
})
})}
</NativePicker>
</View>
)
},
)
Picker.displayName = 'Picker'
components/ui/picker/index.ts
export { Picker, PickerItem, type PickerProps, type PickerItemProps, type PickerRef, type PickerValue } from './Picker'