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
|
|
|
|
2023-04-13 21:45:18 +00:00
|
|
|
import cx 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>) => {
|
2023-04-13 21:45:18 +00:00
|
|
|
const { classNames, ...restProps } = props;
|
2022-10-10 18:50:43 +00:00
|
|
|
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={{
|
2023-04-13 21:45:18 +00:00
|
|
|
input: () => cx("text-emphasis", props.classNames?.input),
|
2023-03-20 11:18:23 +00:00
|
|
|
option: (state) =>
|
2023-04-13 21:45:18 +00:00
|
|
|
cx(
|
2023-04-05 18:14:46 +00:00
|
|
|
"bg-default flex cursor-pointer justify-between py-2.5 px-3 rounded-none text-default ",
|
|
|
|
state.isFocused && "bg-subtle",
|
|
|
|
state.isSelected && "bg-emphasis text-default",
|
2023-03-20 11:18:23 +00:00
|
|
|
props.classNames?.option
|
|
|
|
),
|
2023-04-13 21:45:18 +00:00
|
|
|
placeholder: (state) => cx("text-muted", state.isFocused && variant !== "checkbox" && "hidden"),
|
2023-04-05 18:14:46 +00:00
|
|
|
dropdownIndicator: () => "text-default",
|
2023-03-20 11:18:23 +00:00
|
|
|
control: (state) =>
|
2023-04-13 21:45:18 +00:00
|
|
|
cx(
|
2023-05-30 15:47:22 +00:00
|
|
|
"bg-default border-default !min-h-9 h-9 text-sm leading-4 placeholder:text-sm placeholder:font-normal dark:focus:border-emphasis focus-within:outline-none focus-within:ring-2 focus-within:ring-brand-default hover:border-emphasis rounded-md border",
|
2023-03-20 16:43:32 +00:00
|
|
|
state.isMulti
|
|
|
|
? variant === "checkbox"
|
2023-05-19 06:46:06 +00:00
|
|
|
? "px-3 py-2 h-fit"
|
2023-03-20 16:43:32 +00:00
|
|
|
: state.hasValue
|
2023-05-19 06:46:06 +00:00
|
|
|
? "p-1 h-fit"
|
|
|
|
: "px-3 py-2 h-fit"
|
|
|
|
: "py-2 px-3 h-fit",
|
2023-05-15 13:56:26 +00:00
|
|
|
props.isDisabled && "bg-subtle",
|
2023-03-20 11:18:23 +00:00
|
|
|
props.classNames?.control
|
|
|
|
),
|
2023-04-13 21:45:18 +00:00
|
|
|
singleValue: () => cx("text-emphasis placeholder:text-muted", props.classNames?.singleValue),
|
2023-03-20 11:18:23 +00:00
|
|
|
valueContainer: () =>
|
2023-04-13 21:45:18 +00:00
|
|
|
cx("text-emphasis placeholder:text-muted flex gap-1", props.classNames?.valueContainer),
|
2023-03-20 11:18:23 +00:00
|
|
|
multiValue: () =>
|
2023-04-13 21:45:18 +00:00
|
|
|
cx(
|
2023-04-05 18:14:46 +00:00
|
|
|
"bg-subtle text-default rounded-md py-1.5 px-2 flex items-center text-sm leading-none",
|
2023-03-20 11:18:23 +00:00
|
|
|
props.classNames?.multiValue
|
|
|
|
),
|
|
|
|
menu: () =>
|
2023-04-13 21:45:18 +00:00
|
|
|
cx(
|
2023-04-05 18:14:46 +00:00
|
|
|
"rounded-md bg-default text-sm leading-4 text-default mt-1 border border-subtle",
|
2023-03-20 11:18:23 +00:00
|
|
|
props.classNames?.menu
|
|
|
|
),
|
2023-04-05 18:14:46 +00:00
|
|
|
groupHeading: () => "leading-none text-xs uppercase text-default pl-2.5 pt-4 pb-2",
|
2023-04-13 21:45:18 +00:00
|
|
|
menuList: () => cx("scroll-bar scrollbar-track-w-20 rounded-md", props.classNames?.menuList),
|
2023-03-20 11:18:23 +00:00
|
|
|
indicatorsContainer: (state) =>
|
2023-04-13 21:45:18 +00:00
|
|
|
cx(
|
2023-03-20 11:18:23 +00:00
|
|
|
state.selectProps.menuIsOpen
|
|
|
|
? state.isMulti
|
|
|
|
? "[&>*:last-child]:rotate-180 [&>*:last-child]:transition-transform"
|
|
|
|
: "rotate-180 transition-transform"
|
2023-04-05 18:14:46 +00:00
|
|
|
: "text-default" // Woo it adds another SVG here on multi for some reason
|
2023-03-20 11:18:23 +00:00
|
|
|
),
|
2023-04-05 18:14:46 +00:00
|
|
|
multiValueRemove: () => "text-default py-auto ml-2",
|
2023-04-13 21:45:18 +00:00
|
|
|
...classNames,
|
2022-10-25 00:29:49 +00:00
|
|
|
}}
|
2023-04-13 21:45:18 +00:00
|
|
|
{...restProps}
|
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 (
|
2023-04-13 21:45:18 +00:00
|
|
|
<div className={cx(containerClassName)}>
|
|
|
|
<div className={cx(className)}>
|
2022-09-02 19:00:41 +00:00
|
|
|
{!!label && (
|
2023-04-13 21:45:18 +00:00
|
|
|
<Label htmlFor={id} {...labelProps} className={cx(props.error && "text-error")}>
|
2022-09-02 19:00:41 +00:00
|
|
|
{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 (
|
2023-04-13 21:45:18 +00:00
|
|
|
<div className={cx("relative", remainingProps.className)}>
|
2022-09-02 19:00:41 +00:00
|
|
|
<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>
|
|
|
|
);
|
|
|
|
}
|