Skip to content

fit table columns to content #31

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
5 changes: 3 additions & 2 deletions web/src/CalendarForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const Form = styled.form`

const defaultColumnProperties = {
editable: true,
resizable: true,
};

const columnTooltipRenderer = (value) => {
Expand Down Expand Up @@ -205,7 +206,7 @@ export default function CalendarForm() {
tooltip={tableTooltip}
startingRows={subjectsRows}
defaultRow={defaultSubjectsRow}
cols={subjectsColumns}
columns={subjectsColumns}
onChange={setSubjects}
/>

Expand All @@ -214,7 +215,7 @@ export default function CalendarForm() {
tooltip={tableTooltip}
startingRows={schedulesRows}
defaultRow={defaultSchedulesRow}
cols={schedulesColumns}
columns={schedulesColumns}
onChange={setSchedules}
/>

Expand Down
73 changes: 72 additions & 1 deletion web/src/InputTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import { Editors, Menu } from "react-data-grid-addons";
import DataContextMenu, { deleteRow, insertRow } from './DataContextMenu';
const { ContextMenuTrigger } = Menu;

export default function InputTable({ title, tooltip, startingRows, defaultRow, cols, onChange }) {
export default function InputTable({ title, tooltip, startingRows, defaultRow, columns, onChange }) {
const [values, setValues] = useState(startingRows);
const [canvas, setCanvas] = useState(null);
const [context, setContext] = useState(null);

const onTableUpdated = ({ fromRow, toRow, updated }) => {
const r = values.slice();
Expand All @@ -22,10 +24,79 @@ export default function InputTable({ title, tooltip, startingRows, defaultRow, c
}
setValues(r);

let newCols = formatColumns(columns, r);
console.log(newCols);
setCols(newCols);

// Callback
onChange(r);
};

const formatColumns = (cols, values) => {
// const gridWidth = parseInt(document.querySelector("#root").clientWidth, 10); //selector for grid
let combinedColumnWidth = 0;

for (let i = 0; i < cols.length; i++) {
cols[i].width = getTextWidth(cols, values, i);
combinedColumnWidth += cols[i].width;
}

// if (combinedColumnWidth < gridWidth) {
// data.columns = distributeRemainingSpace(
// combinedColumnWidth,
// data.columns,
// gridWidth
// );
// }
return cols;
}

const getTextWidth = (cols, values, i) => {
const rowValues = [];
const reducer = (a, b) => (a.length > b.length ? a : b);
const cellPadding = 16;
const arrowWidth = 18;
let longestCellData,
longestCellDataWidth,
longestColName,
longestColNameWidth,
longestString;

for (let row of values) {
rowValues.push(row[cols[i].key]);
}

longestCellData = rowValues.reduce(reducer);
longestColName = cols[i].name;
longestCellDataWidth = Math.ceil(
getCanvas().measureText(longestCellData).width
);
longestColNameWidth =
Math.ceil(getCanvas("bold ").measureText(longestColName).width) +
arrowWidth;

longestString = Math.max(longestCellDataWidth, longestColNameWidth);

return longestString + cellPadding;
};

const getCanvas = (fontWeight = "") => {
let ctx = context;

if (!canvas) {
let c = document.createElement("canvas");
setCanvas(c);
ctx = c.getContext("2d")
setContext(ctx);
}

ctx.font = `${fontWeight}24px`;

return ctx;
};

const [cols, setCols] = useState(formatColumns(columns, values));

return (
<div className="col-12">
<label htmlFor={title.toLowerCase()} className="form-label">{title}</label>
Expand Down