select-dropdown.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { Loader } from 'lucide-react'
  2. import { cn } from '@/lib/utils'
  3. import { FormControl } from '@/components/ui/form'
  4. import {
  5. Select,
  6. SelectContent,
  7. SelectItem,
  8. SelectTrigger,
  9. SelectValue,
  10. } from '@/components/ui/select'
  11. type SelectDropdownProps = {
  12. onValueChange?: (value: string) => void
  13. defaultValue: string | undefined
  14. placeholder?: string
  15. isPending?: boolean
  16. items: { label: string; value: string }[] | undefined
  17. disabled?: boolean
  18. className?: string
  19. isControlled?: boolean
  20. }
  21. export function SelectDropdown({
  22. defaultValue,
  23. onValueChange,
  24. isPending,
  25. items,
  26. placeholder,
  27. disabled,
  28. className = '',
  29. isControlled = false,
  30. }: SelectDropdownProps) {
  31. const defaultState = isControlled
  32. ? { value: defaultValue, onValueChange }
  33. : { defaultValue, onValueChange }
  34. return (
  35. <Select {...defaultState}>
  36. <FormControl>
  37. <SelectTrigger disabled={disabled} className={cn(className)}>
  38. <SelectValue placeholder={placeholder ?? 'Select'} />
  39. </SelectTrigger>
  40. </FormControl>
  41. <SelectContent>
  42. {isPending ? (
  43. <SelectItem disabled value='loading' className='h-14'>
  44. <div className='flex items-center justify-center gap-2'>
  45. <Loader className='h-5 w-5 animate-spin' />
  46. {' '}
  47. Loading...
  48. </div>
  49. </SelectItem>
  50. ) : (
  51. items?.map(({ label, value }) => (
  52. <SelectItem key={value} value={value}>
  53. {label}
  54. </SelectItem>
  55. ))
  56. )}
  57. </SelectContent>
  58. </Select>
  59. )
  60. }