2021-09-22 19:52:38 +00:00
|
|
|
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@radix-ui/react-collapsible";
|
2021-09-14 08:45:28 +00:00
|
|
|
import React from "react";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-07-27 02:24:00 +00:00
|
|
|
import { Icon } from "@calcom/ui/Icon";
|
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
import classNames from "@lib/classNames";
|
2021-10-15 10:53:42 +00:00
|
|
|
import { useLocale } from "@lib/hooks/useLocale";
|
2021-09-14 08:45:28 +00:00
|
|
|
|
2021-09-22 19:52:38 +00:00
|
|
|
import { RadioArea, RadioAreaGroup } from "@components/ui/form/radio-area/RadioAreaGroup";
|
|
|
|
|
2022-02-09 00:05:13 +00:00
|
|
|
interface OptionProps
|
|
|
|
extends Pick<React.OptionHTMLAttributes<HTMLOptionElement>, "value" | "label" | "className"> {
|
2021-09-14 08:45:28 +00:00
|
|
|
description?: string;
|
2022-02-09 00:05:13 +00:00
|
|
|
}
|
2021-09-14 08:45:28 +00:00
|
|
|
|
2022-02-09 00:05:13 +00:00
|
|
|
interface RadioAreaSelectProps extends Omit<React.SelectHTMLAttributes<HTMLSelectElement>, "onChange"> {
|
2021-09-14 08:45:28 +00:00
|
|
|
options: OptionProps[]; // allow options to be passed programmatically, like options={}
|
2022-02-09 00:05:13 +00:00
|
|
|
onChange?: (value: string) => void;
|
|
|
|
}
|
2021-09-14 08:45:28 +00:00
|
|
|
|
|
|
|
export const Select = function RadioAreaSelect(props: RadioAreaSelectProps) {
|
2021-10-15 10:53:42 +00:00
|
|
|
const { t } = useLocale();
|
2021-09-14 08:45:28 +00:00
|
|
|
const {
|
|
|
|
options,
|
|
|
|
disabled = !options.length, // if not explicitly disabled and the options length is empty, disable anyway
|
2021-10-15 10:53:42 +00:00
|
|
|
placeholder = t("select"),
|
2021-09-14 08:45:28 +00:00
|
|
|
} = props;
|
|
|
|
|
2022-02-09 00:05:13 +00:00
|
|
|
const getLabel = (value: string | ReadonlyArray<string> | number | undefined) =>
|
2021-09-14 08:45:28 +00:00
|
|
|
options.find((option: OptionProps) => option.value === value)?.label;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Collapsible className={classNames("w-full", props.className)}>
|
|
|
|
<CollapsibleTrigger
|
|
|
|
type="button"
|
|
|
|
disabled={disabled}
|
|
|
|
className={classNames(
|
2022-07-27 02:24:00 +00:00
|
|
|
"border-1 focus:ring-primary-500 mb-1 block w-full cursor-pointer rounded-sm border border-gray-300 bg-white p-2 text-left text-sm",
|
2022-02-09 00:05:13 +00:00
|
|
|
disabled && "cursor-default bg-gray-200 focus:ring-0 "
|
2021-09-14 08:45:28 +00:00
|
|
|
)}>
|
|
|
|
{getLabel(props.value) ?? placeholder}
|
2022-08-03 16:01:29 +00:00
|
|
|
<Icon.FiChevronDown className="float-right h-5 w-5 text-neutral-500" />
|
2021-09-14 08:45:28 +00:00
|
|
|
</CollapsibleTrigger>
|
|
|
|
<CollapsibleContent>
|
|
|
|
<RadioAreaGroup className="space-y-2 text-sm" name={props.name} onChange={props.onChange}>
|
|
|
|
{options.map((option) => (
|
2022-02-09 00:05:13 +00:00
|
|
|
<RadioArea
|
|
|
|
{...option}
|
|
|
|
key={Array.isArray(option.value) ? option.value.join(",") : `${option.value}`}
|
|
|
|
defaultChecked={props.value === option.value}>
|
|
|
|
<strong className="mb-1 block">{option.label}</strong>
|
2021-09-14 08:45:28 +00:00
|
|
|
<p>{option.description}</p>
|
|
|
|
</RadioArea>
|
|
|
|
))}
|
|
|
|
</RadioAreaGroup>
|
|
|
|
</CollapsibleContent>
|
|
|
|
</Collapsible>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Select;
|