Repeater
React Bricks allows for infinite nesting of bricks within bricks using the <Repeater>
component.
Each Repeater can contain either a single repeatable brick type or multiple types.
Usage example
Let’s consider the following “Button” brick that we want to repeat inside a “TextImage” brick:
In our TextImage brick, we need to:
- Add a prop in our interface for the repeated buttons
- Add a
<Repeater>
component in the rendered JSX where items should appear - Add the
repeaterProps
in the brick’sschema
to specify the type of brick to be repeated
1. Add a prop in the brick interface:
2. Add the Repeater
component:
3. Add repeaterItems
in the schema
Note that the name buttons
is the same for both the Repeater’s propName
and the item name in the repeaterItems
.
In this example, our Repeater allows only one type of brick to be repeated (my-button
). By setting max: 2
, we limit the number of buttons that can be added in the repeater.
Repeater Properties
Repeater Properties definition
Property | Definition |
---|---|
propName | Name of the prop containing the repeated items (should match the one in the repeaterItems schema property) |
items | The value of the prop for this repeater, containing the repeated items. Required for Server Components, but always recommended for RSC compatibility |
itemProps? | Optional object with props passed to all the items (e.g., a global configuration shared by all the items). |
renderWrapper? | Optional function taking items as an argument. It should return JSX that wraps the items. Rendered only if there is at least one repeated item. |
renderItemWrapper? | Optional wrapper around each item. Takes item , index and itemsCount as arguments and should return JSX |
The schema’s repeaterItems
Property
While there’s a dedicated section in the docs about the bricks’ schema, we’ll include the interface for repeaterItems
here due to its close relation to the Repeater component.
Where IRepeaterItem
is defined as:
When max
is reached, no more blocks can be added to the repeater.
Setting a min
ensures that React Bricks always maintains at least that number of blocks (adding items with default values).
RepeaterItem Properties definition
Property | Definition |
---|---|
name | Name of the prop containing the repeated items (e.g., “buttons”) |
itemType | Corresponds to the unique name of the repeated Brick type (e.g., “my-button”) |
label? | Label for the group of items in this repeater (title of nested items) |
itemLabel? | Optional label used for the Add / Remove buttons. If not provided, the repeated brick’s label is used as a fallback |
min | Minimum number of repeated items allowed |
max | Maximum number of repeated items allowed |
getDefaultProps | Function that returns the default props for the repeated brick inside of this brick. For example, for a Button brick repeated inside a Form brick, you could specify that the default type should be “submit” instead of “link”. |
items | Allowed item types, when multiple. In this case the main itemType and min are not considered. Each item has its own type , label , min , max and getDefaultProps . |
Multiple item types
itemType
is used for a single type of brick that can be repeated in a Repeater (e.g. a Thumbnail in a Gallery).items
is used for multiple types of brick in a Reapear (e.g., in a Form you may add “TextInput” blocks or “Select” blocks).- With multiple item types, each with its own
min
andmax
, the root item’smin
is ignored, while the overallmax
is still enforced.
Hiding a Repeated Brick from the Brick Selection
Sometimes a brick is designed to be used only within another brick, and you don’t want editors to add it directly to a page as a standalone element. For example, you might have a GalleryImage
brick that should only appear inside a Gallery
brick.
To prevent such a brick from showing up in the list of available bricks when adding a new block, set hideFromAddMenu: true
in the brick’s Schema.
Usage with Server components
When working with Server Components, you need to import from 'react-bricks/rsc'
:
see more in the Server Components documentation.