@@ -6,7 +6,7 @@ import { ButtonWithLoader, ImageWithFallback } from '@Shared/Components'
6
6
import { validateIfImageExist , validateURL } from '@Shared/validations'
7
7
import { ReactComponent as ICPencil } from '@Icons/ic-pencil.svg'
8
8
import { EditImageFormFieldProps , FallbackImageProps } from './types'
9
- import { DEFAULT_IMAGE_DIMENSIONS , DEFAULT_MAX_IMAGE_SIZE } from './constants'
9
+ import { DEFAULT_IMAGE_DIMENSIONS , DEFAULT_MAX_IMAGE_SIZE , EMPTY_PREVIEW_URL_ERROR_MESSAGE } from './constants'
10
10
import './EditImageFormField.scss'
11
11
12
12
const FallbackImage = ( { showEditIcon, defaultIcon } : FallbackImageProps ) => (
@@ -36,6 +36,8 @@ const EditImageFormField = ({
36
36
const [ lastPreviewedURL , setLastPreviewedURL ] = useState < string > ( url )
37
37
const [ isEditing , setIsEditing ] = useState < boolean > ( false )
38
38
const [ isLoading , setIsLoading ] = useState < boolean > ( false )
39
+ // Since we need to show error message for empty preview URL but not propagate it to parent, we need to maintain a separate state
40
+ const [ emptyPreviewURLErrorMessage , setEmptyPreviewURLErrorMessage ] = useState < string > ( '' )
39
41
40
42
const handleEnableEditing = ( ) => {
41
43
setIsEditing ( true )
@@ -47,6 +49,7 @@ const EditImageFormField = ({
47
49
48
50
const handleReset = ( newURL ?: string ) => {
49
51
handleLastPreviewedURLChange ( newURL ?? lastPreviewedURL )
52
+ handleURLChange ( newURL ?? lastPreviewedURL )
50
53
handleError ( '' )
51
54
setIsEditing ( false )
52
55
setIsLoading ( false )
@@ -63,15 +66,19 @@ const EditImageFormField = ({
63
66
const handleChange = ( event : SyntheticEvent ) => {
64
67
const { value } = event . target as HTMLInputElement
65
68
handleURLChange ( value )
66
- if ( value . trim ( ) ) {
67
- handleError ( validateURL ( value , false ) . message )
69
+ if ( ! value . trim ( ) ) {
70
+ handleError ( '' )
71
+ return
68
72
}
73
+ setEmptyPreviewURLErrorMessage ( '' )
74
+ handleError ( validateURL ( value , false ) . message )
69
75
}
70
76
71
77
const handlePreviewImage = async ( ) => {
72
78
if ( ! url ) {
73
79
// Not setting the error since can save without image
74
- toast . error ( 'Please enter a valid image URL' )
80
+ setEmptyPreviewURLErrorMessage ( EMPTY_PREVIEW_URL_ERROR_MESSAGE )
81
+ toast . error ( EMPTY_PREVIEW_URL_ERROR_MESSAGE )
75
82
return
76
83
}
77
84
@@ -159,10 +166,11 @@ const EditImageFormField = ({
159
166
placeholder = "Enter image url"
160
167
value = { url }
161
168
onChange = { handleChange }
162
- error = { errorMessage }
169
+ error = { errorMessage || emptyPreviewURLErrorMessage }
163
170
inputWrapClassName = "w-100"
164
171
dataTestid = { `${ dataTestIdPrefix } -input` }
165
172
onKeyDown = { handleKeyDown }
173
+ autoFocus
166
174
/>
167
175
</ div >
168
176
0 commit comments