-
Notifications
You must be signed in to change notification settings - Fork 89
app/vmui: allow table column reordering #957
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -41,6 +41,8 @@ const TableSettings: FC<TableSettingsProps> = ({ | |||||||
|
|
||||||||
| const [searchColumn, setSearchColumn] = useState(""); | ||||||||
| const [indexFocusItem, setIndexFocusItem] = useState(-1); | ||||||||
| const [draggingColumn, setDraggingColumn] = useState<string | null>(null); | ||||||||
| const [dragOverColumn, setDragOverColumn] = useState<string | null>(null); | ||||||||
|
|
||||||||
| const customColumns = useMemo(() => { | ||||||||
| return selectedColumns.filter(col => !columns.includes(col)); | ||||||||
|
|
@@ -122,6 +124,43 @@ const TableSettings: FC<TableSettingsProps> = ({ | |||||||
| onChangeColumns(columnsArray); | ||||||||
| }, []); | ||||||||
|
|
||||||||
| const handleDragStart = (column: string) => (e: DragEvent) => { | ||||||||
| setDraggingColumn(column); | ||||||||
| e.dataTransfer?.setData("text/plain", column); | ||||||||
| }; | ||||||||
|
|
||||||||
| const handleDragOver = (column: string) => (e: DragEvent) => { | ||||||||
| e.preventDefault(); | ||||||||
| if (dragOverColumn === column) return; | ||||||||
| setDragOverColumn(column); | ||||||||
| }; | ||||||||
|
|
||||||||
| const handleDragLeave = () => { | ||||||||
| setDragOverColumn(null); | ||||||||
| }; | ||||||||
|
|
||||||||
| const handleDrop = (targetColumn: string) => (e: DragEvent) => { | ||||||||
| e.preventDefault(); | ||||||||
| if (!draggingColumn || draggingColumn === targetColumn) return; | ||||||||
|
|
||||||||
| const fromIndex = selectedColumns.indexOf(draggingColumn); | ||||||||
| const toIndex = selectedColumns.indexOf(targetColumn); | ||||||||
| if (fromIndex === -1 || toIndex === -1) return; | ||||||||
|
|
||||||||
| const updatedColumns = [...selectedColumns]; | ||||||||
| updatedColumns.splice(fromIndex, 1); | ||||||||
| updatedColumns.splice(toIndex, 0, draggingColumn); | ||||||||
|
|
||||||||
| handleChangeDisplayColumns(updatedColumns); | ||||||||
| setDragOverColumn(null); | ||||||||
| setDraggingColumn(null); | ||||||||
|
Comment on lines
+155
to
+156
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cleanup logic is duplicated here - consider calling
Suggested change
|
||||||||
| }; | ||||||||
|
|
||||||||
| const handleDragEnd = () => { | ||||||||
| setDragOverColumn(null); | ||||||||
| setDraggingColumn(null); | ||||||||
| }; | ||||||||
|
|
||||||||
| return ( | ||||||||
| <div className="vm-table-settings"> | ||||||||
| <Tooltip title={title}> | ||||||||
|
|
@@ -193,6 +232,39 @@ const TableSettings: FC<TableSettingsProps> = ({ | |||||||
| ))} | ||||||||
| </div> | ||||||||
| </div> | ||||||||
| {!!selectedColumns.length && ( | ||||||||
| <div className="vm-table-settings-modal-columns-order"> | ||||||||
| <div className="vm-table-settings-modal-columns-order__title"> | ||||||||
| Drag to reorder selected columns | ||||||||
| </div> | ||||||||
| <div className="vm-table-settings-modal-columns-order__list"> | ||||||||
| {selectedColumns.map((col) => ( | ||||||||
| <div | ||||||||
| className={classNames({ | ||||||||
| "vm-table-settings-modal-columns-order__item": true, | ||||||||
| "vm-table-settings-modal-columns-order__item_dragging": draggingColumn === col, | ||||||||
| "vm-table-settings-modal-columns-order__item_over": dragOverColumn === col, | ||||||||
| })} | ||||||||
| key={col} | ||||||||
| draggable | ||||||||
| onDragStart={handleDragStart(col)} | ||||||||
| onDragOver={handleDragOver(col)} | ||||||||
| onDragLeave={handleDragLeave} | ||||||||
| onDrop={handleDrop(col)} | ||||||||
| onDragEnd={handleDragEnd} | ||||||||
| > | ||||||||
| <span | ||||||||
| className="vm-table-settings-modal-columns-order__drag" | ||||||||
| aria-hidden="true" | ||||||||
| /> | ||||||||
| <span className="vm-table-settings-modal-columns-order__label"> | ||||||||
| {col} | ||||||||
| </span> | ||||||||
| </div> | ||||||||
| ))} | ||||||||
| </div> | ||||||||
| </div> | ||||||||
| )} | ||||||||
| </div> | ||||||||
| {toggleTableCompact && tableCompact !== undefined && ( | ||||||||
| <div className="vm-table-settings-modal-section"> | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { render, screen } from "@testing-library/preact"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import TableLogs from "./TableLogs"; | ||
|
|
||
| const logs = [ | ||
| { _time: "2025-01-01T00:00:00Z", file: "a.go", _msg: "first message" }, | ||
| { _time: "2025-01-01T00:01:00Z", file: "b.go", _msg: "second message" }, | ||
| ]; | ||
|
|
||
| describe("TableLogs", () => { | ||
| it("renders columns in the provided display order", () => { | ||
| render( | ||
| <TableLogs | ||
| logs={logs} | ||
| displayColumns={["_msg", "file", "_time"]} | ||
| tableCompact={false} | ||
| columns={["_time", "file", "_msg"]} | ||
| rowsPerPage={10} | ||
| /> | ||
| ); | ||
|
|
||
| const headers = screen | ||
| .getAllByRole("columnheader") | ||
| .map((header) => header.textContent?.trim() || ""); | ||
|
|
||
| expect(headers.slice(0, 3)).toEqual(["_msg", "file", "_time"]); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -45,12 +45,19 @@ const TableLogs: FC<TableLogsProps> = ({ logs, displayColumns, tableCompact, col | |||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||
| }, [columns]); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const tableColumnsMap = useMemo(() => { | ||||||||||||||||||||||||||
| return new Map(tableColumns.map((col) => [String(col.key), col])); | ||||||||||||||||||||||||||
| }, [tableColumns]); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const filteredColumns = useMemo(() => { | ||||||||||||||||||||||||||
| if (tableCompact) return compactColumns; | ||||||||||||||||||||||||||
| if (!displayColumns?.length) return []; | ||||||||||||||||||||||||||
| return tableColumns.filter(c => displayColumns.includes(c.key as string)); | ||||||||||||||||||||||||||
| }, [tableColumns, displayColumns, tableCompact]); | ||||||||||||||||||||||||||
| return displayColumns.reduce<typeof tableColumns>((acc, key) => { | ||||||||||||||||||||||||||
| const column = tableColumnsMap.get(key); | ||||||||||||||||||||||||||
| if (column) acc.push(column); | ||||||||||||||||||||||||||
| return acc; | ||||||||||||||||||||||||||
| }, []); | ||||||||||||||||||||||||||
| }, [tableColumnsMap, displayColumns, tableCompact]); | ||||||||||||||||||||||||||
|
Comment on lines
+55
to
+60
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the column config is derived from the key,
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const paginationOffset = useMemo(() => { | ||||||||||||||||||||||||||
| const startIndex = (page - 1) * rowsPerPage; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dragOverColumncan be stale here. This check can be removed - setting the same value won’t trigger a re-render.