Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

- **Breaking** Full-width _Navbar_ (`containerWidth={false}`) now uses a 12px side gutter instead of 24px (16px on small screens), so a default-size menu `IconButton` at its left edge lines up with _SidebarNav_'s item icons. Centred Navbars keep the 24px gutter.
- **Breaking** _User_ no longer renders a `flexGrow` spacer before itself, which added an extra gap next to sibling controls in a `Navbar` slot. If you placed `User` directly in a flex container and relied on it pushing itself to the right, add your own spacer.
- `DiamondDSIntegrations.mrtOptions({ onRowReorder })` adds row reordering: drag with a drop line between rows, plus Move up/down in the row menu.
- `DiamondDSIntegrations.mrtOptions({ onRowReorder })` adds row reordering: drag with a drop line between rows, plus Move to top/up/down/bottom in the row menu, announced to screen readers. `isRowMovable` locks rows.
- `useRowReorder` adds a toggleable reorder mode to MRT tables.

### Fixed

Expand Down
55 changes: 16 additions & 39 deletions dev/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as React from "react";
import {
AppBar,
Box,
Button,
Checkbox,
Chip,
Container,
Expand Down Expand Up @@ -50,6 +49,7 @@ import {
ThemeProvider,
DiamondDSTheme,
DiamondDSIntegrations,
useRowReorder,
} from "../../src/index";
import type { Theme } from "@mui/material/styles";
import type { TypographyProps } from "@mui/material/Typography";
Expand Down Expand Up @@ -205,14 +205,16 @@ export const MidLevelMrtExample = () => {

export const AdvancedMrtExample = () => {
const [rows, setRows] = React.useState(data);
const [reorderMode, setReorderMode] = React.useState(false);

const moveRow = (fromIndex: number, toIndex: number) =>
setRows((prev) => {
const next = [...prev];
next.splice(toIndex, 0, next.splice(fromIndex, 1)[0]);
return next;
});
const reorder = useRowReorder<Experiment>({
onReorder: (fromIndex, toIndex) =>
setRows((prev) => {
const next = [...prev];
next.splice(toIndex, 0, next.splice(fromIndex, 1)[0]);
return next;
}),
isRowMovable: (experiment) => experiment.status !== "Completed",
});

const table = useMaterialReactTable({
columns: mrtColumns,
Expand All @@ -221,38 +223,13 @@ export const AdvancedMrtExample = () => {
enableDensityToggle: false,
enableFullScreenToggle: false,
enableHiding: true,
// Reorder mode swaps checkboxes for drag handles; sorting is off so it can't fight the manual order
enableRowSelection: !reorderMode,
enableRowOrdering: reorderMode,
enableSorting: !reorderMode,
enableRowSelection: true,
getRowId: (row) => row.id,
initialState: { rowSelection: { "EXP-1003": true } },
// MRT only builds column order on mount, so pin the active display column to the left
state: {
columnOrder: [
reorderMode ? "mrt-row-drag" : "mrt-row-select",
...mrtColumns.map((column) => column.accessorKey as string),
],
},
renderTopToolbarCustomActions: ({ table }) => (
<Button
size="small"
color="secondary"
variant={reorderMode ? "contained" : "outlined"}
aria-pressed={reorderMode}
onClick={() => {
// Drop any active sort so dragging works on the current visible order
table.resetSorting();
setReorderMode((prev) => !prev);
}}
>
{reorderMode ? "Done reordering" : "Reorder"}
</Button>
),
renderTopToolbarCustomActions: reorder.renderToggleButton,
mrtTheme: DiamondDSIntegrations.mrtTheme,
...DiamondDSIntegrations.mrtOptions({
onRowReorder: reorderMode ? moveRow : undefined,
}),
...DiamondDSIntegrations.mrtOptions({ fullWidth: true }),
...reorder.tableOptions,
});

return <MaterialReactTable table={table} />;
Expand Down Expand Up @@ -532,8 +509,8 @@ const ComponentsPage = () => {
</Typography>
<Typography variant="body2" color="text.secondary" gutterBottom>
Top toolbar, row selection, sorting and pagination. The{" "}
<code>Reorder</code> toggle swaps the checkboxes for drag handles and
turns sorting off while active.
<code>Reorder rows</code> toggle shows drag handles and a row menu,
and turns sorting off while active. Completed experiments are locked.
</Typography>
<AdvancedMrtExample />
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,50 +333,66 @@ enableHiding: true`}

<p>
Pass <strong>onRowReorder</strong> to{" "}
<strong>DiamondDSIntegrations.mrtOptions()</strong> while reorder mode is
active. It adds drag handles, a drop line between rows, and{" "}
<strong>Move up</strong> / <strong>Move down</strong> in the row menu. Sorting
is turned off while it is set.
<strong>DiamondDSIntegrations.mrtOptions()</strong>. It adds drag handles, a
drop line between rows, and a row menu with <strong>Move to top</strong>,{" "}
<strong>Move up</strong>, <strong>Move down</strong> and{" "}
<strong>Move to bottom</strong>. Moves are announced to screen readers. Sorting
and ranked search results are turned off while it is set.
</p>

<p>
<strong>onRowReorder</strong> receives positions in the source data, so it
stays correct across pages and filters.
stays correct across pages and filters. Set <strong>getRowId</strong>: it
keeps rows, and keyboard focus after a menu move, with the right record when
the data changes. With a typed lambda, give the type:{" "}
<strong>mrtOptions&lt;Task&gt;(...)</strong>. Pass{" "}
<strong>isRowMovable</strong> to lock rows: they get no handle or menu and
can't be dropped on, though they shift when rows move across them.
</p>

<pre className="ds-code-block">
{`const [reorderMode, setReorderMode] = useState(false);
<p>
For a reorder mode that users switch on and off, use{" "}
<strong>useRowReorder</strong>. The drag and menu columns show only while it
is on.
</p>

const moveRow = (fromIndex: number, toIndex: number) =>
setRows((prev) => {
const next = [...prev];
next.splice(toIndex, 0, next.splice(fromIndex, 1)[0]);
return next;
});
<pre className="ds-code-block">
{`const [rows, setRows] = useState(data);

const reorder = useRowReorder<Task>({
onReorder: (fromIndex, toIndex) =>
setRows((prev) => {
const next = [...prev];
next.splice(toIndex, 0, next.splice(fromIndex, 1)[0]);
return next;
}),
isRowMovable: (task) => task.status === "Queued",
});

const table = useMaterialReactTable({
columns,
data: rows,
enableRowSelection: !reorderMode,
getRowId: (row) => row.id,
renderTopToolbarCustomActions: reorder.renderToggleButton,
mrtTheme: DiamondDSIntegrations.mrtTheme,
...DiamondDSIntegrations.mrtOptions({
onRowReorder: reorderMode ? moveRow : undefined,
}),
...DiamondDSIntegrations.mrtOptions(),
...reorder.tableOptions,
});`}
</pre>

<p>
<strong>onRowReorder</strong> also sets the row action menu and{" "}
<strong>muiTableBodyCellProps</strong>. If the table has its own row actions,
combine them rather than replacing the reorder items:
<strong>tableOptions</strong> sets <strong>state.columnVisibility</strong>,
the row action menu and <strong>muiTableBodyCellProps</strong>. If the table
has its own, combine them rather than replacing the reorder ones. A table with
its own row actions should use{" "}
<strong>mrtOptions({"{ onRowReorder }"})</strong> and merge the menu items:
</p>

<pre className="ds-code-block">
{`const dsOptions = DiamondDSIntegrations.mrtOptions({ onRowReorder });

const table = useMaterialReactTable({
...dsOptions,
enableRowActions: true,
renderRowActionMenuItems: (props) => [
...myRowActions(props),
...(dsOptions.renderRowActionMenuItems?.(props) ?? []),
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export * from "./themes/BaseTheme";
export * from "./themes/DiamondTheme";
export * from "./themes/DiamondDSTheme";
export * from "./themes/DiamondDSIntegrations";
export * from "./themes/useRowReorder";
export * from "./themes/GenericTheme";
export * from "./themes/ThemeProvider";
export * from "./themes/ThemeManager";
Expand Down
Loading
Loading