1
- import { useState } from 'react'
1
+ import { useMemo , useState } from 'react'
2
2
import { DEFAULT_SECRET_PLACEHOLDER } from '@Shared/constants'
3
3
import { ReactComponent as ICVisibilityOn } from '@Icons/ic-visibility-on.svg'
4
4
import { ReactComponent as ICVisibilityOff } from '@Icons/ic-visibility-off.svg'
@@ -7,35 +7,41 @@ import { CustomInputProps, PasswordFieldProps } from './types'
7
7
8
8
const PasswordField = ( { onFocus, onBlur, shouldShowDefaultPlaceholderOnBlur, ...props } : PasswordFieldProps ) => {
9
9
const [ fieldType , setFieldType ] = useState < CustomInputProps [ 'type' ] > ( 'password' )
10
+ const [ isFocussed , setIsFocussed ] = useState ( false )
10
11
11
12
const isFieldTypePassword = fieldType === 'password'
12
-
13
- const handleFocus : CustomInputProps [ 'onFocus' ] = ( event ) => {
14
- if ( event . target . value === DEFAULT_SECRET_PLACEHOLDER ) {
15
- // eslint-disable-next-line no-param-reassign
16
- event . target . value = ''
13
+ const value = useMemo ( ( ) => {
14
+ if ( shouldShowDefaultPlaceholderOnBlur && isFocussed && props . value === DEFAULT_SECRET_PLACEHOLDER ) {
15
+ return ''
17
16
}
18
17
18
+ return ! shouldShowDefaultPlaceholderOnBlur ||
19
+ isFocussed ||
20
+ ( typeof props . value === 'string' && props . value ?. length > 0 )
21
+ ? props . value
22
+ : DEFAULT_SECRET_PLACEHOLDER
23
+ } , [ shouldShowDefaultPlaceholderOnBlur , isFocussed , props . value ] )
24
+
25
+ const handleFocus : CustomInputProps [ 'onFocus' ] = ( event ) => {
26
+ setIsFocussed ( true )
19
27
onFocus ?.( event )
20
28
}
21
29
22
30
const handleBlur : CustomInputProps [ 'onBlur' ] = ( event ) => {
23
- if ( shouldShowDefaultPlaceholderOnBlur && ! event . target . value ) {
24
- // eslint-disable-next-line no-param-reassign
25
- event . target . value = DEFAULT_SECRET_PLACEHOLDER
26
- }
31
+ setIsFocussed ( false )
27
32
setFieldType ( 'password' )
28
33
onBlur ?.( event )
29
34
}
30
35
31
36
return (
32
37
< CustomInput
33
38
{ ...props }
39
+ value = { value }
34
40
onFocus = { handleFocus }
35
41
onBlur = { handleBlur }
36
42
type = { fieldType }
37
43
endIconButtonConfig = {
38
- props . value && props . value !== DEFAULT_SECRET_PLACEHOLDER
44
+ value && value !== DEFAULT_SECRET_PLACEHOLDER
39
45
? {
40
46
icon : isFieldTypePassword ? < ICVisibilityOn /> : < ICVisibilityOff /> ,
41
47
onClick : ( ) => {
0 commit comments