Giter Club home page Giter Club logo

Comments (7)

rafalnawojczyk avatar rafalnawojczyk commented on September 26, 2024 1

I'm not sure if that's achievable that easily. It's not a simple field.

The one workaround that I found trying to create a 'Blocks' field that has a defaultValue already set is to define it that way:

defaultValue:[{...CotasDefinition.fields, blockType: 'CotasBlock'}], type: 'blocks', blocks: [Cotas],

Ofc I just assumed that your definition/blockType is named that way, but you will probably need to adjust it. Doing it like that should resolve your problem. If that will work for you - I will try to contribute and make it clearer in DOC's, as right now it's not explicitly stated I think.

To payload pros:
maybe I should contribute and add a function that will help users to create a defaultValue for block field, and create a helper function for that

from payload.

mvmendes avatar mvmendes commented on September 26, 2024 1

Hi Rafał !

With a little change and I got it working! Thank you for your code sample based on my context.

My working code:

I've changed the blockType (block's defined slug) to cotas, and CotasDefinition (CollectionConfig instance) is only "Cotas":

defaultValue: [{ ...Cotas.fields, blockType: 'cotas' }],
A helper function will be a great tool!

from payload.

damnitrahul avatar damnitrahul commented on September 26, 2024

Hi there,

I'm trying to achieve the same thing with my config. I'm getting type error with my config.

const StudioUseCases: CollectionConfig = {
  slug: 'studio-use-cases',
  admin: {
    useAsTitle: 'title',
    group: 'Studio Use Cases Pages'
  },
  access: {
    read: () => true
  },
  fields: [
    textField('title'),
    slugField(),
    blockField('content', [blockHero(), blockFaq(), blockVideo(), blockImage()], {
      defaultValue: [{ ...blockImage().fields }]
    })
  ]
};

export default StudioUseCases;

// block image
export function blockImage() {
  return block(
    'block-image',
    [
      themeField('image'),
      backgroundField('image'),
      mediaField('image'),
      textField('maxWidth', {
        required: false,
        admin: { description: 'e.g. "100%" or "100px"' }
      })
    ],
    {
      interfaceName: 'IBlockImage'
    }
  );
}

// block
export function block(slug: string, fields: Field[] = [], options?: Omit<Block, 'fields' | 'slug'>): Block {
  return {
    slug,
    fields,
    ...options
  };
}

Error

Argument of type '{ defaultValue: { blockType: string; length: number; toString(): string; toLocaleString(): string; pop(): Field; push(...items: Field[]): number; concat(...items: ConcatArray<Field>[]): Field[]; concat(...items: (Field | ConcatArray<...>)[]): Field[]; ... 34 more ...; [Symbol.unscopables]: { ...; }; }[]; }' is not assignable to parameter of type 'Omit<BlockField, "name" | "type">'.
  Property 'blocks' is missing in type '{ defaultValue: { blockType: string; length: number; toString(): string; toLocaleString(): string; pop(): Field; push(...items: Field[]): number; concat(...items: ConcatArray<Field>[]): Field[]; concat(...items: (Field | ConcatArray<...>)[]): Field[]; ... 34 more ...; [Symbol.unscopables]: { ...; }; }[]; }' but required in type 'Omit<BlockField, "name" | "type">'.ts(2345)
types.d.ts(545, 5): 'blocks' is declared here.

I'm quite confused what's expected as defaultValue for the blocks field. There isn't an example on the docs I can follow.

from payload.

rafalnawojczyk avatar rafalnawojczyk commented on September 26, 2024

Well, first thing is that Payload usually doesn't work well when you use hyphens in slugs. You shoud stick to letters/numbers and underscore _. These are usually the safest choices.

Your default value should have declared blockType: 'your_image_block_slug'.

I cannot see your implementation of blockField() thus it's hard to dive deeper. It looks like you are not creating a blocks property in blockField definition.

from payload.

damnitrahul avatar damnitrahul commented on September 26, 2024

Hey @rafalnawojczyk

Thank you for the response. I'm adding more definition below for blockField.
I'm creating the blocks property in the blockField. Not sure what i'm doing wrong.

Noted your feedback about slug. I hope that's not the issue here in this case?

export function blockField(name: string, blocks: Block[], options?: Omit<BlockField, 'name' | 'type'>): BlockField {
  return {
    name,
    type: 'blocks',
    required: true,
    blocks,
    ...options
  };
}

from payload.

rafalnawojczyk avatar rafalnawojczyk commented on September 26, 2024

@damnitrahul Have you tried to Omit a blocks property also? Right now if options has a blocks property - it will overwrite your defined one

from payload.

damnitrahul avatar damnitrahul commented on September 26, 2024

Oh, that did the trick for me. Thanks @rafalnawojczyk!

from payload.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.