-
Notifications
You must be signed in to change notification settings - Fork 6
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
fix(ui-tablemode): adjust the size of the column with the initial data #2320
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ import { | |
} from "@glideapps/glide-data-grid"; | ||
import type { DeepPartial } from "react-hook-form"; | ||
import { useCallback, useEffect, useMemo, useState } from "react"; | ||
import DataGrid, { type DataGridProps } from "./DataGrid"; | ||
import DataGrid, { type DataGridProps, type RowMarkers } from "./DataGrid"; | ||
import { Box, Divider, IconButton, setRef, Tooltip, type SxProps, type Theme } from "@mui/material"; | ||
import useUndo, { type Actions } from "use-undo"; | ||
import UndoIcon from "@mui/icons-material/Undo"; | ||
|
@@ -36,6 +36,8 @@ import useEnqueueErrorSnackbar from "@/hooks/useEnqueueErrorSnackbar"; | |
import useFormCloseProtection from "@/hooks/useCloseFormSecurity"; | ||
import { useUpdateEffect } from "react-use"; | ||
import { toError } from "@/utils/fnUtils"; | ||
import { measureTextWidth } from "@/utils/domUtils"; | ||
import useSafeMemo from "@/hooks/useSafeMemo"; | ||
|
||
type Data = Record<string, Record<string, string | boolean | number>>; | ||
|
||
|
@@ -72,7 +74,7 @@ function DataGridForm<TData extends Data>({ | |
columns, | ||
allowedFillDirections = "vertical", | ||
enableColumnResize, | ||
rowMarkers, | ||
rowMarkers: rowMarkersFromProps, | ||
onSubmit, | ||
onSubmitSuccessful, | ||
onStateChange, | ||
|
@@ -91,8 +93,6 @@ function DataGridForm<TData extends Data>({ | |
// Deep comparison fix the issue but with big data it can be slow. | ||
const isDirty = savedData !== data; | ||
|
||
const rowNames = Object.keys(data); | ||
|
||
const formState = useMemo<DataGridFormState>( | ||
() => ({ | ||
isDirty, | ||
|
@@ -107,16 +107,49 @@ function DataGridForm<TData extends Data>({ | |
|
||
useEffect(() => setRef(apiRef, { data, setData, formState }), [apiRef, data, setData, formState]); | ||
|
||
// Rows cannot be added or removed, so no dependencies are needed | ||
const rowNames = useSafeMemo(() => Object.keys(defaultData), []); | ||
|
||
const columnsWithAdjustedSize = useMemo( | ||
() => | ||
columns.map((column) => ({ | ||
...column, | ||
width: | ||
"width" in column | ||
? column.width | ||
: Math.max( | ||
measureTextWidth(column.title), | ||
...rowNames.map((rowName) => | ||
measureTextWidth(defaultData[rowName][column.id].toString()), | ||
), | ||
), | ||
})), | ||
[columns, defaultData, rowNames], | ||
); | ||
|
||
const columnIds = useMemo(() => columns.map((column) => column.id), [columns]); | ||
|
||
const rowMarkers = useMemo<RowMarkers>( | ||
() => | ||
rowMarkersFromProps || { | ||
kind: "clickable-string", | ||
getTitle: (index) => rowNames[index], | ||
width: Math.max(...rowNames.map((name) => measureTextWidth(name))), | ||
}, | ||
[rowMarkersFromProps, rowNames], | ||
); | ||
Comment on lines
+132
to
+140
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. const rowMarkers = useMemo<RowMarkers>(() => {
if (rowMarkersFromProps) {
return rowMarkersFromProps;
}
const measureText = createTextMeasurer();
const maxWidth = Math.max(...rowNames.map(measureText));
return {
kind: "clickable-string",
getTitle: (index) => rowNames[index],
width: maxWidth,
};
}, [rowMarkersFromProps, rowNames]); |
||
|
||
//////////////////////////////////////////////////////////////// | ||
// Utils | ||
//////////////////////////////////////////////////////////////// | ||
|
||
const getRowAndColumnNames = (location: Item) => { | ||
const [colIndex, rowIndex] = location; | ||
const columnIds = columns.map((column) => column.id); | ||
|
||
return [rowNames[rowIndex], columnIds[colIndex]]; | ||
}; | ||
const getRowAndColumnNames = useCallback( | ||
(location: Item) => { | ||
const [colIndex, rowIndex] = location; | ||
return [rowNames[rowIndex], columnIds[colIndex]]; | ||
}, | ||
[rowNames, columnIds], | ||
); | ||
|
||
const getDirtyValues = () => { | ||
return rowNames.reduce((acc, rowName) => { | ||
|
@@ -190,7 +223,7 @@ function DataGridForm<TData extends Data>({ | |
readonly: true, | ||
}; | ||
}, | ||
[data, columns], | ||
[data, getRowAndColumnNames], | ||
); | ||
|
||
//////////////////////////////////////////////////////////////// | ||
|
@@ -261,15 +294,10 @@ function DataGridForm<TData extends Data>({ | |
> | ||
<DataGrid | ||
getCellContent={getCellContent} | ||
columns={columns} | ||
columns={columnsWithAdjustedSize} | ||
rows={rowNames.length} | ||
onCellsEdited={handleCellsEdited} | ||
rowMarkers={ | ||
rowMarkers || { | ||
kind: "clickable-string", | ||
getTitle: (index) => rowNames[index], | ||
} | ||
} | ||
rowMarkers={rowMarkers} | ||
fillHandle | ||
allowedFillDirections={allowedFillDirections} | ||
enableColumnResize={enableColumnResize} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Copyright (c) 2025, RTE (https://www.rte-france.com) | ||
* | ||
* See AUTHORS.txt | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
* | ||
* This file is part of the Antares project. | ||
*/ | ||
|
||
const CANVAS_CONTEXT = document.createElement("canvas").getContext("2d"); | ||
const BODY_FONT = getCssFont(); | ||
|
||
/** | ||
* Gets the computed style of the given element. | ||
* | ||
* @see https://stackoverflow.com/a/21015393 | ||
* | ||
* @param element - The element to get the style from. | ||
* @param prop - The property to get the value of. | ||
* @returns The computed style of the given element. | ||
*/ | ||
export function getCssStyle(element: HTMLElement, prop: string) { | ||
return window.getComputedStyle(element, null).getPropertyValue(prop); | ||
} | ||
|
||
/** | ||
* Gets the font of the given element, or the `body` element if none is provided. | ||
* The returned value follows the CSS `font` shorthand property format. | ||
* | ||
* @see https://stackoverflow.com/a/21015393 | ||
* | ||
* @param element - The element to get the font from. | ||
* @returns The font of the given element. | ||
*/ | ||
export function getCssFont(element = document.body) { | ||
const fontWeight = getCssStyle(element, "font-weight") || "normal"; | ||
const fontSize = getCssStyle(element, "font-size") || "16px"; | ||
const fontFamily = getCssStyle(element, "font-family") || "Arial"; | ||
|
||
return `${fontWeight} ${fontSize} ${fontFamily}`; | ||
} | ||
|
||
/** | ||
* Uses `canvas.measureText` to compute and return the width of the specified text, | ||
* using the specified canvas font if defined (use font from the "body" otherwise). | ||
* | ||
* @see https://stackoverflow.com/a/21015393 | ||
* | ||
* @param text - The text to be rendered. | ||
* @param [font] - The CSS font that text is to be rendered with (e.g. "bold 14px Arial"). | ||
* If not provided, the font of the `body` element is used. | ||
* @returns The width of the text in pixels. | ||
*/ | ||
export function measureTextWidth(text: string, font?: string) { | ||
if (CANVAS_CONTEXT) { | ||
CANVAS_CONTEXT.font = font || BODY_FONT; | ||
return CANVAS_CONTEXT.measureText(text).width; | ||
} | ||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
here a canvas is re-created at each iteration in the Math.max(), it's expensive
see comment about
domUtils.ts
file for thecreateTextMeasurer()
util using the factory function pattern to avoid re-creation of canvas