Skip to content

Commit

Permalink
feat: Add Access to NgForm with sacInheritForm directive (simpleangul…
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjenni committed Jul 31, 2024
1 parent 6b41c5e commit fb22ba4
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 162 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div style="border: 1px solid" sacInheritForm>
<div style="border: 1px solid" sacInheritForm #formaccess="sacinheritform">
<div>SubForm</div>
<div>
<sac-input
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
import { Component } from '@angular/core';
import { Input } from '@angular/core';
import { EventEmitter } from '@angular/core';
import { Output } from '@angular/core';
import { DoCheck } from '@angular/core';
import {
Component,
DoCheck,
EventEmitter,
Input,
Output,
ViewChild,
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { SACBootstrap3FormModule, SACBootstrap3InputModule } from '@simpleangularcontrols/sac-bootstrap3';

import {
SACBootstrap3FormModule,
SACBootstrap3InputModule,
} from '@simpleangularcontrols/sac-bootstrap3';

@Component({
selector: 'app-subform',
templateUrl: './subform.component.html',
standalone: true,
imports: [SACBootstrap3FormModule, SACBootstrap3InputModule, FormsModule]
selector: 'app-subform',
templateUrl: './subform.component.html',
standalone: true,
imports: [SACBootstrap3FormModule, SACBootstrap3InputModule, FormsModule],
})
export class DemoSubFormComponent implements DoCheck {
// #region Properties

@Input() public mymodel;
@Output() public mymodelChange = new EventEmitter();
@ViewChild('formaccess') public form: SacInheritFormDirective;

// #endregion Properties

@Input() mymodel;
@Output() mymodelChange = new EventEmitter();
// #region Public Methods

ngDoCheck() {
public ngDoCheck() {
if (this.form) {
console.log(this.form.getForm().dirty);
}
this.mymodelChange.next(this.mymodel);
}

// #endregion Public Methods
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div style="border: 1px solid" sacInheritForm>
<div style="border: 1px solid" sacInheritForm #formaccess="sacinheritform">
<div>SubForm</div>
<div>
<sac-input
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
import { Component } from '@angular/core';
import { Input } from '@angular/core';
import { EventEmitter } from '@angular/core';
import { Output } from '@angular/core';
import { DoCheck } from '@angular/core';
import {
Component,
DoCheck,
EventEmitter,
Input,
Output,
ViewChild,
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { SACBootstrap4FormModule, SACBootstrap4InputModule } from '@simpleangularcontrols/sac-bootstrap4';

import {
SACBootstrap4FormModule,
SACBootstrap4InputModule,
} from '@simpleangularcontrols/sac-bootstrap4';

@Component({
selector: 'app-subform',
templateUrl: './subform.component.html',
standalone: true,
imports: [SACBootstrap4FormModule, SACBootstrap4InputModule, FormsModule]
selector: 'app-subform',
templateUrl: './subform.component.html',
standalone: true,
imports: [SACBootstrap4FormModule, SACBootstrap4InputModule, FormsModule],
})
export class DemoSubFormComponent implements DoCheck {
// #region Properties

@Input() public mymodel;
@Output() public mymodelChange = new EventEmitter();
@ViewChild('formaccess') public form: SacInheritFormDirective;

// #endregion Properties

@Input() mymodel;
@Output() mymodelChange = new EventEmitter();
// #region Public Methods

ngDoCheck() {
public ngDoCheck() {
if (this.form) {
console.log(this.form.getForm().dirty);
}
this.mymodelChange.next(this.mymodel);
}

// #endregion Public Methods
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div style="border: 1px solid" sacInheritForm>
<div style="border: 1px solid" sacInheritForm #formaccess="sacinheritform">
<div>SubForm</div>
<div>
<sac-input
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Component } from '@angular/core';
import { Input } from '@angular/core';
import { EventEmitter } from '@angular/core';
import { Output } from '@angular/core';
import { DoCheck } from '@angular/core';
import {
Component,
DoCheck,
EventEmitter,
Input,
Output,
ViewChild,
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import {
SACBootstrap5FormModule,
Expand All @@ -16,10 +19,22 @@ import {
imports: [SACBootstrap5FormModule, SACBootstrap5InputModule, FormsModule],
})
export class DemoSubFormComponent implements DoCheck {
@Input() mymodel;
@Output() mymodelChange = new EventEmitter();
// #region Properties

@Input() public mymodel;
@Output() public mymodelChange = new EventEmitter();
@ViewChild('formaccess') public form: SacInheritFormDirective;

// #endregion Properties

ngDoCheck() {
// #region Public Methods

public ngDoCheck() {
if (this.form) {
console.log(this.form.getForm().dirty);
}
this.mymodelChange.next(this.mymodel);
}

// #endregion Public Methods
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Examples

## Inherit Form

```html
<div sacInheritForm>
<div>SubForm</div>
<div>
<ngInput [(ngModel)]="mymodel.fieldarea2" name="subformField3" label="field 3" [isrequired]="true"></ngInput>
</div>
</div>
```

## Inherit Form with access to Form

HTML Template with access to form.

```html
<div sacInheritForm #formaccess="sacinheritform">
<div>SubForm</div>
<div>
<ngInput [(ngModel)]="mymodel.fieldarea2" name="subformField3" label="field 3" [isrequired]="true"></ngInput>
</div>
</div>
```

Code behind file with access to form.

```ts
import { Component, Input, Output, ViewChild } from '@angular/core';
import { SacInheritFormDirective } from '@simpleangularcontrols/sac-bootstrap3';

@Component({
selector: 'app-subform',
templateUrl: './subform.component.html',
})
export class DemoSubFormComponent {
@Input() mymodel;
@Output() mymodelChange = new EventEmitter();
@ViewChild('formaccess') public form: SacInheritFormDirective;

public accessToForm() {
console.log(this.form.getForm().dirty);
}
}
```
Original file line number Diff line number Diff line change
@@ -1,44 +1,17 @@
import { Directive, SkipSelf } from '@angular/core';
import { Directive, Injector, SkipSelf } from '@angular/core';
import { ControlContainer, NgForm } from '@angular/forms';
import { SacFormCommon } from '@simpleangularcontrols/sac-common';
import { SacFormDirective } from './form';

// #region Classes

/**
* Directive zum erben eines NgForm/NgFormular einer übergeordneten Komponente
*
* @example Implementation in Markup
*
* <div sacInheritForm>
* </div>
*
* @example Model an Sub-Komponente übergeben
*
* <div sacInheritForm>
* <div>SubForm</div>
* <div>
* <ngInput [(ngModel)]="mymodel.fieldarea2" name="subformField3" label="field 3" [isrequired]="true"></ngInput>
* </div>
* </div>
*
*
* (at)Component({
* selector: 'sacInheritForm',
* templateUrl: './subform.component.html'
* })
* export class SubFormComponent implements DoCheck {
*
* (at)Input() mymodel;
* (at)Output() mymodelChange = new EventEmitter();
*
* ngDoCheck() {
* this.mymodelChange.next(this.mymodel);
* }
*}
*
* Directive to inherit an NgForm/NgForm from a parent component
*/
@Directive({
selector: '[sacInheritForm]',
exportAs: 'sacinheritform',
standalone: true,
providers: [
{
provide: SacFormDirective,
Expand All @@ -51,9 +24,20 @@ import { SacFormDirective } from './form';
deps: [NgForm],
},
],
standalone: true,
})
export class SacInheritFormDirective {}
export class SacInheritFormDirective extends SacFormCommon {
// #region Constructors

/**
* Construtor
* @param injector: injector to receive the NgForm instance
*/
constructor(injector: Injector) {
super(injector.get(NgForm));
}

// #endregion Constructors
}

// #endregion Classes

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Examples

## Inherit Form

```html
<div sacInheritForm>
<div>SubForm</div>
<div>
<ngInput [(ngModel)]="mymodel.fieldarea2" name="subformField3" label="field 3" [isrequired]="true"></ngInput>
</div>
</div>
```

## Inherit Form with access to Form

HTML Template with access to form.

```html
<div sacInheritForm #formaccess="sacinheritform">
<div>SubForm</div>
<div>
<ngInput [(ngModel)]="mymodel.fieldarea2" name="subformField3" label="field 3" [isrequired]="true"></ngInput>
</div>
</div>
```

Code behind file with access to form.

```ts
import { Component, Input, Output, ViewChild } from '@angular/core';
import { SacInheritFormDirective } from '@simpleangularcontrols/sac-bootstrap4';

@Component({
selector: 'app-subform',
templateUrl: './subform.component.html',
})
export class DemoSubFormComponent {
@Input() mymodel;
@Output() mymodelChange = new EventEmitter();
@ViewChild('formaccess') public form: SacInheritFormDirective;

public accessToForm() {
console.log(this.form.getForm().dirty);
}
}
```
Loading

0 comments on commit fb22ba4

Please sign in to comment.