import type { TGetFormInitialValues, TFormValue, TFormValues } from "../config/types"; /** * Получает начальные значения формы * @returns {TFormValues} */ export const getFormInitialValues = ({ formRows = [], }: TGetFormInitialValues): TFormValues => { type TFieldLike = { customComponent?: { props?: { name?: string; value?: unknown; }; }; input?: { name?: string; value?: unknown; }; }; const getData = (fromArr: TFieldLike[]) => { return fromArr .map(({ customComponent, input }) => { const { name, value = "" } = customComponent?.props || input || {}; return [ name, value as TFormValue ]; }) .filter((entry): entry is [ string, TFormValue ] => !!entry[0]); }; const fields = formRows.reduce((acc, { cols = [] }) => { return [ ...acc, ...cols.map(({ field }) => ({ ...field })) ]; }, [] as TFieldLike[]); return Object.fromEntries(getData(fields)) as TFormValues; };