Skip to content
This repository was archived by the owner on Nov 30, 2025. It is now read-only.

Commit 227896a

Browse files
authored
Merge pull request #329 from MathSoc/rearrangeable-array-elements
Fix CMS: Rearrangeable array elements #230
2 parents 91b9eb8 + da5d049 commit 227896a

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

public/assets/ts/admin/page-editor/editor-nodes/EditorArrayNode.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ export const EditorArrayNode: React.FC<EditorNodeProps> = (props) => {
2727
}
2828
}, [data]);
2929

30+
const moveItem = (fromIndex: number, toIndex: number) => {
31+
if (toIndex < 0 || toIndex >= transformedData.length) {
32+
return;
33+
}
34+
35+
const newData = [...transformedData];
36+
[newData[fromIndex], newData[toIndex]] = [newData[toIndex], newData[fromIndex]];
37+
38+
setDataValue(path, newData);
39+
setVersion(version + 1);
40+
};
41+
3042
const removeItem = (index: number) => {
3143
setDataValue(path.concat(index.toString()), null);
3244
setVersion(version + 1);
@@ -65,14 +77,22 @@ export const EditorArrayNode: React.FC<EditorNodeProps> = (props) => {
6577
const nextPath = path.concat(index.toString());
6678
return (
6779
<EditorNode
80+
key={`${index}-${version}`}
6881
name={`${props.name} item ${index + 1}`}
6982
headerButtons={[
83+
{
84+
name: "Move Up",
85+
onClick: () => moveItem(index, index - 1),
86+
},
87+
{
88+
name: "Move Down",
89+
onClick: () => moveItem(index, index + 1),
90+
},
7091
{ name: "Remove", onClick: () => removeItem(index) },
7192
]}
7293
/**
7394
* The data index is persistent across re-renders
7495
*/
75-
key={index}
7696
path={nextPath}
7797
value={entry}
7898
theme={"grey"}

0 commit comments

Comments
 (0)