From 89338f72ce141a3f1e3a172175dd68559e8576ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20S=C5=82ysz?= Date: Tue, 17 Nov 2026 20:46:55 +0100 Subject: [PATCH 1/2] =?UTF-8?q?-=20enum=20items:=20Sort=20menu=20in=20the?= =?UTF-8?q?=20items=20dialog=20(Value=20/=20Text,=20A=20=E2=86=92=20Z=20or?= =?UTF-8?q?=20Z=20=E2=86=92=20A,=20natural=20order)=20that=20sorts=20the?= =?UTF-8?q?=20items=20for=20good;=20the=20column=20headers=20no=20longer?= =?UTF-8?q?=20sort=20only=20the=20view?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/dialogs/EditEnumDialog.jsx | 45 +++++++++++++++++-- frontend/src/utils/MaterialTableHelper.jsx | 10 +++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/frontend/src/components/dialogs/EditEnumDialog.jsx b/frontend/src/components/dialogs/EditEnumDialog.jsx index 25f9055a..2fd31b61 100644 --- a/frontend/src/components/dialogs/EditEnumDialog.jsx +++ b/frontend/src/components/dialogs/EditEnumDialog.jsx @@ -5,6 +5,8 @@ import { DialogContent as MuiDialogContent, DialogTitle as MuiDialogTitle, IconButton, + Menu, + MenuItem, styled, Tooltip, useTheme @@ -13,7 +15,8 @@ import { ArrowDropDown, ArrowDropUp, Close as CloseIcon, - MoreHoriz + MoreHoriz, + SortByAlpha } from '@mui/icons-material'; import { getDialogTableHeight, @@ -23,7 +26,8 @@ import { moveDown, moveUp, removeRow, - replaceRow + replaceRow, + sortedRows } from "../../utils/MaterialTableHelper"; import MaterialTable from '@material-table/core'; import PropTypes from 'prop-types'; @@ -91,6 +95,19 @@ export default function EditEnumDialog(props) { const [data, setData] = useState(props.items != null ? props.items : []); // the rows per page of the dialogs, remembered const [pageSize, setPageSize] = useState(getLastPageSizeOnDialog); + // the Sort menu: sorts the items for good (the order of the list in the data); opened at the position of the + // button, because material-table renders its actions again and the button element itself doesn't stay + const [sortMenuPosition, setSortMenuPosition] = useState(null); + + const openSortMenu = event => { + const rect = event.currentTarget.getBoundingClientRect(); + setSortMenuPosition({top: rect.bottom, left: rect.left}); + } + + const sortItems = (field, descending) => { + setData(sortedRows(data, field, descending)); + setSortMenuPosition(null); + } const getColumns = () => { let columnsArray = []; @@ -112,7 +129,9 @@ export default function EditEnumDialog(props) { editComponent: EditComponent }); } - return columnsArray; + // no sorting by the headers: it would only sort the view, while the arrows and the Sort menu change the order + // of the items (options.sorting is not enough in material-table 6) + return columnsArray.map(column => ({...column, sorting: false})); } const handleClickOpen = () => { @@ -186,7 +205,7 @@ export default function EditEnumDialog(props) { paginationType: 'stepped', pageSizeOptions: getPageSizeOptionsOnDialog(), actionsColumnIndex: -1, - maxColumnSort: 1, + sorting: false, selection: false, filtering: false, padding: 'dense', @@ -230,6 +249,13 @@ export default function EditEnumDialog(props) { }), }} actions={[ + { + icon: () => , + tooltip: 'Sort items', + isFreeAction: true, + disabled: data.length < 2, + onClick: openSortMenu + }, rowData => ({ icon: () => , tooltip: 'Move down', @@ -247,6 +273,17 @@ export default function EditEnumDialog(props) { }) ]} /> + setSortMenuPosition(null)} + > + sortItems('value', false)}>Value: A → Z + sortItems('value', true)}>Value: Z → A + sortItems('text', false)}>Text: A → Z + sortItems('text', true)}>Text: Z → A + { return swappedArray(dataCollection, itemId, itemId + 1); } +// Rows sorted for good by a text field: natural order ("item2" before "item10"), letters with diacritics in place, a +// stable sort (rows with the same value keep their order) +const naturalCollator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'}); + +export const sortedRows = (rows, field, descending = false) => + rows.map(withoutTableData).sort((a, b) => { + const result = naturalCollator.compare(String(a[field] ?? ''), String(b[field] ?? '')); + return descending ? -result : result; + }); + function swappedArray(arr, index1, index2) { const newArr = [...arr]; [newArr[index1], newArr[index2]] = [newArr[index2], newArr[index1]]; From 83b7d8bd690822046f37cc56529216c20b6321c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20S=C5=82ysz?= Date: Tue, 17 Nov 2026 20:55:49 +0100 Subject: [PATCH 2/2] - rows with an order of their own can also be dragged onto another row (enum items, columns, class properties, filter and task properties, task actions); the column headers of these tables no longer sort only the view --- .../src/components/dialogs/EditEnumDialog.jsx | 14 +++-- .../utils/ActionPropertiesTable.jsx | 12 ++-- .../src/components/utils/ColumnsTable.jsx | 12 ++-- .../src/components/utils/DraggableBodyRow.jsx | 57 +++++++++++++++++++ .../src/components/utils/PropertiesTable.jsx | 15 ++--- frontend/src/utils/MaterialTableHelper.jsx | 13 +++++ 6 files changed, 102 insertions(+), 21 deletions(-) create mode 100644 frontend/src/components/utils/DraggableBodyRow.jsx diff --git a/frontend/src/components/dialogs/EditEnumDialog.jsx b/frontend/src/components/dialogs/EditEnumDialog.jsx index 2fd31b61..9b0b1219 100644 --- a/frontend/src/components/dialogs/EditEnumDialog.jsx +++ b/frontend/src/components/dialogs/EditEnumDialog.jsx @@ -24,10 +24,12 @@ import { getTableHeaderBackgroundColor, getTableRowBackgroundColor, moveDown, + movedRow, moveUp, removeRow, replaceRow, - sortedRows + sortedRows, + withoutHeaderSorting } from "../../utils/MaterialTableHelper"; import MaterialTable from '@material-table/core'; import PropTypes from 'prop-types'; @@ -35,6 +37,7 @@ import EditIconDialog from "./SelectIconDialog"; import {MessageBox} from "../utils/MessageBox"; import {getLastPageSizeOnDialog, setLastPageSizeOnDialog} from "../../utils/ConfigurationStorage"; import StyledIcon from "../utils/StyledIcon"; +import DraggableBodyRow from "../utils/DraggableBodyRow"; const PREFIX = 'EditEnumDialog'; @@ -129,9 +132,7 @@ export default function EditEnumDialog(props) { editComponent: EditComponent }); } - // no sorting by the headers: it would only sort the view, while the arrows and the Sort menu change the order - // of the items (options.sorting is not enough in material-table 6) - return columnsArray.map(column => ({...column, sorting: false})); + return withoutHeaderSorting(columnsArray); } const handleClickOpen = () => { @@ -205,7 +206,6 @@ export default function EditEnumDialog(props) { paginationType: 'stepped', pageSizeOptions: getPageSizeOptionsOnDialog(), actionsColumnIndex: -1, - sorting: false, selection: false, filtering: false, padding: 'dense', @@ -221,7 +221,9 @@ export default function EditEnumDialog(props) { }) }} components={{ - Container: props =>
+ Container: props =>
, + Row: props => setData(movedRow(data, from, to))}/> }} editable={{ onRowAdd: newData => diff --git a/frontend/src/components/utils/ActionPropertiesTable.jsx b/frontend/src/components/utils/ActionPropertiesTable.jsx index 24be60a3..fbb344af 100644 --- a/frontend/src/components/utils/ActionPropertiesTable.jsx +++ b/frontend/src/components/utils/ActionPropertiesTable.jsx @@ -3,7 +3,9 @@ import { getTableHeaderBackgroundColor, getTableRowBackgroundColor, moveDown, + movedRow, moveUp, + withoutHeaderSorting, removeRow, replaceRow } from "../../utils/MaterialTableHelper"; @@ -17,6 +19,7 @@ import { import ArrowDropDown from "@mui/icons-material/ArrowDropDown"; import ArrowDropUp from "@mui/icons-material/ArrowDropUp"; import MaterialTable, {MTableEditField} from '@material-table/core'; +import DraggableBodyRow from "./DraggableBodyRow"; import React, {useContext, useEffect, useState} from "react"; import EnumsContext from "../../context/enums/EnumsContext"; import {useTheme} from "@mui/material"; @@ -212,7 +215,7 @@ export default function ActionPropertiesTable(props) { ); } } - ]} + ])} data={data} options={{ paging: false, actionsColumnIndex: -1, - maxColumnSort: 1, selection: false, search: true, filtering: false, @@ -278,7 +280,9 @@ export default function ActionPropertiesTable(props) { rowStyle: rowData => ({backgroundColor: getTableRowBackgroundColor(rowData, theme)}) }} components={{ - Container: props =>
+ Container: props =>
, + Row: props => setData(movedRow(data, from, to))}/> }} editable={getEditable()} actions={[ diff --git a/frontend/src/components/utils/ColumnsTable.jsx b/frontend/src/components/utils/ColumnsTable.jsx index 50754604..2da748f8 100644 --- a/frontend/src/components/utils/ColumnsTable.jsx +++ b/frontend/src/components/utils/ColumnsTable.jsx @@ -4,7 +4,9 @@ import { getTableHeaderBackgroundColor, getTableRowBackgroundColor, moveDown, + movedRow, moveUp, + withoutHeaderSorting, removeRow, replaceRow } from "../../utils/MaterialTableHelper"; @@ -15,6 +17,7 @@ import { } from "../../utils/JsonHelper"; import {ArrowDropDown, ArrowDropUp} from "@mui/icons-material"; import MaterialTable from '@material-table/core'; +import DraggableBodyRow from "./DraggableBodyRow"; import React, {useEffect, useState} from "react"; import { getLastPageSizeOnDialog, @@ -65,7 +68,7 @@ export default function ColumnsTable(props) {
+ Container: props =>
, + Row: props => setData(movedRow(columns, from, to))}/> }} editable={{ onRowAdd: newData => diff --git a/frontend/src/components/utils/DraggableBodyRow.jsx b/frontend/src/components/utils/DraggableBodyRow.jsx new file mode 100644 index 00000000..182c2ea7 --- /dev/null +++ b/frontend/src/components/utils/DraggableBodyRow.jsx @@ -0,0 +1,57 @@ +import React, {useState} from 'react'; +import {MTableBodyRow} from '@material-table/core'; +import {useTheme} from '@mui/material'; + +// The position (in the data) of the row being dragged; one drag at a time, so shared by all the rows +let draggedIndex = null; + +// A row of a material-table that can be dragged onto another row to move it there (besides the Move up/down arrows). +// Use: components={{Row: props => setData(movedRow(data, from, to))}/>}} +// Native HTML drag and drop: material-table passes the other props on to the . The row isn't draggable while a row +// is edited (the inputs need the mouse). +export default function DraggableBodyRow({onMoveRow, ...props}) { + const theme = useTheme(); + const [dropMarker, setDropMarker] = useState(null); // 'top' | 'bottom' | null + const index = props.data.tableData.index; + const draggable = !props.hasAnyEditingRow; + + const line = `inset 0 ${dropMarker === 'top' ? 2 : -2}px 0 ${theme.palette.primary.main}`; + + return ( + { + draggedIndex = index; + event.dataTransfer.effectAllowed = 'move'; + // Firefox starts a drag only with some data set + event.dataTransfer.setData('text/plain', ''); + }} + onDragOver={event => { + if (draggedIndex == null || draggedIndex === index) { + return; + } + event.preventDefault(); + event.dataTransfer.dropEffect = 'move'; + setDropMarker(draggedIndex > index ? 'top' : 'bottom'); + }} + onDragLeave={() => setDropMarker(null)} + onDrop={event => { + event.preventDefault(); + setDropMarker(null); + if (draggedIndex != null && draggedIndex !== index) { + onMoveRow(draggedIndex, index); + } + draggedIndex = null; + }} + onDragEnd={() => { + draggedIndex = null; + setDropMarker(null); + }} + /> + ); +} diff --git a/frontend/src/components/utils/PropertiesTable.jsx b/frontend/src/components/utils/PropertiesTable.jsx index 9e5fab6f..9ec45e2f 100644 --- a/frontend/src/components/utils/PropertiesTable.jsx +++ b/frontend/src/components/utils/PropertiesTable.jsx @@ -4,7 +4,9 @@ import { getTableHeaderBackgroundColor, getTableRowBackgroundColor, moveDown, + movedRow, moveUp, + withoutHeaderSorting, removeRow, replaceRow } from "../../utils/MaterialTableHelper"; @@ -12,6 +14,7 @@ import SelectEnumDialog from "../dialogs/SelectEnumDialog"; import {isItemChanged, uuidV4, validateItem} from "../../utils/JsonHelper"; import {ArrowDropDown, ArrowDropUp} from "@mui/icons-material"; import MaterialTable from '@material-table/core'; +import DraggableBodyRow from "./DraggableBodyRow"; import React, {useEffect, useState} from "react"; import { getLastPageSizeOnDialog, @@ -79,15 +82,12 @@ export default function PropertiesTable(props) { return ''; } - console.log("data:"); - console.log(data); - return !delay && (
}, {title: 'UUID', field: 'uuid', hidden: true, type: 'string'} - ]} + ])} data={data} onRowsPerPageChange={onChangeRowsPerPage} options={{ @@ -145,7 +145,6 @@ export default function PropertiesTable(props) { paginationType: 'stepped', pageSizeOptions: getPageSizeOptionsOnDialog(), actionsColumnIndex: -1, - maxColumnSort: 1, selection: false, filtering: false, padding: 'dense', @@ -163,7 +162,9 @@ export default function PropertiesTable(props) { }) }} components={{ - Container: props =>
+ Container: props =>
, + Row: props => setData(movedRow(data, from, to))}/> }} editable={{ onRowAdd: newData => diff --git a/frontend/src/utils/MaterialTableHelper.jsx b/frontend/src/utils/MaterialTableHelper.jsx index e4849493..53c870cd 100644 --- a/frontend/src/utils/MaterialTableHelper.jsx +++ b/frontend/src/utils/MaterialTableHelper.jsx @@ -238,6 +238,19 @@ export const sortedRows = (rows, field, descending = false) => return descending ? -result : result; }); +// Rows with the row at position "from" moved to position "to" (dragging a row onto another one) +export const movedRow = (rows, from, to) => { + const newRows = [...rows]; + const [row] = newRows.splice(from, 1); + newRows.splice(to, 0, row); + return newRows; +} + +// Columns of a table whose rows have an order of their own (Move up/down, dragging): the headers don't sort, because +// that would only sort the view, while the arrows and dragging move the rows in the data (options.sorting is not +// enough in material-table 6) +export const withoutHeaderSorting = columns => columns.map(column => ({...column, sorting: false})); + function swappedArray(arr, index1, index2) { const newArr = [...arr]; [newArr[index1], newArr[index2]] = [newArr[index2], newArr[index1]];