Skip to content

Commit

Permalink
Missing radio groups (#167)
Browse files Browse the repository at this point in the history
* checkboxes working, radios partially working

* remove comments and debug vars

* remove superfluous linebreak

* log error

* comment
  • Loading branch information
jimmoffet authored Jun 6, 2024
1 parent ae80c3b commit c436579
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
20 changes: 17 additions & 3 deletions packages/forms/src/documents/pdf/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ export const createFormOutputFieldData = (
const results = {} as Record<string, { value: any; type: PDFFieldType }>;
Object.entries(output.fields).forEach(([patternId, docField]) => {
if (docField.type === 'not-supported') {
console.error(`unsupported field: ${patternId}: ${docField}`);
return;
}
const outputFieldId = output.formFields[patternId];
if (outputFieldId === '') {
console.error(`empty outputFieldId for patternId: ${patternId}`);
console.error(`empty outputFieldId for field: ${patternId}: ${docField}`);
return;
}
results[outputFieldId] = {
Expand Down Expand Up @@ -71,8 +72,21 @@ const setFormFieldData = (
const field = form.getDropdown(fieldName);
field.select(fieldValue);
} else if (fieldType === 'RadioGroup') {
const field = form.getRadioGroup(fieldName);
field.select(fieldValue);
// TODO: remove this when we have a better way to handle radio groups
try {
const field = form.getRadioGroup(fieldName);
field.select(fieldValue);
} catch (error: any) {
console.error(
`error setting radio field: ${fieldName}: ${error.message}`
);
const field = form.getCheckBox(fieldName);
if (fieldValue) {
field.check();
} else {
field.uncheck();
}
}
} else if (fieldType === 'Paragraph') {
// do nothing
} else {
Expand Down
33 changes: 27 additions & 6 deletions packages/forms/src/documents/pdf/parsing-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export const processApiResponse = async (json: any): Promise<ParsedPdf> => {
}

if (element.component_type === 'checkbox') {
const checkbox = processPatternData<CheckboxPattern>(
const checkboxPattern = processPatternData<CheckboxPattern>(
defaultFormConfig,
parsedPdf,
'checkbox',
Expand All @@ -225,19 +225,27 @@ export const processApiResponse = async (json: any): Promise<ParsedPdf> => {
defaultChecked: element.default_checked,
}
);
if (checkbox) {
rootSequence.push(checkbox.id);
if (checkboxPattern) {
rootSequence.push(checkboxPattern.id);
parsedPdf.outputs[checkboxPattern.id] = {
type: 'CheckBox',
name: element.id,
label: element.label,
value: false,
required: true,
};
}
continue;
}

if (element.component_type === 'radio_group') {
const radioGroup = processPatternData<RadioGroupPattern>(
const radioGroupPattern = processPatternData<RadioGroupPattern>(
defaultFormConfig,
parsedPdf,
'radio-group',
{
label: element.legend,
// outputId: element.id,
options: element.options.map(option => ({
id: option.id,
label: option.label,
Expand All @@ -246,8 +254,21 @@ export const processApiResponse = async (json: any): Promise<ParsedPdf> => {
})),
}
);
if (radioGroup) {
rootSequence.push(radioGroup.id);
if (radioGroupPattern) {
rootSequence.push(radioGroupPattern.id);
parsedPdf.outputs[radioGroupPattern.id] = {
type: 'RadioGroup',
name: element.id,
label: element.legend,
options: element.options.map(option => ({
id: option.id,
label: option.label,
name: option.name,
defaultChecked: option.default_checked,
})),
value: '',
required: true,
};
}
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/documents/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type DocumentFieldValue =
| {
type: 'RadioGroup';
name: string;
options: string[];
options: any[];
label: string;
value: string;
required: boolean;
Expand Down

0 comments on commit c436579

Please sign in to comment.