Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

FormControllWrapperValueAccessor #42

Closed
kbrilla opened this issue Aug 14, 2020 · 3 comments
Closed

FormControllWrapperValueAccessor #42

kbrilla opened this issue Aug 14, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@kbrilla
Copy link

kbrilla commented Aug 14, 2020

Hi, I really like the idea behind this project and especially the touch$ (but would call it touched$ ;-) it's dirty, it's enabled, it's disabled, it's pristine and ** it's touched**, etc so touch is a bit out of place ) feature.
So what I would propose is to add an abstract class like ControlValueAccessor for use with a Control simply wrapping around a FormGroup (AbstractFormControl actually).

Let's say I have the usual Person object. I want to create a ControlValueAccessor that would wrap around the FormGroup. So finally I could use this component as a form component.

It's much easier now with your reactive-forms but would be even better with an abstract FormControllWrapperValueAccessor.

If I'm not clear enough I can elaborate or maybe create a simple proof of concept.

Idea coming from https:https://timdeschryver.dev/blog/working-with-angular-forms-in-an-enterprise-environment


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[x] Feature request
[ ] Documentation issue or request
[ ] Support request
[ ] Other... Please describe:
@kbrilla
Copy link
Author

kbrilla commented Aug 14, 2020

A very simple and naive implementation:

import {ControlValueAccessor } from '@angular/forms';
import { FormGroup} from "@ngneat/reactive-forms";
import {map, takeUntil} from "rxjs/operators";
import {Subject} from "rxjs";
import {OnDestroy} from "@angular/core";

export abstract class FormGroupWrapperValueAccessor<T = any, F = any> implements ControlValueAccessor, OnDestroy{
  public form: FormGroup<F>;
  private destroyed$ = new Subject();

  abstract provideForm() : FormGroup<F>;
  abstract mapFromFormValue: (R) => T;
  abstract mapToFormValue: (T) => F;
  
  constructor(){
    this.form = this.provideForm();
  }
  writeValue(value: T): void {
    this.form.setValue(this.mapToFormValue(value), { emitEvent: false} );
  }

  registerOnChange(fn: <T>(value: (T | null)) => void): void {
    this.form.value$.pipe(takeUntil(this.destroyed$), map(this.mapFromFormValue)).subscribe(fn);
  }

  registerOnTouched(fn: any): void {
    this.form.touch$.pipe(takeUntil(this.destroyed$)).subscribe(fn);
  }

  setDisabledState(isDisabled: boolean): void {
    this.form.setDisable(isDisabled);
  }

  ngOnDestroy(): void {
    this.destroyed$.next();
  }

}

@NetanelBasal
Copy link
Member

Agree about the touch thing. Can you give a more concrete example to your suggestion, please?

@NetanelBasal NetanelBasal added the enhancement New feature or request label Sep 18, 2020
@itayod
Copy link
Contributor

itayod commented Nov 29, 2020

@criskrzysiu that actually sounds interesting and can save some boilerplate, would you like to create us a POC as you suggested?

@ngneat ngneat locked and limited conversation to collaborators Jun 10, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants