-
Notifications
You must be signed in to change notification settings - Fork 105
fix(AnalyticalTable): improve scaleWidthMode
"Grow" and "Smart"
#7752
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
Conversation
Size Change: +750 B (+0.17%) Total Size: 447 kB
ℹ️ View Unchanged
|
Pull Request Test Coverage Report for Build 17643841959Details
💛 - Coveralls |
Without the calculation, horizontal virtualization might not work as expected
Pull Request Test Coverage Report for Build 18585344100Details
💛 - Coveralls |
if (scaleWidthMode === AnalyticalTableScaleWidthMode.Smart) { | ||
return calculateSmartColumns(columns, instance, hiddenColumns); | ||
return calculateSmartAndGrowColumns(columns, instance, hiddenColumns); | ||
} | ||
|
||
if (scaleWidthMode === AnalyticalTableScaleWidthMode.Grow) { | ||
return calculateSmartAndGrowColumns(columns, instance, hiddenColumns, true); | ||
} |
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.
Following suggestion:
if (scaleWidthMode === AnalyticalTableScaleWidthMode.Smart) {
const calculatedColumns = calculateSmartAndGrowColumns(visibleColumns, instance, hiddenColumns)
// Map calculated widths back to all columns (including hidden ones)
return columns.map((column) => {
const isHidden = hiddenColumns.includes(column.id ?? column.accessor)
if (isHidden) {
// Return hidden columns unchanged
return column
}
// Find the calculated column
const calculatedColumn = calculatedColumns.find(
(col) => (col.id ?? col.accessor) === (column.id ?? column.accessor)
)
return calculatedColumn || column
})
}
if (scaleWidthMode === AnalyticalTableScaleWidthMode.Grow) {
const calculatedColumns = calculateSmartAndGrowColumns(visibleColumns, instance, hiddenColumns, true)
// Map calculated widths back to all columns (including hidden ones)
return columns.map((column) => {
const isHidden = hiddenColumns.includes(column.id ?? column.accessor)
if (isHidden) {
// Return hidden columns unchanged
return column
}
// Find the calculated column
const calculatedColumn = calculatedColumns.find(
(col) => (col.id ?? col.accessor) === (column.id ?? column.accessor)
)
return calculatedColumn || column
})
}
Explanation: When using the suggested fix in my app, I get flickering hidden columns + hidden columns are shown upon expanding a subrow (tree table).
My best guess would be that when you return fewer columns than you received, react-table thinks columns have been added/removed, triggering a recalculation with different visibleColumns.length and resetting hidden columns.
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.
Hi @Na1k
sorry you had to wait so long for my review, I was tied up with other tasks.
With the new .map()
+ .find()
approach, every visible column is checked against all calculated columns. If we have around 100 columns, this could mean up to 10,000 comparisons, which might make the table noticeably slower, depending on the features it implements and custom cells it renders.
I'm also still not sure why this change is required. My assumption is that it only worked consistently for you before because of a side-effect. Previously, the column calculation was triggered too often, and each calculation caused a reflow. Now, the times the costly calculation is triggered have been reduced to a minimum and also the reflow problem has been prevented, which is probably why the behavior is different for you.
When initially setting the internal hiddenColumns
state via useEffect
, it can cause a race condition because the tableInstance
Ref might not have been updated before the effect runs. For setting the initial state, we recommend using the reactTableOptions.initialState
property. You can read more about it here. Here you can find the different approaches for setting the initial table state, including an example of the recommended way. I tested it with the scaleWidthMode
changes included and only in case of using useEffect
to set the initial state, columns sometimes weren't hidden. Could you please check if the issue still persists when using the initialState
approach?
If you need to change the state after the initial load, you can use the table instance, but it’s recommended to either use useControlledState in reactTableOptions
or register a custom stateReducer
via a plugin hook. If you want, I can draft a small example showing how an implementation could look.
Since this change is now required by one of our stakeholders, we will need to merge and publish it soon. Starting with v2.15.2
, the change will be included. If you still encounter issues after updating, especially when setting the hidden state via initialState
, please let us know.
Hi @Na1k, Thanks for the suggestion! I’ll take a look as soon as possible. Currently I’m busy preparing the next minor release, but we’ll put this fix on hold for now until I had the time to check your suggestion. |
…5/webcomponents-react into fix/at-scale-width-smart-grow
🎉 This PR is included in version v2.15.2 🎉 The release is available on v2.15.2 Your semantic-release bot 📦🚀 |
Fixes #7790