1
1
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'
3
3
import { WizTextDetail } from '..'
4
4
import { ClearInputButton } from '../components/ClearInputButton'
5
5
import { PasteInputButton } from '../components/PasteInputButton'
6
6
import { ShowSecretsButton } from '../components/ShowSecretsButton'
7
7
import { DisplayMode } from '../contexts/DisplayModeContext'
8
8
import { getEnterPlaceholder , InputCommonProps , useInput } from './Input'
9
9
import { WizFormGroup } from './WizFormGroup'
10
+ import useResizeObserver from '@react-hook/resize-observer'
10
11
11
12
export type WizTextAreaProps = InputCommonProps < string > & {
12
13
label : string
@@ -21,6 +22,31 @@ export function WizTextArea(props: WizTextAreaProps) {
21
22
// Hide initially if a value is set
22
23
const [ showSecrets , setShowSecrets ] = useState ( ! value )
23
24
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
+
24
50
const onChange = useCallback < NonNullable < TextAreaProps [ 'onChange' ] > > ( ( _event , value ) => setValue ( value ) , [ setValue ] )
25
51
26
52
if ( hidden ) return < Fragment />
@@ -48,8 +74,9 @@ export function WizTextArea(props: WizTextAreaProps) {
48
74
type = { ! props . secret || showSecrets ? 'text' : 'password' }
49
75
spellCheck = "false"
50
76
resizeOrientation = "vertical"
51
- autoResize = { ! ! value } // Only enable after text has been entered; bug with initial size calculation
77
+ autoResize
52
78
readOnlyVariant = { props . readonly ? 'default' : undefined }
79
+ ref = { textAreaRef }
53
80
/>
54
81
) }
55
82
{ ! disabled && value !== '' && props . secret && (
0 commit comments