Skip to content

Commit a254e29

Browse files
committed
fix(ReactiveForms): implement onTouch and make FormControl optional
1 parent daeddab commit a254e29

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/formatter-parser.directive.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,23 @@ export class FormatterParserDirective implements ControlValueAccessor, OnInit {
7575
onControlInput($event: KeyboardEvent) {
7676
const rawValue: any = this.inputElement.value;
7777

78+
// If there is a reactive FormControl present trigger onTouch
79+
if (this.onTouch) {
80+
this.onTouch();
81+
}
82+
7883
// write value to view (visible text of the form control)
7984
this.inputElement.value = this.formatterParserView
8085
.reduce((state: any, transform: IFormatterParserFn) => transform(state).result, rawValue || null);
8186

8287
// write value to model (value stored in FormControl)
8388
const modelValue = this.formatterParserModel
8489
.reduce((state: any, transform: IFormatterParserFn) => transform(state).result, rawValue || null);
85-
this.onModelChange(modelValue);
90+
91+
// If there is a reactive formControl present update its model
92+
if (this.onModelChange) {
93+
this.onModelChange(modelValue);
94+
}
8695
}
8796

8897

@@ -96,10 +105,14 @@ export class FormatterParserDirective implements ControlValueAccessor, OnInit {
96105
// write value to model (value stored in FormControl)
97106
const modelValue = this.formatterParserModel
98107
.reduce((state: any, transform: IFormatterParserFn) => transform(state).result, rawValue);
108+
99109
// prevent cyclic function calls
100110
if (rawValue !== modelValue) {
101-
// @TODO consider other way to call patchValue
102-
this.formControl.patchValue(modelValue);
111+
// If there is a reactive FormControl present update its model
112+
if (this.onModelChange) {
113+
// @TODO consider other way to call patchValue
114+
this.formControl.patchValue(modelValue);
115+
}
103116
}
104117

105118
}

0 commit comments

Comments
 (0)