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.

Payload 3 - RowLabel

default discord avatar
tobedamobelast year
6

Hey folks! Hope you can help us 🙂



We studied the docs and already used the search in discord but we still need yout help.



What is the trick in dynamically changing the RowLabel in an ArrayField. Like the docs say here (

https://payloadcms.com/docs/fields/array#row-label

) wie tried to build a custom RowLabel component



'use client'

import { useRowLabel } from '@payloadcms/ui'

export const NavigationRowLabel = () => {
  const { data, rowNumber } = useRowLabel<{ title?: string }>()

  const customLabel = `${data.title || 'Slide'} ${String(rowNumber).padStart(2, '0')}

  return <div>Custom Label: {customLabel}</div>
}

and imported and used it inside


admin : { 
  components: { 
    RowLabel: NavigationRowLabel
  } 
}

Here is our first problem:


Using it that way, typescript yells at us



Das Objektliteral kann nur bekannte Eigenschaften angeben, und "RowLabel" ist im Typ "{ afterInput?: CustomComponent[] | undefined; beforeInput?: CustomComponent[] | undefined; Error?: CustomComponent<JSONFieldErrorClientComponent | JSONFieldErrorServerComponent> | undefined; Label?: CustomComponent<...> | undefined; } & { ...; }" nicht vorhanden.ts(2353)

or


The object literal can only specify known properties, and "RowLabel" is not in type "{ afterInput?: CustomComponent[] | undefined; beforeInput?: CustomComponent[] | undefined; Error?: CustomComponent<JSONFieldErrorClientComponent | JSONFieldErrorServerComponent> | undefined; Label?: CustomComponent<...> | undefined; } & { ...; }". present.ts(2353)

When we use the component by its path, like this


components: {
  RowLabel: '@/fields/navigation/NavigationRowLabel',
}

it works after wie changed the components export default (otherwise the importMap is broken) but we have no access to data or rowNumbers inside our component. But we cant to use some siblingField-data inside the RowLabel.



Can someone help us solving this problem? 🙂



Greetings!


Toby

  • default discord avatar
    aaronksaunderslast year

    u need to use the component on the actual fields in the collection, here is an example.



        {
          name: 'images',
          type: 'array',
          admin: {
            components: {
              RowLabel: '@/collections/Vehicles/CustomImageRowLabel',
            },
          },
          fields: [
            {
              name: 'image',
              type: 'upload',
              relationTo: 'media',
              required: true,
            },
            {
              name: 'caption',
              type: 'text',
              defaultValue: 'Vehicle Image',
            },
          ],
        },
  • default discord avatar
    tobedamobelast year

    Thanks! We already tried it this way. Referencing the component by its path, but no success. The component works but the useRowLabel hook has no data. The whole hook returns an object with data, rowNumber but there is no content in it.

  • default discord avatar
    aaronksaunderslast year

    Just create a small project with the collection and the custom component and post it to GitHub I will take a look 👀. I copied that code from a working project of mine

  • default discord avatar
    tobedamobelast year

    thanks for looking at my code. especially for the time you would have spent on it. I thought before I do that I'll try it one last time with your advice and voila, it works. So the RowLabel that I create has to be in the same folder as my collection? Can you say that in general? I think this should be included in the documentation 😄 I can't imagine that I'm the only one who stumbles upon it 🙂



    here is my working code now:



    'use client'
    
    import { useRowLabel } from '@payloadcms/ui'
    
    const RowLabel = () => {
      const { data, rowNumber } = useRowLabel<{ title?: string }>()
    
      // Do something with the data of the row entry
    
      const customLabel = `${data.title || 'Slide'} ${String(rowNumber).padStart(2, '0')} `
    
      return <div>Custom Label: {customLabel}</div>
    }
    
    export default RowLabel


    Notice that this file/component lies in the same directory the collection lies in where it is used.



    Here:



    globals > Navigation > RowLabel > index.tsx
  • default discord avatar
    aaronksaunderslast year

    It doesn’t need to be in same folder, it sounds like there was some other issue, but glad u got it working

  • default discord avatar
    masa_89238last year
    pnpm generate:importmap

    for fix

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.