Skip to content

Commit

Permalink
fix(primeng/p-inputNumber): reactive form updateOn blur
Browse files Browse the repository at this point in the history
  • Loading branch information
volvachev authored and Egor Volvachev committed Feb 25, 2023
1 parent fa4a718 commit ddc1d26
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/app/components/inputnumber/inputnumber.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, EventEmitter, forwardRef, Input, NgModule, OnInit, Output, SimpleChanges, ViewChild, ViewEncapsulation } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, EventEmitter, forwardRef, Injector, Input, NgModule, OnInit, Output, SimpleChanges, ViewChild, ViewEncapsulation } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR, NgControl } from '@angular/forms';
import { ButtonModule } from 'primeng/button';
import { DomHandler } from 'primeng/dom';
import { InputTextModule } from 'primeng/inputtext';
Expand Down Expand Up @@ -271,7 +271,9 @@ export class InputNumber implements OnInit, ControlValueAccessor {
if (this.timer) this.clearTimer();
}

constructor(public el: ElementRef, private cd: ChangeDetectorRef) {}
private ngControl: NgControl | null = null;

constructor(public el: ElementRef, private cd: ChangeDetectorRef, private readonly injector: Injector) {}

ngOnChanges(simpleChange: SimpleChanges) {
const props = ['locale', 'localeMatcher', 'mode', 'currency', 'currencyDisplay', 'useGrouping', 'minFractionDigits', 'maxFractionDigits', 'prefix', 'suffix'];
Expand All @@ -281,6 +283,8 @@ export class InputNumber implements OnInit, ControlValueAccessor {
}

ngOnInit() {
this.ngControl = this.injector.get(NgControl, null, { optional: true });

this.constructParser();

this.initialized = true;
Expand Down Expand Up @@ -1060,8 +1064,15 @@ export class InputNumber implements OnInit, ControlValueAccessor {
}

updateModel(event, value) {
const isBlurUpdateOnMode = this.ngControl?.control?.updateOn === 'blur';

if (this.value !== value) {
this.value = value;

if (!(isBlurUpdateOnMode && this.focused)) {
this.onModelChange(value);
}
} else if (isBlurUpdateOnMode) {
this.onModelChange(value);
}

Expand Down

0 comments on commit ddc1d26

Please sign in to comment.