Skip to content

Commit

Permalink
fix: prevent table component from breaking with invalid data
Browse files Browse the repository at this point in the history
  • Loading branch information
vsgoulart committed Dec 19, 2024
1 parent 2787373 commit 910f8e2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
26 changes: 16 additions & 10 deletions packages/form-js-viewer/src/render/components/form-fields/Table.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDefined, isNumber, isObject, isString } from 'min-dash';
import { isDefined, isNil, isNumber, isObject, isString } from 'min-dash';
import { useExpressionEvaluation } from '../../hooks';
import { useEffect, useState } from 'preact/hooks';
import { formFieldClasses, prefixId } from '../Util';
Expand Down Expand Up @@ -45,7 +45,9 @@ export function Table(props) {
const evaluatedColumns = useEvaluatedColumns(columnsExpression || '', columns);
const columnKeys = evaluatedColumns.map(({ key }) => key);
const evaluatedDataSource = useExpressionEvaluation(dataSource);
const data = Array.isArray(evaluatedDataSource) ? evaluatedDataSource.filter((i) => i !== undefined) : [];
const data = Array.isArray(evaluatedDataSource)
? evaluatedDataSource.filter((entry) => !isNil(entry) || typeof entry !== 'object')
: [];
const sortedData = sortBy === null ? data : sortByColumn(data, sortBy.key, sortBy.direction);

/** @type {unknown[][]} */
Expand All @@ -57,14 +59,6 @@ export function Table(props) {
setCurrentPage(0);
}, [rowCount, sortBy]);

const serializeCellData = (cellData) => {
if (cellData !== null && typeof cellData === 'object') {
return JSON.stringify(cellData);
}

return cellData;
};

/** @param {string} key */
function toggleSortBy(key) {
setSortBy((current) => {
Expand Down Expand Up @@ -342,3 +336,15 @@ function getHeaderAriaLabel(sortBy, key, label) {

return `Click to sort by ${label} ascending`;
}

/**
* @param {unknown} cellData
* @returns string
*/
function serializeCellData(cellData) {
if (cellData !== null && typeof cellData === 'object') {
return JSON.stringify(cellData);
}

return `${cellData || ''}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ describe('DocumentPreview', function () {
// when
const field = config.create();

console.log({ field });

// then
expect(field).to.eql({
label: 'Document preview',
Expand Down

0 comments on commit 910f8e2

Please sign in to comment.