Skip to content

Commit f8d9c96

Browse files
authored
fix(input): avoid repeat accesses to check if element is a textarea (#19115)
Since the node's type can't change, it's unnecessary to check the DOM in the `_isTextarea` on every invocation. This affects the MDC form field in particular since it's used in a data binding.
1 parent 7fde305 commit f8d9c96

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/material-experimental/mdc-input/input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {MatInput as BaseMatInput} from '@angular/material/input';
2525
'[class.mat-form-field-autofill-control]': 'false',
2626
'[class.mat-input-element]': 'false',
2727
'[class.mat-input-server]': '_isServer',
28-
'[class.mat-mdc-textarea-input]': '_isTextarea()',
28+
'[class.mat-mdc-textarea-input]': '_isTextarea',
2929
// Native input properties that are overwritten by Angular inputs need to be synced with
3030
// the native input element. Otherwise property bindings for those don't work.
3131
'[id]': 'id',

src/material/input/input.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,13 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
9696
_ariaDescribedby: string;
9797

9898
/** Whether the component is being rendered on the server. */
99-
_isServer = false;
99+
readonly _isServer: boolean;
100100

101101
/** Whether the component is a native html select. */
102-
_isNativeSelect = false;
102+
readonly _isNativeSelect: boolean;
103+
104+
/** Whether the component is a textarea. */
105+
readonly _isTextarea: boolean;
103106

104107
/**
105108
* Implemented as part of MatFormFieldControl.
@@ -182,7 +185,7 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
182185
// When using Angular inputs, developers are no longer able to set the properties on the native
183186
// input element. To ensure that bindings for `type` work, we need to sync the setter
184187
// with the native property. Textarea elements don't support the type property or attribute.
185-
if (!this._isTextarea() && getSupportedInputTypes().has(this._type)) {
188+
if (!this._isTextarea && getSupportedInputTypes().has(this._type)) {
186189
(this._elementRef.nativeElement as HTMLInputElement).type = this._type;
187190
}
188191
}
@@ -234,6 +237,7 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
234237
super(_defaultErrorStateMatcher, _parentForm, _parentFormGroup, ngControl);
235238

236239
const element = this._elementRef.nativeElement;
240+
const nodeName = element.nodeName.toLowerCase();
237241

238242
// If no input value accessor was explicitly specified, use the element as the input value
239243
// accessor.
@@ -264,7 +268,8 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
264268
}
265269

266270
this._isServer = !this._platform.isBrowser;
267-
this._isNativeSelect = element.nodeName.toLowerCase() === 'select';
271+
this._isNativeSelect = nodeName === 'select';
272+
this._isTextarea = nodeName === 'textarea';
268273

269274
if (this._isNativeSelect) {
270275
this.controlType = (element as HTMLSelectElement).multiple ? 'mat-native-select-multiple' :
@@ -344,11 +349,6 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
344349
// FormsModule or ReactiveFormsModule, because Angular forms also listens to input events.
345350
}
346351

347-
/** Determines if the component host is a textarea. */
348-
_isTextarea() {
349-
return this._elementRef.nativeElement.nodeName.toLowerCase() === 'textarea';
350-
}
351-
352352
/** Does some manual dirty checking on the native input `value` property. */
353353
protected _dirtyCheckNativeValue() {
354354
const newValue = this._elementRef.nativeElement.value;

tools/public_api_guard/material/input.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ export declare class MatInput extends _MatInputMixinBase implements MatFormField
99
protected _disabled: boolean;
1010
protected _elementRef: ElementRef<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>;
1111
protected _id: string;
12-
_isNativeSelect: boolean;
13-
_isServer: boolean;
12+
readonly _isNativeSelect: boolean;
13+
readonly _isServer: boolean;
14+
readonly _isTextarea: boolean;
1415
protected _neverEmptyInputTypes: string[];
1516
protected _platform: Platform;
1617
protected _previousNativeValue: any;
@@ -44,7 +45,6 @@ export declare class MatInput extends _MatInputMixinBase implements MatFormField
4445
_focusChanged(isFocused: boolean): void;
4546
protected _isBadInput(): boolean;
4647
protected _isNeverEmpty(): boolean;
47-
_isTextarea(): boolean;
4848
_onInput(): void;
4949
protected _validateType(): void;
5050
focus(options?: FocusOptions): void;

0 commit comments

Comments
 (0)