From a1b93ac907c87c7aafd5eac8da0160255df44480 Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Mon, 29 Jan 2024 19:50:34 +0530 Subject: [PATCH 1/2] clarify usage of `` component --- components/repeater/readme.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/components/repeater/readme.md b/components/repeater/readme.md index 949be97c..109c9d0f 100644 --- a/components/repeater/readme.md +++ b/components/repeater/readme.md @@ -11,15 +11,33 @@ A Repeater component that allows you to add repeater fields. The type definition of the attribute needs to be set as an array and the name of the attribute should be passed to the `attribute` prop of the `Repeater` component. +For example, if a repeater item unit is a group field containing a text field and a checkbox field, the attribute would be defined in `block.json` as: + +```json +"attributes": { + "repeaterFieldData": { + "type": "array", + "default": [ + { + "text": "", + "checked": false + } + ], + } +} +``` + +**Note:** You should not provide an `id` to the repeater item unit. The Repeater component will automatically generate an `id` for each item. + ```js import { Repeater } from '@10up/block-components'; export function BlockEdit(props) { const { attributes } = props; - const { items } = attributes; + const { repeaterFieldData } = attributes; return ( - + {( item, index, setItem, removeItem ) => ( <> setItem(value)} /> From efcf67bc3601180542df366a69169881162b69b6 Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Wed, 31 Jan 2024 13:04:04 +0530 Subject: [PATCH 2/2] fix for empty default --- components/repeater/index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/components/repeater/index.js b/components/repeater/index.js index 780a16e5..1e6365a8 100644 --- a/components/repeater/index.js +++ b/components/repeater/index.js @@ -42,7 +42,9 @@ export const AttributeRepeater = ({ children, attribute, addButton, allowReorder }; }); - defaultRepeaterData[0].id = uuid(); + if ( defaultRepeaterData.length ) { + defaultRepeaterData[0].id = uuid(); + } const handleOnChange = (value) => { updateBlockAttributes(clientId, { [attribute]: value }); @@ -114,7 +116,7 @@ export const AbstractRepeater = ({ const defaultValueCopy = JSON.parse(JSON.stringify(defaultValue)); if (!defaultValue.length) { - defaultValueCopy.push([]); + defaultValueCopy.push({}); } defaultValueCopy[0].id = uuid();