Overview
TheCustomSelect component is a fully-featured dropdown select input that replaces native HTML select elements with a custom-styled alternative. It includes support for icons, validation states, disabled states, and dark mode.
Location: ~/workspace/source/Fronted/src/components/common/CustomSelect.jsx:4
Features
- Custom dropdown UI with smooth animations
- Optional icon support for visual enhancement
- Error state styling with validation feedback
- Disabled state handling
- Dark mode compatible
- Click-outside to close functionality
- Clear selection option
- Keyboard-friendly interface
Props
The name attribute for the select input, used when handling form submissions
The currently selected value from the options array
Callback function triggered when selection changes. Receives an event object with
target.name and target.valueArray of string options to display in the dropdown
When true, disables user interaction with the select component
When true, applies error styling (red border and text color)
A Lucide React icon component to display on the left side of the select button
Placeholder text shown when no value is selected
Import
Usage Examples
- Basic Usage
- With Icon
- With Validation
- Disabled State
Visual Behavior
The CustomSelect component renders as:- Closed State: A button displaying the current value or placeholder, with a chevron icon
- Open State: Expands to show a dropdown list with all options
- Selected Option: Highlighted with a check icon and colored background
- Clear Option: First item in the list allows clearing the selection
Styling States
Default State
- Semi-transparent white background
- Subtle border
- Dark mode compatible colors
Open State
- Ring border with indigo accent
- Elevated dropdown with shadow
- Rotated chevron icon
Error State
- Red border color
- Red text color for icon and text
- Visual feedback for validation failures
Disabled State
- Reduced opacity (50%)
- Cursor not-allowed
- Non-interactive
Implementation Details
Click Outside Detection
The component uses a ref and event listener to detect clicks outside the dropdown and automatically close it:Event Handler Pattern
The onChange event mimics native select behavior by passing an event object:Accessibility
- Semantic button element for the trigger
- Keyboard navigation support (click to open)
- Focus management
- Disabled state properly communicated
- ARIA-compatible structure
Best Practices
Use with Form State Management
Use with Form State Management
Always integrate CustomSelect with your form state management solution (useState, React Hook Form, Formik, etc.) to ensure controlled component behavior.
Provide Clear Options
Provide Clear Options
Keep option text concise and descriptive. Long text will be truncated with ellipsis.
Combine with Validation
Combine with Validation
Use the error prop in combination with your validation logic to provide visual feedback to users.
Use Icons Meaningfully
Use Icons Meaningfully
Only add icons when they enhance understanding of the field’s purpose (e.g., User icon for user type selection).