fix: set initialValues alongside values during onSuccess (#10825)

### What?
Initial values should be set from the server when `acceptValues` is
true.

### Why?
This is needed since we take the values from the server after a
successful form submission.

### How?
Add `initialValue` into `serverPropsToAccept` when `acceptValues` is
true.

Fixes https://github.com/payloadcms/payload/issues/10820

---------

Co-authored-by: Alessio Gravili <alessio@gravili.de>
This commit is contained in:
Jarrod Flesch
2025-02-10 16:49:06 -05:00
committed by GitHub
parent 5dadccea39
commit fde526e07f
10 changed files with 200 additions and 16 deletions

View File

@@ -47,7 +47,6 @@ export const FilterOptionsBlock: Block = {
type: 'relationship',
relationTo: 'text-fields',
filterOptions: ({ siblingData }) => {
console.log('SD', siblingData)
// @ts-expect-error
if (!siblingData?.groupText) {
return true

View File

@@ -0,0 +1,61 @@
'use client'
import type { SerializedParagraphNode, SerializedTextNode } from '@payloadcms/richtext-lexical'
import { useForm } from '@payloadcms/ui'
import React from 'react'
export const ClearState = ({ fieldName }: { fieldName: string }) => {
const { dispatchFields, fields } = useForm()
const clearState = React.useCallback(() => {
const newState = {
root: {
type: 'root',
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
detail: 0,
format: 0,
mode: 'normal',
style: '',
text: '',
version: 1,
} as SerializedTextNode,
],
direction: 'ltr',
format: '',
indent: 0,
textFormat: 0,
textStyle: '',
version: 1,
} as SerializedParagraphNode,
],
direction: 'ltr',
format: '',
indent: 0,
version: 1,
},
}
dispatchFields({
type: 'REPLACE_STATE',
state: {
...fields,
[fieldName]: {
...fields[fieldName],
initialValue: newState,
value: newState,
},
},
})
}, [dispatchFields, fields, fieldName])
return (
<button id={`clear-lexical-${fieldName}`} onClick={clearState} type="button">
Clear State
</button>
)
}

View File

@@ -269,6 +269,40 @@ describe('lexicalMain', () => {
})
})
test('should be able to externally mutate editor state', async () => {
await navigateToLexicalFields()
const richTextField = page.locator('.rich-text-lexical').nth(1).locator('.editor-scroller') // first
await expect(richTextField).toBeVisible()
await richTextField.click() // Use click, because focus does not work
await page.keyboard.type('some text')
const spanInEditor = richTextField.locator('span').first()
await expect(spanInEditor).toHaveText('some text')
await saveDocAndAssert(page)
await page.locator('#clear-lexical-lexicalSimple').click()
await expect(spanInEditor).not.toBeAttached()
})
// This test ensures that the second state clear change is respected too, even though
// initialValue is stale and equal to the previous state change result value-wise
test('should be able to externally mutate editor state twice', async () => {
await navigateToLexicalFields()
const richTextField = page.locator('.rich-text-lexical').nth(1).locator('.editor-scroller') // first
await expect(richTextField).toBeVisible()
await richTextField.click() // Use click, because focus does not work
await page.keyboard.type('some text')
const spanInEditor = richTextField.locator('span').first()
await expect(spanInEditor).toHaveText('some text')
await saveDocAndAssert(page)
await page.locator('#clear-lexical-lexicalSimple').click()
await expect(spanInEditor).not.toBeAttached()
await richTextField.click()
await page.keyboard.type('some text')
await expect(spanInEditor).toHaveText('some text')
await page.locator('#clear-lexical-lexicalSimple').click()
await expect(spanInEditor).not.toBeAttached()
})
test('should be able to bold text using floating select toolbar', async () => {
await navigateToLexicalFields()
const richTextField = page.locator('.rich-text-lexical').nth(2) // second

View File

@@ -321,6 +321,20 @@ export const LexicalFields: CollectionConfig = {
],
}),
},
{
type: 'ui',
name: 'clearLexicalState',
admin: {
components: {
Field: {
path: '/collections/Lexical/components/ClearState.js#ClearState',
clientProps: {
fieldName: 'lexicalSimple',
},
},
},
},
},
{
name: 'lexicalWithBlocks',
type: 'richText',