diff --git a/packages/forms/src/blueprint.ts b/packages/forms/src/blueprint.ts index 4bcfae75..442e8334 100644 --- a/packages/forms/src/blueprint.ts +++ b/packages/forms/src/blueprint.ts @@ -11,6 +11,7 @@ import { type FieldsetPattern, type PagePattern, type PageSetPattern, + type RepeaterPattern, type SequencePattern, } from './patterns'; import { type Blueprint, type FormOutput, type FormSummary } from './types'; @@ -396,45 +397,75 @@ export const copyPattern = ( return { bp: updatedBp, pattern: newPattern }; }; -export const addPatternToFieldset = ( +export const addPatternToCompoundField = ( bp: Blueprint, - fieldsetPatternId: PatternId, + patternId: PatternId, pattern: Pattern, + type: 'fieldset' | 'repeater', index?: number ): Blueprint => { - const fieldsetPattern = bp.patterns[fieldsetPatternId] as FieldsetPattern; - if (fieldsetPattern.type !== 'fieldset') { - throw new Error('Pattern is not a page.'); + const targetPattern = bp.patterns[patternId] as + | FieldsetPattern + | RepeaterPattern; + if (targetPattern.type !== type) { + throw new Error(`Pattern is not a ${type}.`); } - let updatedPagePattern: PatternId[]; - - if (index !== undefined) { - updatedPagePattern = [ - ...fieldsetPattern.data.patterns.slice(0, index + 1), - pattern.id, - ...fieldsetPattern.data.patterns.slice(index + 1), - ]; - } else { - updatedPagePattern = [...fieldsetPattern.data.patterns, pattern.id]; - } + const updatedPatterns = + index !== undefined + ? [ + ...targetPattern.data.patterns.slice(0, index + 1), + pattern.id, + ...targetPattern.data.patterns.slice(index + 1), + ] + : [...targetPattern.data.patterns, pattern.id]; return { ...bp, patterns: { ...bp.patterns, - [fieldsetPattern.id]: { - ...fieldsetPattern, + [targetPattern.id]: { + ...targetPattern, data: { - ...fieldsetPattern.data, - patterns: updatedPagePattern, + ...targetPattern.data, + patterns: updatedPatterns, }, - } satisfies FieldsetPattern, + } satisfies FieldsetPattern | RepeaterPattern, [pattern.id]: pattern, }, }; }; +export const addPatternToFieldset = ( + bp: Blueprint, + fieldsetPatternId: PatternId, + pattern: Pattern, + index?: number +): Blueprint => { + return addPatternToCompoundField( + bp, + fieldsetPatternId, + pattern, + 'fieldset', + index + ); +}; + +export const addPatternToRepeater = ( + bp: Blueprint, + repeaterPatternId: PatternId, + pattern: Pattern, + index?: number +): Blueprint => { + return addPatternToCompoundField( + bp, + repeaterPatternId, + pattern, + 'repeater', + index + ); +}; + export const addPageToPageSet = ( bp: Blueprint, pattern: Pattern diff --git a/packages/forms/src/builder/index.ts b/packages/forms/src/builder/index.ts index e4db9c4c..f31057f1 100644 --- a/packages/forms/src/builder/index.ts +++ b/packages/forms/src/builder/index.ts @@ -2,8 +2,8 @@ import { type VoidResult } from '@atj/common'; import { addPageToPageSet, addPatternToFieldset, - addPatternToRepeater, addPatternToPage, + addPatternToRepeater, copyPattern, createOnePageBlueprint, movePatternBetweenPages, diff --git a/packages/forms/src/index.ts b/packages/forms/src/index.ts index 526a63bb..9adfe2cd 100644 --- a/packages/forms/src/index.ts +++ b/packages/forms/src/index.ts @@ -29,4 +29,4 @@ export { type FormRoute, type RouteData, getRouteDataFromQueryString, -} from './route-data.js'; \ No newline at end of file +} from './route-data.js'; diff --git a/packages/forms/src/services/submit-form.test.ts b/packages/forms/src/services/submit-form.test.ts index 5dba1149..6e6726d3 100644 --- a/packages/forms/src/services/submit-form.test.ts +++ b/packages/forms/src/services/submit-form.test.ts @@ -97,11 +97,11 @@ describe('submitForm', () => { data: { errors: { 'element-1': { - message: 'Required', + message: 'Invalid input', type: 'custom', }, 'element-2': { - message: 'Required', + message: 'Invalid input', type: 'custom', }, }, @@ -297,7 +297,7 @@ describe('multi-page form', () => { data: { errors: { 'element-1': { - message: 'Required', + message: 'Invalid input', type: 'custom', }, },