2023-01-06 20:05:23 +00:00
|
|
|
import React from "react";
|
|
|
|
|
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
|
2023-04-13 02:10:23 +00:00
|
|
|
type RadioAreaProps = React.InputHTMLAttributes<HTMLInputElement> & { classNames?: { container?: string } };
|
2023-01-06 20:05:23 +00:00
|
|
|
|
|
|
|
const RadioArea = React.forwardRef<HTMLInputElement, RadioAreaProps>(
|
2023-04-13 02:10:23 +00:00
|
|
|
({ children, className, classNames: innerClassNames, ...props }, ref) => {
|
2023-01-06 20:05:23 +00:00
|
|
|
return (
|
|
|
|
<label className={classNames("relative flex", className)}>
|
|
|
|
<input
|
|
|
|
ref={ref}
|
2023-04-05 18:14:46 +00:00
|
|
|
className="text-emphasis bg-subtle border-emphasis focus:ring-none peer absolute top-[0.9rem] left-3 align-baseline"
|
2023-01-06 20:05:23 +00:00
|
|
|
type="radio"
|
|
|
|
{...props}
|
|
|
|
/>
|
2023-04-13 02:10:23 +00:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
"text-default peer-checked:border-emphasis border-subtle rounded-md border p-4 pt-3 pl-10",
|
|
|
|
innerClassNames?.container
|
|
|
|
)}>
|
2023-01-06 20:05:23 +00:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2023-01-27 01:50:56 +00:00
|
|
|
type MaybeArray<T> = T[] | T;
|
|
|
|
type ChildrenOfType<T extends React.ElementType> = MaybeArray<
|
|
|
|
React.ReactElement<React.ComponentPropsWithoutRef<T>>
|
|
|
|
>;
|
|
|
|
interface RadioAreaGroupProps extends Omit<React.ComponentPropsWithoutRef<"div">, "onChange" | "children"> {
|
2023-01-06 20:05:23 +00:00
|
|
|
onChange?: (value: string) => void;
|
2023-01-27 01:50:56 +00:00
|
|
|
children: ChildrenOfType<typeof RadioArea>;
|
2023-01-06 20:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const RadioAreaGroup = ({ children, className, onChange, ...passThroughProps }: RadioAreaGroupProps) => {
|
|
|
|
const childrenWithProps = React.Children.map(children, (child) => {
|
|
|
|
if (onChange && React.isValidElement(child)) {
|
|
|
|
return React.cloneElement(child, {
|
|
|
|
onChange: (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
onChange(e.target.value);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return child;
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<div className={className} {...passThroughProps}>
|
|
|
|
{childrenWithProps}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
RadioAreaGroup.displayName = "RadioAreaGroup";
|
|
|
|
RadioArea.displayName = "RadioArea";
|
|
|
|
|
|
|
|
const Item = RadioArea;
|
|
|
|
const Group = RadioAreaGroup;
|
|
|
|
|
|
|
|
export { RadioArea, RadioAreaGroup, Item, Group };
|