-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add API to create basic forms (#24)
- Loading branch information
1 parent
8cf9eef
commit d03d875
Showing
11 changed files
with
319 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,21 @@ | ||
import {Routes} from "@angular/router"; | ||
import { Routes } from '@angular/router'; | ||
|
||
export const routes: Routes = [ | ||
{ | ||
path: '', | ||
redirectTo: 'simple-form', | ||
pathMatch: 'full' | ||
redirectTo: 'basic-form', | ||
pathMatch: 'full', | ||
}, | ||
{ | ||
path: 'basic-form', | ||
loadComponent: () => import('./basic-form/basic-form.component'), | ||
}, | ||
{ | ||
path: 'simple-form', | ||
loadComponent: () => import('./simple-form.component') | ||
loadComponent: () => import('./simple-form/simple-form.component'), | ||
}, | ||
{ | ||
path: 'multi-page-form', | ||
loadChildren: () => import('./multi-page-form/multi-page-form.routes') | ||
} | ||
loadChildren: () => import('./multi-page-form/multi-page-form.routes'), | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Component, effect, inject } from '@angular/core'; | ||
import { | ||
SignalFormBuilder, | ||
SignalInputDebounceDirective, | ||
SignalInputDirective, | ||
SignalInputErrorDirective, | ||
withErrorComponent, | ||
} from '@ng-signal-forms'; | ||
import { JsonPipe, NgFor, NgIf } from '@angular/common'; | ||
import { FormsModule } from '@angular/forms'; | ||
import { CustomErrorComponent } from '../custom-input-error.component'; | ||
|
||
@Component({ | ||
selector: 'app-basic-form', | ||
template: ` | ||
<div class="container"> | ||
<div> | ||
<div> | ||
<label>Name</label> | ||
<input ngModel [formField]="form.controls.name" /> | ||
</div> | ||
<div> | ||
<label>Age</label> | ||
<input type="number" ngModel [formField]="form.controls.age" /> | ||
</div> | ||
</div> | ||
<div> | ||
<button (click)="reset()">Reset form</button> | ||
<h3>States</h3> | ||
<pre | ||
>{{ | ||
{ | ||
state: form.state(), | ||
dirtyState: form.dirtyState(), | ||
touchedState: form.touchedState(), | ||
valid: form.valid() | ||
} | json | ||
}} | ||
</pre> | ||
<h3>Value</h3> | ||
<pre>{{ form.value() | json }}</pre> | ||
<h3>Errors</h3> | ||
<pre>{{ form.errorsArray() | json }}</pre> | ||
</div> | ||
</div> | ||
`, | ||
standalone: true, | ||
imports: [ | ||
JsonPipe, | ||
FormsModule, | ||
SignalInputDirective, | ||
SignalInputErrorDirective, | ||
NgIf, | ||
NgFor, | ||
SignalInputDebounceDirective, | ||
], | ||
providers: [withErrorComponent(CustomErrorComponent)], | ||
}) | ||
export default class BasicFormComponent { | ||
private sfb = inject(SignalFormBuilder); | ||
|
||
form = this.sfb.createFormGroup<{ name: string; age: number | null }>({ | ||
name: 'Alice', | ||
age: null, | ||
}); | ||
|
||
formChanged = effect(() => { | ||
console.log('form changed:', this.form.value()); | ||
}); | ||
|
||
nameChanged = effect(() => { | ||
console.log('name changed:', this.form.controls.name.value()); | ||
}); | ||
|
||
ageChanged = effect(() => { | ||
console.log('age changed:', this.form.controls.age.value()); | ||
}); | ||
|
||
reset() { | ||
this.form.reset(); | ||
} | ||
|
||
setForm() { | ||
// TODO: allow form values to be set | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,34 @@ | ||
import {inject, Injectable, Injector, WritableSignal} from "@angular/core"; | ||
import {FormField, createFormField, FormFieldOptions, FormFieldOptionsCreator} from "./form-field"; | ||
import {FormGroup, createFormGroup,FormGroupOptions} from "./form-group"; | ||
import { inject, Injectable, Injector, WritableSignal } from '@angular/core'; | ||
import { | ||
FormField, | ||
createFormField, | ||
FormFieldOptions, | ||
FormFieldOptionsCreator, | ||
} from './form-field'; | ||
import { FormGroup, createFormGroup, FormGroupOptions } from './form-group'; | ||
import { FormGroupCreator } from './models'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
providedIn: 'root', | ||
}) | ||
export class SignalFormBuilder { | ||
private injector = inject(Injector); | ||
|
||
public createFormField<Value>( | ||
value: Value | WritableSignal<Value>, | ||
options?: FormFieldOptions | FormFieldOptionsCreator<Value>, | ||
options?: FormFieldOptions | FormFieldOptionsCreator<Value> | ||
): FormField<Value> { | ||
return createFormField(value, options, this.injector); | ||
} | ||
|
||
public createFormGroup< | ||
Controls extends | { [p: string]: FormField | FormGroup } | ||
| WritableSignal<any[]> | ||
>( | ||
formGroupCreator: () => Controls, | ||
public createFormGroup<FormFields extends FormGroupCreator>( | ||
formGroupCreator: FormFields | (() => FormFields), | ||
options?: FormGroupOptions | ||
): FormGroup<Controls> { | ||
return createFormGroup(formGroupCreator, options, this.injector); | ||
): FormGroup<FormFields> { | ||
return createFormGroup<FormFields>( | ||
formGroupCreator, | ||
options, | ||
this.injector | ||
); | ||
} | ||
} |
Oops, something went wrong.