import clsx from "clsx";
import { orderBy } from "lodash";
import { ChangeEvent, FC, HTMLAttributes, ReactNode } from "react";
import { Accordion, AccordionProps } from "../Accordion";
import { Checkbox } from "../Checkbox";
import styles from "./styles.module.css";
export interface CheckboxInfo {
description?: string;
disabled?: boolean;
initiallyEnabled: boolean;
label: string;
onChange?: (e: ChangeEvent) => void;
onChangeConfirm?: (
newIsEnabled: boolean,
accept: () => void,
reject: () => void
) => void;
sortOrder?: number;
value?: string | number;
}
interface CheckboxGroupProps
extends Omit, "title"> {
accordionDefaultOpen?: boolean;
accordionSummary?: string;
checkboxes: Array;
disabled?: boolean;
description?: ReactNode;
name: string;
onToggleAll?: (e: ChangeEvent) => void;
showAccordion?: boolean;
themed?: boolean;
toggleAllLabel?: string;
title: ReactNode;
variant?: "default" | "inline" | "collapse";
}
export const CheckboxGroup: FC = ({
accordionSummary = "Options",
accordionDefaultOpen,
checkboxes,
className,
disabled,
description,
name,
onToggleAll,
showAccordion,
themed,
title,
toggleAllLabel = "Enable All Options",
variant = "default",
...props
}) => {
const isInlineVariant = variant === "inline";
const isCollapseVariant = variant === "collapse";
const sortedCheckboxes = orderBy(checkboxes, "sortOrder");
const allChecksEnabled = sortedCheckboxes.every(
(checkbox) => checkbox.initiallyEnabled
);
// Determine whether to render a div or an Accordion
const ContentWrapper = showAccordion ? Accordion : "div";
// Set props specific to Accordion if needed
const accordionProps = showAccordion
? {
summary: accordionSummary,
forceShow: accordionDefaultOpen,
variant: "text",
}
: {};
const checkboxList = sortedCheckboxes.map((checkbox, i) => {
const id = `${name}-${i}`;
return (
{checkbox.description && (
)}
);
});
/** Handle Collapse Variant separately since it's structure is different */
if (isCollapseVariant)
return (
{title}
}
variant="text"
forceShow={accordionDefaultOpen}
{...props}
>
{description && (
{description}
)}
{onToggleAll && (
)}
{checkboxList}
);
return (
{!isInlineVariant && onToggleAll && (
)}
{!isInlineVariant && (
{checkboxList}
)}
);
};