Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drive material date picker control from reactive form

I had date picker which should dynamically disable and enable by checkbox toggle click. All components from library angular material 6. And cause I using a reactive approach for my form handlers I can't simply use [disable] directive. I got further error:

It looks like you're using the disabled attribute with a reactive form directive. If you set disabled to true
when you set up this control in your component class, the disabled attribute will actually be set in the DOM for
you. We recommend using this approach to avoid 'changed after checked' errors.

Example: 
form = new FormGroup({
  first: new FormControl({value: 'Nancy', disabled: true}, Validators.required),
  last: new FormControl('Drew', Validators.required)
});

And I have idea replace FormContol inside FormGroup directly in TS, something like this:

toggleDatePicker() {
  this.isDateRange = !this.isDateRange;
  this.form.removeControl('to');
  if (this.isDateRange) {
    this.form.addControl('to', new FormControl({value: new Date(), disabled: false}))
  } else {
    this.form.addControl('to', new FormControl({value: null, disabled: true}))
  }
} 

And this work for input tag but mat-datepicker-toggle remains with the initial state. mat-datepicker-toggle state not depending on FormControl.

<mat-form-field>
  <input
    matInput
   [matDatepicker]="to"
   formControlName="to"
   [disabled]="isDateRange"
  >
<mat-datepicker-toggle matSuffix [for]="to"></mat-datepicker-toggle> // THIS IS NO CHANGE(
  <mat-datepicker #to></mat-datepicker>
</mat-form-field>

Independent from my FormControl manipulation mat-datepicker-toggle always in the same state:

OFF enter image description here ON enter image description here

How can I manipulate mat-datepicker-toggle thought FromControl?

like image 377
Pavel Avatar asked Jan 01 '23 14:01

Pavel


1 Answers

There are disable() and enable() methods on the control you will need to use to programmatically toggle the control disabled state.

Please review this stackblitz example

https://stackblitz.com/edit/angular-lenyzk?embed=1&file=app/datepicker-overview-example.ts

HTML Template

<form [formGroup]="form">
    <mat-checkbox (click)="toggleCtrState()">Toggle Control State</mat-checkbox>
    <mat-form-field>
        <input
        matInput
        [matDatepicker]="to"
        formControlName="to"
        >
    <mat-datepicker-toggle matSuffix [for]="to"></mat-datepicker-toggle>
    <mat-datepicker #to></mat-datepicker>
</mat-form-field>
</form>

Component

import { Component } from '@angular/core';
import { FormControl, FormGroup, FormBuilder, Validators } from '@angular/forms';

/** @title Basic datepicker */
@Component({
  selector: 'datepicker-overview-example',
  templateUrl: 'datepicker-overview-example.html',
  styleUrls: ['datepicker-overview-example.css'],
})
export class DatepickerOverviewExample {

  form = new FormGroup({
    to: new FormControl('', Validators.required),
  });

  toggleCtrState() {
    const ctrl = this.form.get('to');
    if (ctrl.disabled) {
      ctrl.enable();
    } else {
      ctrl.disable();
    }
  }
}
like image 93
Marshal Avatar answered Feb 04 '23 12:02

Marshal