Skip to content

Commit a5052da

Browse files
authored
fix(forms): write Signal Forms checkboxes, radios and selects in user mode (#360)
1 parent 6e3daca commit a5052da

2 files changed

Lines changed: 53 additions & 4 deletions

File tree

‎packages/devtools/src/__tests__/forms-actions.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ Component({
160160
</form>`,
161161
})(Shirt);
162162

163+
class Trip {
164+
model = signal({ terms: false, pace: 'slow', seat: 'aisle' });
165+
form = form(this.model);
166+
}
167+
Component({
168+
selector: 'trip-form',
169+
imports: [FormField, FormRoot],
170+
template: `<form [formRoot]="form">
171+
<input id="terms" type="checkbox" [formField]="form.terms" />
172+
<input id="slow" type="radio" value="slow" [formField]="form.pace" />
173+
<input id="fast" type="radio" value="fast" [formField]="form.pace" />
174+
<select id="seat" [formField]="form.seat">
175+
<option value="aisle">Aisle</option>
176+
<option value="window">Window</option>
177+
</select>
178+
</form>`,
179+
})(Trip);
180+
163181
async function render<T>(type: new () => T) {
164182
const fixture = TestBed.createComponent(type);
165183
fixture.detectChanges();
@@ -411,6 +429,23 @@ describe('form actions on template-driven and Signal Forms', () => {
411429
).toContain('ngModel');
412430
});
413431

432+
it('writes Signal Forms checkboxes, radios and selects through the page in user mode', async () => {
433+
const fixture = await render(Trip);
434+
const ctx = contextFor(fixture.nativeElement);
435+
const model = fixture.componentInstance.model;
436+
const write = (path: string, value: unknown) =>
437+
runFormAction(ctx, { action: 'set-value', formId: 'form-1', path, value, mode: 'user' });
438+
439+
expect((await write('terms', true)).ok).toBe(true);
440+
expect(model().terms).toBe(true);
441+
expect((await write('terms', false)).ok).toBe(true);
442+
expect(model().terms).toBe(false);
443+
expect((await write('pace', 'fast')).ok).toBe(true);
444+
expect(model().pace).toBe('fast');
445+
expect((await write('seat', 'window')).ok).toBe(true);
446+
expect(model().seat).toBe('window');
447+
});
448+
414449
it('writes Signal Forms fields, refuses hidden ones and submits through the form root', async () => {
415450
const fixture = await render(Profile);
416451
const ctx = contextFor(fixture.nativeElement);

‎packages/devtools/src/forms-actions.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,19 @@ export function guardedFields(
397397
return overflow ? null : out;
398398
}
399399

400+
// A real click on a checkbox, radio or select fires input, then change. Signal Forms
401+
// listens for input and the reactive and template-driven accessors listen for change.
402+
function dispatchPick(element: Element): void {
403+
element.dispatchEvent(new Event('input', { bubbles: true }));
404+
element.dispatchEvent(new Event('change', { bubbles: true }));
405+
}
406+
400407
function nativeWrite(element: Element, value: unknown): boolean {
401408
if (element instanceof HTMLInputElement) {
402409
if (element.type === 'file') return false;
403410
if (element.type === 'checkbox') {
404411
element.checked = !!value;
405-
element.dispatchEvent(new Event('change', { bubbles: true }));
412+
dispatchPick(element);
406413
return true;
407414
}
408415
if (element.type === 'radio') {
@@ -414,7 +421,7 @@ function nativeWrite(element: Element, value: unknown): boolean {
414421
) as HTMLInputElement | undefined;
415422
if (!target) return false;
416423
target.checked = true;
417-
target.dispatchEvent(new Event('change', { bubbles: true }));
424+
dispatchPick(target);
418425
return true;
419426
}
420427
element.value = value == null ? '' : String(value);
@@ -467,7 +474,7 @@ function selectWrite(
467474
select.selectedIndex = index;
468475
expected = options[index].value;
469476
}
470-
select.dispatchEvent(new Event('change', { bubbles: true }));
477+
dispatchPick(select);
471478
return { expected };
472479
}
473480

@@ -540,7 +547,14 @@ function writeValue(
540547
}
541548
if (viaDom && nativeWrite(element, value)) {
542549
if (mode === 'user') element.dispatchEvent(new Event('blur'));
543-
return null;
550+
const picked =
551+
element instanceof HTMLInputElement &&
552+
(element.type === 'checkbox' || element.type === 'radio');
553+
if (!picked) return null;
554+
const expected = element.type === 'checkbox' ? !!value : value;
555+
const deferred = found.kind !== 'signal' && read(() => node['updateOn'], 'change') !== 'change';
556+
const stored = deferred ? node['_pendingValue'] : rawValue(found, node);
557+
return sameOption(stored, expected) ? null : `holds ${label(stored)} after the write`;
544558
}
545559
if (found.kind === 'template' && leaf) {
546560
const dir = directivesOf(node);

0 commit comments

Comments
 (0)