Skip to content

Commit

Permalink
feat(reactive-forms): implemente disable in form control
Browse files Browse the repository at this point in the history
  • Loading branch information
fetrarij committed Aug 27, 2020
1 parent 6e85a69 commit 7c3265b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions demo/src/app/reactive-form/reactive-form.component.html
Expand Up @@ -13,7 +13,15 @@ <h6>Multi Select</h6>
</div>
<div class="col s6">
<br/>
<button type="button"
class="ngx-daterangepicker-action waves-effect waves-light btn"
[ngClass]="{'red': !form.disabled, 'green': form.disabled}"
(click)="toggleDisable(form)">
{{form.disabled ? 'Enable' : 'Disable'}}
</button>
&nbsp;
<button type="submit" class="ngx-daterangepicker-action waves-effect waves-light btn" >Submit</button>
&nbsp;
</div>
</div>
</form>
Expand Down
7 changes: 7 additions & 0 deletions demo/src/app/reactive-form/reactive-form.component.ts
Expand Up @@ -39,4 +39,11 @@ export class ReactiveFormComponent {
submit2() {
console.log(this.form2.value);
}
toggleDisable(form: FormGroup) {
if (form.disabled) {
form.enable();
} else {
form.disable();
}
}
}
11 changes: 10 additions & 1 deletion src/daterangepicker/daterangepicker.directive.ts
Expand Up @@ -15,7 +15,8 @@ import {
KeyValueDiffers,
Output,
EventEmitter,
Renderer2
Renderer2,
HostBinding
} from '@angular/core';
import { DaterangepickerComponent } from './daterangepicker.component';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
Expand Down Expand Up @@ -44,6 +45,7 @@ export class DaterangepickerDirective implements OnInit, OnChanges, DoCheck {
private _onChange = Function.prototype;
private _onTouched = Function.prototype;
private _validatorChange = Function.prototype;
private _disabled: boolean;
private _value: any;
private localeDiffer: KeyValueDiffer<string, any>;
@Input()
Expand Down Expand Up @@ -154,6 +156,7 @@ export class DaterangepickerDirective implements OnInit, OnChanges, DoCheck {
@Output('datesUpdated') datesUpdated: EventEmitter<Object> = new EventEmitter();
@Output() startDateChanged: EventEmitter<Object> = new EventEmitter();
@Output() endDateChanged: EventEmitter<Object> = new EventEmitter();
@HostBinding('disabled') get disabled() { return this._disabled; }
constructor(
public viewContainerRef: ViewContainerRef,
public _changeDetectorRef: ChangeDetectorRef,
Expand Down Expand Up @@ -232,6 +235,9 @@ export class DaterangepickerDirective implements OnInit, OnChanges, DoCheck {
}

open(event?: any) {
if (this.disabled) {
return;
}
this.picker.show(event);
setTimeout(() => {
this.setPosition();
Expand Down Expand Up @@ -262,6 +268,9 @@ export class DaterangepickerDirective implements OnInit, OnChanges, DoCheck {
registerOnTouched(fn) {
this._onTouched = fn;
}
setDisabledState(state: boolean): void {
this._disabled = state;
}
private setValue(val: any) {
if (val) {
this.value = val;
Expand Down

0 comments on commit 7c3265b

Please sign in to comment.