Skip to content

Commit

Permalink
Merge pull request #1368 from nithinrdy/nithin/accessible-resizing
Browse files Browse the repository at this point in the history
Add an accessible resizing option
  • Loading branch information
shamsmosowi authored Sep 8, 2023
2 parents 8f957ec + 7ca6fe3 commit 0099bd1
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/atoms/tableScope/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
BulkWriteFunction,
} from "@src/types/table";
import { updateRowData } from "@src/utils/table";
import { Table } from "@tanstack/react-table";

/** Root atom from which others are derived */
export const tableIdAtom = atom("");
Expand Down Expand Up @@ -47,6 +48,8 @@ export const tableColumnsOrderedAtom = atom<ColumnConfig[]>((get) => {
["desc", "asc"]
);
});
/** Store the table */
export const reactTableAtom = atom<Table<TableRow> | null>(null);
/** Reducer function to convert from array of columns to columns object */
export const tableColumnsReducer = (
a: Record<string, ColumnConfig>,
Expand Down
2 changes: 1 addition & 1 deletion src/atoms/tableScope/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const columnMenuAtom = atom<{
* ```
*/
export const columnModalAtom = atomWithHash<{
type: "new" | "name" | "type" | "config";
type: "new" | "name" | "type" | "config" | "setColumnWidth";
columnKey?: string;
index?: number;
} | null>("columnModal", null, { replaceState: true });
Expand Down
11 changes: 11 additions & 0 deletions src/components/ColumnMenu/ColumnMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from "@src/assets/icons";
import ArrowDownwardIcon from "@mui/icons-material/ArrowDownward";
import ArrowUpwardIcon from "@mui/icons-material/ArrowUpward";
import StraightenIcon from "@mui/icons-material/Straighten";
import EditIcon from "@mui/icons-material/EditOutlined";
import SettingsIcon from "@mui/icons-material/SettingsOutlined";
import EvalIcon from "@mui/icons-material/PlayCircleOutline";
Expand Down Expand Up @@ -265,6 +266,16 @@ export default function ColumnMenu({
: column.type
),
},
{
key: "setColumnWidth",
label: "Set Column Width",
icon: <StraightenIcon />,
onClick: () => {
openColumnModal({ type: "setColumnWidth", columnKey: column.key });
handleClose();
},
disabled: column.resizable === false,
},
];

const configActions: IMenuContentsProps["menuItems"] = [
Expand Down
4 changes: 4 additions & 0 deletions src/components/ColumnModals/ColumnModals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import NewColumnModal from "./NewColumnModal";
import NameChangeModal from "./NameChangeModal";
import TypeChangeModal from "./TypeChangeModal";
import ColumnConfigModal from "./ColumnConfigModal";
import SetColumnWidthModal from "./SetColumnWidthModal";

import {
tableScope,
Expand Down Expand Up @@ -40,5 +41,8 @@ export default function ColumnModals() {
if (columnModal.type === "config")
return <ColumnConfigModal onClose={onClose} column={column} />;

if (columnModal.type === "setColumnWidth")
return <SetColumnWidthModal onClose={onClose} column={column} />;

return null;
}
70 changes: 70 additions & 0 deletions src/components/ColumnModals/SetColumnWidthModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { useEffect, useState } from "react";
import { IColumnModalProps } from ".";
import { reactTableAtom } from "@src/atoms/tableScope";
import { tableScope } from "@src/atoms/tableScope";
import { useAtom } from "jotai";

import { TextField } from "@mui/material";
import Modal from "@src/components/Modal";

export default function SetColumnWidthModal({
onClose,
column,
}: IColumnModalProps) {
const [reactTable] = useAtom(reactTableAtom, tableScope);
const [newWidth, setWidth] = useState<number>(0);

useEffect(() => {
// Set the initial width to the current column width once the table is fetched.
setWidth(reactTable?.getAllColumns()[column.index].getSize() || 0);
}, [reactTable, column]);

const handleUpdate = () => {
reactTable?.setColumnSizing((old) => {
const newSizing = { ...old };
// Set the new width for the column.
newSizing[column.fieldName] = newWidth;
return newSizing;
});
onClose();
};

return (
<Modal
onClose={onClose}
title="Set Column Width"
maxWidth="xs"
children={
<form
id="column-width-modal"
onSubmit={(e) => {
e.preventDefault();
handleUpdate();
}}
>
<TextField
value={newWidth}
autoFocus
variant="filled"
id="name"
label="Width"
type="number"
fullWidth
onChange={(e) => setWidth(Number(e.target.value))}
/>
</form>
}
actions={{
primary: {
children: "Update",
type: "submit",
form: "column-width-modal",
},
secondary: {
onClick: onClose,
children: "Cancel",
},
}}
/>
);
}
6 changes: 6 additions & 0 deletions src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import EmptyState from "@src/components/EmptyState";
import {
tableScope,
tableSchemaAtom,
reactTableAtom,
tableColumnsOrderedAtom,
tableRowsAtom,
tableNextPageAtom,
Expand Down Expand Up @@ -118,6 +119,7 @@ export default function Table({
const [tableRows] = useAtom(tableRowsAtom, tableScope);
const [tableNextPage] = useAtom(tableNextPageAtom, tableScope);
const [tablePage, setTablePage] = useAtom(tablePageAtom, tableScope);
const setReactTable = useSetAtom(reactTableAtom, tableScope);

const updateColumn = useSetAtom(updateColumnAtom, tableScope);

Expand Down Expand Up @@ -264,6 +266,10 @@ export default function Table({
state: { ...prev.state, columnVisibility, columnPinning, columnSizing },
onColumnSizingChange: setColumnSizing,
}));
// Update the reactTable atom when table state changes.
useMemo(() => {
setReactTable(table);
}, [table, setReactTable]);
// Get rows and columns for virtualization
const { rows } = table.getRowModel();
const leafColumns = table.getVisibleLeafColumns();
Expand Down

1 comment on commit 0099bd1

@vercel
Copy link

@vercel vercel bot commented on 0099bd1 Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

rowy-typedoc – ./

rowy-typedoc-git-develop-rowy.vercel.app
rowy-typedoc.vercel.app
rowy-typedoc-rowy.vercel.app

Please sign in to comment.