import { Checkbox, CheckboxProps, FormControl, FormControlLabel, FormControlLabelProps, FormGroup, FormHelperText, FormLabel, useTheme, } from '@mui/material' import { Control, FieldError, FieldPath, FieldValues, PathValue, useController, UseControllerProps, } from 'react-hook-form' import {useFormError} from './FormErrorProvider' import { forwardRef, type ReactElement, ReactNode, Ref, RefAttributes, } from 'react' import {useTransform} from './useTransform' import {propertyExists} from './utils' export type CheckboxButtonGroupProps< TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, TValue = unknown, > = { options: (TValue | unknown)[] helperText?: ReactNode name: TName required?: boolean parseError?: (error: FieldError) => ReactNode label?: string labelKey?: string valueKey?: string onChange?: (data: TValue[]) => void returnObject?: boolean disabled?: boolean row?: boolean control?: Control rules?: UseControllerProps['rules'] checkboxColor?: CheckboxProps['color'] labelProps?: Omit transform?: { input?: (value: PathValue) => TValue[] output?: (value: TValue[]) => PathValue } defaultValue?: TValue[] } type CheckboxButtonGroupComponent = < TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, TValue = unknown, >( props: CheckboxButtonGroupProps & RefAttributes ) => ReactElement const CheckboxButtonGroup = forwardRef(function CheckboxButtonGroup< TFieldValues extends FieldValues = FieldValues, TName extends FieldPath = FieldPath, TValue = unknown, >( props: CheckboxButtonGroupProps, ref: Ref ) { const { helperText, options, label, name, parseError, required, labelKey = 'label', valueKey = 'id', returnObject, disabled, row, control, checkboxColor, rules, labelProps, transform, defaultValue = [], ...rest } = props const theme = useTheme() const errorMsgFn = useFormError() const customErrorFn = parseError || errorMsgFn const { field, fieldState: {error, invalid}, } = useController({ name, rules: required ? {required: 'This field is required'} : rules, disabled, control, defaultValue: defaultValue as PathValue, }) const {value: selectedOptions, onChange} = useTransform< TFieldValues, TName, TValue[] >({ value: field.value, onChange: field.onChange, transform: { input: typeof transform?.input === 'function' ? transform.input : (value) => { return Array.isArray(value) ? value : ([] as TValue[]) }, output: transform?.output, }, }) const handleChange = (option: unknown) => { const optionValue = propertyExists(option, valueKey) ? option[valueKey] : option const existsAtIndex = selectedOptions.findIndex((selectedOption) => { const selectedOptionValue = propertyExists(selectedOption, valueKey) ? selectedOption[valueKey] : selectedOption return optionValue === selectedOptionValue }) const newValues = ( existsAtIndex === -1 ? [...selectedOptions, option] : selectedOptions.filter((_, index) => existsAtIndex !== index) ).map((selectedOption) => returnObject || !propertyExists(selectedOption, valueKey) ? selectedOption : selectedOption[valueKey] ) as TValue[] onChange(newValues) if (typeof rest.onChange === 'function') { rest.onChange(newValues) } } const renderHelperText = error ? typeof customErrorFn === 'function' ? customErrorFn(error) : error.message : helperText return ( {label ? {label} : null} {options.map((option) => { const optionValue = propertyExists(option, valueKey) ? option[valueKey] : option const optionLabel = propertyExists(option, labelKey) ? option[labelKey] : option const isChecked = selectedOptions.some((selectedOption) => { const selectedOptionValue = propertyExists(selectedOption, valueKey) ? selectedOption[valueKey] : selectedOption return selectedOptionValue === optionValue }) return ( handleChange(option)} /> } label={optionLabel as ReactNode} key={`${optionValue}`} /> ) })} {renderHelperText && {renderHelperText}} ) }) CheckboxButtonGroup.displayName = 'CheckboxButtonGroup' export default CheckboxButtonGroup as CheckboxButtonGroupComponent