// React 19 compatibility layer replacing deprecated react-form while preserving form APIs. import * as React from 'react'; export type FormValues = Record; export type FormErrors = Record; export type FormValue = any; export type RenderReturn = React.ReactNode; export type ValidateValuesFunction = (values: FormValues) => FormErrors; // Legacy type aliases export type Nested = T; export type FormFunctionProps = FormApi; export interface FieldApi { getValue(): any; setValue(value: any): void; setTouched(touched: boolean): void; } export interface FormState { values: FormValues; touched: Record; errors: FormErrors; } export interface FormApi { values: FormValues; touched: Record; errors: FormErrors; submitForm(e?: React.SyntheticEvent | any): void; setError(field: Path, error: any): void; getFormState(): FormState; setFormState(state: Partial): void; setAllValues(values: FormValues): void; setValue(field: Path, value: any): void; setTouched(field: Path, touched: boolean): void; resetAll(): void; } export interface FieldProps { field: Path; formApi?: FormApi; qeid?: string; } interface FormProps { defaultValues?: FormValues; validateError?: ValidateValuesFunction; onSubmit?: (vals: FormValues, event?: React.SyntheticEvent | any, api?: FormApi) => any; onSubmitFailure?: (errors: FormErrors) => any; preSubmit?: (vals: FormValues) => FormValues; getApi?: (api: FormApi) => void; formDidUpdate?: (state: FormState) => any; children: (api: FormApi) => RenderReturn; } const FormContext = React.createContext(null); // Field paths accept the same forms as react-form: // - dotted strings: 'spec.source.repoURL' // - bracket-indexed strings: 'resources[0]', 'spec.destinations[0].server' // - array of keys: ['window.applications', 0] (dotted entries are still split) export type Path = string | Array; function parsePath(path: Path): Array { const segments = Array.isArray(path) ? path : [path]; const keys: Array = []; for (const segment of segments) { if (typeof segment === 'number') { keys.push(segment); continue; } // Split 'a.b[0].c' / 'a.b' / 'a[0][1]' into individual keys. let buf = ''; for (let i = 0; i < segment.length; i++) { const ch = segment[i]; if (ch === '.') { if (buf) { keys.push(buf); buf = ''; } } else if (ch === '[') { if (buf) { keys.push(buf); buf = ''; } const end = segment.indexOf(']', i + 1); const idx = segment.slice(i + 1, end === -1 ? segment.length : end); const n = Number(idx); keys.push(Number.isFinite(n) && /^\d+$/.test(idx) ? n : idx); i = end === -1 ? segment.length : end; } else { buf += ch; } } if (buf) { keys.push(buf); } } return keys; } function cloneContainer(value: any, nextKey: string | number): any { const wantsArray = typeof nextKey === 'number'; if (Array.isArray(value)) { return value.slice(); } if (value != null && typeof value === 'object') { return {...value}; } return wantsArray ? [] : {}; } function deepSet(obj: Record, path: Path, value: any): Record { const keys = parsePath(path); if (keys.length === 0) return obj; const rootIsArray = Array.isArray(obj); const result: any = rootIsArray ? (obj as any).slice() : {...obj}; let cursor: any = result; for (let i = 0; i < keys.length - 1; i++) { const key = keys[i]; const nextKey = keys[i + 1]; cursor[key] = cloneContainer(cursor[key], nextKey); cursor = cursor[key]; } cursor[keys[keys.length - 1]] = value; return result; } function deepGet(obj: Record, path: Path): any { const keys = parsePath(path); let cursor: any = obj; for (const key of keys) { if (cursor == null) return undefined; cursor = cursor[key]; } return cursor; } function withFieldApi( Component: React.ComponentType, ): React.ComponentType { return function FieldConnectedComponent(props: any) { const contextApi = React.useContext(FormContext); const propApi = (props as {formApi?: FormApi}).formApi; const formApi = propApi || contextApi; const field = (props as {field: Path}).field; if (!formApi) { throw new Error('FormField components must be used inside
or be passed formApi'); } const fieldApi: FieldApi = { getValue: () => deepGet(formApi.values, field), setValue: (value) => { formApi.setValue(field, value); }, setTouched: (touched) => { formApi.setTouched(field, touched); }, }; return ; }; } export function FormField( component: React.ComponentType, ): React.ComponentType { return withFieldApi(component); } export const Text = FormField((props: React.InputHTMLAttributes & FieldProps & {fieldApi: FieldApi}) => { const {fieldApi, onBlur, onChange, field: _field, formApi: _formApi, ...rest} = props as any; const value = fieldApi.getValue() ?? ''; return ( ) => { fieldApi.setValue(event.currentTarget.value); if (onChange) { onChange(event); } }} onBlur={(event: React.FocusEvent) => { fieldApi.setTouched(true); if (onBlur) { onBlur(event); } }} /> ); }); export const TextArea = FormField((props: React.TextareaHTMLAttributes & FieldProps & {fieldApi: FieldApi}) => { const {fieldApi, onBlur, onChange, field: _field, formApi: _formApi, ...rest} = props as any; const value = fieldApi.getValue() ?? ''; return (