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(form): initialize arrays when receiving items #2959

Open
wants to merge 2 commits into
base: 24.5
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -500,7 +500,7 @@ export class BinderNode<M extends AbstractModel = AbstractModel> extends EventTa
: undefined;

const defaultValue: Value<M> | undefined = this.parent
? (this.parent.defaultValue as { readonly [key in typeof key]: Value<M> })[this.model[_key]]
? (this.parent.defaultValue as { readonly [key in typeof key]: Value<M> } | undefined)[this.model[_key]]
: undefined;

if (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 () => {
let 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
Loading