Skip to content

Commit c7a87c0

Browse files
authored
fix(richtext-lexical): block names were not loaded (#14698)
Previously, setting a block name for a lexical block caused the block name field to appear empty after reloading the page. This regression occurred after merging the form state with the initial state, as the initial state doesn't include the blockName field (since it's `admin.disabled`). Fixes #14650
1 parent e40a4b7 commit c7a87c0

File tree

2 files changed

+59
-1
lines changed
  • packages/richtext-lexical/src/features/blocks/client/component
  • test/lexical/collections/_LexicalFullyFeatured/db

2 files changed

+59
-1
lines changed

packages/richtext-lexical/src/features/blocks/client/component/index.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export const BlockComponent: React.FC<Props> = (props) => {
104104

105105
// Merge current formData values into the cached form state
106106
// This ensures that when the component remounts (e.g., due to view changes), we don't lose user edits
107-
return Object.fromEntries(
107+
const mergedState = Object.fromEntries(
108108
Object.entries(cachedFormState).map(([fieldName, fieldState]) => [
109109
fieldName,
110110
fieldName in formData
@@ -116,6 +116,16 @@ export const BlockComponent: React.FC<Props> = (props) => {
116116
: fieldState,
117117
]),
118118
)
119+
120+
// Manually add blockName, as it's not part of cachedFormState
121+
mergedState.blockName = {
122+
initialValue: formData.blockName,
123+
passesCondition: true,
124+
valid: true,
125+
value: formData.blockName,
126+
}
127+
128+
return mergedState
119129
})
120130

121131
const hasMounted = useRef(false)

test/lexical/collections/_LexicalFullyFeatured/db/e2e.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,52 @@ describe('Lexical Fully Featured - database', () => {
247247
},
248248
)
249249
})
250+
251+
test('ensure block name can be saved and loaded', async ({ page }) => {
252+
await lexical.slashCommand('myblock')
253+
await expect(lexical.editor.locator('.LexicalEditorTheme__block')).toBeVisible()
254+
255+
const blockNameInput = lexical.editor.locator('#blockName')
256+
257+
/**
258+
* Test on create
259+
*/
260+
await assertNetworkRequests(
261+
page,
262+
`/admin/collections/${lexicalFullyFeaturedSlug}`,
263+
async () => {
264+
await blockNameInput.fill('Testing 123')
265+
},
266+
{
267+
minimumNumberOfRequests: 2,
268+
allowedNumberOfRequests: 3,
269+
},
270+
)
271+
272+
await expect(blockNameInput).toHaveValue('Testing 123')
273+
await saveDocAndAssert(page)
274+
await expect(blockNameInput).toHaveValue('Testing 123')
275+
await page.reload()
276+
await expect(blockNameInput).toHaveValue('Testing 123')
277+
278+
/**
279+
* Test on update
280+
*/
281+
await assertNetworkRequests(
282+
page,
283+
`/admin/collections/${lexicalFullyFeaturedSlug}`,
284+
async () => {
285+
await blockNameInput.fill('Updated blockname')
286+
},
287+
{
288+
minimumNumberOfRequests: 2,
289+
allowedNumberOfRequests: 2,
290+
},
291+
)
292+
await expect(blockNameInput).toHaveValue('Updated blockname')
293+
await saveDocAndAssert(page)
294+
await expect(blockNameInput).toHaveValue('Updated blockname')
295+
await page.reload()
296+
await expect(blockNameInput).toHaveValue('Updated blockname')
297+
})
250298
})

0 commit comments

Comments
 (0)