import { classNames } from '@/utils'; import React, { useEffect, useState } from 'react'; import ErrorMessage from './ErrorMessage'; import SimpleCheckbox from './SimpleCheckbox'; export interface SimpleCheckboxGroupProps { name?: string; label?: string; helpText?: string; className?: string; options: Array<{ value: string | number; label: string; indeterminate?: boolean; }>; defaultValue?: Array; errors?: string[]; onChange?: (checkedList: Array) => void; } const SimpleCheckboxGroup: React.FC = (props) => { const [checkedList, setCheckedList] = useState>(props.defaultValue ?? []); useEffect(() => props.onChange && props.onChange(checkedList), [checkedList]); const hasErrors = Boolean(props.errors && props.errors.length > 0); return ( <> {props.label && ( )} {props.helpText &&

{props.helpText}

}
{props.options.map((option, optionIndex) => (
val === option.value)} indeterminate={option.indeterminate} onChange={(e) => { if (e.target.checked) { setCheckedList((list) => [...list, option.value]); } else { setCheckedList((list) => list.filter((val) => val !== option.value)); } }} />
))}
); }; export default SimpleCheckboxGroup;