Skip to content

Commit 202650f

Browse files
committed
fix: handling for password field
1 parent 01a22db commit 202650f

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/Shared/Components/CustomInput/PasswordField.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react'
1+
import { useMemo, useState } from 'react'
22
import { DEFAULT_SECRET_PLACEHOLDER } from '@Shared/constants'
33
import { ReactComponent as ICVisibilityOn } from '@Icons/ic-visibility-on.svg'
44
import { ReactComponent as ICVisibilityOff } from '@Icons/ic-visibility-off.svg'
@@ -7,35 +7,41 @@ import { CustomInputProps, PasswordFieldProps } from './types'
77

88
const PasswordField = ({ onFocus, onBlur, shouldShowDefaultPlaceholderOnBlur, ...props }: PasswordFieldProps) => {
99
const [fieldType, setFieldType] = useState<CustomInputProps['type']>('password')
10+
const [isFocussed, setIsFocussed] = useState(false)
1011

1112
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 ''
1716
}
1817

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)
1927
onFocus?.(event)
2028
}
2129

2230
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)
2732
setFieldType('password')
2833
onBlur?.(event)
2934
}
3035

3136
return (
3237
<CustomInput
3338
{...props}
39+
value={value}
3440
onFocus={handleFocus}
3541
onBlur={handleBlur}
3642
type={fieldType}
3743
endIconButtonConfig={
38-
props.value && props.value !== DEFAULT_SECRET_PLACEHOLDER
44+
value && value !== DEFAULT_SECRET_PLACEHOLDER
3945
? {
4046
icon: isFieldTypePassword ? <ICVisibilityOn /> : <ICVisibilityOff />,
4147
onClick: () => {

0 commit comments

Comments
 (0)