From 82ffdfd275fea1b6010ae0b310b532f3e19d40bb Mon Sep 17 00:00:00 2001 From: Stratos Iordanidis Date: Sun, 10 May 2026 00:05:23 +0200 Subject: [PATCH] Autofill menu item's labels on page change --- src/components/menus/MenuEditor/SortableItem.jsx | 6 ++++++ src/components/menus/MenuEditor/index.jsx | 6 +++--- src/components/menus/MenuEditor/utils/menuUtils.js | 3 +++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/components/menus/MenuEditor/SortableItem.jsx b/src/components/menus/MenuEditor/SortableItem.jsx index 9ff0ae98..d5dc6912 100644 --- a/src/components/menus/MenuEditor/SortableItem.jsx +++ b/src/components/menus/MenuEditor/SortableItem.jsx @@ -4,6 +4,7 @@ import { ChevronRight, ChevronDown, GripVertical, Plus, Trash2 } from "lucide-re import { IconButton } from "../../ui/Button"; import Tooltip from "../../ui/Tooltip"; import MenuCombobox from "./MenuCombobox"; +import { MENU_DEFAULT_LABEL_NEW_ITEM, MENU_DEFAULT_LABEL_NEW_CHILD } from "./utils/menuUtils"; // SortableItem component - memoized const SortableItem = memo(function SortableItem({ @@ -81,11 +82,16 @@ const SortableItem = memo(function SortableItem({ const page = pages.find((p) => p.value === value); if (page) { + + const trimmedLabel = String(item.label ?? "").trim(); + const isDefaultLabel = !trimmedLabel || trimmedLabel === MENU_DEFAULT_LABEL_NEW_ITEM || trimmedLabel === MENU_DEFAULT_LABEL_NEW_CHILD; + // User selected an internal page - store pageUuid and derive link from current slug onEdit(item.id, { ...item, pageUuid: page.value, link: `${page.slug}.html`, + label: isDefaultLabel ? page.label : item.label }); } else { // Custom URL - explicitly clear pageUuid because menu item updates are merged. diff --git a/src/components/menus/MenuEditor/index.jsx b/src/components/menus/MenuEditor/index.jsx index 55306964..2a19c1ad 100644 --- a/src/components/menus/MenuEditor/index.jsx +++ b/src/components/menus/MenuEditor/index.jsx @@ -15,7 +15,7 @@ import { debounce, isEqual } from "lodash"; import Button from "../../ui/Button"; import SortableList from "./SortableList"; import DragOverlayComponent from "./DragOverlay"; -import { ensureIds, findItemById, getItemAtPath, generateId } from "./utils/menuUtils"; +import { ensureIds, findItemById, getItemAtPath, generateId, MENU_DEFAULT_LABEL_NEW_ITEM, MENU_DEFAULT_LABEL_NEW_CHILD } from "./utils/menuUtils"; import { flattenTree, getProjection, @@ -307,7 +307,7 @@ function MenuEditor({ initialItems = [], onChange, onDeleteItem }) { ...prevItems, { id: generateId(), - label: "New Item", + label: MENU_DEFAULT_LABEL_NEW_ITEM, link: "", items: [], }, @@ -412,7 +412,7 @@ function MenuEditor({ initialItems = [], onChange, onDeleteItem }) { // Add the new child parentItem.items.push({ id: generateId(), - label: "New Child", + label: MENU_DEFAULT_LABEL_NEW_CHILD, link: "", items: [], }); diff --git a/src/components/menus/MenuEditor/utils/menuUtils.js b/src/components/menus/MenuEditor/utils/menuUtils.js index 6d9bc4c2..dfd86863 100644 --- a/src/components/menus/MenuEditor/utils/menuUtils.js +++ b/src/components/menus/MenuEditor/utils/menuUtils.js @@ -1,5 +1,8 @@ import { v4 as uuidv4 } from "uuid"; +export const MENU_DEFAULT_LABEL_NEW_ITEM = "New Item"; +export const MENU_DEFAULT_LABEL_NEW_CHILD = "New Child"; + // Generate a unique ID export const generateId = () => `item-${uuidv4()}`;