SortDropdown.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { ArrowDownNarrowWide, ArrowDownWideNarrow } from 'lucide-react'
  2. import {
  3. Button,
  4. DropdownMenu,
  5. DropdownMenuContent,
  6. DropdownMenuRadioGroup,
  7. DropdownMenuRadioItem,
  8. DropdownMenuSub,
  9. DropdownMenuSubContent,
  10. DropdownMenuSubTrigger,
  11. DropdownMenuTrigger,
  12. } from 'ui'
  13. type SortOption = {
  14. label: string
  15. value: string
  16. }
  17. interface SortDropdownProps {
  18. options: SortOption[]
  19. value: string
  20. setValue: (value: string) => void
  21. }
  22. export const SortDropdown = ({ options, value, setValue }: SortDropdownProps) => {
  23. const [sortColumn, sortOrder] = value.split('_')
  24. const columnLabel = options.find((x) => x.value === sortColumn)?.label
  25. return (
  26. <DropdownMenu>
  27. <DropdownMenuTrigger asChild>
  28. <Button
  29. type="default"
  30. icon={sortOrder === 'desc' ? <ArrowDownWideNarrow /> : <ArrowDownNarrowWide />}
  31. >
  32. Sorted by {columnLabel ?? sortColumn}
  33. </Button>
  34. </DropdownMenuTrigger>
  35. <DropdownMenuContent className="w-44" align="start">
  36. <DropdownMenuRadioGroup value={value} onValueChange={setValue}>
  37. {options.map((option) => {
  38. return (
  39. <DropdownMenuSub key={option.value}>
  40. <DropdownMenuSubTrigger>Sort by {option.label}</DropdownMenuSubTrigger>
  41. <DropdownMenuSubContent>
  42. <DropdownMenuRadioItem value={`${option.value}_asc`}>
  43. Ascending
  44. </DropdownMenuRadioItem>
  45. <DropdownMenuRadioItem value={`${option.value}_desc`}>
  46. Descending
  47. </DropdownMenuRadioItem>
  48. </DropdownMenuSubContent>
  49. </DropdownMenuSub>
  50. )
  51. })}
  52. </DropdownMenuRadioGroup>
  53. </DropdownMenuContent>
  54. </DropdownMenu>
  55. )
  56. }