2023-05-25 09:05:10 +00:00
|
|
|
import { useId } from "@radix-ui/react-id";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { InputHTMLAttributes } from "react";
|
|
|
|
import React, { forwardRef } from "react";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
|
|
|
|
type Props = InputHTMLAttributes<HTMLInputElement> & {
|
|
|
|
label?: React.ReactNode;
|
|
|
|
description: string;
|
|
|
|
descriptionAsLabel?: boolean;
|
|
|
|
informationIconText?: string;
|
|
|
|
error?: boolean;
|
2022-11-25 12:59:25 +00:00
|
|
|
className?: string;
|
2023-04-11 21:44:14 +00:00
|
|
|
descriptionClassName?: string;
|
2022-07-23 00:39:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const CheckboxField = forwardRef<HTMLInputElement, Props>(
|
2023-01-31 22:29:24 +00:00
|
|
|
({ label, description, error, disabled, ...rest }, ref) => {
|
2022-07-23 00:39:50 +00:00
|
|
|
const descriptionAsLabel = !label || rest.descriptionAsLabel;
|
2023-05-25 09:05:10 +00:00
|
|
|
const id = useId();
|
2022-07-23 00:39:50 +00:00
|
|
|
return (
|
|
|
|
<div className="block items-center sm:flex">
|
|
|
|
{label && (
|
|
|
|
<div className="min-w-48 mb-4 sm:mb-0">
|
|
|
|
{React.createElement(
|
|
|
|
descriptionAsLabel ? "div" : "label",
|
|
|
|
{
|
2023-04-05 18:14:46 +00:00
|
|
|
className: classNames("flex text-sm font-medium text-emphasis"),
|
2022-07-23 00:39:50 +00:00
|
|
|
...(!descriptionAsLabel
|
|
|
|
? {
|
2023-05-25 09:05:10 +00:00
|
|
|
htmlFor: rest.id ? rest.id : id,
|
2022-07-23 00:39:50 +00:00
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
},
|
|
|
|
label
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div className="w-full">
|
2023-04-11 21:44:14 +00:00
|
|
|
<div className="relative flex items-center">
|
2022-07-23 00:39:50 +00:00
|
|
|
{React.createElement(
|
|
|
|
descriptionAsLabel ? "label" : "div",
|
|
|
|
{
|
|
|
|
className: classNames(
|
|
|
|
"relative flex items-start",
|
2023-04-05 18:14:46 +00:00
|
|
|
!error && descriptionAsLabel ? "text-emphasis" : "text-emphasis",
|
2023-06-23 09:05:37 +00:00
|
|
|
error && "text-error"
|
2022-07-23 00:39:50 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
<>
|
2023-04-24 21:37:16 +00:00
|
|
|
<div className="flex h-5 items-center">
|
2022-07-23 00:39:50 +00:00
|
|
|
<input
|
|
|
|
{...rest}
|
|
|
|
ref={ref}
|
|
|
|
type="checkbox"
|
2023-01-02 08:41:39 +00:00
|
|
|
disabled={disabled}
|
2023-05-25 09:05:10 +00:00
|
|
|
id={rest.id ? rest.id : id}
|
2022-07-23 00:39:50 +00:00
|
|
|
className={classNames(
|
2023-06-23 09:05:37 +00:00
|
|
|
"text-primary-600 focus:ring-primary-500 border-default bg-default focus:bg-default active:bg-default h-4 w-4 rounded checked:hover:bg-gray-600 focus:outline-none focus:ring-0 ltr:mr-2 rtl:ml-2",
|
2022-07-23 00:39:50 +00:00
|
|
|
!error && disabled
|
2023-05-15 13:56:26 +00:00
|
|
|
? "cursor-not-allowed bg-gray-300 checked:bg-gray-300 hover:bg-gray-300 hover:checked:bg-gray-300"
|
2023-06-23 09:05:37 +00:00
|
|
|
: "hover:bg-subtle hover:border-emphasis checked:bg-gray-800",
|
|
|
|
error &&
|
|
|
|
"border-error hover:bg-error hover:border-error checked:bg-darkerror checked:hover:border-error checked:hover:bg-darkerror",
|
2022-11-25 12:59:25 +00:00
|
|
|
rest.className
|
2022-07-23 00:39:50 +00:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-04-11 21:44:14 +00:00
|
|
|
<span className={classNames("text-sm", rest.descriptionClassName)}>{description}</span>
|
2022-07-23 00:39:50 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{/* {informationIconText && <InfoBadge content={informationIconText}></InfoBadge>} */}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
CheckboxField.displayName = "CheckboxField";
|
|
|
|
|
|
|
|
export default CheckboxField;
|