Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent table component from breaking with invalid data #1338

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't really find a way to test this. Karma didn't throw any errors and it would render an empty table.

If you have any ideas let me know.

: [];
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
Loading