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

Binding the colorPicker to an input in a Reactive Form #79

Open
Samoulou opened this issue Jan 24, 2019 · 1 comment
Open

Binding the colorPicker to an input in a Reactive Form #79

Samoulou opened this issue Jan 24, 2019 · 1 comment

Comments

@Samoulou
Copy link

When connecting the colorpicker to an input in a Reactive From using MccColorPickerOrigin and MccConnectedColorPicker it put the input controls as pristine: false.

The binding between the color picker and the input is certainly detected as a modification after the init.

The binding shouldn't make the input controls as pristine

@samogot
Copy link

samogot commented Feb 17, 2019

I've bypassed it by creating my own control that implements ControlValueAccessor and wraps MccColorPickerComponent

IMPORTANT NOTICE: I only need to track confirmed changes as dirty, that's why I use selected event with distinctUntilChanged. this.formData.valueChanges.pipe(first()) and skip(1) is needed to to know first value after initialization (for distinct to work) but don't trigger change for it

color-picker.component.html:

<mat-form-field class="full-width">
  <input #origin="mccColorPickerOrigin" (blur)="_OnTouched()" [formControl]="formData" [placeholder]="placeholder"
    matInput mccColorPickerOrigin/>
  <mcc-color-picker #picker [mccConnectedColorPickerOrigin]="origin" class="color-prefix" matPrefix mccConnectedColorPicker>
  </mcc-color-picker>
</mat-form-field>

color-picker.component.ts:

import { Component, forwardRef, Input, ViewChild } from '@angular/core';
import { ControlValueAccessor, FormBuilder, NG_VALUE_ACCESSOR } from "@angular/forms";
import { distinctUntilChanged, first, map, skip } from "rxjs/operators";
import { concat } from "rxjs";

@Component({
  selector: 'app-color-picker',
  templateUrl: './color-picker.component.html',
  styleUrls: ['./color-picker.component.scss'],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => ColorPickerComponent),
      multi: true
    }
  ]
})
export class ColorPickerComponent implements ControlValueAccessor {

  @Input()
  placeholder: string;

  @ViewChild('picker') picker;

  formData = this.fb.control("");

  private _OnTouched: () => void;

  constructor(private fb: FormBuilder) {
  }

  registerOnChange(fn: (_: string) => void): void {
    concat(this.formData.valueChanges.pipe(first()), this.picker.selected).pipe(
      distinctUntilChanged(),
      skip(1)
    ).subscribe(fn);
  }

  registerOnTouched(fn: any): void {
    this._OnTouched = fn;
  }

  setDisabledState(isDisabled: boolean): void {
    if (isDisabled) {
      this.formData.disable();
    } else {
      this.formData.enable();
    }
  }

  writeValue(str: string): void {
    this.formData.setValue(str);
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants