2022-07-23 00:39:50 +00:00
|
|
|
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@radix-ui/react-collapsible";
|
|
|
|
import React from "react";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { FieldValues, Path, UseFormReturn } from "react-hook-form";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-05-17 12:47:44 +00:00
|
|
|
import { ChevronDown } from "@calcom/ui/components/icon";
|
2022-11-23 02:55:25 +00:00
|
|
|
|
|
|
|
import { RadioArea, RadioAreaGroup } from "./RadioAreaGroup";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
interface OptionProps
|
|
|
|
extends Pick<React.OptionHTMLAttributes<HTMLOptionElement>, "value" | "label" | "className"> {
|
|
|
|
description?: string;
|
|
|
|
}
|
|
|
|
|
2023-01-06 20:05:23 +00:00
|
|
|
export type FieldPath<TFieldValues extends FieldValues> = Path<TFieldValues>;
|
|
|
|
interface RadioAreaSelectProps<TFieldValues extends FieldValues>
|
|
|
|
extends Omit<React.SelectHTMLAttributes<HTMLSelectElement>, "onChange" | "form"> {
|
2022-07-23 00:39:50 +00:00
|
|
|
options: OptionProps[]; // allow options to be passed programmatically, like options={}
|
|
|
|
onChange?: (value: string) => void;
|
2023-01-06 20:05:23 +00:00
|
|
|
form: UseFormReturn<TFieldValues>;
|
|
|
|
name: FieldPath<TFieldValues>;
|
2022-07-23 00:39:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 20:05:23 +00:00
|
|
|
export const Select = function RadioAreaSelect<TFieldValues extends FieldValues>(
|
|
|
|
props: RadioAreaSelectProps<TFieldValues>
|
|
|
|
) {
|
2022-07-23 00:39:50 +00:00
|
|
|
const { t } = useLocale();
|
|
|
|
const {
|
|
|
|
options,
|
2023-01-06 20:05:23 +00:00
|
|
|
form,
|
2022-07-23 00:39:50 +00:00
|
|
|
disabled = !options.length, // if not explicitly disabled and the options length is empty, disable anyway
|
|
|
|
placeholder = t("select"),
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const getLabel = (value: string | ReadonlyArray<string> | number | undefined) =>
|
|
|
|
options.find((option: OptionProps) => option.value === value)?.label;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Collapsible className={classNames("w-full", props.className)}>
|
|
|
|
<CollapsibleTrigger
|
|
|
|
type="button"
|
|
|
|
disabled={disabled}
|
|
|
|
className={classNames(
|
2023-04-05 18:14:46 +00:00
|
|
|
"focus:ring-primary-500 border-default bg-default mb-1 block w-full cursor-pointer rounded-sm border border p-2 text-left shadow-sm sm:text-sm",
|
|
|
|
disabled && "bg-emphasis cursor-default focus:ring-0 "
|
2022-07-23 00:39:50 +00:00
|
|
|
)}>
|
|
|
|
{getLabel(props.value) ?? placeholder}
|
2023-05-17 12:47:44 +00:00
|
|
|
<ChevronDown className="text-subtle float-right h-5 w-5" />
|
2022-07-23 00:39:50 +00:00
|
|
|
</CollapsibleTrigger>
|
|
|
|
<CollapsibleContent>
|
2023-01-06 20:05:23 +00:00
|
|
|
<RadioAreaGroup className="space-y-2 text-sm" onChange={props.onChange}>
|
2022-07-23 00:39:50 +00:00
|
|
|
{options.map((option) => (
|
|
|
|
<RadioArea
|
2023-01-06 20:05:23 +00:00
|
|
|
{...form.register(props.name)}
|
2022-07-23 00:39:50 +00:00
|
|
|
{...option}
|
2023-01-06 20:05:23 +00:00
|
|
|
key={Array.isArray(option.value) ? option.value.join(",") : `${option.value}`}>
|
2022-07-23 00:39:50 +00:00
|
|
|
<strong className="mb-1 block">{option.label}</strong>
|
|
|
|
<p>{option.description}</p>
|
|
|
|
</RadioArea>
|
|
|
|
))}
|
|
|
|
</RadioAreaGroup>
|
|
|
|
</CollapsibleContent>
|
|
|
|
</Collapsible>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Select;
|