2021-10-20 15:42:40 +00:00
|
|
|
import React, { ReactNode, useState } from "react";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
import classNames from "@lib/classNames";
|
|
|
|
|
2021-10-20 15:42:40 +00:00
|
|
|
type RadioAreaProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, "onChange"> & {
|
|
|
|
onChange?: (value: string) => void;
|
|
|
|
defaultChecked?: boolean;
|
2021-09-14 08:45:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const RadioArea = (props: RadioAreaProps) => {
|
|
|
|
return (
|
|
|
|
<label
|
|
|
|
className={classNames(
|
2022-02-09 00:05:13 +00:00
|
|
|
"border-1 block border p-4 focus:outline-none focus:ring focus:ring-neutral-500",
|
2021-11-04 14:30:37 +00:00
|
|
|
props.checked && "border-brand",
|
2021-09-14 08:45:28 +00:00
|
|
|
props.className
|
|
|
|
)}>
|
|
|
|
<input
|
2021-10-20 15:42:40 +00:00
|
|
|
onChange={(e) => {
|
|
|
|
if (typeof props.onChange === "function") {
|
|
|
|
props.onChange(e.target.value);
|
|
|
|
}
|
|
|
|
}}
|
2021-09-14 08:45:28 +00:00
|
|
|
checked={props.checked}
|
2021-10-20 15:42:40 +00:00
|
|
|
className="float-right ml-3 text-neutral-900 focus:ring-neutral-500"
|
2021-09-14 08:45:28 +00:00
|
|
|
name={props.name}
|
|
|
|
value={props.value}
|
|
|
|
type="radio"
|
|
|
|
/>
|
|
|
|
{props.children}
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-10-20 15:42:40 +00:00
|
|
|
type ChildrenProps = {
|
|
|
|
props: RadioAreaProps;
|
|
|
|
children?: ReactNode;
|
2021-09-14 08:45:28 +00:00
|
|
|
};
|
2022-05-17 19:31:49 +00:00
|
|
|
interface RadioAreaGroupProps extends Omit<React.ComponentPropsWithoutRef<"div">, "onChange" | "children"> {
|
2021-10-20 15:42:40 +00:00
|
|
|
children: ChildrenProps | ChildrenProps[];
|
|
|
|
name?: string;
|
|
|
|
onChange?: (value: string) => void;
|
|
|
|
}
|
2021-09-14 08:45:28 +00:00
|
|
|
|
2021-10-20 15:42:40 +00:00
|
|
|
const RadioAreaGroup = ({ children, name, onChange, ...passThroughProps }: RadioAreaGroupProps) => {
|
2021-09-14 08:45:28 +00:00
|
|
|
const [checkedIdx, setCheckedIdx] = useState<number | null>(null);
|
|
|
|
|
|
|
|
const changeHandler = (value: string, idx: number) => {
|
|
|
|
if (onChange) {
|
|
|
|
onChange(value);
|
|
|
|
}
|
|
|
|
setCheckedIdx(idx);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div {...passThroughProps}>
|
2021-10-20 15:42:40 +00:00
|
|
|
{(Array.isArray(children) ? children : [children]).map((child, idx: number) => {
|
|
|
|
if (checkedIdx === null && child.props.defaultChecked) {
|
|
|
|
setCheckedIdx(idx);
|
2021-09-14 08:45:28 +00:00
|
|
|
}
|
2021-10-20 15:42:40 +00:00
|
|
|
return (
|
|
|
|
<Item
|
|
|
|
{...child.props}
|
|
|
|
key={idx}
|
|
|
|
name={name}
|
|
|
|
checked={idx === checkedIdx}
|
|
|
|
onChange={(value: string) => changeHandler(value, idx)}>
|
|
|
|
{child.props.children}
|
|
|
|
</Item>
|
|
|
|
);
|
|
|
|
})}
|
2021-09-14 08:45:28 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const Item = RadioArea;
|
|
|
|
const Group = RadioAreaGroup;
|
|
|
|
|
|
|
|
export { RadioArea, RadioAreaGroup, Item, Group };
|