Skip to content

Commit

Permalink
Fieldset legends
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnaab committed Feb 29, 2024
1 parent c7df495 commit 45fab67
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 33 deletions.
5 changes: 2 additions & 3 deletions packages/design/src/FormManager/FormEdit/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const createPreviewComponents = (
const previewComponents: ComponentForPattern = {};
// TODO: Create a configurable way to to define preview components.
for (const [patternType, Component] of Object.entries(components)) {
if (patternType === 'sequence') {
if (patternType === 'sequence' || patternType === 'fieldset') {
previewComponents[patternType] = Component;
/*
previewComponents[patternType] = createSequencePatternPreviewComponent(
Expand All @@ -50,10 +50,9 @@ const createPreviewComponents = (
);
*/
} else if (patternType === 'form-summary') {
console.log('skipping form-summary...');
previewComponents[patternType] = Component;
} else {
previewComponents[patternType] = Component;
//previewComponents[patternType] = Component;
previewComponents[patternType] = createPatternPreviewComponent(
Component,
uswdsRoot
Expand Down
8 changes: 2 additions & 6 deletions packages/design/src/FormManager/FormEdit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ export default function FormEdit({
const form = result.data;
return (
<div className="editFormPage">
<h1>Form Editor Portal</h1>
<p className="usa-intro">
Welcome to the Form Editor Portal, where you can effortlessly
personalize your form by modifying labels, attributes, and other
settings to better suit your needs.
</p>
<h1>Edit form</h1>
<p className="usa-intro">Your form has been imported for web delivery.</p>
<PreviewContextProvider config={context.config} initialForm={form}>
<EditForm
context={context}
Expand Down
13 changes: 7 additions & 6 deletions packages/design/src/config/view/Fieldset/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import React from 'react';

import { type Pattern } from '@atj/forms';
import { type ParagraphElement } from '@atj/forms/src/config/elements/paragraph';
import { type FieldsetPattern, type Pattern } from '@atj/forms';

import { type FormElementComponent } from '../../../Form';

const FormSummary: FormElementComponent<Pattern<ParagraphElement>> = ({
const FormSummary: FormElementComponent<Pattern<FieldsetPattern>> = ({
pattern,
children,
}) => {
return (
<>
<p>{pattern.data.text}</p>
</>
<fieldset className="usa-fieldset">
<legend className="usa-legend">{pattern.legend}</legend>
{children}
</fieldset>
);
};
export default FormSummary;
3 changes: 2 additions & 1 deletion packages/design/src/config/view/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Fieldset from './Fieldset';
import FormSummary from './FormSummary';
import Paragraph from './Paragraph';
import Sequence from './Sequence';
import SubmissionConfirmation from './SubmissionConfirmation';
import TextInput from './TextInput';

import { type ComponentForPattern } from '../../Form';

export const defaultFormElementComponents: ComponentForPattern = {
fieldset: Fieldset,
'form-summary': FormSummary,
input: TextInput,
paragraph: Paragraph,
Expand Down
18 changes: 10 additions & 8 deletions packages/documents/src/pdf/mock-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { type InputElement } from '@atj/forms/src/config/elements/input';

import json from './al_name_change.json' assert { type: 'json' };
import { FieldsetElement } from '@atj/forms/src/config/elements/fieldset';

const TxInput = z.object({
input_type: z.literal('Tx'),
Expand Down Expand Up @@ -103,11 +104,11 @@ export const parseAlabamaNameChangeForm = (): ParsedPdf => {
};
const rootSequence: FormElementId[] = [];
for (const element of extracted.elements) {
const sequenceElements: FormElementId[] = [];
const fieldsetElements: FormElementId[] = [];
for (const input of element.inputs) {
if (input.input_type === 'Tx') {
const id = input.input_params.output_id.toLowerCase();
sequenceElements.push(id);
fieldsetElements.push(id);
parsedPdf.elements[id] = {
type: 'input',
id,
Expand All @@ -127,20 +128,21 @@ export const parseAlabamaNameChangeForm = (): ParsedPdf => {
};
}
}
if (sequenceElements.length === 1) {
rootSequence.push(sequenceElements[0]);
} else if (sequenceElements.length > 1) {
if (fieldsetElements.length === 1) {
rootSequence.push(fieldsetElements[0]);
} else if (fieldsetElements.length > 1) {
parsedPdf.elements[element.id] = {
id: element.id,
type: 'sequence',
type: 'fieldset',
data: {
elements: sequenceElements,
legend: element.element_params.text,
elements: fieldsetElements,
},
default: {
elements: [],
},
required: true,
};
} as FieldsetElement;
rootSequence.push(element.id);
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/forms/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type FormConfig } from '.';
import { fieldsetConfig } from './elements/fieldset';
import { inputConfig } from './elements/input';
import { paragraphConfig } from './elements/paragraph';
import { sequenceConfig } from './elements/sequence';
Expand All @@ -8,6 +9,7 @@ import { sequenceConfig } from './elements/sequence';
// understand the usage scenarios better.
export const defaultFormConfig: FormConfig = {
elements: {
fieldset: fieldsetConfig,
input: inputConfig,
paragraph: paragraphConfig,
sequence: sequenceConfig,
Expand Down
35 changes: 26 additions & 9 deletions packages/forms/src/config/elements/fieldset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ import * as z from 'zod';

import { type FormElementConfig } from '..';
import { type FormElement, type FormElementId } from '../../element';
import { type Pattern, createPromptForElement } from '../../pattern';
import {
type FieldsetPattern,
type Pattern,
createPromptForElement,
} from '../../pattern';
import { safeZodParse } from '../../util/zod';
import { getFormElement } from '../..';

export type FieldsetElement = FormElement<{
legend: string;
elements: FormElementId;
legend?: string;
elements: FormElementId[];
}>;

const fieldsetSchema = z.object({
legend: z.string(),
const FieldsetSchema = z.array(z.string());

const configSchema = z.object({
legend: z.string().optional(),
elements: z.array(z.string()),
});

Expand All @@ -21,17 +28,27 @@ export const fieldsetConfig: FormElementConfig<FieldsetElement> = {
elements: [],
},
parseData: (_, obj) => {
return safeZodParse(fieldsetSchema, obj);
return safeZodParse(FieldsetSchema, obj);
},
parseConfigData: obj => safeZodParse(configSchema, obj),
getChildren(element, elements) {
return element.data.elements.map(
(elementId: string) => elements[elementId]
);
},
createPrompt(config, session, element, options): Pattern[] {
return element.data.elements.map((elementId: string) => {
const element = session.form.elements[elementId];
createPrompt(config, session, element, options) {
const children = element.data.elements.map((elementId: string) => {
const element = getFormElement(session.form, elementId);
return createPromptForElement(config, session, element, options);
});
return {
pattern: {
_children: children,
_elementId: element.id,
type: 'fieldset',
legend: element.data.legend,
} satisfies Pattern<FieldsetPattern>,
children,
};
},
};
5 changes: 5 additions & 0 deletions packages/forms/src/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export type ParagraphPattern = {
text: string;
};

export type FieldsetPattern = {
type: 'fieldset';
legend: string;
};

export type Pattern<T = {}> = {
_elementId: FormElementId;
_children: PromptPart[];
Expand Down

0 comments on commit 45fab67

Please sign in to comment.