TimezoneSelection.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { CheckIcon, ChevronsUpDown, Globe } from 'lucide-react'
  2. import { useId, useState } from 'react'
  3. import {
  4. Button,
  5. cn,
  6. Command,
  7. CommandEmpty,
  8. CommandGroup,
  9. CommandInput,
  10. CommandItem,
  11. CommandList,
  12. Popover,
  13. PopoverContent,
  14. PopoverTrigger,
  15. ScrollArea,
  16. } from 'ui'
  17. import { ALL_TIMEZONES } from './PITR.constants'
  18. import type { Timezone } from './PITR.types'
  19. interface TimezoneSelectionProps {
  20. selectedTimezone: Timezone
  21. onSelectTimezone: (timezone: Timezone) => void
  22. }
  23. export const TimezoneSelection = ({
  24. selectedTimezone,
  25. onSelectTimezone,
  26. }: TimezoneSelectionProps) => {
  27. const [open, setOpen] = useState(false)
  28. const listboxId = useId()
  29. const timezoneOptions = ALL_TIMEZONES.map((option) => option.text)
  30. return (
  31. <div className="w-full">
  32. <Popover open={open} onOpenChange={setOpen}>
  33. <PopoverTrigger asChild>
  34. <Button
  35. role="combobox"
  36. aria-expanded={open}
  37. aria-controls={listboxId}
  38. className="w-[350px] justify-between"
  39. size="small"
  40. icon={<Globe />}
  41. iconRight={<ChevronsUpDown size={14} strokeWidth={1.5} />}
  42. >
  43. {selectedTimezone
  44. ? timezoneOptions.find((option) => option === selectedTimezone.text)
  45. : 'Select timezone...'}
  46. </Button>
  47. </PopoverTrigger>
  48. <PopoverContent id={listboxId} className="w-[350px] p-0">
  49. <Command>
  50. <CommandInput placeholder="Search timezone..." className="h-9" />
  51. <CommandList>
  52. <CommandEmpty>No timezones found...</CommandEmpty>
  53. <CommandGroup>
  54. <ScrollArea className="h-72">
  55. {timezoneOptions.map((option) => (
  56. <CommandItem
  57. key={option}
  58. value={option}
  59. onSelect={(text) => {
  60. const selectedTimezone = ALL_TIMEZONES.find(
  61. (option) => option.text === text
  62. )
  63. if (selectedTimezone) {
  64. onSelectTimezone(selectedTimezone)
  65. setOpen(false)
  66. }
  67. }}
  68. >
  69. {option}
  70. <CheckIcon
  71. className={cn(
  72. 'ml-auto h-4 w-4',
  73. selectedTimezone.text === option ? 'opacity-100' : 'opacity-0'
  74. )}
  75. />
  76. </CommandItem>
  77. ))}
  78. </ScrollArea>
  79. </CommandGroup>
  80. </CommandList>
  81. </Command>
  82. </PopoverContent>
  83. </Popover>
  84. </div>
  85. )
  86. }