Skip to content

Commit

Permalink
fix: field instruction item should no longer be created twice in Reac…
Browse files Browse the repository at this point in the history
…t strict mode
  • Loading branch information
snorrees committed Jun 27, 2023
1 parent 938b8cb commit b64d95b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions plugin/src/assistDocument/components/AssistDocumentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
AssistField,
instructionParam,
} from '../../types'
import {useEffect, useMemo} from 'react'
import {useEffect, useMemo, useRef} from 'react'
import {
FormCallbacksProvider,
FormCallbacksValue,
Expand Down Expand Up @@ -39,6 +39,9 @@ export function AssistDocumentForm(props: ObjectInputProps) {
const id = value?._id
const fields = value?.fields

// need this to not fire onChange twice in React strict mode
const onChangeCalled = useRef(false)

const targetDocumentType = useMemo(() => {
if (!id) {
return undefined
Expand Down Expand Up @@ -88,8 +91,9 @@ export function AssistDocumentForm(props: ObjectInputProps) {
}
}, [title, documentSchema, onChange, id])

const fieldMissing = fields?.find((f) => f._key !== pathKey)
useEffect(() => {
if (activePath || !pathKey) {
if (onChangeCalled.current || !fieldMissing || activePath || !pathKey) {
return
}
onChange([
Expand All @@ -106,7 +110,8 @@ export function AssistDocumentForm(props: ObjectInputProps) {
['fields', -1]
),
])
}, [activePath, onChange, pathKey])
onChangeCalled.current = true
}, [activePath, onChange, pathKey, fieldMissing])

const {onPathOpen, ...formCallbacks} = useFormCallbacks()

Expand Down

0 comments on commit b64d95b

Please sign in to comment.