2022-09-02 19:00:41 +00:00
|
|
|
import { useId } from "@radix-ui/react-id";
|
|
|
|
import * as React from "react";
|
2023-03-20 11:18:23 +00:00
|
|
|
import type { GroupBase, Props, SingleValue, MultiValue } from "react-select";
|
|
|
|
import ReactSelect from "react-select";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
2022-08-03 16:01:29 +00:00
|
|
|
import classNames from "@calcom/lib/classNames";
|
2022-09-02 19:00:41 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
|
2022-12-18 23:16:20 +00:00
|
|
|
import { Label } from "../inputs/Label";
|
2023-03-20 11:18:23 +00:00
|
|
|
import { getReactSelectProps } from "./selectTheme";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
2022-08-03 16:01:29 +00:00
|
|
|
export type SelectProps<
|
|
|
|
Option,
|
|
|
|
IsMulti extends boolean = false,
|
|
|
|
Group extends GroupBase<Option> = GroupBase<Option>
|
2023-03-20 16:43:32 +00:00
|
|
|
> = Props<Option, IsMulti, Group> & { variant?: "default" | "checkbox" };
|
2022-07-23 00:39:50 +00:00
|
|
|
|
2022-12-18 23:16:20 +00:00
|
|
|
export const Select = <
|
2022-08-03 16:01:29 +00:00
|
|
|
Option,
|
|
|
|
IsMulti extends boolean = false,
|
|
|
|
Group extends GroupBase<Option> = GroupBase<Option>
|
2022-10-10 18:50:43 +00:00
|
|
|
>({
|
|
|
|
components,
|
2023-03-20 11:18:23 +00:00
|
|
|
menuPlacement,
|
2023-03-20 16:43:32 +00:00
|
|
|
variant = "default",
|
2022-10-10 18:50:43 +00:00
|
|
|
...props
|
|
|
|
}: SelectProps<Option, IsMulti, Group>) => {
|
|
|
|
const reactSelectProps = React.useMemo(() => {
|
2023-01-12 16:29:37 +00:00
|
|
|
return getReactSelectProps<Option, IsMulti, Group>({
|
|
|
|
components: components || {},
|
2023-03-20 11:18:23 +00:00
|
|
|
menuPlacement,
|
2023-01-12 16:29:37 +00:00
|
|
|
});
|
2023-03-20 11:18:23 +00:00
|
|
|
}, [components, menuPlacement]);
|
2022-10-10 18:50:43 +00:00
|
|
|
|
2023-03-20 11:18:23 +00:00
|
|
|
// Annoyingly if we update styles here we have to update timezone select too
|
|
|
|
// We cant create a generate function for this as we can't force state changes - onSelect styles dont change for example
|
2022-10-25 00:29:49 +00:00
|
|
|
return (
|
|
|
|
<ReactSelect
|
|
|
|
{...reactSelectProps}
|
2023-03-20 11:18:23 +00:00
|
|
|
classNames={{
|
|
|
|
input: () => classNames("dark:text-darkgray-900 text-gray-900", props.classNames?.input),
|
|
|
|
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) =>
|
2023-03-20 16:43:32 +00:00
|
|
|
classNames(
|
|
|
|
"text-gray-400 text-sm dark:text-darkgray-400",
|
|
|
|
state.isFocused && variant !== "checkbox" && "hidden"
|
|
|
|
),
|
2023-03-20 11:18:23 +00:00
|
|
|
dropdownIndicator: () => "text-gray-600 dark:text-darkgray-400",
|
|
|
|
control: (state) =>
|
|
|
|
classNames(
|
|
|
|
"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 ",
|
2023-03-20 16:43:32 +00:00
|
|
|
state.isMulti
|
|
|
|
? variant === "checkbox"
|
|
|
|
? "px-3 py-2"
|
|
|
|
: state.hasValue
|
|
|
|
? "p-1"
|
|
|
|
: "px-3 py-2"
|
|
|
|
: "py-2 px-3",
|
2023-03-20 11:18:23 +00:00
|
|
|
props.classNames?.control
|
|
|
|
),
|
|
|
|
singleValue: () =>
|
|
|
|
classNames(
|
|
|
|
"dark:text-darkgray-900 dark:placeholder:text-darkgray-500 text-black placeholder:text-gray-400",
|
|
|
|
props.classNames?.singleValue
|
|
|
|
),
|
|
|
|
valueContainer: () =>
|
|
|
|
classNames(
|
|
|
|
"dark:text-darkgray-900 dark:placeholder:text-darkgray-500 text-black placeholder:text-gray-400 flex gap-1",
|
|
|
|
props.classNames?.valueContainer
|
|
|
|
),
|
|
|
|
multiValue: () =>
|
|
|
|
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",
|
|
|
|
props.classNames?.multiValue
|
|
|
|
),
|
|
|
|
menu: () =>
|
|
|
|
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 ",
|
|
|
|
props.classNames?.menu
|
|
|
|
),
|
2023-03-20 18:46:22 +00:00
|
|
|
groupHeading: () =>
|
|
|
|
"leading-none text-xs uppercase text-gray-600 dark:text-darkgray-600 pl-2.5 pt-4 pb-2",
|
2023-03-20 11:18:23 +00:00
|
|
|
menuList: () => classNames("scroll-bar scrollbar-track-w-20 rounded-md", props.classNames?.menuList),
|
|
|
|
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",
|
|
|
|
...props.classNames,
|
2022-10-25 00:29:49 +00:00
|
|
|
}}
|
2023-03-20 11:18:23 +00:00
|
|
|
{...props}
|
2022-10-25 00:29:49 +00:00
|
|
|
/>
|
|
|
|
);
|
2022-10-10 18:50:43 +00:00
|
|
|
};
|
2022-07-23 00:39:50 +00:00
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
export const SelectField = function SelectField<
|
|
|
|
Option,
|
2022-10-10 18:50:43 +00:00
|
|
|
IsMulti extends boolean = false,
|
2022-09-02 19:00:41 +00:00
|
|
|
Group extends GroupBase<Option> = GroupBase<Option>
|
|
|
|
>(
|
|
|
|
props: {
|
2023-03-02 18:15:28 +00:00
|
|
|
required?: boolean;
|
2022-09-02 19:00:41 +00:00
|
|
|
name?: string;
|
|
|
|
containerClassName?: string;
|
|
|
|
label?: string;
|
|
|
|
labelProps?: React.ComponentProps<typeof Label>;
|
|
|
|
className?: string;
|
|
|
|
error?: string;
|
2022-10-10 18:50:43 +00:00
|
|
|
} & SelectProps<Option, IsMulti, Group>
|
2022-09-02 19:00:41 +00:00
|
|
|
) {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const { label = t(props.name || ""), containerClassName, labelProps, className, ...passThrough } = props;
|
|
|
|
const id = useId();
|
|
|
|
return (
|
|
|
|
<div className={classNames(containerClassName)}>
|
|
|
|
<div className={classNames(className)}>
|
|
|
|
{!!label && (
|
|
|
|
<Label htmlFor={id} {...labelProps} className={classNames(props.error && "text-red-900")}>
|
|
|
|
{label}
|
|
|
|
</Label>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<Select {...passThrough} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: It should replace Select after through testing
|
|
|
|
*/
|
|
|
|
export function SelectWithValidation<
|
|
|
|
Option extends { label: string; value: string },
|
2022-10-10 18:50:43 +00:00
|
|
|
IsMulti extends boolean = false,
|
2022-09-02 19:00:41 +00:00
|
|
|
Group extends GroupBase<Option> = GroupBase<Option>
|
|
|
|
>({
|
|
|
|
required = false,
|
|
|
|
onChange,
|
|
|
|
value,
|
|
|
|
...remainingProps
|
2022-10-10 18:50:43 +00:00
|
|
|
}: SelectProps<Option, IsMulti, Group> & { required?: boolean }) {
|
2022-09-02 19:00:41 +00:00
|
|
|
const [hiddenInputValue, _setHiddenInputValue] = React.useState(() => {
|
|
|
|
if (value instanceof Array || !value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return value.value || "";
|
|
|
|
});
|
|
|
|
|
|
|
|
const setHiddenInputValue = React.useCallback((value: MultiValue<Option> | SingleValue<Option>) => {
|
|
|
|
let hiddenInputValue = "";
|
|
|
|
if (value instanceof Array) {
|
|
|
|
hiddenInputValue = value.map((val) => val.value).join(",");
|
|
|
|
} else {
|
|
|
|
hiddenInputValue = value?.value || "";
|
|
|
|
}
|
|
|
|
_setHiddenInputValue(hiddenInputValue);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (!value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setHiddenInputValue(value);
|
|
|
|
}, [value, setHiddenInputValue]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classNames("relative", remainingProps.className)}>
|
|
|
|
<Select
|
|
|
|
value={value}
|
|
|
|
{...remainingProps}
|
|
|
|
onChange={(value, ...remainingArgs) => {
|
|
|
|
setHiddenInputValue(value);
|
|
|
|
if (onChange) {
|
|
|
|
onChange(value, ...remainingArgs);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{required && (
|
|
|
|
<input
|
|
|
|
tabIndex={-1}
|
|
|
|
autoComplete="off"
|
|
|
|
style={{
|
|
|
|
opacity: 0,
|
|
|
|
width: "100%",
|
|
|
|
height: 1,
|
|
|
|
position: "absolute",
|
|
|
|
}}
|
|
|
|
value={hiddenInputValue}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
|
|
onChange={() => {}}
|
|
|
|
// TODO:Not able to get focus to work
|
|
|
|
// onFocus={() => selectRef.current?.focus()}
|
|
|
|
required={required}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|