UIPackage React Native
Search

Select

Single-choice picker composed from caller-owned options and a native modal sheet, with selected value announcements and focus restoration.

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 select

Source files

Expand a file to read the source the installer copies into your app.

components/ui/select/Select.tsx
import * as React from 'react'
import { Platform, Pressable, View, type PressableProps } from 'react-native'
import { Check, ChevronDown } from 'lucide-react-native'
import { Button, type ButtonProps } from '@/components/ui/button'
import { Sheet, SheetContent, type SheetContentProps } from '@/components/ui/sheet'
import { Text } from '@/components/ui/text'
import { useOverlay, useOverlayTriggerRef, type OverlayProps } from '@/lib/overlay'
import { useThemeColors } from '@/lib/theme'
import { cn } from '@/lib/utils'

export interface SelectOption {
  value: string
  label: string
}

export interface SelectProps extends OverlayProps {
  value?: SelectOption | null
  defaultValue?: SelectOption | null
  onValueChange?: (option: SelectOption) => void
  disabled?: boolean
}

interface SelectContextValue {
  value: SelectOption | null
  disabled: boolean
  select: (option: SelectOption) => void
}

const SelectContext = React.createContext<SelectContextValue | null>(null)

function useSelect() {
  const select = React.useContext(SelectContext)
  if (!select) throw new Error('Select controls must be inside Select')
  return select
}

export function Select({
  value,
  defaultValue = null,
  onValueChange,
  disabled = false,
  children,
  ...props
}: SelectProps) {
  const [internal, setInternal] = React.useState(defaultValue)
  const resolved = value === undefined ? internal : value
  const select = (option: SelectOption) => {
    if (disabled || resolved?.value === option.value) return
    if (value === undefined) setInternal(option)
    onValueChange?.(option)
  }
  return (
    <SelectContext.Provider value={{ value: resolved, disabled, select }}>
      <Sheet {...props}>{children}</Sheet>
    </SelectContext.Provider>
  )
}

export interface SelectTriggerProps extends Omit<ButtonProps, 'variant' | 'textClassName' | 'size'> {
  size?: Exclude<ButtonProps['size'], 'icon'>
  placeholder?: string
  invalid?: boolean
}

export const SelectTrigger = React.forwardRef<React.ComponentRef<typeof Pressable>, SelectTriggerProps>(
  (
    {
      placeholder = 'Choose an option',
      invalid,
      disabled,
      onPress,
      children,
      className,
      accessibilityValue,
      accessibilityState,
      ...props
    },
    ref,
  ) => {
    const select = useSelect()
    const colors = useThemeColors()
    const { open, setOpen } = useOverlay()
    const trigger = useOverlayTriggerRef(ref)
    const isDisabled = select.disabled || !!disabled
    return (
      <Button
        {...props}
        variant="outline"
        ref={trigger.ref}
        accessibilityRole={Platform.OS === 'ios' ? 'button' : 'combobox'}
        accessibilityState={{ ...accessibilityState, expanded: open, disabled: isDisabled }}
        accessibilityValue={{ ...accessibilityValue, text: select.value?.label ?? placeholder }}
        disabled={isDisabled}
        className={cn('justify-between', invalid && 'border-destructive', className)}
        onPress={(event) => {
          if (isDisabled) return
          onPress?.(event)
          if (!event.isDefaultPrevented()) {
            trigger.capture()
            setOpen(true)
          }
        }}
      >
        <Text className={cn('min-w-0 flex-1', !select.value && 'text-muted-foreground')}>
          {children ?? select.value?.label ?? placeholder}
        </Text>
        <ChevronDown size={20} color={colors.foreground} aria-hidden />
      </Button>
    )
  },
)
SelectTrigger.displayName = 'SelectTrigger'

export const SelectContent = React.forwardRef<React.ComponentRef<typeof View>, SheetContentProps>(
  ({ children, ...props }, ref) => (
    <SheetContent ref={ref} {...props}>
      <View accessible={false} accessibilityRole="radiogroup" accessibilityLabel={props.title} className="gap-[8px]">
        {children}
      </View>
    </SheetContent>
  ),
)
SelectContent.displayName = 'SelectContent'
export type SelectContentProps = SheetContentProps

export interface SelectItemProps extends PressableProps {
  value: string
  label: string
  children?: React.ReactNode
  className?: string
}

export const SelectItem = React.forwardRef<React.ComponentRef<typeof Pressable>, SelectItemProps>(
  ({ value, label, children, disabled, onPress, className, accessibilityState, accessibilityLabel, ...props }, ref) => {
    const select = useSelect()
    const colors = useThemeColors()
    const { setOpen } = useOverlay()
    const checked = select.value?.value === value
    const isDisabled = select.disabled || !!disabled
    return (
      <Pressable
        ref={ref}
        {...props}
        accessibilityRole="radio"
        accessibilityLabel={accessibilityLabel ?? label}
        accessibilityState={{ ...accessibilityState, checked, disabled: isDisabled }}
        disabled={isDisabled}
        onPress={(event) => {
          if (isDisabled) return
          onPress?.(event)
          if (event.isDefaultPrevented()) return
          select.select({ value, label })
          setOpen(false)
        }}
        className={cn(
          'active:bg-muted min-h-[48px] flex-row items-center gap-3 rounded-md px-3 py-3',
          checked && 'bg-secondary',
          isDisabled && 'opacity-50',
          className,
        )}
      >
        <View aria-hidden pointerEvents="none" className="min-w-0 flex-1 gap-1">
          <Text className="font-medium">{label}</Text>
          {children}
        </View>
        <View aria-hidden className="size-[20px] items-center justify-center">
          {checked ? <Check size={20} color={colors.foreground} aria-hidden /> : null}
        </View>
      </Pressable>
    )
  },
)
SelectItem.displayName = 'SelectItem'
components/ui/select/index.ts
export {
  Select,
  SelectTrigger,
  SelectContent,
  SelectItem,
  type SelectProps,
  type SelectOption,
  type SelectTriggerProps,
  type SelectContentProps,
  type SelectItemProps,
} from './Select'

Dependencies

Registry items

npm packages

  • lucide-react-native
  • react-native-svg
Browse controls and interactions