fix: passing blocks field test suite (#5529)
This commit is contained in:
@@ -145,8 +145,7 @@ export function fieldReducer(state: FormState, action: FieldAction): FormState {
|
||||
}
|
||||
|
||||
case 'REPLACE_ROW': {
|
||||
const { blockType, path, rowIndex: rowIndexArg } = action
|
||||
const subFieldState: FormState = {}
|
||||
const { blockType, path, rowIndex: rowIndexArg, subFieldState = {} } = action
|
||||
|
||||
const { remainingFields, rows: siblingRows } = separateRows(path, state)
|
||||
const rowIndex = Math.max(0, Math.min(rowIndexArg, siblingRows?.length - 1 || 0))
|
||||
|
||||
@@ -4,13 +4,16 @@ import { useForm } from '@payloadcms/ui/forms/Form'
|
||||
import { useField } from '@payloadcms/ui/forms/useField'
|
||||
import * as React from 'react'
|
||||
|
||||
import { blockFieldsSlug } from '../../../../slugs.js'
|
||||
import './index.scss'
|
||||
|
||||
const baseClass = 'custom-blocks-field-management'
|
||||
|
||||
const blocksPath = 'customBlocks'
|
||||
|
||||
export const AddCustomBlocks: React.FC = () => {
|
||||
const { addFieldRow, replaceFieldRow } = useForm()
|
||||
const { value } = useField<number>({ path: 'customBlocks' })
|
||||
const { value } = useField<number>({ path: blocksPath })
|
||||
|
||||
return (
|
||||
<div className={baseClass}>
|
||||
@@ -20,7 +23,8 @@ export const AddCustomBlocks: React.FC = () => {
|
||||
onClick={() =>
|
||||
addFieldRow({
|
||||
data: { block1Title: 'Block 1: Prefilled Title', blockType: 'block-1' },
|
||||
path: 'customBlocks',
|
||||
path: blocksPath,
|
||||
schemaPath: `${blockFieldsSlug}.${blocksPath}.block-1`,
|
||||
})
|
||||
}
|
||||
type="button"
|
||||
@@ -33,7 +37,8 @@ export const AddCustomBlocks: React.FC = () => {
|
||||
onClick={() =>
|
||||
addFieldRow({
|
||||
data: { block2Title: 'Block 2: Prefilled Title', blockType: 'block-2' },
|
||||
path: 'customBlocks',
|
||||
path: blocksPath,
|
||||
schemaPath: `${blockFieldsSlug}.${blocksPath}.block-2`,
|
||||
})
|
||||
}
|
||||
type="button"
|
||||
@@ -48,8 +53,9 @@ export const AddCustomBlocks: React.FC = () => {
|
||||
onClick={() =>
|
||||
replaceFieldRow({
|
||||
data: { block1Title: 'REPLACED BLOCK', blockType: 'block-1' },
|
||||
path: 'customBlocks',
|
||||
path: blocksPath,
|
||||
rowIndex: value - 1,
|
||||
schemaPath: `${blockFieldsSlug}.${blocksPath}.block-1`,
|
||||
})
|
||||
}
|
||||
type="button"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { CollectionConfig } from 'payload/types'
|
||||
import type { BlockField } from 'payload/types'
|
||||
|
||||
import { slateEditor } from '@payloadcms/richtext-slate'
|
||||
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
||||
|
||||
import { blockFieldsSlug, textFieldsSlug } from '../../slugs.js'
|
||||
import { AddCustomBlocks } from './components/AddCustomBlocks/index.js'
|
||||
@@ -22,7 +22,7 @@ export const getBlocksField = (prefix?: string): BlockField => ({
|
||||
{
|
||||
name: 'richText',
|
||||
type: 'richText',
|
||||
editor: slateEditor({}),
|
||||
editor: lexicalEditor({}),
|
||||
},
|
||||
],
|
||||
},
|
||||
@@ -311,6 +311,15 @@ const BlockFields: CollectionConfig = {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'ui',
|
||||
type: 'ui',
|
||||
admin: {
|
||||
components: {
|
||||
Field: AddCustomBlocks,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'relationshipBlocks',
|
||||
type: 'blocks',
|
||||
@@ -327,15 +336,6 @@ const BlockFields: CollectionConfig = {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'ui',
|
||||
type: 'ui',
|
||||
admin: {
|
||||
components: {
|
||||
Field: AddCustomBlocks,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@@ -664,7 +664,9 @@ describe('fields', () => {
|
||||
await expect(firstRow).toHaveValue('first row')
|
||||
|
||||
await page.click('#action-save', { delay: 100 })
|
||||
await expect(page.locator('.Toastify')).toContainText('Please correct invalid fields')
|
||||
await expect(page.locator('.Toastify')).toContainText(
|
||||
'The following field is invalid: blocksWithMinRows',
|
||||
)
|
||||
})
|
||||
|
||||
describe('row manipulation', () => {
|
||||
|
||||
Reference in New Issue
Block a user