Skip to content

Commit de3ebb6

Browse files
authored
fix: handle falsy values on table serialization (#1404)
1 parent 4a80c15 commit de3ebb6

File tree

2 files changed

+54
-1
lines changed
  • packages/form-js-viewer

2 files changed

+54
-1
lines changed

packages/form-js-viewer/src/render/components/form-fields/Table.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,5 +346,9 @@ function serializeCellData(cellData) {
346346
return JSON.stringify(cellData);
347347
}
348348

349-
return `${cellData || ''}`;
349+
if (cellData === null || cellData === undefined) {
350+
return '';
351+
}
352+
353+
return `${cellData}`;
350354
}

packages/form-js-viewer/test/spec/render/components/form-fields/Table.spec.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,55 @@ describe('Table', function () {
203203
expect(secondRow.querySelectorAll('.fjs-table-td')[2].textContent).to.eql('');
204204
});
205205

206+
it('should handle falsy values in table cells', function () {
207+
// when
208+
const DATA = [
209+
{
210+
id: 0,
211+
name: false,
212+
date: '',
213+
},
214+
{
215+
id: null,
216+
name: undefined,
217+
date: 'valid',
218+
},
219+
];
220+
221+
const { container } = createTable({
222+
initialData: {
223+
data: DATA,
224+
},
225+
field: {
226+
...defaultField,
227+
columns: MOCK_COLUMNS,
228+
dataSource: '=data',
229+
},
230+
services: {
231+
expressionLanguage: {
232+
isExpression: () => true,
233+
evaluate: () => DATA,
234+
},
235+
},
236+
});
237+
238+
// then
239+
const bodyRows = container.querySelectorAll('.fjs-table-body .fjs-table-tr');
240+
expect(bodyRows).to.have.length(2);
241+
242+
const [firstRow, secondRow] = bodyRows;
243+
244+
expect(firstRow.querySelectorAll('.fjs-table-td')).to.have.length(3);
245+
expect(firstRow.querySelectorAll('.fjs-table-td')[0].textContent).to.eql('0');
246+
expect(firstRow.querySelectorAll('.fjs-table-td')[1].textContent).to.eql('false');
247+
expect(firstRow.querySelectorAll('.fjs-table-td')[2].textContent).to.eql('');
248+
249+
expect(secondRow.querySelectorAll('.fjs-table-td')).to.have.length(3);
250+
expect(secondRow.querySelectorAll('.fjs-table-td')[0].textContent).to.eql('');
251+
expect(secondRow.querySelectorAll('.fjs-table-td')[1].textContent).to.eql('');
252+
expect(secondRow.querySelectorAll('.fjs-table-td')[2].textContent).to.eql('valid');
253+
});
254+
206255
it('should have pagination', async function () {
207256
// when
208257
const DATA = [

0 commit comments

Comments
 (0)