Skip to content

Commit 4defa33

Browse files
fix(ui): add RowLabelProvider context for blocks row labels (#11664)
1 parent 3f6699f commit 4defa33

File tree

4 files changed

+53
-25
lines changed

4 files changed

+53
-25
lines changed

docs/fields/blocks.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,22 @@ export const CustomBlocksFieldLabelClient: BlocksFieldLabelClientComponent = ({
297297
}
298298
```
299299

300+
### Row Label
301+
302+
```tsx
303+
'use client'
304+
305+
import { useRowLabel } from '@payloadcms/ui'
306+
307+
export const BlockRowLabel = () => {
308+
const { data, rowNumber } = useRowLabel<{ title?: string }>()
309+
310+
const customLabel = `${data.type} ${String(rowNumber).padStart(2, '0')} `
311+
312+
return <div>Custom Label: {customLabel}</div>
313+
}
314+
```
315+
300316
## Block References
301317

302318
If you have multiple blocks used in multiple places, your Payload Config can grow in size, potentially sending more data to the client and requiring more processing on the server. However, you can optimize performance by defining each block **once** in your Payload Config and then referencing its slug wherever it's used instead of passing the entire block config.

packages/ui/src/fields/Blocks/BlockRow.tsx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import type { ClientBlock, ClientField, Labels, Row, SanitizedFieldPermissions } from 'payload'
33

44
import { getTranslation } from '@payloadcms/translations'
5-
import React, { Fragment } from 'react'
5+
import React from 'react'
66

77
import type { UseDraggableSortableReturn } from '../../elements/DraggableSortable/useDraggableSortable/types.js'
88
import type { RenderFieldsProps } from '../../forms/RenderFields/types.js'
@@ -13,6 +13,7 @@ import { Pill } from '../../elements/Pill/index.js'
1313
import { ShimmerEffect } from '../../elements/ShimmerEffect/index.js'
1414
import { useFormSubmitted } from '../../forms/Form/context.js'
1515
import { RenderFields } from '../../forms/RenderFields/index.js'
16+
import { RowLabel } from '../../forms/RowLabel/index.js'
1617
import { useThrottledValue } from '../../hooks/useThrottledValue.js'
1718
import { useTranslation } from '../../providers/Translation/index.js'
1819
import { RowActions } from './RowActions.js'
@@ -146,21 +147,28 @@ export const BlockRow: React.FC<BlocksFieldProps> = ({
146147
<ShimmerEffect height="1rem" width="8rem" />
147148
) : (
148149
<div className={`${baseClass}__block-header`}>
149-
{Label || (
150-
<Fragment>
151-
<span className={`${baseClass}__block-number`}>
152-
{String(rowIndex + 1).padStart(2, '0')}
153-
</span>
154-
<Pill
155-
className={`${baseClass}__block-pill ${baseClass}__block-pill-${row.blockType}`}
156-
pillStyle="white"
157-
>
158-
{getTranslation(block.labels.singular, i18n)}
159-
</Pill>
160-
{showBlockName && <SectionTitle path={`${path}.blockName`} readOnly={readOnly} />}
161-
{fieldHasErrors && <ErrorPill count={errorCount} i18n={i18n} withMessage />}
162-
</Fragment>
163-
)}
150+
<RowLabel
151+
CustomComponent={Label}
152+
label={
153+
<>
154+
<span className={`${baseClass}__block-number`}>
155+
{String(rowIndex + 1).padStart(2, '0')}
156+
</span>
157+
<Pill
158+
className={`${baseClass}__block-pill ${baseClass}__block-pill-${row.blockType}`}
159+
pillStyle="white"
160+
>
161+
{getTranslation(block.labels.singular, i18n)}
162+
</Pill>
163+
{showBlockName && (
164+
<SectionTitle path={`${path}.blockName`} readOnly={readOnly} />
165+
)}
166+
</>
167+
}
168+
path={path}
169+
rowNumber={rowIndex}
170+
/>
171+
{fieldHasErrors && <ErrorPill count={errorCount} i18n={i18n} withMessage />}
164172
</div>
165173
)
166174
}

packages/ui/src/forms/RowLabel/index.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ export const RowLabel: React.FC<RowLabelProps> = (props) => {
1717
<RenderCustomComponent
1818
CustomComponent={CustomComponent}
1919
Fallback={
20-
<span
21-
className={[baseClass, className].filter(Boolean).join(' ')}
22-
style={{
23-
pointerEvents: 'none',
24-
}}
25-
>
26-
{label}
27-
</span>
20+
typeof label === 'string' ? (
21+
<span
22+
className={[baseClass, className].filter(Boolean).join(' ')}
23+
style={{
24+
pointerEvents: 'none',
25+
}}
26+
>
27+
{label}
28+
</span>
29+
) : (
30+
label
31+
)
2832
}
2933
/>
3034
</RowLabelProvider>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export type RowLabelProps = {
22
readonly className?: string
33
readonly CustomComponent?: React.ReactNode
4-
readonly label?: string
4+
readonly label?: React.ReactNode | string
55
readonly path: string
66
readonly rowNumber?: number
77
}

0 commit comments

Comments
 (0)