-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: conditional blocks #13801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: conditional blocks #13801
Conversation
valid?: boolean | ||
value: T | ||
} | ||
} & Pick< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using Pick is preferred over re-defining types. That way, we also bring over JSDocs
validate: memoizedValidate, | ||
}) | ||
|
||
const { clientBlocks, clientBlocksAfterFilter } = useMemo(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same as the previous clientBlocks = useMemo()
, but it also filters blocks now according to blocksFilterOptions returned from the form state.
We need both the filtered and unfiltered client blocks. The filtered ones are only used for the drawer. We still need the ability to display existing blocks that no longer match the filter and fail validation.
blockReferences?: (Block | BlockSlug)[] | ||
blocks: Block[] | ||
defaultValue?: DefaultValue | ||
filterOptions?: BlocksFilterOptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reason for keeping name filterOptions
despite different type is in PR description
📦 esbuild Bundle Analysis for payloadThis analysis was generated by esbuild-bundle-analyzer. 🤖
Largest pathsThese visualization shows top 20 largest paths in the bundle.Meta file: packages/next/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_shared.json, Out file: esbuild/exports/shared.js
Meta file: packages/richtext-lexical/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_shared.json, Out file: esbuild/exports/shared_optimized/index.js
DetailsNext to the size is how much the size has increased or decreased compared with the base branch of this PR.
|
* Undefined values for blocksFilterOptions coming back should be treated as "all blocks allowed" and | ||
* should always be merged in. | ||
*/ | ||
newState[path].blocksFilterOptions = incomingField.blocksFilterOptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without this, if blocksFilterOptions goes from a value to undefined, it would be ignored
/** | ||
* @deprecated - readOnly is no longer returned from useField. Remove this in 4.0. | ||
*/ | ||
readOnly?: boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readOnly currently is always undefined. I think it's only there to prevent a breaking change (narrower type, project won't build)
This PR introduces support for conditionally setting allowable block types via a new
field.filterOptions
property on the blocks field.Closes the following feature requests: #5348, #4668 (partly)
Example
Screenshot.2025-09-15.at.17.49.06.mp4
Updated error toast:
Rationale
Why not
block.condition
?filterOptions
in relationship fields, rather thancondition
.Why not call it
blocksFilterOptions
?Although the type differs from relationship fields, this property is named
filterOptions
(and notblocksFilterOptions
) for consistency across field types. For example, the Select field also usesfilterOptions
despite its type being unique.