Skip to content

Commit 4bfb937

Browse files
committed
Workaround for TextArea autoResize issue
Signed-off-by: Kevin Cormier <kcormier@redhat.com>
1 parent 1c6ed9c commit 4bfb937

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/inputs/WizTextArea.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { InputGroup, TextArea as PFTextArea, TextAreaProps, TextInput } from '@patternfly/react-core'
2-
import { Fragment, useCallback, useState } from 'react'
2+
import { Fragment, useCallback, useRef, useState } from 'react'
33
import { WizTextDetail } from '..'
44
import { ClearInputButton } from '../components/ClearInputButton'
55
import { PasteInputButton } from '../components/PasteInputButton'
66
import { ShowSecretsButton } from '../components/ShowSecretsButton'
77
import { DisplayMode } from '../contexts/DisplayModeContext'
88
import { getEnterPlaceholder, InputCommonProps, useInput } from './Input'
99
import { WizFormGroup } from './WizFormGroup'
10+
import useResizeObserver from '@react-hook/resize-observer'
1011

1112
export type WizTextAreaProps = InputCommonProps<string> & {
1213
label: string
@@ -21,6 +22,31 @@ export function WizTextArea(props: WizTextAreaProps) {
2122
// Hide initially if a value is set
2223
const [showSecrets, setShowSecrets] = useState(!value)
2324

25+
// Workaround for problem with PatternFly TextArea autoResize feature
26+
// scrollHeight is still 0 when this code runs in PatternFly
27+
const textAreaRef = useRef<HTMLTextAreaElement>(null)
28+
const [initialHeightSet, setInitialHeightSet] = useState(false)
29+
const setInitialHeight = useCallback(() => {
30+
const field = textAreaRef.current
31+
if (!initialHeightSet && field) {
32+
const parent = field.parentElement
33+
if (parent) {
34+
parent.style.setProperty('height', 'inherit')
35+
const computed = window.getComputedStyle(field)
36+
// Calculate the height
37+
const height =
38+
parseInt(computed.getPropertyValue('border-top-width')) +
39+
parseInt(computed.getPropertyValue('padding-top')) +
40+
field.scrollHeight +
41+
parseInt(computed.getPropertyValue('padding-bottom')) +
42+
parseInt(computed.getPropertyValue('border-bottom-width'))
43+
parent.style.setProperty('height', `${height}px`)
44+
setInitialHeightSet(true)
45+
}
46+
}
47+
}, [initialHeightSet])
48+
useResizeObserver(textAreaRef, setInitialHeight)
49+
2450
const onChange = useCallback<NonNullable<TextAreaProps['onChange']>>((_event, value) => setValue(value), [setValue])
2551

2652
if (hidden) return <Fragment />
@@ -48,8 +74,9 @@ export function WizTextArea(props: WizTextAreaProps) {
4874
type={!props.secret || showSecrets ? 'text' : 'password'}
4975
spellCheck="false"
5076
resizeOrientation="vertical"
51-
autoResize={!!value} // Only enable after text has been entered; bug with initial size calculation
77+
autoResize
5278
readOnlyVariant={props.readonly ? 'default' : undefined}
79+
ref={textAreaRef}
5380
/>
5481
)}
5582
{!disabled && value !== '' && props.secret && (

0 commit comments

Comments
 (0)