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 demo data from being created on edited tables #1005

Merged
merged 2 commits into from
Jan 30, 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
3 changes: 2 additions & 1 deletion packages/form-js-playground/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export default [
'@codemirror/language',
'codemirror',
'classnames',
'min-dom'
'min-dom',
'min-dash'
],
onwarn
},
Expand Down
10 changes: 8 additions & 2 deletions packages/form-js-playground/src/components/PlaygroundRoot.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -142,9 +142,15 @@
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;
}
Expand Down Expand Up @@ -203,7 +209,7 @@
form.destroy();
formEditor.destroy();
};
}, []);

Check warning on line 212 in packages/form-js-playground/src/components/PlaygroundRoot.js

View workflow job for this annotation

GitHub Actions / Build (macos-latest, 20)

React Hook useEffect has missing dependencies: 'additionalModules', 'data', 'editorAdditionalModules', 'editorProperties', 'emit', 'exporterConfig', 'propertiesPanelConfig', 'resultData', 'viewerAdditionalModules', and 'viewerProperties'. Either include them or remove the dependency array

Check warning on line 212 in packages/form-js-playground/src/components/PlaygroundRoot.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-20.04, 20)

React Hook useEffect has missing dependencies: 'additionalModules', 'data', 'editorAdditionalModules', 'editorProperties', 'emit', 'exporterConfig', 'propertiesPanelConfig', 'resultData', 'viewerAdditionalModules', and 'viewerProperties'. Either include them or remove the dependency array

Check warning on line 212 in packages/form-js-playground/src/components/PlaygroundRoot.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 20)

React Hook useEffect has missing dependencies: 'additionalModules', 'data', 'editorAdditionalModules', 'editorProperties', 'emit', 'exporterConfig', 'propertiesPanelConfig', 'resultData', 'viewerAdditionalModules', and 'viewerProperties'. Either include them or remove the dependency array

Check warning on line 212 in packages/form-js-playground/src/components/PlaygroundRoot.js

View workflow job for this annotation

GitHub Actions / Build (macos-latest, 20)

React Hook useEffect has missing dependencies: 'additionalModules', 'data', 'editorAdditionalModules', 'editorProperties', 'emit', 'exporterConfig', 'propertiesPanelConfig', 'resultData', 'viewerAdditionalModules', and 'viewerProperties'. Either include them or remove the dependency array

Check warning on line 212 in packages/form-js-playground/src/components/PlaygroundRoot.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-20.04, 20)

React Hook useEffect has missing dependencies: 'additionalModules', 'data', 'editorAdditionalModules', 'editorProperties', 'emit', 'exporterConfig', 'propertiesPanelConfig', 'resultData', 'viewerAdditionalModules', and 'viewerProperties'. Either include them or remove the dependency array

Check warning on line 212 in packages/form-js-playground/src/components/PlaygroundRoot.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 20)

React Hook useEffect has missing dependencies: 'additionalModules', 'data', 'editorAdditionalModules', 'editorProperties', 'emit', 'exporterConfig', 'propertiesPanelConfig', 'resultData', 'viewerAdditionalModules', and 'viewerProperties'. Either include them or remove the dependency array

useEffect(() => {
dataEditorRef.current.setValue(toString(initialData));
Expand Down
49 changes: 47 additions & 2 deletions packages/form-js-playground/test/spec/Playground.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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() {

Expand Down
47 changes: 35 additions & 12 deletions packages/form-js-viewer/src/render/components/form-fields/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ const type = 'table';
* @property {string} label
* @property {string} key
*
* @typedef Field
* @property {string} id
* @property {Array<Column>} [columns]
* @property {string} [columnsExpression]
* @property {string} [label]
* @property {number} [rowCount]
* @property {string} [dataSource]
*
* @typedef Props
* @property {Object} field
* @property {string} field.id
* @property {Array<Column>} [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}
Expand Down Expand Up @@ -263,11 +265,32 @@ 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' }
],

/**
* @experimental
*
* A function that generates demo data for a new field on the form playground.
* @param {Field} field
*/
generateInitialDemoData: (field) => {
Copy link
Contributor

@Skaiir Skaiir Jan 29, 2024

Choose a reason for hiding this comment

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

Generally, let's be careful with these kinds of changes. Our config object is tied in with the custom component framework and should be treated more or less as public API. Of course we're never obligated but changing how it's structured is something we should not do often.

I highly doubt anyone was using this already, considering how unique this stuff is, but this a breaking change if we were very strict about it.

Maybe it's worth annotating this as experimental, so that we can be safe in the future if we do decide to remove it entirely (which I believe will happen when we have more robust ways of doing demo data).

Copy link
Contributor

Choose a reason for hiding this comment

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

Or moving it to an experimental sub-object, which should get the point across 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Skaiir I added the @experimental tag

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 /////////////////////////////
Expand Down
Loading