Skip to content
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

Release #293

Merged
merged 6 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions components/repeater/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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();
Expand Down
22 changes: 20 additions & 2 deletions components/repeater/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Repeater attribute="items">
<Repeater attribute="repeaterFieldData">
{( item, index, setItem, removeItem ) => (
<>
<TextControl key={index} value={item} onChange={(value) => setItem(value)} />
Expand Down
Loading