Skip to content

Commit

Permalink
Add any empty check (null/blank valus) to conditional rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadsw committed Sep 24, 2024
1 parent 58a2573 commit 8175e07
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ const cellRenderers: {
const watchFields = f.unique(
filterArray(
definitions.map(({ condition }) =>
condition?.type === 'Value' ? condition?.field[0].name : undefined
condition?.type !== 'Always' ? condition?.field[0].name : undefined
)
)
);
Expand Down
10 changes: 10 additions & 0 deletions specifyweb/frontend/js_src/lib/components/FormEditor/viewSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,15 @@ const rowsSpec = (table: SpecifyTable | undefined) =>
if (isAlways) return { type: 'Always' } as const;
const [rawField, ...condition] = rawCondition.split('=');
const field = syncers.field(table?.name).serializer(rawField);
const isEmpty = condition.at(-1) === 'EMPTY';
return field === undefined
? undefined
: isEmpty
? ({
type: 'Empty',
field,
value: '',
} as const)
: ({
type: 'Value',
field,
Expand All @@ -154,8 +161,11 @@ const rowsSpec = (table: SpecifyTable | undefined) =>
if (props.type === 'Always') return localized('always');
const { field, value } = props;
const joined = syncers.field(table?.name).deserializer(field);
const isEmpty = props.type === 'Empty';
return joined === undefined || joined.length === 0
? undefined
: isEmpty
? localized(`${joined}=EMPTY`)
: localized(`${joined}=${value}`);
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ const conditionalTinyFormView = strictParseXml(
<cell colSpan="4" />
</row>
</rows>
<rows condition="cataloger=EMPTY" colDef="2px,4px,1px">
<row>
<cell colSpan="4" />
</row>
</rows>
</cell>`
);
const parsedConditionalTinyView: ParsedFormDefinition = {
Expand Down Expand Up @@ -444,7 +449,7 @@ describe('parseFormDefinition', () => {
formDefinition.map(({ condition, ...rest }) => ({
...rest,
condition:
condition === undefined || condition.type !== 'Value'
condition === undefined || condition.type === 'Always'
? condition
: {
...condition,
Expand All @@ -465,6 +470,14 @@ describe('parseFormDefinition', () => {
},
definition: parsedConditionalTinyView,
},
{
condition: {
type: 'Empty',
field: ['cataloger'],
value: '',
},
definition: parsedConditionalTinyView,
},
]);
});
});
Expand Down
32 changes: 24 additions & 8 deletions specifyweb/frontend/js_src/lib/components/FormParse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,13 @@ export type FormCondition =
readonly value: string;
}
>
| State<
'Empty',
{
readonly field: RA<LiteralField | Relationship>;
readonly value: '';
}
>
| State<'Always'>
| undefined;

Expand Down Expand Up @@ -508,14 +515,23 @@ export async function parseFormDefinition(
const value = condition.slice(1).join('=');
const parsedField = table.getFields(condition[0]);
if (Array.isArray(parsedField)) {
return {
condition: {
type: 'Value',
field: parsedField,
value,
},
definition,
} as const;
return value === 'EMPTY'
? ({
condition: {
type: 'Empty',
field: parsedField,
value: '',
},
definition,
} as const)
: ({
condition: {
type: 'Value',
field: parsedField,
value,
},
definition,
} as const);
}
}

Expand Down

0 comments on commit 8175e07

Please sign in to comment.