@@ -15,6 +15,7 @@ import {
15
15
useDocumentStore ,
16
16
useTranslation ,
17
17
} from 'sanity'
18
+ import { useEffectEvent } from 'use-effect-event'
18
19
19
20
import { Delay } from '../../../../components'
20
21
import { structureLocaleNamespace } from '../../../../i18n'
@@ -28,8 +29,6 @@ interface FormViewProps {
28
29
margins : [ number , number , number , number ]
29
30
}
30
31
31
- const preventDefault = ( ev : FormEvent ) => ev . preventDefault ( )
32
-
33
32
export const FormView = forwardRef < HTMLDivElement , FormViewProps > ( function FormView ( props , ref ) {
34
33
const { hidden, margins} = props
35
34
@@ -103,21 +102,23 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
103
102
} , [ documentId , documentStore , documentType , patchChannel ] )
104
103
105
104
const hasRev = Boolean ( value ?. _rev )
105
+ const handleInitialRev = useEffectEvent ( ( ) => {
106
+ // this is a workaround for an issue that caused the document pushed to withDocument to get
107
+ // stuck at the first initial value.
108
+ // This effect is triggered only when the document goes from not having a revision, to getting one
109
+ // so it will kick in as soon as the document is received from the backend
110
+ patchChannel . publish ( {
111
+ type : 'mutation' ,
112
+ patches : [ ] ,
113
+ snapshot : value ,
114
+ } )
115
+ } )
106
116
useEffect ( ( ) => {
107
117
if ( hasRev ) {
108
- // this is a workaround for an issue that caused the document pushed to withDocument to get
109
- // stuck at the first initial value.
110
- // This effect is triggered only when the document goes from not having a revision, to getting one
111
- // so it will kick in as soon as the document is received from the backend
112
- patchChannel . publish ( {
113
- type : 'mutation' ,
114
- patches : [ ] ,
115
- snapshot : value ,
116
- } )
118
+ handleInitialRev ( )
117
119
}
118
- // React to changes in hasRev only
119
- // eslint-disable-next-line react-hooks/exhaustive-deps
120
- } , [ hasRev ] )
120
+ // React to changes in hasRev only, useEffectEvent let's us do this without supressing the linter
121
+ } , [ handleInitialRev , hasRev ] )
121
122
122
123
const [ formRef , setFormRef ] = useState < null | HTMLDivElement > ( null )
123
124
@@ -165,7 +166,7 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
165
166
width = { 1 }
166
167
>
167
168
< PresenceOverlay margins = { margins } >
168
- < Box as = "form" onSubmit = { preventDefault } ref = { setRef } >
169
+ < Box as = "form" onSubmit = { handleSubmit } ref = { setRef } >
169
170
{ connectionState === 'connecting' && ! editState ?. draft && ! editState ?. published ? (
170
171
< Delay ms = { 300 } >
171
172
{ /* TODO: replace with loading block */ }
@@ -223,6 +224,7 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
223
224
</ Container >
224
225
)
225
226
} )
227
+ FormView . displayName = 'ForwardRef(FormView)'
226
228
227
229
function prepareMutationEvent ( event : DocumentMutationEvent ) : PatchMsg {
228
230
const patches = event . mutations . map ( ( mut ) => mut . patch ) . filter ( Boolean )
@@ -246,3 +248,7 @@ function prepareRebaseEvent(event: DocumentRebaseEvent): PatchMsg {
246
248
) ,
247
249
}
248
250
}
251
+
252
+ function handleSubmit ( ev : FormEvent ) {
253
+ ev . preventDefault ( )
254
+ }
0 commit comments