2022-09-02 19:00:41 +00:00
|
|
|
import { useId } from "@radix-ui/react-id";
|
|
|
|
import * as React from "react";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type {
|
2022-09-14 07:27:00 +00:00
|
|
|
GroupBase,
|
|
|
|
Props,
|
|
|
|
SingleValue,
|
|
|
|
MultiValue,
|
2022-10-10 18:50:43 +00:00
|
|
|
SelectComponentsConfig,
|
2023-01-12 16:29:37 +00:00
|
|
|
MenuPlacement,
|
2022-09-14 07:27:00 +00:00
|
|
|
} from "react-select";
|
2023-02-16 22:39:57 +00:00
|
|
|
import ReactSelect, { components as reactSelectComponents } 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";
|
2022-10-10 18:50:43 +00:00
|
|
|
import {
|
|
|
|
ControlComponent,
|
|
|
|
InputComponent,
|
|
|
|
MenuComponent,
|
|
|
|
MenuListComponent,
|
|
|
|
OptionComponent,
|
|
|
|
SingleValueComponent,
|
|
|
|
ValueContainerComponent,
|
|
|
|
MultiValueComponent,
|
|
|
|
} from "./components";
|
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>
|
|
|
|
> = Props<Option, IsMulti, Group>;
|
2022-07-23 00:39:50 +00:00
|
|
|
|
2022-10-10 18:50:43 +00:00
|
|
|
export const getReactSelectProps = <
|
|
|
|
Option,
|
|
|
|
IsMulti extends boolean = false,
|
|
|
|
Group extends GroupBase<Option> = GroupBase<Option>
|
|
|
|
>({
|
2022-09-14 07:27:00 +00:00
|
|
|
className,
|
2022-10-10 18:50:43 +00:00
|
|
|
components,
|
2023-01-12 16:29:37 +00:00
|
|
|
menuPlacement = "auto",
|
2022-10-10 18:50:43 +00:00
|
|
|
}: {
|
|
|
|
className?: string;
|
|
|
|
components: SelectComponentsConfig<Option, IsMulti, Group>;
|
2023-01-12 16:29:37 +00:00
|
|
|
menuPlacement?: MenuPlacement;
|
2022-10-10 18:50:43 +00:00
|
|
|
}) => ({
|
2023-01-12 16:29:37 +00:00
|
|
|
menuPlacement,
|
2022-10-10 18:50:43 +00:00
|
|
|
className: classNames("block h-[36px] w-full min-w-0 flex-1 rounded-md", className),
|
|
|
|
classNamePrefix: "cal-react-select",
|
|
|
|
components: {
|
|
|
|
...reactSelectComponents,
|
|
|
|
IndicatorSeparator: () => null,
|
|
|
|
Input: InputComponent,
|
|
|
|
Option: OptionComponent,
|
|
|
|
Control: ControlComponent,
|
|
|
|
SingleValue: SingleValueComponent,
|
|
|
|
Menu: MenuComponent,
|
|
|
|
MenuList: MenuListComponent,
|
|
|
|
ValueContainer: ValueContainerComponent,
|
|
|
|
MultiValue: MultiValueComponent,
|
|
|
|
...components,
|
|
|
|
},
|
|
|
|
});
|
2022-09-14 07:27:00 +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
|
|
|
>({
|
|
|
|
className,
|
|
|
|
components,
|
2022-11-30 16:13:20 +00:00
|
|
|
styles,
|
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>({
|
|
|
|
className,
|
|
|
|
components: components || {},
|
|
|
|
});
|
2022-10-10 18:50:43 +00:00
|
|
|
}, [className, components]);
|
|
|
|
|
2022-10-25 00:29:49 +00:00
|
|
|
return (
|
|
|
|
<ReactSelect
|
|
|
|
{...reactSelectProps}
|
|
|
|
{...props}
|
|
|
|
styles={{
|
|
|
|
option: (defaultStyles, state) => ({
|
|
|
|
...defaultStyles,
|
|
|
|
backgroundColor: state.isSelected
|
|
|
|
? state.isFocused
|
|
|
|
? "var(--brand-color)"
|
|
|
|
: "var(--brand-color)"
|
|
|
|
: state.isFocused
|
|
|
|
? "var(--brand-color-dark-mode)"
|
|
|
|
: "var(--brand-text-color)",
|
|
|
|
}),
|
2022-11-30 16:13:20 +00:00
|
|
|
...styles,
|
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-12-18 23:16:20 +00:00
|
|
|
type IconLeadingProps = {
|
|
|
|
icon: React.ReactNode;
|
|
|
|
children?: React.ReactNode;
|
|
|
|
} & React.ComponentProps<typeof reactSelectComponents.Control>;
|
|
|
|
|
|
|
|
export const IconLeading = ({ icon, children, ...props }: IconLeadingProps) => {
|
|
|
|
return (
|
|
|
|
<reactSelectComponents.Control {...props}>
|
|
|
|
{icon}
|
|
|
|
{children}
|
|
|
|
</reactSelectComponents.Control>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
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: {
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|