Skip to content
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
7 changes: 7 additions & 0 deletions .changeset/bitter-seas-strive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@getodk/xforms-engine': patch
'@getodk/scenario': patch
'@getodk/web-forms': patch
---

Allow for multiple setvalue actions to reference the same element
30 changes: 29 additions & 1 deletion packages/scenario/test/actions-events/set-value-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ describe('setvalue action', () => {
});

describe('region repeats', () => {
describe('[`setvalue`] source in repeat', () => {
describe('`setvalue` source in repeat', () => {
// ported from: https://github.com/getodk/javarosa/blob/2dd8e15e9f3110a86f8d7d851efc98627ae5692e/src/test/java/org/javarosa/core/model/actions/SetValueActionTest.java#L251
it('updates destination in the same repeat instance', async () => {
const scenario = await Scenario.init(
Expand Down Expand Up @@ -728,4 +728,32 @@ describe('setvalue action', () => {
expect(newInstance.attributeOf('/data/element', 'attr').getValue()).toBe('7');
});
});

it('allows multiple `setvalue` elements with the same `ref`', async () => {
const scenario = await Scenario.init(
'Setvalue multiple',
html(
head(
title('Setvalue multiple'),
model(
mainInstance(t('data id="setvalue-multiple"', t('repeat id=""', t('source')))),
setvalueLiteral('odk-instance-first-load', '/data/repeat/source', 'first')
)
),
body(
repeat(
'/data/repeat',
input(
'/data/repeat/source',
setvalueLiteral('odk-new-repeat', '/data/repeat/source', 'second')
)
)
)
)
);
expect(scenario.answerOf('/data/repeat[1]/source').getValue()).toBe('first');
scenario.createNewRepeat('/data/repeat');
expect(scenario.answerOf('/data/repeat[1]/source').getValue()).toBe('first');
expect(scenario.answerOf('/data/repeat[2]/source').getValue()).toBe('second');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,8 @@ export const createInstanceValueState = (context: ValueContext): InstanceValueSt
createCalculation(context, setValue, calculate);
}

const action = context.definition.model.actions.get(context.contextReference());
if (action) {
dispatchAction(context, setValue, action);
}
const actions = context.definition.model.actions.get(context.contextReference());
actions?.forEach((action) => dispatchAction(context, setValue, action));

return guardDownstreamReadonlyWrites(context, relevantValueState);
});
Expand Down
45 changes: 21 additions & 24 deletions packages/xforms-engine/src/parse/model/ModelActionMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { ModelDefinition } from './ModelDefinition.ts';

const REPEAT_REGEX = /(\[[^\]]*\])/gm;

export class ModelActionMap extends Map<string, ActionDefinition> {
export class ModelActionMap extends Map<string, ActionDefinition[]> {
static fromModel(model: ModelDefinition): ModelActionMap {
return new this(model);
}
Expand All @@ -19,39 +19,36 @@ export class ModelActionMap extends Map<string, ActionDefinition> {
return ref.replace(REPEAT_REGEX, '');
}

private static processActions(
protected constructor(model: ModelDefinition) {
super();
this.addAll(model, model.form.xformDOM.setValues, SET_VALUE_LOCAL_NAME);
this.addAll(model, model.form.xformDOM.setGeopoints, SET_GEOPOINT_LOCAL_NAME);
}

override get(ref: string): ActionDefinition[] | undefined {
return super.get(ModelActionMap.getKey(ref));
}

private addAll(
model: ModelDefinition,
elements: readonly DOMSetGeopointElement[] | readonly DOMSetValueElement[],
type: string
): Array<[string, ActionDefinition]> {
return elements.map((element) => {
) {
for (const element of elements) {
const action = new ActionDefinition(model, element);
if (action.events.includes(XFORM_EVENT.odkNewRepeat)) {
throw new Error(`Model contains "${type}" element with "odk-new-repeat" event`);
}
const key = ModelActionMap.getKey(action.ref);
return [key, action];
});
}

protected constructor(model: ModelDefinition) {
const entries: Array<[string, ActionDefinition]> = [
...ModelActionMap.processActions(model, model.form.xformDOM.setValues, SET_VALUE_LOCAL_NAME),
...ModelActionMap.processActions(
model,
model.form.xformDOM.setGeopoints,
SET_GEOPOINT_LOCAL_NAME
),
];
super(entries);
}

override get(ref: string): ActionDefinition | undefined {
return super.get(ModelActionMap.getKey(ref));
this.add(action);
}
}

add(action: ActionDefinition) {
const key = ModelActionMap.getKey(action.ref);
this.set(key, action);
if (this.has(key)) {
this.get(key)!.push(action);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I noticed that TypeScript has been less good at understanding the code and requiring non-null assertions (!) more often. I'll check the TypeScript config to see if there's anything that could improve that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Apparently this is not new: microsoft/TypeScript#9619

One option is to just call get instead of has and check if the result is null but this feels cleaner to me...

Copy link
Collaborator

Choose a reason for hiding this comment

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

I also prefer using has

} else {
this.set(key, [action]);
}
}
}