Select
A customizable select/dropdown component for choosing from multiple options.
Select
The Select component provides a user-friendly way to select an option from a list of choices. It features a floating label, custom styling, and integrates with React Native's ActionSheet for a native feel.
Features
- Floating Label: Label animates from placeholder to top position when focused or selected
- Error Handling: Displays error messages with distinct styling
- Native ActionSheet: Uses native selection UI for a platform-native experience
- Customizable Styling: Control for input and container appearance
- Dark Mode Support: Automatic light/dark mode styles
Import
Props
Prop | Type | Default | Description |
---|---|---|---|
label | string | Required | Text label for the select field |
placeholder | string | '' | Placeholder text when no option is selected |
options | Array<{ label: string, value: string | number }> | Required | Array of options to select from |
value | string | number | — | Currently selected value |
onChange | (value: string | number) => void | Required | Function called when selection changes |
error | string | — | Error message to display below the select |
className | string | '' | Additional Tailwind classes for the container |
Usage Examples
Basic Select
Select with Error
Custom Styled Select
Form Integration Example
Filterable Options Example
Best Practices
Clear Labels and Placeholders
Use descriptive labels and placeholders that clearly indicate what should be selected:
Default Values
Provide default selections when appropriate:
Grouping Related Options
For long lists, consider using separator headings or indentation in the option labels:
Implementation Details
The Select component uses React Native's TouchableOpacity
for the selection trigger and ActionSheet
for displaying the options on iOS. On Android, it uses a custom modal with a similar appearance.
The component features a floating label implementation using Animated
, which transitions based on focus state and selection state. The label moves from inside the input (as a placeholder) to above the input when an option is selected.
When the user taps on the component, an action sheet or modal appears showing all available options. Upon selection, the chosen option's label is displayed in the field.
The component uses the useThemeColors
hook to apply appropriate color schemes for both light and dark modes.
Notes
- The component automatically adapts to the platform (iOS or Android)
- The floating label animation occurs when the select is focused or has a selected option
- The select border color changes based on focus state and error state
- The component integrates with form validation patterns similar to the Input component
- For extremely long lists of options, consider implementing a searchable select with filtering capability