Skip to content

Commit

Permalink
Merge pull request #4911 from specify/issue-4910
Browse files Browse the repository at this point in the history
Handle case when `row` element contains no children
  • Loading branch information
melton-jason committed May 13, 2024
2 parents 3fb636e + 131ad7b commit 2ddbff3
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {
parseFormTableColumns,
getColumnDefinitions,
getColumnDefinition,
parseRows,
} = exportsForTests;

requireContext();
Expand Down Expand Up @@ -529,3 +530,105 @@ theories(getColumnDefinition, [
out: 'B',
},
]);

const testRows = `
<viewdef>
<rows>
<row>
<cell type="label" labelFor="tt" label="test" />
</row>
<row>
<cell type="field" name=" stationFieldNumber " uiType="text" colSpan="4" align="right" id="tt" />
<cell type="field" name="collectingTrip.text1" uiType="checkbox" label="2" colSpan="1" align="right" />
</row>
<row>
</row>
<row>
<cell type="subview" id="dt" viewname="Collectors" name="collectors"/>
</row>
</rows>
</viewdef>`;

test('parseRows', async () => {
const viewDef = xml(testRows);
const rowsContainer = viewDef.children.rows[0];
const rawRows = rowsContainer?.children?.row ?? [];

await expect(parseRows(rawRows, tables.CollectingEvent)).resolves.toEqual([
[
{
align: 'right',
ariaLabel: undefined,
colSpan: 1,
fieldNames: undefined,
id: undefined,
labelForCellId: 'tt',
text: 'test',
title: undefined,
type: 'Label',
verticalAlign: 'center',
visible: true,
},
],
[
{
align: 'left',
ariaLabel: undefined,
colSpan: 2,
fieldDefinition: {
defaultValue: undefined,
isReadOnly: false,
max: undefined,
maxLength: undefined,
min: undefined,
minLength: undefined,
step: undefined,
type: 'Text',
},
fieldNames: ['stationFieldNumber'],
id: 'tt',
isRequired: false,
type: 'Field',
verticalAlign: 'center',
visible: true,
},
{
align: 'left',
ariaLabel: undefined,
colSpan: 1,
fieldDefinition: {
defaultValue: undefined,
isReadOnly: false,
label: '2',
printOnSave: false,
type: 'Checkbox',
},
fieldNames: ['collectingTrip', 'text1'],
id: undefined,
isRequired: false,
type: 'Field',
verticalAlign: 'center',
visible: true,
},
],
[],
[
{
align: 'left',
ariaLabel: undefined,
colSpan: 1,
fieldNames: ['collectors'],
formType: 'formTable',
icon: undefined,
id: 'dt',
isButton: false,
isCollapsed: false,
sortField: undefined,
type: 'SubView',
verticalAlign: 'stretch',
viewName: 'Collectors',
visible: true,
},
],
]);
});
61 changes: 34 additions & 27 deletions specifyweb/frontend/js_src/lib/components/FormParse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,33 +494,7 @@ export async function parseFormDefinition(
? getColumnDefinitions(viewDefinition)
: directColumnDefinitions
),
await Promise.all(
rows.map(async (row, index) => {
const context = getLogContext();
pushContext({
type: 'Child',
tagName: 'row',
extras: { row: index + 1 },
});

const data = await Promise.all(
row.children.cell?.map(async (cell, index) => {
const context = getLogContext();
pushContext({
type: 'Child',
tagName: 'cell',
extras: { cell: index + 1 },
});
const data = await parseFormCell(table, cell);

setLogContext(context);
return data;
})
);
setLogContext(context);
return data ?? [];
})
),
await parseRows(rows, table),
table
);

Expand Down Expand Up @@ -571,6 +545,38 @@ const getColumnDefinition = (
typeof os === 'string' ? getParsedAttribute(child, 'os') === os : true
)?.text;

const parseRows = async (
rawRows: RA<SimpleXmlNode>,
table: SpecifyTable
): Promise<RA<RA<FormCellDefinition>>> =>
Promise.all(
rawRows.map(async (row, index) => {
const context = getLogContext();
pushContext({
type: 'Child',
tagName: 'row',
extras: { row: index + 1 },
});

const data = await Promise.all(
(row.children.cell ?? []).map(async (cell, index) => {
const context = getLogContext();
pushContext({
type: 'Child',
tagName: 'cell',
extras: { cell: index + 1 },
});
const data = await parseFormCell(table, cell);

setLogContext(context);
return data;
})
);
setLogContext(context);
return data ?? [];
})
);

export const exportsForTests = {
views,
parseViewDefinitions,
Expand All @@ -579,4 +585,5 @@ export const exportsForTests = {
parseFormTableColumns,
getColumnDefinitions,
getColumnDefinition,
parseRows,
};

0 comments on commit 2ddbff3

Please sign in to comment.