Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions packages/forms/src/directives/select_control_value_accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
*/

import {
afterNextRender,
ChangeDetectorRef,
Directive,
ElementRef,
EnvironmentInjector,
forwardRef,
Host,
inject,
Input,
OnDestroy,
Optional,
Provider,
Renderer2,
runInInjectionContext,
ɵRuntimeError as RuntimeError,
} from '@angular/core';

Expand Down Expand Up @@ -135,12 +140,55 @@ export class SelectControlValueAccessor
}

private _compareWith: (o1: any, o2: any) => boolean = Object.is;
private readonly _injector = inject(EnvironmentInjector);
private readonly cdr = inject(ChangeDetectorRef);
private _queuedWrite = false;

/**
* This is needed to efficiently set the select value when adding/removing options. If
* writeValue is instead called for every added/removed option, this results in exponentially
* more _compareValue calls than the number of option elements (issue #41330).
*
* Secondly, calling writeValue when rendering individual option elements instead of after they
* are all rendered caused an issue in Safari and IE 11 where the first option element failed
* to be deselected when no option matched the select ngModel. This was because Angular would
* set the select element's value property before appending the option's child text node to the
* DOM (issue #14505).
*
* Finally, this approach is necessary to avoid an issue with delayed element removal when
* using the animations module (in all browsers). Otherwise when a selected option is removed
* (so no option matches the ngModel anymore), Angular would change the select element value
* before actually removing the option from the DOM. Then when the option is finally removed
* from the DOM, the browser would change the select value to that of the first option, even
* though it doesn't match the ngModel (issue #18430).
*
* @internal
*/
_writeValueAfterRender(): void {
if (this._queuedWrite) {
return;
}

this._queuedWrite = true;

runInInjectionContext(this._injector, () => {
Comment thread
theodorejb marked this conversation as resolved.
Resolved
afterNextRender({
write: () => {
this._queuedWrite = false;
this.writeValue(this.value);
},
});
});
}

/**
* Sets the "value" property on the select element.
* @nodoc
*/
writeValue(value: any): void {
if (value !== this.value) {
this.cdr.markForCheck();
}
this.value = value;
const id: string | null = this._getOptionId(value);
const valueString = _buildValueString(id, value);
Expand Down Expand Up @@ -215,7 +263,7 @@ export class NgSelectOption implements OnDestroy {
if (this._select == null) return;
this._select._optionMap.set(this.id, value);
this._setElementValue(_buildValueString(this.id, value));
this._select.writeValue(this._select.value);
this._select._writeValueAfterRender();
}

/**
Expand All @@ -226,7 +274,7 @@ export class NgSelectOption implements OnDestroy {
@Input('value')
set value(value: any) {
this._setElementValue(value);
if (this._select) this._select.writeValue(this._select.value);
if (this._select) this._select._writeValueAfterRender();
}

/** @internal */
Expand All @@ -238,7 +286,7 @@ export class NgSelectOption implements OnDestroy {
ngOnDestroy(): void {
if (this._select) {
this._select._optionMap.delete(this.id);
this._select.writeValue(this._select.value);
this._select._writeValueAfterRender();
}
}
}
2 changes: 2 additions & 0 deletions packages/forms/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ ts_library(
# Visible to //:saucelabs_unit_tests_poc target
visibility = ["//:__pkg__"],
deps = [
"//packages/animations/browser",
"//packages/common",
"//packages/core",
"//packages/core/testing",
"//packages/forms",
"//packages/platform-browser",
"//packages/platform-browser/animations",
"//packages/platform-browser/testing",
"//packages/private/testing",
"@npm//rxjs",
Expand Down
Loading