How to add a condition to a custom field component?

discord user avatar
tylandavis
last year
4

I have a color picker field component that I want to use in multiple collections, and I want to add a condition for when to show it on different collections. What is the best way to add more properties to config object?

  • default discord avatar
    Jarrodlast year

    Is your custom field component a function already? i.e. in your configs you are saying


    // ...other fields
    colorPicker(),
    // ...more fields


    If not, you will need to convert it into a function, that when called returns the config object.



    Once you have that, you can modify that function to take some arguments. You will likely want to pass the field

    name

    that you will need to access the field off of

    siblingData[${name}]

    within the condition,

    and

    the value you want to conditionally show the field when equal to.



    in the end it could look like:


    // ...other fields
    colorPicker('some-field', 'some-value'),
    // ...more fields


    You could also pass 1 object instead of 2 strings as the argument, if that is easier to read for you, like:


    // ...other fields
    colorPicker({ fieldName: 'some-field', fieldValue: 'some-value' }),
    // ...more fields
  • discord user avatar
    tylandavis
    last year

    thanks @Jarrod! that makes a lot of sense, this is exactly what I was looking for.

  • default discord avatar
    Jarrodlast year

    beautiful!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.