mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-16 18:17:31 +08:00
fix(angular): make sure angular form control onChange is fired when needed (#16126)
* Make sure, that angular form's onChange is not fired, when writeValue is called * Update angular/src/directives/control-value-accessors/select-value-accessor.ts Co-Authored-By: mmlleevvyy <levy@codeandmore.pl> * set muteOnChange to false by default
This commit is contained in:
@ -24,14 +24,25 @@ export class BooleanValueAccessor implements ControlValueAccessor {
|
|||||||
onChange: (value: any) => void;
|
onChange: (value: any) => void;
|
||||||
onTouched: () => void;
|
onTouched: () => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether onChange should be mutted (not be fired). Will be true only when writeValue was called, which
|
||||||
|
* means that value changed inside angular form (e.g. calling setValue on a control).
|
||||||
|
*/
|
||||||
|
private muteOnChange = false;
|
||||||
|
|
||||||
writeValue(value: any) {
|
writeValue(value: any) {
|
||||||
|
this.muteOnChange = true;
|
||||||
this.element.nativeElement.checked = value;
|
this.element.nativeElement.checked = value;
|
||||||
setIonicClasses(this.element);
|
setIonicClasses(this.element);
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('ionChange', ['$event.target.checked'])
|
@HostListener('ionChange', ['$event.target.checked'])
|
||||||
_handleIonChange(value: any) {
|
_handleIonChange(value: any) {
|
||||||
this.onChange(value);
|
if (!this.muteOnChange) {
|
||||||
|
this.onChange(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.muteOnChange = false;
|
||||||
|
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
setIonicClasses(this.element);
|
setIonicClasses(this.element);
|
||||||
|
@ -24,7 +24,15 @@ export class NumericValueAccessor implements ControlValueAccessor {
|
|||||||
onChange: (value: any) => void;
|
onChange: (value: any) => void;
|
||||||
onTouched: () => void;
|
onTouched: () => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether onChange should be mutted (not be fired). Will be true only when writeValue was called, which
|
||||||
|
* means that value changed inside angular form (e.g. calling setValue on a control).
|
||||||
|
*/
|
||||||
|
private muteOnChange = false;
|
||||||
|
|
||||||
writeValue(value: any) {
|
writeValue(value: any) {
|
||||||
|
this.muteOnChange = true;
|
||||||
|
|
||||||
// The value needs to be normalized for IE9, otherwise it is set to 'null' when null
|
// The value needs to be normalized for IE9, otherwise it is set to 'null' when null
|
||||||
// Probably not an issue for us, but it doesn't really cost anything either
|
// Probably not an issue for us, but it doesn't really cost anything either
|
||||||
this.element.nativeElement.value = value == null ? '' : value;
|
this.element.nativeElement.value = value == null ? '' : value;
|
||||||
@ -33,7 +41,11 @@ export class NumericValueAccessor implements ControlValueAccessor {
|
|||||||
|
|
||||||
@HostListener('ionChange', ['$event.target.value'])
|
@HostListener('ionChange', ['$event.target.value'])
|
||||||
_handleInputEvent(value: any) {
|
_handleInputEvent(value: any) {
|
||||||
this.onChange(value);
|
if (!this.muteOnChange) {
|
||||||
|
this.onChange(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.muteOnChange = false;
|
||||||
|
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
setIonicClasses(this.element);
|
setIonicClasses(this.element);
|
||||||
|
@ -25,7 +25,14 @@ export class RadioValueAccessor implements ControlValueAccessor {
|
|||||||
this.onTouched = () => {/**/};
|
this.onTouched = () => {/**/};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether onChange should be mutted (not be fired). Will be true only when writeValue was called, which
|
||||||
|
* means that value changed inside angular form (e.g. calling setValue on a control).
|
||||||
|
*/
|
||||||
|
private muteOnChange = false;
|
||||||
|
|
||||||
writeValue(value: any) {
|
writeValue(value: any) {
|
||||||
|
this.muteOnChange = true;
|
||||||
this.element.nativeElement.checked = this.value = value;
|
this.element.nativeElement.checked = this.value = value;
|
||||||
|
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
@ -35,7 +42,11 @@ export class RadioValueAccessor implements ControlValueAccessor {
|
|||||||
|
|
||||||
@HostListener('ionSelect', ['$event.target.checked'])
|
@HostListener('ionSelect', ['$event.target.checked'])
|
||||||
_handleIonSelect(value: any) {
|
_handleIonSelect(value: any) {
|
||||||
this.onChange(value);
|
if (!this.muteOnChange) {
|
||||||
|
this.onChange(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.muteOnChange = false;
|
||||||
|
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
setIonicClasses(this.element);
|
setIonicClasses(this.element);
|
||||||
|
@ -24,7 +24,14 @@ export class SelectValueAccessor implements ControlValueAccessor {
|
|||||||
onChange: (value: any) => void;
|
onChange: (value: any) => void;
|
||||||
onTouched: () => void;
|
onTouched: () => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether onChange should be mutted (not be fired). Will be true only when writeValue was called, which
|
||||||
|
* means that value changed inside angular form (e.g. calling setValue on a control).
|
||||||
|
*/
|
||||||
|
private muteOnChange = false;
|
||||||
|
|
||||||
writeValue(value: any) {
|
writeValue(value: any) {
|
||||||
|
this.muteOnChange = true;
|
||||||
this.element.nativeElement.value = value;
|
this.element.nativeElement.value = value;
|
||||||
|
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
@ -34,7 +41,11 @@ export class SelectValueAccessor implements ControlValueAccessor {
|
|||||||
|
|
||||||
@HostListener('ionChange', ['$event.target.value'])
|
@HostListener('ionChange', ['$event.target.value'])
|
||||||
_handleChangeEvent(value: any) {
|
_handleChangeEvent(value: any) {
|
||||||
this.onChange(value);
|
if (!this.muteOnChange) {
|
||||||
|
this.onChange(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.muteOnChange = false;
|
||||||
|
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
setIonicClasses(this.element);
|
setIonicClasses(this.element);
|
||||||
|
@ -24,7 +24,14 @@ export class TextValueAccessor implements ControlValueAccessor {
|
|||||||
onChange: (value: any) => void;
|
onChange: (value: any) => void;
|
||||||
onTouched: () => void;
|
onTouched: () => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether onChange should be mutted (not be fired). Will be true only when writeValue was called, which
|
||||||
|
* means that value changed inside angular form (e.g. calling setValue on a control).
|
||||||
|
*/
|
||||||
|
private muteOnChange = false;
|
||||||
|
|
||||||
writeValue(value: any) {
|
writeValue(value: any) {
|
||||||
|
this.muteOnChange = true;
|
||||||
this.element.nativeElement.value = value;
|
this.element.nativeElement.value = value;
|
||||||
|
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
@ -34,7 +41,11 @@ export class TextValueAccessor implements ControlValueAccessor {
|
|||||||
|
|
||||||
@HostListener('ionChange', ['$event.target.value'])
|
@HostListener('ionChange', ['$event.target.value'])
|
||||||
_handleInputEvent(value: any) {
|
_handleInputEvent(value: any) {
|
||||||
this.onChange(value);
|
if (!this.muteOnChange) {
|
||||||
|
this.onChange(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.muteOnChange = false;
|
||||||
|
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
setIonicClasses(this.element);
|
setIonicClasses(this.element);
|
||||||
|
Reference in New Issue
Block a user