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

ControlValueAccessor #1

Open
AndreyKotofotoff opened this issue May 18, 2018 · 3 comments
Open

ControlValueAccessor #1

AndreyKotofotoff opened this issue May 18, 2018 · 3 comments
Labels
need doc That issue should be shown in documentation resolved That issue was resolved

Comments

@AndreyKotofotoff
Copy link

When? ))

@thekiba thekiba added enhancement New feature or request good first issue Good for newcomers labels May 18, 2018
@tiberiuzuld
Copy link

Hello,
I implemented a workaround to have this feature implemented and have dynamic forms created at run time by using reactive forms.
I pass the formControl in the context and set inside the dynamic component on the input.

// parent form
  <ng-container
    *ngxComponentOutlet="component; context: {formControl: myFormControl};">
  </ng-container>
// dynamic component
<app-my-dynamic-component [formControl]="formControl"></alc-my-dynamic-component>

@thekiba thekiba added help wanted Extra attention is needed and removed good first issue Good for newcomers labels Sep 27, 2019
@thekiba thekiba added need doc That issue should be shown in documentation resolved That issue was resolved and removed enhancement New feature or request help wanted Extra attention is needed labels Jul 17, 2020
@ambeur
Copy link

ambeur commented Oct 13, 2020

Dynamic CVA can be achieved this way:

@Component({
  selector: 'cva-adapter',
  template: `
    <ng-container
      [ngxComponentOutlet]="component"
      (ngxComponentOutletActivate)="bind($event)"
    ></ng-container>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  providers: cvaProviders(CvaAdapterComponent)
})
export class CvaAdapterComponent implements ControlValueAccessor, Validators, OnDestroy {
  @Input() public component: any;
 
  public childInstance: ControlValueAccessor<any>;
  public onChange: (_: any) => void;
  public onTouched: (_: any) => void;
  public value: BehaviorSubject<any> = new BehaviorSubject<any>(null);
 
  public registerOnChange(fn: any): void {
    this.onChange = fn;
  }
 
  public registerOnTouched(fn: any): void {
    this.onTouched = fn;
  }
 
  public writeValue(obj: any): void {
    this.value.next(obj);
  }
 
  public validate(): ValidationErrors | null {
    return this.childInstance ? this.childInstance.validate() : null;
  }
 
  public setDisabledState(isDisabled: boolean): void {
    if (this.childInstance?.setDisabledState) {
      this.childInstance.setDisabledState(isDisabled);
    }
  }
 
  public bind(childInstance: ControlValueAccessor<any>) {
    this.childInstance = childInstance;
    this.childInstance.registerOnChange(this.onChange);
    this.childInstance.registerOnTouched(this.onTouched);
    this.value.pipe(
      takeUntil() // Don't forget to unsubscribe
    ).subscribe(value => {
      this.childInstance.writeValue(value);
    });
  }
 
  ngOnDestroy(): void {
    this.childInstance = undefined;
  }
}

@alQlagin
Copy link
Contributor

alQlagin commented Oct 13, 2020

Hey @zvnt! I have working example with dynamic forms here https://stackblitz.com/edit/angular-form-builder-lcsabo?file=src%2Fapp%2Fdynamic-field%2Fdynamic-field.component.ts

AutoCVA is @thekiba `s approach to reduce cva interface boilerplate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need doc That issue should be shown in documentation resolved That issue was resolved
Projects
None yet
Development

No branches or pull requests

5 participants