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={{
|
2023-04-05 18:14:46 +00:00
|
|
|
input: () => classNames("text-emphasis", props.classNames?.input),
|
2023-03-20 11:18:23 +00:00
|
|
|
option: (state) =>
|
|
|
|
classNames(
|
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
|
|
|
|
),
|
|
|
|
placeholder: (state) =>
|
2023-04-05 18:14:46 +00:00
|
|
|
classNames("text-muted", state.isFocused && variant !== "checkbox" && "hidden"),
|
|
|
|
dropdownIndicator: () => "text-default",
|
2023-03-20 11:18:23 +00:00
|
|
|
control: (state) =>
|
|
|
|
classNames(
|
2023-04-05 18:14:46 +00:00
|
|
|
"bg-default border-default !min-h-9 text-sm leading-4 placeholder:text-sm placeholder:font-normal focus-within:ring-2 focus-within:ring-emphasis hover:border-emphasis 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
|
|
|
|
),
|
2023-04-05 18:14:46 +00:00
|
|
|
singleValue: () => classNames("text-emphasis placeholder:text-muted", props.classNames?.singleValue),
|
2023-03-20 11:18:23 +00:00
|
|
|
valueContainer: () =>
|
2023-04-05 18:14:46 +00:00
|
|
|
classNames("text-emphasis placeholder:text-muted flex gap-1", props.classNames?.valueContainer),
|
2023-03-20 11:18:23 +00:00
|
|
|
multiValue: () =>
|
|
|
|
classNames(
|
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: () =>
|
|
|
|
classNames(
|
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-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"
|
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-03-20 11:18:23 +00:00
|
|
|
...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 && (
|
2023-04-05 18:14:46 +00:00
|
|
|
<Label htmlFor={id} {...labelProps} className={classNames(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 (
|
|
|
|
<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>
|
|
|
|
);
|
|
|
|
}
|