From 8e2c9ca797555293f902466c507c09a1be41fe3e Mon Sep 17 00:00:00 2001 From: Vinicius Goulart Date: Thu, 25 Jan 2024 13:11:38 -0300 Subject: [PATCH] fix: Prevent demo data from being created on edited tables --- packages/form-js-playground/rollup.config.js | 3 +- .../src/components/PlaygroundRoot.js | 10 +++- .../test/spec/Playground.spec.js | 49 ++++++++++++++++++- .../render/components/form-fields/Table.js | 45 ++++++++++++----- 4 files changed, 90 insertions(+), 17 deletions(-) diff --git a/packages/form-js-playground/rollup.config.js b/packages/form-js-playground/rollup.config.js index 8ace6dee3..4e0705e84 100644 --- a/packages/form-js-playground/rollup.config.js +++ b/packages/form-js-playground/rollup.config.js @@ -60,7 +60,8 @@ export default [ '@codemirror/language', 'codemirror', 'classnames', - 'min-dom' + 'min-dom', + 'min-dash' ], onwarn }, diff --git a/packages/form-js-playground/src/components/PlaygroundRoot.js b/packages/form-js-playground/src/components/PlaygroundRoot.js index 94e1c449c..910e7449c 100644 --- a/packages/form-js-playground/src/components/PlaygroundRoot.js +++ b/packages/form-js-playground/src/components/PlaygroundRoot.js @@ -1,5 +1,5 @@ import { useRef, useEffect, useState, useCallback } from 'preact/hooks'; - +import { isFunction } from 'min-dash'; import download from 'downloadjs'; import classNames from 'classnames'; @@ -142,9 +142,15 @@ export function PlaygroundRoot(props) { formEditor.on('formField.add', ({ formField }) => { const formFields = formEditor.get('formFields'); const { config } = formFields.get(formField.type); - const { initialDemoData } = config; + const { generateInitialDemoData } = config; const { id } = formField; + if (!isFunction(generateInitialDemoData)) { + return; + } + + const initialDemoData = generateInitialDemoData(formField); + if ([ initialDemoData, id ].includes(undefined)) { return; } diff --git a/packages/form-js-playground/test/spec/Playground.spec.js b/packages/form-js-playground/test/spec/Playground.spec.js index d8a4bfdba..cfee923e2 100644 --- a/packages/form-js-playground/test/spec/Playground.spec.js +++ b/packages/form-js-playground/test/spec/Playground.spec.js @@ -170,8 +170,21 @@ describe('playground', function() { const attrs = { id: 'table', type: 'table', - dataSource: 'people', - columnsExpression: 'peopleColumns' + dataSource: '=table', + columns: [ + { + key: 'id', + label: 'ID' + }, + { + key: 'name', + label: 'Name' + }, + { + key: 'date', + label: 'Date' + } + ] }; await act(() => { @@ -203,6 +216,38 @@ describe('playground', function() { ]); }); + it('should not append sample data', async function() { + + // given + const attrs = { + id: 'table', + type: 'table', + dataSource: '=table', + columnsExpression: '=peopleColumns' + }; + + await act(() => { + playground = new Playground({ + container, + schema + }); + }); + + const editor = playground.getEditor(); + const modeling = editor.get('modeling'); + + // when + await act(() => { + const { schema } = editor._getState(); + modeling.addFormField(attrs, schema, 0); + }); + + // then + const dataEditor = playground.getDataEditor(); + + expect(dataEditor.getValue()).to.be.empty; + }); + it('should NOT attach to empty parent', async function() { diff --git a/packages/form-js-viewer/src/render/components/form-fields/Table.js b/packages/form-js-viewer/src/render/components/form-fields/Table.js index a751b5733..197ae13d8 100644 --- a/packages/form-js-viewer/src/render/components/form-fields/Table.js +++ b/packages/form-js-viewer/src/render/components/form-fields/Table.js @@ -23,14 +23,16 @@ const type = 'table'; * @property {string} label * @property {string} key * + * @typedef Field + * @property {string} id + * @property {Array} [columns] + * @property {string} [columnsExpression] + * @property {string} [label] + * @property {number} [rowCount] + * @property {string} [dataSource] + * * @typedef Props - * @property {Object} field - * @property {string} field.id - * @property {Array} [field.columns] - * @property {string} [field.columnsExpression] - * @property {string} [field.label] - * @property {number} [field.rowCount] - * @property {string} [field.dataSource] + * @property {Field} field * * @param {Props} props * @returns {import("preact").JSX.Element} @@ -263,11 +265,30 @@ Table.config = { ], }; }, - initialDemoData: [ - { id: 1, name: 'John Doe', date: '31.01.2023' }, - { id: 2, name: 'Erika Muller', date: '20.02.2023' }, - { id: 3, name: 'Dominic Leaf', date: '11.03.2023' } - ], + + /** + * + * @param {Field} field + */ + generateInitialDemoData: (field) => { + const demoData = [ + { id: 1, name: 'John Doe', date: '31.01.2023' }, + { id: 2, name: 'Erika Muller', date: '20.02.2023' }, + { id: 3, name: 'Dominic Leaf', date: '11.03.2023' } + ]; + const demoDataKeys = Object.keys(demoData[0]); + const { columns, id, dataSource } = field; + + if (!Array.isArray(columns) || columns.length === 0 || dataSource !== `=${id}`) { + return; + } + + if (!columns.map(({ key })=>key).every(key => demoDataKeys.includes(key))) { + return; + } + + return demoData; + } }; // helpers /////////////////////////////