Skip to content

feat: allow hiding the blockName field visible in blocks' headers via admin.disableBlockName #11301

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/fields/blocks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ The Blocks Field inherits all of the default options from the base [Field Admin
| **`group`** | Text or localization object used to group this Block in the Blocks Drawer. |
| **`initCollapsed`** | Set the initial collapsed state |
| **`isSortable`** | Disable order sorting by setting this value to `false` |
| **`disableBlockName`** | Hide the blockName field by setting this value to `true` |

#### Customizing the way your block is rendered in Lexical

Expand Down Expand Up @@ -165,7 +166,7 @@ The `blockType` is saved as the slug of the block that has been selected.

**`blockName`**

The Admin Panel provides each block with a `blockName` field which optionally allows editors to label their blocks for better editability and readability.
The Admin Panel provides each block with a `blockName` field which optionally allows editors to label their blocks for better editability and readability. This can be visually hidden via `admin.disableBlockName`.

## Example

Expand Down
9 changes: 9 additions & 0 deletions packages/payload/src/fields/config/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ export const createClientBlocks = ({
clientBlock.jsx = jsxResolved
}

if (block?.admin?.disableBlockName) {
// Check for existing admin object, this way we don't have to spread it in
if (clientBlock.admin) {
clientBlock.admin.disableBlockName = block.admin.disableBlockName
} else {
clientBlock.admin = { disableBlockName: block.admin.disableBlockName }
}
}

if (block.labels) {
clientBlock.labels = {} as unknown as LabelsClient

Expand Down
67 changes: 67 additions & 0 deletions packages/payload/src/fields/config/sanitize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,71 @@ describe('sanitizeFields', () => {
expect(sanitizedFields).toStrictEqual([])
})
})
describe('blocks', () => {
it('should maintain admin.blockName true after sanitization', async () => {
const fields: Field[] = [
{
name: 'noLabelBlock',
type: 'blocks',
blocks: [
{
slug: 'number',
admin: {
disableBlockName: true,
},
fields: [
{
name: 'testNumber',
type: 'number',
},
],
},
],
label: false,
},
]
const sanitizedField = (
await sanitizeFields({
config,
fields,
validRelationships: [],
})
)[0] as BlocksField

const sanitizedBlock = sanitizedField.blocks[0]

expect(sanitizedBlock.admin?.disableBlockName).toStrictEqual(true)
})
it('should default admin.disableBlockName to true after sanitization', async () => {
const fields: Field[] = [
{
name: 'noLabelBlock',
type: 'blocks',
blocks: [
{
slug: 'number',
fields: [
{
name: 'testNumber',
type: 'number',
},
],
},
],
label: false,
},
]
const sanitizedField = (
await sanitizeFields({
config,
fields,
validRelationships: [],
})
)[0] as BlocksField

const sanitizedBlock = sanitizedField.blocks[0]

expect(sanitizedBlock.admin?.disableBlockName).toStrictEqual(undefined)
})
})
})
8 changes: 7 additions & 1 deletion packages/payload/src/fields/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,12 @@ export type Block = {
}
/** Extension point to add your custom data. Available in server and client. */
custom?: Record<string, any>
/**
* Hides the block name field from the Block's header
*
* @default false
*/
disableBlockName?: boolean
group?: Record<string, string> | string
jsx?: PayloadComponent
}
Expand Down Expand Up @@ -1402,7 +1408,7 @@ export type Block = {
}

export type ClientBlock = {
admin?: Pick<Block['admin'], 'custom' | 'group'>
admin?: Pick<Block['admin'], 'custom' | 'disableBlockName' | 'group'>
fields: ClientField[]
labels?: LabelsClient
} & Pick<Block, 'imageAltText' | 'imageURL' | 'jsx' | 'slug'>
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/src/fields/Blocks/BlockRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export const BlockRow: React.FC<BlocksFieldProps> = ({

const fieldHasErrors = hasSubmitted && errorCount > 0

const showBlockName = !block.admin?.disableBlockName

const classNames = [
`${baseClass}__row`,
fieldHasErrors ? `${baseClass}__row--has-errors` : `${baseClass}__row--no-errors`,
Expand Down Expand Up @@ -155,7 +157,7 @@ export const BlockRow: React.FC<BlocksFieldProps> = ({
>
{getTranslation(block.labels.singular, i18n)}
</Pill>
<SectionTitle path={`${path}.blockName`} readOnly={readOnly} />
{showBlockName && <SectionTitle path={`${path}.blockName`} readOnly={readOnly} />}
{fieldHasErrors && <ErrorPill count={errorCount} i18n={i18n} withMessage />}
</Fragment>
)}
Expand Down
35 changes: 31 additions & 4 deletions test/fields/collections/Blocks/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { AdminUrlUtil } from '../../../helpers/adminUrlUtil.js'
import { initPayloadE2ENoConfig } from '../../../helpers/initPayloadE2ENoConfig.js'
import { reInitializeDB } from '../../../helpers/reInitializeDB.js'
import { RESTClient } from '../../../helpers/rest.js'
import { TEST_TIMEOUT_LONG } from '../../../playwright.config.js'
import { POLL_TOPASS_TIMEOUT, TEST_TIMEOUT_LONG } from '../../../playwright.config.js'

const filename = fileURLToPath(import.meta.url)
const currentFolder = path.dirname(filename)
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('Block fields', () => {
const addedRow = page.locator('#field-blocks .blocks-field__row').last()
await expect(addedRow).toBeVisible()
await expect(addedRow.locator('.blocks-field__block-header')).toHaveText(
'Custom Block Label: Content 04',
'Custom Block Label: Content 05',
)
})

Expand Down Expand Up @@ -155,7 +155,7 @@ describe('Block fields', () => {
await duplicateButton.click()

const blocks = page.locator('#field-blocks > .blocks-field__rows > div')
expect(await blocks.count()).toEqual(4)
expect(await blocks.count()).toEqual(5)
})

test('should save when duplicating subblocks', async () => {
Expand All @@ -170,7 +170,7 @@ describe('Block fields', () => {
await duplicateButton.click()

const blocks = page.locator('#field-blocks > .blocks-field__rows > div')
expect(await blocks.count()).toEqual(4)
expect(await blocks.count()).toEqual(5)

await page.click('#action-save')
await expect(page.locator('.payload-toast-container')).toContainText('successfully')
Expand Down Expand Up @@ -345,6 +345,33 @@ describe('Block fields', () => {
})
})

describe('blockNames', () => {
test('should show blockName field', async () => {
await page.goto(url.create)

const blockWithBlockname = page.locator('#field-blocks .blocks-field__rows #blocks-row-1')

const blocknameField = blockWithBlockname.locator('.section-title')

await expect(async () => await expect(blocknameField).toBeVisible()).toPass({
timeout: POLL_TOPASS_TIMEOUT,
})

await expect(blocknameField).toHaveAttribute('data-value', 'Second block')
})

test("should not show blockName field when it's disabled", async () => {
await page.goto(url.create)
const blockWithBlockname = page.locator('#field-blocks .blocks-field__rows #blocks-row-3')

await expect(
async () => await expect(blockWithBlockname.locator('.section-title')).toBeHidden(),
).toPass({
timeout: POLL_TOPASS_TIMEOUT,
})
})
})

describe('block groups', () => {
test('should render group labels', async () => {
await page.goto(url.create)
Expand Down
13 changes: 13 additions & 0 deletions test/fields/collections/Blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ export const getBlocksField = (prefix?: string): BlocksField => ({
},
],
},
{
slug: prefix ? `${prefix}NoBlockname` : 'noBlockname',
interfaceName: prefix ? `${prefix}NoBlockname` : 'NoBlockname',
admin: {
disableBlockName: true,
},
fields: [
{
name: 'text',
type: 'text',
},
],
},
{
slug: prefix ? `${prefix}Number` : 'number',
interfaceName: prefix ? `${prefix}NumberBlock` : 'NumberBlock',
Expand Down
4 changes: 4 additions & 0 deletions test/fields/collections/Blocks/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export const getBlocksFieldSeedData = (prefix?: string): any => [
},
],
},
{
blockType: prefix ? `${prefix}NoBlockname` : 'noBlockname',
text: 'Hello world',
},
]

export const blocksDoc: Partial<BlockField> = {
Expand Down
Loading