Skip to content

Commit 22bef35

Browse files
committed
fix: handle falsy values on table serialization
1 parent 4a80c15 commit 22bef35

File tree

2 files changed

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

2 files changed

+56
-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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,57 @@ 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+
// First row: falsy values that should be displayed
245+
expect(firstRow.querySelectorAll('.fjs-table-td')).to.have.length(3);
246+
expect(firstRow.querySelectorAll('.fjs-table-td')[0].textContent).to.eql('0'); // 0 should display
247+
expect(firstRow.querySelectorAll('.fjs-table-td')[1].textContent).to.eql('false'); // false should display
248+
expect(firstRow.querySelectorAll('.fjs-table-td')[2].textContent).to.eql(''); // empty string should be empty
249+
250+
// Second row: null/undefined should be empty, valid string should display
251+
expect(secondRow.querySelectorAll('.fjs-table-td')).to.have.length(3);
252+
expect(secondRow.querySelectorAll('.fjs-table-td')[0].textContent).to.eql(''); // null should be empty
253+
expect(secondRow.querySelectorAll('.fjs-table-td')[1].textContent).to.eql(''); // undefined should be empty
254+
expect(secondRow.querySelectorAll('.fjs-table-td')[2].textContent).to.eql('valid'); // valid string should display
255+
});
256+
206257
it('should have pagination', async function () {
207258
// when
208259
const DATA = [

0 commit comments

Comments
 (0)