Skip to content

Commit

Permalink
fix(cdk/text-field): handle undefined placeholder (#24159)
Browse files Browse the repository at this point in the history
Fixes that the string "undefined" would be used as the placeholder of an autosize `textarea` if the value is `undefined`.

Fixes #24154.

(cherry picked from commit 93e09c6)
  • Loading branch information
crisbeto authored and wagnermaciel committed Jan 6, 2022
1 parent 37898c3 commit 7705cae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/cdk/text-field/autosize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,13 @@ describe('CdkTextareaAutosize', () => {
.withContext('Expected textarea to have a scrollbar.')
.toBeLessThan(textarea.scrollHeight);
}));

it('should handle an undefined placeholder', () => {
fixture.componentInstance.placeholder = undefined!;
fixture.detectChanges();

expect(textarea.hasAttribute('placeholder')).toBe(false);
});
});

// Styles to reset padding and border to make measurement comparisons easier.
Expand Down
8 changes: 7 additions & 1 deletion src/cdk/text-field/autosize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
}
set placeholder(value: string) {
this._cachedPlaceholderHeight = undefined;
this._textareaElement.placeholder = value;

if (value) {
this._textareaElement.setAttribute('placeholder', value);
} else {
this._textareaElement.removeAttribute('placeholder');
}

this._cacheTextareaPlaceholderHeight();
}

Expand Down

0 comments on commit 7705cae

Please sign in to comment.