Skip to content

Commit 02d7e71

Browse files
authored
fix(text-area): autoresize (#2720)
1 parent e6ca914 commit 02d7e71

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

.changeset/empty-ties-heal.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
"@patternfly/elements": patch
3+
---
4+
`<pf-text-area>`: auto-resize attribute now works as expected

elements/pf-text-area/pf-text-area.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export class PfTextArea extends LitElement {
144144

145145
static readonly formAssociated = true;
146146

147-
static override shadowRootOptions: ShadowRootInit = { ...LitElement.shadowRootOptions, delegatesFocus: true };
147+
static override readonly shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true };
148148

149149
/** Accessible label for the input when no `<label>` element is provided. */
150150
@property({ reflect: true, attribute: 'accessible-label' }) accessibleLabel?: string;
@@ -160,7 +160,7 @@ export class PfTextArea extends LitElement {
160160
/** Flag to show if the input is disabled. */
161161
@property({ type: Boolean, reflect: true }) disabled = false;
162162

163-
/** Flag to show if the input is required. */
163+
/** Flag to show if the text area is required. */
164164
@property({ type: Boolean, reflect: true }) required = false;
165165

166166
/** Flag to show if the input is read only. */
@@ -175,9 +175,11 @@ export class PfTextArea extends LitElement {
175175
/** Sets the orientation to limit the resize to */
176176
@property() resize?: 'horizontal' | 'vertical' | 'both';
177177

178-
/** Sets the orientation to limit the resize to */
178+
/** Flag to modify height based on contents. */
179179
@property({ type: Boolean, attribute: 'auto-resize' }) autoResize = false;
180180

181+
#style?: CSSStyleDeclaration;
182+
181183
#logger = new Logger(this);
182184

183185
#internals = InternalsController.of(this);
@@ -198,6 +200,7 @@ export class PfTextArea extends LitElement {
198200
return html`
199201
<textarea id="textarea" class="${classMap(classes)}"
200202
@input="${this.#onInput}"
203+
@change="${this.#onInput}"
201204
?disabled="${this.matches(':disabled') || this.disabled}"
202205
?readonly="${this.readonly}"
203206
?required="${this.required}"
@@ -208,12 +211,26 @@ export class PfTextArea extends LitElement {
208211
`;
209212
}
210213

214+
override firstUpdated(): void {
215+
if (this.autoResize) {
216+
this.#autoSetHeight();
217+
}
218+
}
219+
211220
#onInput(event: Event) {
212221
if (event.target instanceof HTMLTextAreaElement) {
213222
const { value } = event.target;
214223
this.value = value;
215224
this.#internals.setFormValue(value);
216225
}
226+
if (this.autoResize) {
227+
this.#autoSetHeight();
228+
}
229+
}
230+
231+
#autoSetHeight() {
232+
this.#input.style.setProperty('--pf-c-form-control--textarea--Height', `auto`);
233+
this.#input.style.setProperty('--pf-c-form-control--textarea--Height', `${this.#input.scrollHeight}px`);
217234
}
218235

219236
#setValidityFromInput() {

0 commit comments

Comments
 (0)