Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Pass argument / prop into Custom RowLabel component

default discord avatar
szymciodrift5 months ago
3

Hi, as in the title is it possible to pass it? Now i'm passing only path to the component, but i would like to pass a certain field name to be used as a label



Ok, thats how I've figured it out (almost). I'm taking a name of the first field in array, then reading its value from 'data'. If first field's value is empty I'm returning default field labels.


How to correctly set type of Props?


'use client'

import { useRowLabel } from '@payloadcms/ui'
import React from 'react'

import { ArrayFieldServerProps, LabelsClient } from 'payload'

type Props = Pick<ArrayFieldServerProps, 'field'> & {
  field: {
    fields: Array<{ name: string }>
  }
  labels: LabelsClient
}

export const ArrayRowLabel: React.FC<Props> = ({ field }) => {
  const { data, rowNumber } = useRowLabel<{ groupTitle?: string }>()

  const firstFieldName = field?.fields[0]?.name
  const fieldNameToLabel = firstFieldName && typeof firstFieldName === 'string' && firstFieldName

  const defaultFieldLabel = field.labels?.singular

  const customLabel =
    (fieldNameToLabel && data[fieldNameToLabel]) ||
    `${defaultFieldLabel} ${String(rowNumber! + 1).padStart(2, '0')}`

  return <div>{customLabel}</div>
}
  • default discord avatar
    zed05475 months ago

    Custom components can be defined in the following format as well:



    {
      path: '/path/to/comp#NamedIfNecessary`,
      clientProps: { ... } // This can be custom props you want to pass to the component which payload will include
    }
  • default discord avatar
    szymciodrift5 months ago

    Thank you! Finally somebody answered here... I'll give it a try tomorrow



    It works. Thank you for help ❤️

  • default discord avatar
    zed05475 months ago

    My pleasure

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.