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: properly pass this variable context to dynamic list elems #1086

Merged
merged 1 commit into from
Mar 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ export class RepeatRenderManager {

return (
<>
{displayValues.map((value, index) =>
{displayValues.map((itemValue, itemIndex) =>
<RepetitionScaffold
Copy link
Contributor

Choose a reason for hiding this comment

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

(nitpick, something to keep in mind for the future)

I would have { ...restProps } at the start do that it can't override any explicitly named props in future and help avoid this issue.

React will evaluate properties in order so you can define elements like so

<SomeComponent
  overridableProperty={...}
  {...restProps}
  nonOverridableProperty={...}
/>

key={ index }
index={ index }
value={ value }
key={ itemIndex }
itemIndex={ itemIndex }
itemValue={ itemValue }
parentExpressionContextInfo={ parentExpressionContextInfo }
repeaterField={ repeaterField }
RowsRenderer={ RowsRenderer }
Expand Down Expand Up @@ -183,8 +183,8 @@ export class RepeatRenderManager {
* Individual repetition of a repeated field and context scaffolding.
*
* @param {Object} props
* @param {number} props.index
* @param {Object} props.value
* @param {number} props.itemIndex
* @param {Object} props.itemValue
* @param {Object} props.parentExpressionContextInfo
* @param {Object} props.repeaterField
* @param {Function} props.RowsRenderer
Expand All @@ -196,8 +196,8 @@ export class RepeatRenderManager {
const RepetitionScaffold = (props) => {

const {
index,
value,
itemIndex,
itemValue,
parentExpressionContextInfo,
repeaterField,
RowsRenderer,
Expand All @@ -209,15 +209,15 @@ const RepetitionScaffold = (props) => {

const elementProps = useMemo(() => ({
...restProps,
indexes: { ...(indexes || {}), [ repeaterField.id ]: index }
}), [ index, indexes, repeaterField.id, restProps ]);
indexes: { ...(indexes || {}), [ repeaterField.id ]: itemIndex }
}), [ itemIndex, indexes, repeaterField.id, restProps ]);

const localExpressionContextInfo = useMemo(() => ({
data: parentExpressionContextInfo.data,
this: value,
this: itemValue,
parent: buildExpressionContext(parentExpressionContextInfo),
i: [ ...parentExpressionContextInfo.i , index + 1 ]
}), [ index, parentExpressionContextInfo, value ]);
i: [ ...parentExpressionContextInfo.i , itemIndex + 1 ]
}), [ itemIndex, parentExpressionContextInfo, itemValue ]);

return !showRemove ?
<LocalExpressionContext.Provider value={ localExpressionContextInfo }>
Expand All @@ -229,7 +229,7 @@ const RepetitionScaffold = (props) => {
<RowsRenderer { ...elementProps } />
</LocalExpressionContext.Provider>
</div>
<button type="button" class="fjs-repeat-row-remove" aria-label={ `Remove list item ${index + 1}` } onClick={ () => onDeleteItem(index) }>
<button type="button" class="fjs-repeat-row-remove" aria-label={ `Remove list item ${itemIndex + 1}` } onClick={ () => onDeleteItem(itemIndex) }>
<div class="fjs-repeat-row-remove-icon-container">
<DeleteSvg />
</div>
Expand Down
36 changes: 36 additions & 0 deletions packages/form-js-viewer/test/spec/Form.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { CustomFormFieldsModule } from './custom';
import conditionSchema from './condition.json';
import conditionErrorsSchema from './condition-errors.json';
import conditionErrorsDynamicListSchema from './condition-errors-dynamic-list.json';
import dynamicListVariablesSchema from './dynamic-list-variables.json';
import hiddenFieldsConditionalSchema from './hidden-fields-conditional.json';
import hiddenFieldsExpressionSchema from './hidden-fields-expression.json';
import disabledSchema from './disabled.json';
Expand Down Expand Up @@ -489,6 +490,41 @@ describe('Form', function() {
});


it('should properly pass local context to dynamic list elements', async function() {

// given
const data = {
list: [
{
key: 1
},
{
key: 2
}
]
};

// when
await bootstrapForm({
container,
data,
schema: dynamicListVariablesSchema
});

// then
expect(form).to.exist;

const repeatRowContainers = container.querySelectorAll('.fjs-repeat-row-container');
expect(repeatRowContainers).to.have.length(2);

repeatRowContainers.forEach((repeatRowContainer, index) => {
const textInput = repeatRowContainer.querySelector('.fjs-form-field-text p');
expect(textInput.textContent).to.eql(data.list[index].key.toString());
});

});


const runFocusBlurTest = function(id, index, selector) {

it('focus and blur events should trigger for ' + id, async function() {
Expand Down
43 changes: 43 additions & 0 deletions packages/form-js-viewer/test/spec/dynamic-list-variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"$schema": "../../../form-json-schema/resources/schema.json",
"schemaVersion": 15,
"id": "Form_0zdct1y",
"components": [
{
"components": [
{
"text": "{{this.key}}",
"type": "text",
"layout": {
"row": "Row_0ieso1a",
"columns": null
},
"id": "Field_0qywh6n"
},
{
"label": "Number",
"type": "number",
"layout": {
"row": "Row_0ieso1a",
"columns": null
},
"id": "Field_16p25os",
"key": "key"
}
],
"showOutline": true,
"isRepeating": true,
"allowAddRemove": true,
"defaultRepetitions": 1,
"label": "Dynamic list",
"type": "dynamiclist",
"layout": {
"row": "Row_18s5yz0",
"columns": null
},
"id": "Field_1dji6mu",
"path": "list"
}
],
"type": "default"
}
Loading