Skip to content

Commit 2073bf6

Browse files
committed
fix: allow FormView to be compiled
1 parent f3fbe57 commit 2073bf6

File tree

3 files changed

+108
-117
lines changed

3 files changed

+108
-117
lines changed

packages/sanity/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@
261261
"tar-fs": "^2.1.1",
262262
"tar-stream": "^3.1.7",
263263
"use-device-pixel-ratio": "^1.1.0",
264+
"use-effect-event": "^1.0.2",
264265
"use-hot-module-reload": "^2.0.0",
265266
"use-sync-external-store": "^1.2.0",
266267
"vite": "^4.5.1",

packages/sanity/src/structure/panes/document/documentPanel/documentViews/FormView.tsx

+21-15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
useDocumentStore,
1616
useTranslation,
1717
} from 'sanity'
18+
import {useEffectEvent} from 'use-effect-event'
1819

1920
import {Delay} from '../../../../components'
2021
import {structureLocaleNamespace} from '../../../../i18n'
@@ -28,8 +29,6 @@ interface FormViewProps {
2829
margins: [number, number, number, number]
2930
}
3031

31-
const preventDefault = (ev: FormEvent) => ev.preventDefault()
32-
3332
export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormView(props, ref) {
3433
const {hidden, margins} = props
3534

@@ -103,21 +102,23 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
103102
}, [documentId, documentStore, documentType, patchChannel])
104103

105104
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+
})
106116
useEffect(() => {
107117
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()
117119
}
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])
121122

122123
const [formRef, setFormRef] = useState<null | HTMLDivElement>(null)
123124

@@ -165,7 +166,7 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
165166
width={1}
166167
>
167168
<PresenceOverlay margins={margins}>
168-
<Box as="form" onSubmit={preventDefault} ref={setRef}>
169+
<Box as="form" onSubmit={handleSubmit} ref={setRef}>
169170
{connectionState === 'connecting' && !editState?.draft && !editState?.published ? (
170171
<Delay ms={300}>
171172
{/* TODO: replace with loading block */}
@@ -223,6 +224,7 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
223224
</Container>
224225
)
225226
})
227+
FormView.displayName = 'ForwardRef(FormView)'
226228

227229
function prepareMutationEvent(event: DocumentMutationEvent): PatchMsg {
228230
const patches = event.mutations.map((mut) => mut.patch).filter(Boolean)
@@ -246,3 +248,7 @@ function prepareRebaseEvent(event: DocumentRebaseEvent): PatchMsg {
246248
),
247249
}
248250
}
251+
252+
function handleSubmit(ev: FormEvent) {
253+
ev.preventDefault()
254+
}

0 commit comments

Comments
 (0)