Skip to content

Commit 229aae1

Browse files
committed
Add a minimized option to repeaters
Repeaters with many rows get long in the sidebar (#95). With "minimized": true the rows that exist when the field mounts start collapsed. Rows added in the editor start expanded so they can be filled in, and a row the user expands stays expanded, stored like the collapsed rows of other repeaters but under its own key. The option is in the block and extend schemas, the types and the field reference. A test block and an e2e test cover collapsed existing rows, an expanded new row and an expanded row after a reload.
1 parent 28b2a57 commit 229aae1

10 files changed

Lines changed: 163 additions & 14 deletions

File tree

‎docs/docs/blocks/attributes/field-types.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ Renders a set of fields that can be repeated.
604604
|----------|------|-------------|
605605
| `min` | `number` | Minimum amount of rows. |
606606
| `max` | `number` | Maximum amount of rows. |
607+
| `minimized` | `boolean` | Collapse the existing rows when the editor opens. Rows added in the editor start expanded. |
607608
| `textButton` | `string` | Text for the add button. |
608609
| `textMinimized` | `string \| object` | Text that will be displayed when rows are minimized. |
609610
| `textRemove` | `string \| boolean` | Text to display in alert when removing repeater row. |

‎includes/llm/blockstudio-llm-full.txt‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎schemas/block.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5378,6 +5378,10 @@
53785378
"type": "number",
53795379
"description": "Maximum amount of rows."
53805380
},
5381+
"minimized": {
5382+
"type": "boolean",
5383+
"description": "Collapse the existing rows when the editor opens. Rows added in the editor start expanded."
5384+
},
53815385
"textButton": {
53825386
"type": "string",
53835387
"description": "Text for the add button."
@@ -6558,6 +6562,10 @@
65586562
"type": "number",
65596563
"description": "Maximum amount of rows."
65606564
},
6565+
"minimized": {
6566+
"type": "boolean",
6567+
"description": "Collapse the existing rows when the editor opens. Rows added in the editor start expanded."
6568+
},
65616569
"textButton": {
65626570
"type": "string",
65636571
"description": "Text for the add button."

‎schemas/extend.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,6 +2391,10 @@
23912391
"type": "number",
23922392
"description": "Maximum amount of rows."
23932393
},
2394+
"minimized": {
2395+
"type": "boolean",
2396+
"description": "Collapse the existing rows when the editor opens. Rows added in the editor start expanded."
2397+
},
23942398
"textButton": {
23952399
"type": "string",
23962400
"description": "Text for the add button."
@@ -3571,6 +3575,10 @@
35713575
"type": "number",
35723576
"description": "Maximum amount of rows."
35733577
},
3578+
"minimized": {
3579+
"type": "boolean",
3580+
"description": "Collapse the existing rows when the editor opens. Rows added in the editor start expanded."
3581+
},
35743582
"textButton": {
35753583
"type": "string",
35763584
"description": "Text for the add button."

‎schemas/manifest.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"blockstudio_ref": "819e0fae64db0da17737140e5afe72fcb93b12e0",
55
"gutenberg_schema_url": "https://raw.githubusercontent.com/WordPress/gutenberg/trunk/schemas/json/block.json",
66
"files": {
7-
"block.json": "e1626c2e397c3030f7a70850065e20cea1368fb898d33e6dc0f5958a848ad8b9",
7+
"block.json": "7683c42c7cf3d2ec7e5e31549bbc48cf1478969eb681adb95d956549a1a20d92",
88
"blockstudio.json": "a3ddcd99366b6ea066e7ea03af434a710a7910753c0046f192e219703abaf8d9",
9-
"extend.json": "9f51be7775541dbc1335b02e21b75443e9254cf9e2ff0e6daa37b6c171f44658",
9+
"extend.json": "e39996afdc1cbb912db42a7b0ffc989f3d6f26a7fb011ecdd533d6a9178d0ca3",
1010
"field.json": "e92dfbd4f8232d363f97d91b0276fe6006c37df5056a22195ccfcb028883ef1e",
1111
"page.json": "5d871fc1ef86f8ae954ed3f7700dcedd162055c8e1d64279e794484b57ddcdf2"
1212
}

‎src/blocks/components/fields/components/repeater.tsx‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const DragElement = ({
6565
moveDown,
6666
moveUp,
6767
remove,
68+
startMinimized,
6869
transformed,
6970
}: {
7071
attributes: BlockstudioBlockAttributes;
@@ -81,6 +82,7 @@ const DragElement = ({
8182
moveDown?: BlockstudioFieldsListMove;
8283
moveUp?: BlockstudioFieldsListMove;
8384
remove: BlockstudioFieldsRepeaterAddRemove;
85+
startMinimized: boolean;
8486
transformed: BlockstudioBlock['blockstudio'];
8587
}) => {
8688
let textMinimized = __('Repeater element') as string;
@@ -118,12 +120,21 @@ const DragElement = ({
118120
}${item?.textMinimized?.suffix || ''}`;
119121
}
120122

121-
let storage = localStorage.getItem('blockstudioRepeater')
122-
? JSON.parse(localStorage.getItem('blockstudioRepeater') || '')
123+
// The storage lists the rows that differ from the default of their
124+
// repeater: minimized rows, or expanded rows when the repeater sets minimized.
125+
const storageKey = item?.minimized
126+
? 'blockstudioRepeaterExpanded'
127+
: 'blockstudioRepeater';
128+
let storage = localStorage.getItem(storageKey)
129+
? JSON.parse(localStorage.getItem(storageKey) || '')
123130
: [];
124131
const id = `${postId}-${block.name}-${getId(index)}`;
125132
const [isHover, setIsHover] = useState(false);
126-
const [isMinimized, setIsMinimized] = useState(!!storage.includes(id));
133+
const [isMinimized, setIsMinimized] = useState(
134+
item?.minimized
135+
? startMinimized && !storage.includes(id)
136+
: !!storage.includes(id),
137+
);
127138

128139
const repeaterId = draggableId
129140
.replaceAll('[', '')
@@ -319,16 +330,13 @@ const DragElement = ({
319330
icon: chevronDown,
320331
className: 'blockstudio-repeater__minimize',
321332
onClick: () => {
322-
if (storage.includes(id)) {
323-
storage = storage.filter((item: string) => item !== id);
324-
} else {
333+
const next = !isMinimized;
334+
storage = storage.filter((entry: string) => entry !== id);
335+
if (next !== !!item?.minimized) {
325336
storage.push(id);
326337
}
327-
localStorage.setItem(
328-
'blockstudioRepeater',
329-
JSON.stringify(storage),
330-
);
331-
setIsMinimized(!isMinimized);
338+
localStorage.setItem(storageKey, JSON.stringify(storage));
339+
setIsMinimized(next);
332340
},
333341
reverse: isMinimized,
334342
condition: true,
@@ -473,6 +481,12 @@ export const Repeater = ({
473481
v: string[];
474482
}) => {
475483
const [groups, setGroups] = useState<Any[]>([]);
484+
// With minimized, the rows that exist when the field mounts start
485+
// collapsed; rows added afterwards start expanded so they can be filled in.
486+
const initialRows = useRef(v?.length ?? 0);
487+
if ((v?.length ?? 0) < initialRows.current) {
488+
initialRows.current = v?.length ?? 0;
489+
}
476490
const innerId = id === '' ? item.id : id;
477491
const listId = id || item.id || '';
478492

@@ -553,6 +567,7 @@ export const Repeater = ({
553567
moveDown={moveDown}
554568
moveUp={moveUp}
555569
remove={remove}
570+
startMinimized={index < initialRows.current}
556571
transformed={transformed as Any}
557572
key={draggableId}
558573
/>

‎src/types/block.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,10 @@ export interface BlockstudioAttribute {
643643
* The number of rows the textarea should contain.
644644
*/
645645
rows?: number;
646+
/**
647+
* Collapse the existing rows when the editor opens. Rows added in the editor start expanded.
648+
*/
649+
minimized?: boolean;
646650
/**
647651
* Text that will be displayed when rows are minimized.
648652
*/
@@ -999,6 +1003,10 @@ export interface AttributeAttribute {
9991003
* Accepts all other field types besides another tabs.
10001004
*/
10011005
attributes?: Attribute[];
1006+
/**
1007+
* Collapse the existing rows when the editor opens. Rows added in the editor start expanded.
1008+
*/
1009+
minimized?: boolean;
10021010
/**
10031011
* Text that will be displayed when rows are minimized.
10041012
*/
@@ -3660,6 +3668,7 @@ const typeMap: any = {
36603668
{ json: 'stylisedUi', js: 'stylisedUi', typ: u(undefined, true) },
36613669
{ json: 'tabs', js: 'tabs', typ: u(undefined, a(r('FluffyTab'))) },
36623670
{ json: 'rows', js: 'rows', typ: u(undefined, 3.14) },
3671+
{ json: 'minimized', js: 'minimized', typ: u(undefined, true) },
36633672
{
36643673
json: 'textMinimized',
36653674
js: 'textMinimized',
@@ -3835,6 +3844,7 @@ const typeMap: any = {
38353844
js: 'attributes',
38363845
typ: u(undefined, a(r('Attribute'))),
38373846
},
3847+
{ json: 'minimized', js: 'minimized', typ: u(undefined, true) },
38383848
{
38393849
json: 'textMinimized',
38403850
js: 'textMinimized',
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { expect, test, Page } from '@playwright/test';
2+
import {
3+
getSharedPage,
4+
getEditorCanvas,
5+
resetPageState,
6+
addBlock,
7+
openSidebar,
8+
saveAndReload,
9+
count,
10+
} from '../../utils/playwright-utils';
11+
12+
let page: Page;
13+
14+
const row = (index: number) =>
15+
page.locator(`[data-rfd-draggable-id="items[${index}]"]`);
16+
const rowInput = (index: number) =>
17+
row(index).locator('.blockstudio-fields__field--text input').first();
18+
19+
test.describe.configure({ mode: 'serial' });
20+
21+
test.beforeAll(async ({ browser }) => {
22+
page = await getSharedPage(browser);
23+
await resetPageState(page);
24+
await page.evaluate(() => window.localStorage.clear());
25+
});
26+
27+
test.describe('repeater minimized', () => {
28+
test('insert block', async () => {
29+
await addBlock(page, 'type-repeater-minimized');
30+
const canvas = await getEditorCanvas(page);
31+
await count(canvas, '.is-root-container > .wp-block', 1);
32+
});
33+
34+
test('existing rows start collapsed', async () => {
35+
await openSidebar(page);
36+
await count(page, '[data-rfd-draggable-id^="items["]', 2);
37+
await expect(row(0).getByText('Collapsed: First')).toBeVisible();
38+
await expect(row(1).getByText('Collapsed: Second')).toBeVisible();
39+
await expect(rowInput(0)).toBeHidden();
40+
await expect(rowInput(1)).toBeHidden();
41+
});
42+
43+
test('an added row starts expanded', async () => {
44+
await page.click('text=Add item');
45+
await count(page, '[data-rfd-draggable-id^="items["]', 3);
46+
await expect(rowInput(2)).toBeVisible();
47+
});
48+
49+
test('an expanded row stays expanded after a reload', async () => {
50+
await row(0).locator('.blockstudio-repeater__minimize').first().click();
51+
await expect(rowInput(0)).toBeVisible();
52+
await saveAndReload(page);
53+
const canvas = await getEditorCanvas(page);
54+
await canvas.locator('.is-root-container > .wp-block').first().click();
55+
await openSidebar(page);
56+
await expect(rowInput(0)).toBeVisible();
57+
await expect(rowInput(1)).toBeHidden();
58+
});
59+
60+
test('clean up', async () => {
61+
await page.evaluate(() => window.localStorage.clear());
62+
await resetPageState(page);
63+
});
64+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "https://blockstudio.dev/schema/block",
3+
"name": "blockstudio/type-repeater-minimized",
4+
"title": "Native Repeater Minimized",
5+
"category": "blockstudio-test-native",
6+
"icon": "star-filled",
7+
"description": "Tests a repeater whose existing rows start collapsed.",
8+
"keywords": ["native", "repeater", "minimized"],
9+
"blockstudio": {
10+
"conditions": [
11+
[
12+
{
13+
"type": "postType",
14+
"operator": "==",
15+
"value": "post"
16+
}
17+
]
18+
],
19+
"attributes": [
20+
{
21+
"id": "items",
22+
"type": "repeater",
23+
"label": "Items",
24+
"minimized": true,
25+
"textButton": "Add item",
26+
"textMinimized": { "id": "title", "prefix": "Collapsed: " },
27+
"default": [{ "title": "First" }, { "title": "Second" }],
28+
"attributes": [
29+
{
30+
"id": "title",
31+
"type": "text",
32+
"label": "Title"
33+
}
34+
]
35+
}
36+
]
37+
}
38+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="blockstudio-test__block" id="{{ b.name|replace({'/': '-'}) }}">
2+
<h1>ID: {{ b.name|replace({'/': '-'}) }}</h1>
3+
<code>Attributes: {{ a|json_encode }}</code>
4+
<code>Block: {{ b|json_encode }}</code>
5+
</div>

0 commit comments

Comments
 (0)