ReactSelectField
Description
An enhanced select field powered by the react-select library. Provides advanced features like searchable dropdown, multi-select, custom styling, and better user experience compared to native select elements.
Import
import { ReactSelectField } from '@components/common/form/ReactSelectField';
Usage
import { Form } from '@components/common/form/Form';
import { ReactSelectField } from '@components/common/form/ReactSelectField';
function ProductForm() {
const categoryOptions = [
{ value: 'electronics', label: 'Electronics' },
{ value: 'clothing', label: 'Clothing' },
{ value: 'books', label: 'Books' }
];
return (
<Form action="/api/products">
<ReactSelectField
name="category"
label="Category"
options={categoryOptions}
placeholder="Select a category..."
/>
</Form>
);
}
Props
| Name | Type | Default | Description |
|---|---|---|---|
| name | FieldPath<T> | - | Field name (required) |
| options | SelectOption[] | - | Array of options (required) |
| label | string | - | Label text displayed above the select |
| placeholder | string | - | Placeholder text when no option is selected |
| isMulti | boolean | false | Enable multiple selection |
| isSearchable | boolean | true | Enable search/filter functionality |
| isClearable | boolean | false | Show clear button to remove selection |
| isDisabled | boolean | false | Disable the select |
| defaultValue | any | - | Default selected value(s) |
| required | boolean | false | Makes the field required |
| error | string | - | Custom error message |
| helperText | string | - | Helper text shown below the select |
| validation | RegisterOptions<T> | - | React Hook Form validation rules |
| wrapperClassName | string | 'form-field' | CSS class for the wrapper div |
SelectOption Interface
interface SelectOption {
value: any;
label: string;
[key: string]: any; // Additional custom properties
}
Example: Multi-Select
import { Form } from '@components/common/form/Form';
import { ReactSelectField } from '@components/common/form/ReactSelectField';
function ProductForm() {
const tagOptions = [
{ value: 'new', label: 'New Arrival' },
{ value: 'sale', label: 'On Sale' },
{ value: 'featured', label: 'Featured' },
{ value: 'trending', label: 'Trending' }
];
return (
<Form action="/api/products">
<ReactSelectField
name="tags"
label="Product Tags"
options={tagOptions}
isMulti={true}
placeholder="Select tags..."
defaultValue={['new', 'featured']}
/>
</Form>
);
}
Example: Searchable with Clear Button
import { Form } from '@components/common/form/Form';
import { ReactSelectField } from '@components/common/form/ReactSelectField';
function CustomerForm() {
const countryOptions = [
{ value: 'us', label: 'United States' },
{ value: 'uk', label: 'United Kingdom' },
{ value: 'ca', label: 'Canada' },
{ value: 'au', label: 'Australia' },
{ value: 'de', label: 'Germany' }
];
return (
<Form action="/api/customers">
<ReactSelectField
name="country"
label="Country"
options={countryOptions}
isSearchable={true}
isClearable={true}
placeholder="Search and select country..."
required={true}
/>
</Form>
);
}
Example: With Validation
import { Form } from '@components/common/form/Form';
import { ReactSelectField } from '@components/common/form/ReactSelectField';
function OrderForm() {
const shippingOptions = [
{ value: 'standard', label: 'Standard Shipping (5-7 days)' },
{ value: 'express', label: 'Express Shipping (2-3 days)' },
{ value: 'overnight', label: 'Overnight Shipping (1 day)' }
];
return (
<Form action="/api/orders">
<ReactSelectField
name="shippingMethod"
label="Shipping Method"
options={shippingOptions}
required={true}
validation={{
required: 'Please select a shipping method'
}}
helperText="Choose your preferred delivery speed"
/>
</Form>
);
}
Example: Custom Option Properties
import { Form } from '@components/common/form/Form';
import { ReactSelectField } from '@components/common/form/ReactSelectField';
function ProductForm() {
const supplierOptions = [
{
value: 'supplier1',
label: 'Acme Corp',
region: 'North America',
rating: 5
},
{
value: 'supplier2',
label: 'Global Supplies Ltd',
region: 'Europe',
rating: 4
}
];
return (
<Form action="/api/products">
<ReactSelectField
name="supplier"
label="Supplier"
options={supplierOptions}
placeholder="Select supplier..."
/>
</Form>
);
}
Dependencies
This component uses react-select library for enhanced select functionality. The library is included in EverShop dependencies.
Features
- Searchable: Type to filter options
- Multi-Select: Select multiple options at once
- Clearable: Option to clear selection
- Keyboard Navigation: Full keyboard support
- Custom Styling: Tailwind CSS integrated styles
- Controller Integration: Uses React Hook Form Controller
- Value Handling: Automatically handles single and multi-select values
- Error Display: Shows validation errors
- TypeScript Support: Full type safety with generics
Styling
The component includes custom styles:
- Border: Gray border with blue focus ring
- Control height: Auto-adjusted
- Hover effects: Subtle border color change
- Focus state: Blue border with shadow
- Integrates with form error states
Value Format
- Single select: Returns the selected option's value (e.g.,
'electronics') - Multi-select: Returns array of values (e.g.,
['new', 'featured'])
Related Components
- Form - Parent form component
- ReactSelectCreatableField - Select with create option
- SelectField - Native HTML select