Skip to content

Commit

Permalink
Small code quality improvements (angular#9884)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeigl committed May 8, 2018
1 parent 01d1658 commit 3b111e9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
15 changes: 7 additions & 8 deletions src/cdk/text-field/autosize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ describe('CdkTextareaAutosize', () => {

fixtureWithoutAutosize.detectChanges();

let previousHeight = textarea.clientHeight;
const previousHeight = textarea.clientHeight;

fixtureWithoutAutosize.componentInstance.content = `
Line
Expand All @@ -242,25 +242,24 @@ describe('CdkTextareaAutosize', () => {
expect(textarea.clientHeight)
.toEqual(previousHeight, 'Expected textarea to still have the same size.');
expect(textarea.clientHeight)
.toBeLessThan(textarea.scrollHeight,
'Expected textarea height to be less than its scrollHeight');
.toBeLessThan(textarea.scrollHeight, 'Expected textarea to a have scrollbar.');

autosize.enabled = true;
fixtureWithoutAutosize.detectChanges();

expect(textarea.clientHeight)
.toBeGreaterThan(previousHeight, 'Expected textarea to have grown after enabling.');
.toBeGreaterThan(previousHeight,
'Expected textarea to have grown after enabling autosize.');
expect(textarea.clientHeight)
.toBe(textarea.scrollHeight, 'Expected textarea height to match its scrollHeight');
.toBe(textarea.scrollHeight, 'Expected textarea not to have a scrollbar');

autosize.enabled = false;
fixtureWithoutAutosize.detectChanges();

expect(textarea.clientHeight)
.toEqual(previousHeight, 'Expected textarea to again have the original size.');
.toEqual(previousHeight, 'Expected textarea to have the original size.');
expect(textarea.clientHeight)
.toBeLessThan(textarea.scrollHeight,
'Expected textarea height to be less than its scrollHeight again');
.toBeLessThan(textarea.scrollHeight, 'Expected textarea to have a scrollbar.');
}));
});

Expand Down
22 changes: 12 additions & 10 deletions src/cdk/text-field/autosize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
private _maxRows: number;
private _enabled: boolean = true;

private get textarea(): HTMLTextAreaElement {
return this._elementRef.nativeElement as HTMLTextAreaElement;
}
private _textareaElement: HTMLTextAreaElement;

/** Minimum amount of rows in the textarea. */
@Input('cdkAutosizeMinRows')
Expand Down Expand Up @@ -82,7 +80,9 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
constructor(
private _elementRef: ElementRef,
private _platform: Platform,
private _ngZone: NgZone) {}
private _ngZone: NgZone) {
this._textareaElement = this._elementRef.nativeElement as HTMLTextAreaElement;
}

/** Sets the minimum height of the textarea as determined by minRows. */
_setMinHeight(): void {
Expand All @@ -107,7 +107,7 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
ngAfterViewInit() {
if (this._platform.isBrowser) {
// Remember the height which we started with in case autosizing is disabled
this._initialHeight = this.textarea.style.height;
this._initialHeight = this._textareaElement.style.height;

this.resizeToFitContent();

Expand All @@ -126,7 +126,7 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {

/** Sets a style property on the textarea element. */
private _setTextareaStyle(property: string, value: string): void {
this.textarea.style[property] = value;
this._textareaElement.style[property] = value;
}

/**
Expand All @@ -142,7 +142,7 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
}

// Use a clone element because we have to override some styles.
let textareaClone = this.textarea.cloneNode(false) as HTMLTextAreaElement;
let textareaClone = this._textareaElement.cloneNode(false) as HTMLTextAreaElement;
textareaClone.rows = 1;

// Use `position: absolute` so that this doesn't cause a browser layout and use
Expand All @@ -163,9 +163,9 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
// See Firefox bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=33654
textareaClone.style.overflow = 'hidden';

this.textarea.parentNode!.appendChild(textareaClone);
this._textareaElement.parentNode!.appendChild(textareaClone);
this._cachedLineHeight = textareaClone.clientHeight;
this.textarea.parentNode!.removeChild(textareaClone);
this._textareaElement.parentNode!.removeChild(textareaClone);

// Min and max heights have to be re-calculated if the cached line height changes
this._setMinHeight();
Expand Down Expand Up @@ -240,10 +240,12 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
* Resets the textarea to it's original size
*/
reset() {
// Do not try to change the textarea, if the initialHeight has not been determined yet
// This might potentially remove styles when reset() is called before ngAfterViewInit
if (this._initialHeight === undefined) {
return;
}
this.textarea.style.height = this._initialHeight;
this._textareaElement.style.height = this._initialHeight;
}

_noopInputHandler() {
Expand Down

0 comments on commit 3b111e9

Please sign in to comment.