Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: table columns reordering and pinning

Brad Barbinf39a5790ad73
diff --git a/apps/erp/app/components/Table/Table.tsx b/apps/erp/app/components/Table/Table.tsx
index dea3444807..bcc3247d1d 100644
--- a/apps/erp/app/components/Table/Table.tsx
+++ b/apps/erp/app/components/Table/Table.tsx
@@ -508,31 +508,65 @@ const Table = <T extends object>({
const [columnSizeMap, setColumnSizeMap] = useState<ColumnSizeMap>(new Map());
useEffect(() => {
- // Allow time for the table to render and delayed column size transitions to occur before calculating column widths
- const timeout = setTimeout(() => {
+ const calculateColumnWidths = () => {
const tableWrapperEl = getTableWrapperEl();
- const columnWidths: ColumnSizeMap = new Map();
+ if (!tableWrapperEl) return;
- let totalWidth = 0;
+ const columnWidths: ColumnSizeMap = new Map();
+ let leftPinnedWidth = 0;
+ // First pass - calculate widths
table.getHeaderGroups().forEach(({ headers }) => {
headers.forEach((header) => {
- const headerEl = tableWrapperEl?.querySelector(
+ const headerEl = tableWrapperEl.querySelector(
getHeaderElSelector(header.id)
);
+ const width = headerEl?.clientWidth ?? 0;
+
+ if (header.column.getIsPinned() === "left") {
+ columnWidths.set(header.id, {
+ width,
+ startX: leftPinnedWidth,
+ });
+ leftPinnedWidth += width;
+ } else {
+ columnWidths.set(header.id, {
+ width,
+ startX: 0, // Will be calculated in second pass
+ });
+ }
+ });
+ });
- columnWidths.set(header.id, {
- width: headerEl?.clientWidth ?? 0,
- startX: totalWidth,
- });
- totalWidth += headerEl?.clientWidth ?? 0;
+ // Second pass - calculate non-pinned positions
+ let currentX = leftPinnedWidth;
+ table.getHeaderGroups().forEach(({ headers }) => {
+ headers.forEach((header) => {
+ if (!header.column.getIsPinned()) {
+ columnWidths.set(header.id, {
+ width: columnWidths.get(header.id)?.width ?? 0,
+ startX: currentX,
+ });
+ currentX += columnWidths.get(header.id)?.width ?? 0;
+ }
});
});
setColumnSizeMap(columnWidths);
- }, 500);
+ };
+
+ // Initial calculation
+ calculateColumnWidths();
- return () => clearTimeout(timeout);
+ // Add resize observer to handle window/table size changes
+ const tableWrapper = getTableWrapperEl();
+ if (tableWrapper) {
+ const resizeObserver = new ResizeObserver(() => {
+ calculateColumnWidths();
+ });
+ resizeObserver.observe(tableWrapper);
+ return () => resizeObserver.disconnect();
+ }
}, [
getTableWrapperEl,
table,
@@ -541,30 +575,22 @@ const Table = <T extends object>({
columnOrder,
withSelectableRows,
]);
-
// const lastLeftPinnedColumn = table
// .getLeftVisibleLeafColumns()
// .findLast((c) => c.getIsPinned() === "left");
const getPinnedStyles = (column: Column<T>): CSSProperties => {
const isPinned = column.getIsPinned();
+ if (!isPinned) return {};
- return {
- // display: "inline-block",
- left:
- isPinned === "left"
- ? columnSizeMap.get(column.id)?.startX ?? 0
- : undefined,
- right: isPinned === "right" ? column.getAfter("right") : undefined,
- position: isPinned ? "sticky" : "relative",
- zIndex: isPinned ? 1 : 0,
- // backgroundColor: isPinned ? "hsl(var(--card))" : undefined,
- boxShadow: isPinned
- ? isPinned === "left"
- ? "4px 0 6px -2px rgba(0, 0, 0, 0.1)"
- : "-4px 0 6px -2px rgba(0, 0, 0, 0.1)"
- : "none",
+ const pinnedPosition = columnSizeMap.get(column.id);
+ const startX = pinnedPosition?.startX ?? 0;
+ return {
+ position: "sticky",
+ left: isPinned === "left" ? startX : undefined,
+ right: isPinned === "right" ? 0 : undefined,
+ zIndex: 2,
maxWidth: isPinned === "right" ? 60 : undefined,
};
};
@@ -575,16 +601,6 @@ const Table = <T extends object>({
delay: 300,
});
- useEffect(() => {
- console.log({
- savedView: {
- columnOrder,
- columnPinning,
- columnVisibility,
- },
- });
- }, [columnOrder, columnPinning, columnVisibility, savedView]);
-
return (
<VStack
spacing={0}
diff --git a/apps/erp/app/components/Table/components/Cell.tsx b/apps/erp/app/components/Table/components/Cell.tsx
index 32947ef181..0c91737f05 100644
--- a/apps/erp/app/components/Table/components/Cell.tsx
+++ b/apps/erp/app/components/Table/components/Cell.tsx
@@ -20,6 +20,7 @@ type CellProps<T> = {
getPinnedStyles: (column: Column<any, unknown>) => CSSProperties;
onClick?: () => void;
onUpdate?: (updates: Record<string, unknown>) => void;
+ table: any;
};
const Cell = <T extends object>({
@@ -33,6 +34,7 @@ const Cell = <T extends object>({
getPinnedStyles,
onClick,
onUpdate,
+ table,
}: CellProps<T>) => {
const { ref, tabIndex, onFocus } = useMovingCellRef(isSelected);
const [hasError, setHasError] = useState(false);
@@ -50,6 +52,8 @@ const Cell = <T extends object>({
? editableComponents[accessorKey]
: null;
+ const isPinned = cell.column.getIsPinned();
+
return (
<Td
className={cn(
@@ -59,12 +63,16 @@ const Cell = <T extends object>({
isEditMode && "border-border border-r",
hasError && "ring-inset ring-2 ring-red-500",
isSelected && "!ring-inset !ring-2 !ring-ring",
- isSelected && hasEditableTableCellComponent && "!bg-background"
+ isSelected && hasEditableTableCellComponent && "!bg-background",
+ "transition-[left,right,box-shadow] duration-200",
+ isPinned && "bg-card"
)}
ref={ref}
style={{
...getPinnedStyles(cell.column),
width: cell.column.getSize(),
+ margin: 0,
+ borderSpacing: 0,
}}
data-row={cell.row.index}
data-column={columnIndex}
@@ -106,7 +114,8 @@ const MemoizedCell = memo(
next.isEditMode === prev.isEditMode &&
next.cell.getValue() === prev.cell.getValue() &&
next.cell.getContext() === prev.cell.getContext() &&
- next.pinnedColumns === prev.pinnedColumns
+ next.pinnedColumns === prev.pinnedColumns &&
+ next.columnIndex === prev.columnIndex
) as typeof Cell;
export default MemoizedCell;
diff --git a/apps/erp/app/components/Table/components/Columns.tsx b/apps/erp/app/components/Table/components/Columns.tsx
index bebf248ec5..c1654afe80 100644
--- a/apps/erp/app/components/Table/components/Columns.tsx
+++ b/apps/erp/app/components/Table/components/Columns.tsx
@@ -54,12 +54,40 @@ const Columns = <T extends object>({
values={columnOrder}
onReorder={(newOrder: ColumnOrderState) => {
if (withSelectableRows) newOrder.unshift("select");
+
+ // Get first non-pinned column index
+ const firstNonPinnedIndex = columns.findIndex(
+ (col) => !col.getIsPinned() && isColumnToggable(col)
+ );
+
+ // For each column in new order, if it's before first non-pinned,
+ // pin it to left and make visible. Otherwise unpin it.
+ newOrder.forEach((columnId, index) => {
+ const column = columns.find((col) => col.id === columnId);
+ if (column && isColumnToggable(column)) {
+ if (index < firstNonPinnedIndex) {
+ column.pin("left");
+ if (!column.getIsVisible()) {
+ column.toggleVisibility(true);
+ }
+ } else if (column.getIsPinned()) {
+ column.pin(false);
+ }
+ }
+ });
+
setColumnOrder(newOrder);
}}
className="w-full space-y-2"
>
- {columns.reduce<JSX.Element[]>((acc, column) => {
- if (isColumnToggable(column))
+ {columns.reduce<JSX.Element[]>((acc, column, index) => {
+ if (isColumnToggable(column)) {
+ const prevColumn = columns[index - 1];
+ const canPin =
+ !prevColumn ||
+ prevColumn.getIsPinned() ||
+ !isColumnToggable(prevColumn);
+
acc.push(
<Reorder.Item
key={column.id}
@@ -79,28 +107,88 @@ const Columns = <T extends object>({
<IconButton
aria-label="Toggle column"
icon={column.getIsPinned() ? <LuPin /> : <LuPinOff />}
- onClick={(e) => {
+ onClick={() => {
if (column.getIsPinned()) {
column.pin(false);
- } else {
+
+ // Get index of last pinned column
+ const lastPinnedIndex = columns.reduce(
+ (acc, col, i) => {
+ if (
+ col.getIsPinned() &&
+ isColumnToggable(col)
+ ) {
+ return i;
+ }
+ return acc;
+ },
+ -1
+ );
+
+ // Move column after pinned columns
+ const newOrder = [...columnOrder];
+ const colIndex = newOrder.indexOf(column.id);
+ if (colIndex > -1) {
+ newOrder.splice(colIndex, 1);
+ newOrder.splice(
+ lastPinnedIndex + 1,
+ 0,
+ column.id
+ );
+ setColumnOrder(newOrder);
+ }
+ } else if (canPin) {
column.pin("left");
- // when a column is pinned, we assure that it's visible
+ // Make column visible when pinned
if (!column.getIsVisible()) {
- column.getToggleVisibilityHandler()(e);
+ column.toggleVisibility(true);
}
}
}}
variant="ghost"
+ disabled={!column.getIsPinned() && !canPin}
/>
<IconButton
aria-label="Toggle column"
icon={column.getIsVisible() ? <LuEye /> : <LuEyeOff />}
- onClick={column.getToggleVisibilityHandler()}
+ onClick={() => {
+ // When hiding a column, unpin it and move after pinned columns
+ if (column.getIsVisible()) {
+ if (column.getIsPinned()) {
+ column.pin(false);
+
+ // Get index of last pinned column
+ const lastPinnedIndex = columns.reduce(
+ (acc, col, i) => {
+ if (
+ col.getIsPinned() &&
+ isColumnToggable(col)
+ ) {
+ return i;
+ }
+ return acc;
+ },
+ -1
+ );
+
+ // Move column after pinned columns
+ const newOrder = [...columnOrder];
+ const colIndex = newOrder.indexOf(column.id);
+ if (colIndex > -1) {
+ newOrder.splice(colIndex, 1);
+ newOrder.splice(lastPinnedIndex, 0, column.id);
+ setColumnOrder(newOrder);
+ }
+ }
+ }
+ column.toggleVisibility();
+ }}
variant="ghost"
/>
</HStack>
</Reorder.Item>
);
+ }
return acc;
}, [])}
</Reorder.Group>
diff --git a/apps/erp/app/components/Table/components/Row.tsx b/apps/erp/app/components/Table/components/Row.tsx
index fc57ccc87f..bb08f960e1 100644
--- a/apps/erp/app/components/Table/components/Row.tsx
+++ b/apps/erp/app/components/Table/components/Row.tsx
@@ -93,7 +93,8 @@ const MemoizedRow = memo(
next.row.index === prev.selectedCell?.row &&
next.selectedCell?.column === prev.selectedCell?.column &&
next.isEditing === prev.isEditing &&
- next.isEditMode === prev.isEditMode
+ next.isEditMode === prev.isEditMode &&
+ next.pinnedColumns === prev.pinnedColumns
) as typeof Row;
export default MemoizedRow;
diff --git a/packages/react/src/Resizable.tsx b/packages/react/src/Resizable.tsx
index 7bc0bc5a70..a94b9a6abf 100644
--- a/packages/react/src/Resizable.tsx
+++ b/packages/react/src/Resizable.tsx
@@ -29,7 +29,7 @@ const ResizableHandle = ({
}) => (
<ResizablePrimitive.PanelResizeHandle
className={cn(
- "relative flex w-[3px] items-center justify-center bg-foreground/10 after:absolute after:inset-y-0 after:left-1/2 after:w-1 after:-translate-x-1/2 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-1 data-[panel-group-direction=vertical]:h-px data-[panel-group-direction=vertical]:w-full data-[panel-group-direction=vertical]:after:left-0 data-[panel-group-direction=vertical]:after:h-1 data-[panel-group-direction=vertical]:after:w-full data-[panel-group-direction=vertical]:after:-translate-y-1/2 data-[panel-group-direction=vertical]:after:translate-x-0 [&[data-panel-group-direction=vertical]>div]:rotate-90",
+ "relative flex w-px items-center justify-center bg-foreground/10 after:absolute after:inset-y-0 after:left-1/2 after:w-px after:-translate-x-1/2 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-1 data-[panel-group-direction=vertical]:h-px data-[panel-group-direction=vertical]:w-full data-[panel-group-direction=vertical]:after:left-0 data-[panel-group-direction=vertical]:after:h-1 data-[panel-group-direction=vertical]:after:w-full data-[panel-group-direction=vertical]:after:-translate-y-1/2 data-[panel-group-direction=vertical]:after:translate-x-0 [&[data-panel-group-direction=vertical]>div]:rotate-90",
className
)}
{...props}