Skip to content

Commit 93e09c6

Browse files
authored
fix(cdk/text-field): handle undefined placeholder (#24159)
Fixes that the string "undefined" would be used as the placeholder of an autosize `textarea` if the value is `undefined`. Fixes #24154.
1 parent a6d21a1 commit 93e09c6

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/cdk/text-field/autosize.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,13 @@ describe('CdkTextareaAutosize', () => {
373373
.withContext('Expected textarea to have a scrollbar.')
374374
.toBeLessThan(textarea.scrollHeight);
375375
}));
376+
377+
it('should handle an undefined placeholder', () => {
378+
fixture.componentInstance.placeholder = undefined!;
379+
fixture.detectChanges();
380+
381+
expect(textarea.hasAttribute('placeholder')).toBe(false);
382+
});
376383
});
377384

378385
// Styles to reset padding and border to make measurement comparisons easier.

src/cdk/text-field/autosize.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,13 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
100100
}
101101
set placeholder(value: string) {
102102
this._cachedPlaceholderHeight = undefined;
103-
this._textareaElement.placeholder = value;
103+
104+
if (value) {
105+
this._textareaElement.setAttribute('placeholder', value);
106+
} else {
107+
this._textareaElement.removeAttribute('placeholder');
108+
}
109+
104110
this._cacheTextareaPlaceholderHeight();
105111
}
106112

0 commit comments

Comments
 (0)