Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(@sanity): remove warning for when child within PTE has no key #6565

Merged
merged 8 commits into from
May 6, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ export function createWithSchemaTypes({
const key = span._key || keyGenerator()
Transforms.setNodes(editor, {...span, _type: schemaTypes.span.name, _key: key}, {at: path})
}

// catches cases when the children are missing keys but excludes it when the normalize is running the node as the editor object
if (node._key === undefined && (path.length === 1 || path.length === 2)) {
debug('Setting missing key on child node without a key')
const key = keyGenerator()
Transforms.setNodes(editor, {_key: key}, {at: path})
}
normalizeNode(entry)
}
return editor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {describe, expect, it, jest} from '@jest/globals'
import {render, waitFor} from '@testing-library/react'
import {createRef, type RefObject} from 'react'

import {PortableTextEditorTester, schemaType} from '../../editor/__tests__/PortableTextEditorTester'
import {PortableTextEditor} from '../../editor/PortableTextEditor'

describe('when PTE would display warnings, instead it self solves', () => {
it('when child at index is missing required _key in block with _key', async () => {
const editorRef: RefObject<PortableTextEditor> = createRef()
const initialValue = [
{
_key: '1',
_type: 'myTestBlockType',
children: [
{
_type: 'span',
marks: [],
text: 'Hello with a new key',
},
],
markDefs: [],
style: 'normal',
},
]

const onChange = jest.fn()
render(
<PortableTextEditorTester
onChange={onChange}
ref={editorRef}
schemaType={schemaType}
value={initialValue}
/>,
)
await waitFor(() => {
if (editorRef.current) {
PortableTextEditor.focus(editorRef.current)
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
{
_key: '1',
_type: 'myTestBlockType',
children: [
{
_key: '3',
_type: 'span',
text: 'Hello with a new key',
marks: [],
},
],
markDefs: [],
style: 'normal',
},
])
}
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,18 @@ export function createOperationToPatches(types: PortableTextMemberSchemaTypes):
const blockKey = block._key
const childKey = child._key
const patches: Patch[] = []
Object.keys(operation.newProperties).forEach((keyName) => {
const val = get(operation.newProperties, keyName)
patches.push(set(val, [{_key: blockKey}, 'children', {_key: childKey}, keyName]))
const keys = Object.keys(operation.newProperties)
keys.forEach((keyName) => {
// Special case for setting _key on a child. We have to target it by index and not the _key.
if (keys.length === 1 && keyName === '_key') {
const val = get(operation.newProperties, keyName)
patches.push(
set(val, [{_key: blockKey}, 'children', block.children.indexOf(child), keyName]),
)
} else {
const val = get(operation.newProperties, keyName)
patches.push(set(val, [{_key: blockKey}, 'children', {_key: childKey}, keyName]))
}
})
return patches
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,22 +307,6 @@ export function validateValue(
return true
}

if (!child._key || typeof child._key !== 'string') {
const newChild = {...child, _key: keyGenerator()}
resolution = {
patches: [set(newChild, [{_key: blk._key}, 'children', cIndex])],
description: `Child at index ${cIndex} is missing required _key in block with _key ${blk._key}.`,
action: 'Set a new random _key on the object',
item: blk,

i18n: {
description: 'inputs.portable-text.invalid-value.missing-child-key.description',
action: 'inputs.portable-text.invalid-value.missing-child-key.action',
values: {key: blk._key, index: cIndex},
},
}
return true
}
// Verify that children have valid types
if (!child._type) {
resolution = {
Expand Down
Loading