import React from 'react'; import type { IFormInputProps, OmitControlledInputPropsFrom } from '../../presentation'; import { createFakeReactSyntheticEvent, orEmptyString, validationClassName } from '../../presentation'; const { useEffect } = React; interface IWhenChecklistInputProps extends IFormInputProps, OmitControlledInputPropsFrom> { options?: IWhenChecklistInputOption[]; } interface IWhenChecklistInputOption { label: string; value: string; additionalFields?: React.ReactNode; } interface ICheckBoxProps { option: IWhenChecklistInputOption; selected: string[]; onChange: (event: React.ChangeEvent) => any; inputClassName: string; inputProps: any; } function CheckBox({ option, selected, onChange, inputClassName, inputProps }: ICheckBoxProps) { function handleChange({ target }: React.ChangeEvent) { const newValue = !selected.includes(target.value) ? selected.concat(target.value) : selected.filter((v: string) => v !== target.value); onChange(createFakeReactSyntheticEvent({ value: newValue, name: target.name })); } return ( <> {selected.includes(option.value) && option.additionalFields} ); } export function WhenChecklistInput(props: IWhenChecklistInputProps) { const { value, validation, inputClassName, options, onChange, ...inputProps } = props; // Naively call the the field's onBlur handler // This is what Formik uses to mark the field as touched function touchField() { props.onBlur && props.onBlur(createFakeReactSyntheticEvent({ name: props.name, value })); } useEffect(touchField, []); const selectedValues: string[] = value || []; return (
    {options.map((option: IWhenChecklistInputOption) => (
  • ))}
); }