2024-05-23 08:11:30 +00:00
|
|
|
import React from "react";
|
2024-05-23 10:28:14 +00:00
|
|
|
import { Field, Label, Radio, RadioGroup } from "@headlessui/react";
|
|
|
|
import { cn } from "../../helpers";
|
|
|
|
// helpers
|
2024-05-23 08:11:30 +00:00
|
|
|
|
|
|
|
type RadioInputProps = {
|
|
|
|
label: string | React.ReactNode | undefined;
|
2024-05-29 05:51:45 +00:00
|
|
|
wrapperClassName?: string;
|
|
|
|
fieldClassName?: string;
|
|
|
|
buttonClassName?: string;
|
2024-05-23 10:28:14 +00:00
|
|
|
labelClassName?: string;
|
2024-05-23 08:11:30 +00:00
|
|
|
ariaLabel?: string;
|
2024-05-29 05:51:45 +00:00
|
|
|
options: { label: string | React.ReactNode; value: string; disabled?: boolean }[];
|
2024-05-23 08:11:30 +00:00
|
|
|
vertical?: boolean;
|
|
|
|
selected: string;
|
2024-05-23 10:28:14 +00:00
|
|
|
onChange: (value: string) => void;
|
|
|
|
className?: string;
|
2024-05-23 08:11:30 +00:00
|
|
|
};
|
|
|
|
|
2024-05-24 09:37:44 +00:00
|
|
|
export const RadioInput = ({
|
2024-05-23 10:28:14 +00:00
|
|
|
label: inputLabel,
|
2024-05-29 05:51:45 +00:00
|
|
|
labelClassName: inputLabelClassName = "",
|
|
|
|
wrapperClassName: inputWrapperClassName = "",
|
|
|
|
fieldClassName: inputFieldClassName = "",
|
|
|
|
buttonClassName: inputButtonClassName = "",
|
2024-05-23 10:28:14 +00:00
|
|
|
options,
|
|
|
|
vertical,
|
|
|
|
selected,
|
|
|
|
ariaLabel,
|
|
|
|
onChange,
|
|
|
|
className,
|
|
|
|
}: RadioInputProps) => {
|
2024-05-23 08:11:30 +00:00
|
|
|
const wrapperClass = vertical ? "flex flex-col gap-1" : "flex gap-2";
|
|
|
|
|
|
|
|
const setSelected = (value: string) => {
|
2024-05-23 10:28:14 +00:00
|
|
|
onChange(value);
|
2024-05-23 08:11:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let aria = ariaLabel ? ariaLabel.toLowerCase().replace(" ", "-") : "";
|
|
|
|
if (!aria && typeof inputLabel === "string") {
|
|
|
|
aria = inputLabel.toLowerCase().replace(" ", "-");
|
|
|
|
} else {
|
|
|
|
aria = "radio-input";
|
|
|
|
}
|
|
|
|
|
2024-05-23 10:28:14 +00:00
|
|
|
// return <h1>Hello</h1>;
|
|
|
|
|
2024-05-23 08:11:30 +00:00
|
|
|
return (
|
2024-05-23 10:28:14 +00:00
|
|
|
<RadioGroup value={selected} onChange={setSelected} aria-label={aria} className={className}>
|
|
|
|
<Label className={cn(`mb-2`, inputLabelClassName)}>{inputLabel}</Label>
|
2024-05-29 05:51:45 +00:00
|
|
|
<div className={cn(`${wrapperClass}`, inputWrapperClassName)}>
|
|
|
|
{options.map(({ value, label, disabled }, index) => (
|
|
|
|
<Field key={index} className={cn("flex items-center gap-2", inputFieldClassName)}>
|
2024-05-23 08:11:30 +00:00
|
|
|
<Radio
|
|
|
|
value={value}
|
2024-05-29 05:51:45 +00:00
|
|
|
className={cn(
|
|
|
|
"group flex size-5 items-center justify-center rounded-full border border-custom-border-400 bg-custom-background-500 data-[checked]:bg-custom-primary-200 data-[checked]:border-custom-primary-100 cursor-pointer data-[disabled]:bg-custom-background-200 data-[disabled]:border-custom-border-200 data-[disabled]:cursor-not-allowed",
|
|
|
|
inputButtonClassName
|
|
|
|
)}
|
2024-05-23 10:28:14 +00:00
|
|
|
disabled={disabled}
|
2024-05-23 08:11:30 +00:00
|
|
|
>
|
|
|
|
<span className="invisible size-2 rounded-full bg-white group-data-[checked]:visible" />
|
|
|
|
</Radio>
|
2024-05-23 10:28:14 +00:00
|
|
|
<Label className="text-base cursor-pointer">{label}</Label>
|
2024-05-23 08:11:30 +00:00
|
|
|
</Field>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</RadioGroup>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-05-24 09:37:44 +00:00
|
|
|
export default RadioInput;
|