Skip to content
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: 4 additions & 0 deletions packages/forms/signals/src/directive/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ function isRelevantSelectMutation(mutation: MutationRecord) {
if (mutation.target instanceof Comment) {
return false;
}
// A text node's data changed, which may change the implicit value of an `<option>`.
if (mutation.type === 'characterData') {
return true;
}
// Otherwise if any non-comment nodes were added / removed it is relevant.
for (const node of mutation.addedNodes) {
if (!(node instanceof Comment)) {
Expand Down
28 changes: 28 additions & 0 deletions packages/forms/signals/test/web/form_field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4862,6 +4862,34 @@ describe('field directive', () => {
expect(fix.componentInstance.select()!.nativeElement.value).toEqual('four');
});

it('should resync the select value when the text of an option without a value changes', async () => {
@Component({
imports: [FormField],
template: `
<select #select [formField]="f">
<option>one</option>
<option>{{ label() }}</option>
</select>
`,
})
class TestCmp {
f = form(signal('two'));
select = viewChild<ElementRef<HTMLSelectElement>>('select');
label = signal('pending');
}

const fix = act(() => TestBed.createComponent(TestCmp));
await fix.whenStable();
const cmp = fix.componentInstance as TestCmp;

expect(cmp.select()!.nativeElement.value).toEqual('');

act(() => cmp.label.set('two'));
await fix.whenStable();

expect(cmp.select()!.nativeElement.value).toEqual('two');
});

it('initializes a required value input before the component lifecycle runs', () => {
let initialValue: string | undefined = undefined;
@Component({
Expand Down
Loading