import { Box, Flex, Stack, } from '@chakra-ui/react'; import { ListCheckBox } from 'components/list-check-box'; import { forwardRef, ReactNode } from 'react'; import { useFormContext, } from 'react-hook-form'; import { Service } from 'types'; interface ServicesSelectListProps { list: Service[]; renderCheckboxContent: (service: Service) => ReactNode; } export const ServicesSelectList = forwardRef( function({ list, renderCheckboxContent }, ref) { const form = useFormContext(); const handleServiceToggle = (serviceId: number) => { const currentServices = form.getValues('services'); const isSelected = currentServices.includes(serviceId); let updatedServices; if (isSelected) { updatedServices = currentServices.filter((id: number) => id !== serviceId); } else { updatedServices = [...currentServices, serviceId]; } form.setValue('services', updatedServices, { shouldValidate: true }); }; return ( {list.map((service: Service) => ( handleServiceToggle(service.id)} value={String(service.id)} renderContent={() => renderCheckboxContent(service)} /> ))} ); } );