'use client'; import React, { useEffect, useRef, useState } from 'react'; import { TextInput, TextInputMask, Textarea, Label, DatePicker, Checkbox, Select, } from '@trussworks/react-uswds'; import { handleMapDateValidation, handleMapArrayValidation, handleChartDateValidation, } from './inputValidation'; interface FieldProps { fieldName: string; value: string; hint?: string; onChange: (value: string) => void; isRequired?: boolean; isDate?: boolean; numeric?: boolean; onBlur?: (value: string) => void; onFocus?: (value: string) => void; type?: string; componentProps: any; propName: string; customClass?: string; placeHolder?: string; draftInputs?: any; inputErrors?: any; setDraftInputs?: (value: any) => void; setInputErrors?: (value: any) => void; options?: string[]; } const checkRequired = (isRequired, value) => { return isRequired && !value ? { validationStatus: 'error' } : ''; }; const colorSchemes = [ 'Blues', 'Greens', 'Greys', 'Oranges', 'Purples', 'Reds', 'Turbo', 'Viridis', 'Inferno', 'Magma', 'Plasma', 'Cividis', 'Warm', 'Cool', 'CubehelixDefault', ]; const setInput = (props) => { const { value, isRequired, type, fieldName, hint, onChange, componentProps, propName, placeHolder, validateAgainst, draftInputs, setDraftInputs, inputErrors, setInputErrors, options, } = props; if (options && Array.isArray(options)) { return ( <> {hint} ); } const cleanedType = type !== undefined && type.toLowerCase(); const [draft, setDraft] = useState(value); const timeoutRef = useRef | null>(null); useEffect(() => { if (propName === 'dateFormat' && draft != draftInputs.draftDateFormat) { setDraftInputs({ ...draftInputs, draftDateFormat: draft }); } if ( propName === 'highlightStart' && draft != draftInputs.draftHighlightStart ) { setDraftInputs({ ...draftInputs, draftHighlightStart: draft }); } if (propName === 'highlightEnd' && draft != draftInputs.draftHighlightEnd) { setDraftInputs({ ...draftInputs, draftHighlightEnd: draft }); } clearTimeout(timeoutRef.current); timeoutRef.current = setTimeout(() => { if (validateAgainst) { if ( propName === 'dateFormat' || propName === 'highlightStart' || propName === 'highlightEnd' ) { handleChartDateValidation( propName, draftInputs, setInputErrors, inputErrors, draft, onChange, componentProps, ); } else if (validateAgainst === 'defaultDateFormat') { handleMapDateValidation( propName, draftInputs, inputErrors, setInputErrors, draft, onChange, componentProps, ); } else if (validateAgainst === 'centerFormat') { handleMapArrayValidation( propName, draftInputs, inputErrors, setInputErrors, draft, onChange, componentProps, ); } else { onChange({ ...componentProps, [propName]: draft }); } } }, 400); return () => clearTimeout(timeoutRef.current); }, [draft, draftInputs]); //Format date and submitted dates need to work or else the chart will throw an error. switch (cleanedType) { case 'date': return ( //CHORE: Need to clean up or delete <> {hint} ); break; case 'checkbox': return ( onChange({ ...componentProps, [propName]: e.target.value }) } /> ); break; case 'select': return ( <> {hint} ); break; case 'area': return ( <> {hint}