Skip to content

Commit

Permalink
fix(core/form): prevent onFocus for root object paths being called by…
Browse files Browse the repository at this point in the history
… editing form (#6610)

This will prevent any input calling element.onFocus() on any opened block or inline-object
inside the PT-input, as that will close the editing modal for them (through DocumentPaneProvider)
  • Loading branch information
skogsmaskin authored and ricokahler committed May 14, 2024
1 parent 14be9b6 commit 516ada5
Showing 1 changed file with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {pathFor} from '@sanity/util/paths'
import {isEqual, pathFor} from '@sanity/util/paths'
import {createRef, type MutableRefObject, type ReactNode, useContext, useRef} from 'react'
import {PortableTextMemberItemsContext} from 'sanity/_singletons'

Expand All @@ -7,7 +7,7 @@ import {type FIXME} from '../../../../FIXME'
import {FormInput} from '../../../components'
import {isMemberArrayOfObjects} from '../../../members/object/fields/asserters'
import {type ArrayOfObjectsItemMember, type ObjectFormNode} from '../../../store'
import {type PortableTextInputProps} from '../../../types'
import {type ObjectInputProps, type PortableTextInputProps} from '../../../types'
import {isArrayOfObjectsFieldMember, isBlockType} from '../_helpers'
import {type PortableTextEditorElement} from '../Compositor'
import {type PortableTextMemberItem} from '../PortableTextInput'
Expand Down Expand Up @@ -130,6 +130,23 @@ const reconcilePortableTextMembers = ({
let input: ReactNode = null

if ((isObject && item.member !== existingItem?.member) || item.node !== existingItem?.node) {
// Render input with onFocus noop for calling elementProps.onFocus directly on the editor nodes themselves
// This is to avoid closing the editing modal for them in the PT-input setting.
const _renderInput = (renderInputProps: ObjectInputProps) => {
const isObjectInputPath = isEqual(renderInputProps.path, item.member.item.path)
if (isObjectInputPath) {
return renderInput({
...renderInputProps,
elementProps: {
...renderInputProps.elementProps,
onFocus: () => {
// no-op
},
},
})
}
return renderInput(renderInputProps)
}
const inputProps = {
absolutePath: pathFor(item.node.path),
includeField: false,
Expand All @@ -139,7 +156,7 @@ const reconcilePortableTextMembers = ({
renderBlock,
renderField,
renderInlineBlock,
renderInput,
renderInput: _renderInput,
renderItem,
renderPreview,
schemaType,
Expand Down

0 comments on commit 516ada5

Please sign in to comment.