Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

working radio button mapping, ignore empty non-required fields #332

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"tsup": "^8.2.4",
"turbo": "^2.0.14",
"typescript": "^5.5.4",
"vitest": "^2.0.5",
"vitest": "^2.1.2",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's bug in 2.0.5 that prevents the ! exclusion in the workspace config from working properly

vitest-dev/vitest#6499

"vitest-mock-extended": "^2.0.0"
}
}
}
28 changes: 20 additions & 8 deletions packages/forms/src/documents/pdf/generate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { PDFDocument, type PDFForm } from 'pdf-lib';
import {
PDFDocument,
PDFName,
createPDFAcroFields,
type PDFForm,
} from 'pdf-lib';

import { Result } from '@atj/common';
import { type FormOutput } from '../../index.js';
Expand Down Expand Up @@ -119,14 +124,21 @@ const setFormFieldData = (
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();
// This logic should work even if pdf-lib misidentifies the field type
// TODO: radioParent should contain the name, not the id
const [radioParent, radioChild] = fieldValue.split('.');
if (radioChild) {
// TODO: resolve import failure when spaces are present in name, id
const radioChildWithSpace = radioChild.replace('_', ' ');
const field = form.getField(fieldName);
const acroField = field.acroField;
acroField.dict.set(PDFName.of('V'), PDFName.of(radioChildWithSpace));
const kids = createPDFAcroFields(acroField.Kids()).map(_ => _[0]);
kids.forEach(kid => {
kid.dict.set(PDFName.of('AS'), PDFName.of(radioChildWithSpace));
});
} else {
field.uncheck();
// do nothing
}
}
} else if (fieldType === 'Paragraph' || fieldType === 'RichText') {
Expand Down
5 changes: 2 additions & 3 deletions packages/forms/src/documents/pdf/parsing-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const RadioGroupOption = z.object({
});

const RadioGroup = z.object({
// id: z.string(),
id: z.string(),
component_type: z.literal('radio_group'),
legend: z.string(),
options: RadioGroupOption.array(),
Expand Down Expand Up @@ -238,7 +238,7 @@ export const processApiResponse = async (json: any): Promise<ParsedPdf> => {
pagePatterns[element.page] = (pagePatterns[element.page] || []).concat(
radioGroupPattern.id
);
/*

parsedPdf.outputs[radioGroupPattern.id] = {
type: 'RadioGroup',
name: element.id,
Expand All @@ -252,7 +252,6 @@ export const processApiResponse = async (json: any): Promise<ParsedPdf> => {
value: '',
required: true,
};
*/
}
continue;
}
Expand Down
9 changes: 9 additions & 0 deletions packages/forms/src/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ export const validatePattern = (
}
const parseResult = patternConfig.parseUserInput(pattern, value);
if (!parseResult.success) {
if ('required' in pattern.data) {
if (pattern.data.required === false && !value) {
console.log('Parse result failed for blank input that is not required');
return {
success: true,
data: value,
};
}
}
return {
success: false,
error: parseResult.error,
Expand Down
Loading
Loading