Skip to content

Commit

Permalink
fix(form): initialize arrays when receiving items (#2959) (CP: 24.6) (#…
Browse files Browse the repository at this point in the history
…3126)

fix(form): initialize arrays when receiving items (#2959)

* fix(form): initialize arrays when receiving items

Fixes #2949

* fix(lit-form): make it build

* test(lit-form): make linter happy

---------

Co-authored-by: Anton Platonov <[email protected]>
Co-authored-by: Vlad Rindevich <[email protected]>
Co-authored-by: Luciano Vernaschi <[email protected]>
  • Loading branch information
4 people authored Jan 13, 2025
1 parent ccb2ef2 commit ee0e161
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/ts/lit-form/src/BinderNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class BinderNode<M extends AbstractModel = AbstractModel> extends EventTa
}

set value(value: Value<M> | undefined) {
this.initializeValue();
this.initializeValue(true);
this.#setValueState(value, undefined);
}

Expand Down
17 changes: 16 additions & 1 deletion packages/ts/lit-form/test/Binder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { customElement } from 'lit/decorators.js';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
// API to test
import { Binder, type BinderConfiguration } from '../src/index.js';
import { Binder, type BinderConfiguration, m } from '../src/index.js';
import {
type Employee,
EmployeeModel,
Expand Down Expand Up @@ -302,6 +302,21 @@ describe('@vaadin/hilla-lit-form', () => {
assert.isTrue('supervisor' in binder.value);
});

it('should support optional array', async () => {
const arrayBinderNode = binder.for(binder.model.colleagues);
assert.isUndefined(arrayBinderNode.value);
assert.isUndefined(arrayBinderNode.defaultValue);

arrayBinderNode.value = [EmployeeModel.createEmptyValue()];
const [itemModel] = m.items(arrayBinderNode.model);
assert.deepEqual(binder.for(itemModel).value, expectedEmptyEmployee);
assert.deepEqual(arrayBinderNode.defaultValue, []);
assert.isTrue(arrayBinderNode.dirty);

await binder.validate();
assert.isFalse(binder.invalid);
});

it('should initialize parent optional on child binderNode access', () => {
binder.for(binder.model.supervisor.supervisor);

Expand Down

0 comments on commit ee0e161

Please sign in to comment.