2023-03-02 20:42:41 +00:00
|
|
|
import { useMemo, useState } from "react";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { ITimezoneOption, ITimezone, Props as SelectProps } from "react-timezone-select";
|
|
|
|
import BaseSelect, { allTimezones } from "react-timezone-select";
|
2022-08-24 20:18:42 +00:00
|
|
|
|
2023-03-20 11:18:23 +00:00
|
|
|
import { classNames } from "@calcom/lib";
|
2023-03-02 20:42:41 +00:00
|
|
|
import { filterByCities, addCitiesToDropdown, handleOptionLabel } from "@calcom/lib/timezone";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
|
|
|
|
2023-01-05 12:04:28 +00:00
|
|
|
import { getReactSelectProps } from "../select";
|
2022-10-10 18:50:43 +00:00
|
|
|
|
2023-03-02 20:42:41 +00:00
|
|
|
export interface ICity {
|
|
|
|
city: string;
|
|
|
|
timezone: string;
|
|
|
|
}
|
|
|
|
|
2023-03-26 19:19:13 +00:00
|
|
|
export function TimezoneSelect({
|
|
|
|
className,
|
|
|
|
components,
|
|
|
|
variant = "default",
|
|
|
|
...props
|
|
|
|
}: SelectProps & { variant?: "default" | "minimal" }) {
|
2023-03-02 20:42:41 +00:00
|
|
|
const [cities, setCities] = useState<ICity[]>([]);
|
|
|
|
const { data, isLoading } = trpc.viewer.public.cityTimezones.useQuery(undefined, {
|
|
|
|
trpc: { context: { skipBatch: true } },
|
|
|
|
});
|
|
|
|
const handleInputChange = (tz: string) => {
|
|
|
|
if (data) setCities(filterByCities(tz, data));
|
|
|
|
};
|
|
|
|
|
2022-10-10 18:50:43 +00:00
|
|
|
const reactSelectProps = useMemo(() => {
|
2023-03-20 11:18:23 +00:00
|
|
|
return getReactSelectProps({
|
|
|
|
components: components || {},
|
|
|
|
});
|
|
|
|
}, [components]);
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<BaseSelect
|
2023-03-20 11:18:23 +00:00
|
|
|
className={className}
|
2023-03-02 20:42:41 +00:00
|
|
|
isLoading={isLoading}
|
|
|
|
isDisabled={isLoading}
|
2022-10-10 18:50:43 +00:00
|
|
|
{...reactSelectProps}
|
2022-08-24 20:18:42 +00:00
|
|
|
timezones={{
|
|
|
|
...allTimezones,
|
2023-03-02 20:42:41 +00:00
|
|
|
...addCitiesToDropdown(cities),
|
2022-08-24 20:18:42 +00:00
|
|
|
"America/Asuncion": "Asuncion",
|
|
|
|
}}
|
2023-03-02 20:42:41 +00:00
|
|
|
onInputChange={handleInputChange}
|
2022-08-24 20:18:42 +00:00
|
|
|
{...props}
|
2023-02-08 12:08:00 +00:00
|
|
|
formatOptionLabel={(option) => <p className="truncate">{(option as ITimezoneOption).value}</p>}
|
2023-03-02 20:42:41 +00:00
|
|
|
getOptionLabel={(option) => handleOptionLabel(option as ITimezoneOption, cities)}
|
2023-03-20 11:18:23 +00:00
|
|
|
classNames={{
|
2023-03-26 19:19:13 +00:00
|
|
|
input: (state) =>
|
|
|
|
classNames("dark:text-darkgray-900 text-gray-900", props.classNames?.input?.(state)),
|
2023-03-20 11:18:23 +00:00
|
|
|
option: (state) =>
|
|
|
|
classNames(
|
|
|
|
"dark:bg-darkgray-100 flex cursor-pointer justify-between py-2.5 px-3 rounded-none text-gray-700 dark:text-darkgray-700",
|
|
|
|
state.isFocused && "dark:!bg-darkgray-200 !bg-gray-100",
|
|
|
|
state.isSelected && "dark:!bg-darkgray-300 !bg-gray-200 text-gray-900 !dark:text-darkgray-900",
|
|
|
|
props.classNames?.option
|
|
|
|
),
|
|
|
|
placeholder: (state) =>
|
|
|
|
classNames("text-gray-400 text-sm dark:text-darkgray-400", state.isFocused && "hidden"),
|
|
|
|
dropdownIndicator: () => "text-gray-600 dark:text-darkgray-400",
|
2023-03-26 19:19:13 +00:00
|
|
|
control: (state) =>
|
|
|
|
classNames(
|
|
|
|
variant === "default"
|
|
|
|
? "dark:bg-darkgray-100 dark:border-darkgray-300 !min-h-9 border-gray-300 bg-white text-sm leading-4 placeholder:text-sm placeholder:font-normal focus-within:ring-2 focus-within:ring-gray-800 hover:border-gray-400 dark:focus-within:ring-darkgray-900 rounded-md border py-2 px-3"
|
|
|
|
: "text-sm ",
|
|
|
|
props.classNames?.control?.(state)
|
|
|
|
), // We remove all styling here to fit theme of booking page - no min-h also
|
|
|
|
singleValue: (state) =>
|
2023-03-20 11:18:23 +00:00
|
|
|
classNames(
|
|
|
|
"dark:text-darkgray-900 dark:placeholder:text-darkgray-500 text-black placeholder:text-gray-400",
|
2023-03-26 19:19:13 +00:00
|
|
|
props.classNames?.singleValue?.(state)
|
2023-03-20 11:18:23 +00:00
|
|
|
),
|
2023-03-26 19:19:13 +00:00
|
|
|
valueContainer: (state) =>
|
2023-03-20 11:18:23 +00:00
|
|
|
classNames(
|
|
|
|
"dark:text-darkgray-900 dark:placeholder:text-darkgray-500 text-black placeholder:text-gray-400 flex gap-1",
|
2023-03-26 19:19:13 +00:00
|
|
|
props.classNames?.valueContainer?.(state)
|
2023-03-20 11:18:23 +00:00
|
|
|
),
|
2023-03-26 19:19:13 +00:00
|
|
|
multiValue: (state) =>
|
2023-03-20 11:18:23 +00:00
|
|
|
classNames(
|
|
|
|
"dark:bg-darkgray-200 dark:text-darkgray-700 rounded-md bg-gray-100 text-gray-700 py-1.5 px-2 flex items-center text-sm leading-none",
|
2023-03-26 19:19:13 +00:00
|
|
|
props.classNames?.multiValue?.(state)
|
2023-03-20 11:18:23 +00:00
|
|
|
),
|
2023-03-26 19:19:13 +00:00
|
|
|
menu: (state) =>
|
2023-03-20 11:18:23 +00:00
|
|
|
classNames(
|
|
|
|
"dark:bg-darkgray-100 rounded-md bg-white text-sm leading-4 dark:text-white mt-1 border border-gray-200 dark:border-darkgray-200 ",
|
2023-03-26 19:19:13 +00:00
|
|
|
props.classNames?.menu?.(state)
|
2023-03-20 11:18:23 +00:00
|
|
|
),
|
2023-03-26 19:19:13 +00:00
|
|
|
menuList: (state) =>
|
|
|
|
classNames("scroll-bar scrollbar-track-w-20 rounded-md", props.classNames?.menuList?.(state)),
|
2023-03-20 11:18:23 +00:00
|
|
|
indicatorsContainer: (state) =>
|
|
|
|
classNames(
|
|
|
|
state.selectProps.menuIsOpen
|
|
|
|
? state.isMulti
|
|
|
|
? "[&>*:last-child]:rotate-180 [&>*:last-child]:transition-transform"
|
|
|
|
: "rotate-180 transition-transform"
|
|
|
|
: "text-gray-600 dark:text-darkgray-600" // Woo it adds another SVG here on multi for some reason
|
|
|
|
),
|
|
|
|
multiValueRemove: () => "text-gray-600 dark:text-darkgray-600 py-auto ml-2",
|
|
|
|
}}
|
2022-08-24 20:18:42 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export type { ITimezone, ITimezoneOption };
|