1
- import React , { forwardRef , useEffect , useState } from 'react' ;
1
+ import React , { forwardRef , useEffect , useRef , useState } from 'react' ;
2
2
import { extractClassNameProps , extractWidthProps , extractWrapperMarginProps } from '../../utils/extractProps' ;
3
3
import { useGeneratedId } from '../../utils/hooks/useGeneratedId' ;
4
4
import { BottomLinedInput } from './BottomLinedInput' ;
@@ -13,31 +13,36 @@ const InnerInput = forwardRef<HTMLDivElement, InputWrapperProps & InputProps>(
13
13
const { classNameProps, restProps : withoutClassName } = extractClassNameProps ( props ) ;
14
14
const { marginProps, restProps : withoutMargin } = extractWrapperMarginProps ( withoutClassName ) ;
15
15
const { widthProps, restProps } = extractWidthProps ( withoutMargin ) ;
16
+ // TODO replace with ref when implementing https://github.com/freenowtech/wave/issues/169
17
+ const inputRef = useRef < HTMLInputElement > ( ) ;
16
18
17
19
const { label, onChange, size, id : providedId , variant, ...rest } = restProps ;
18
20
const id = useGeneratedId ( providedId ) ;
19
21
20
- const [ hasValue , setHasValue ] = useState ( rest . value && rest . value . toString ( ) . length > 0 ) ;
22
+ const [ hasValue , setHasValue ] = useState ( rest ?. value ?. toString ( ) . length > 0 ) ;
23
+ const hasUncontrolledValue = inputRef ?. current ?. value ?. length > 0 ;
21
24
22
- const handleChange = event => {
25
+ const handleChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
26
+ setHasValue ( event . target . value . length > 0 ) ;
23
27
if ( onChange ) {
24
28
onChange ( event ) ;
25
29
}
26
30
} ;
27
31
28
32
useEffect ( ( ) => {
29
- setHasValue ( rest . value && rest . value . toString ( ) . length > 0 ) ;
33
+ setHasValue ( rest ? .value ? .toString ( ) . length > 0 ) ;
30
34
} , [ rest . value ] ) ;
31
35
32
36
if ( variant === 'boxed' ) {
33
37
return (
34
38
< InputWrapper ref = { ref } { ...classNameProps } { ...marginProps } { ...widthProps } >
35
39
< BoxedInput
36
40
{ ...rest }
41
+ ref = { inputRef }
37
42
variant = { variant }
38
43
id = { id }
39
44
size = { size }
40
- hasValue = { hasValue }
45
+ hasValue = { hasValue || hasUncontrolledValue }
41
46
onChange = { handleChange }
42
47
/>
43
48
{ label && (
@@ -57,7 +62,7 @@ const InnerInput = forwardRef<HTMLDivElement, InputWrapperProps & InputProps>(
57
62
variant = { variant }
58
63
id = { id }
59
64
size = { size }
60
- hasValue = { hasValue }
65
+ hasValue = { hasValue || hasUncontrolledValue }
61
66
onChange = { handleChange }
62
67
/>
63
68
{ label && (
0 commit comments