-
Notifications
You must be signed in to change notification settings - Fork 104
feat(AnalyticalTable): introduce autoResize column feature (#3196) #5758
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
46f0bee
Feat(AnalyticalTable): introduce autoResize column feature (#3196)
Papa-Santo d5879a2
Refactored code into useAutoResize hook. Changed pattern to resemble …
Papa-Santo 3142f0f
autoResizable supports all element types and all table types
Papa-Santo c8e19dd
refactor: encapsulate logic in hook, support rtl, improve performance
Lukas742 e65c73b
Adapted last commit to support custom cells. Fixed bug on age column
Papa-Santo 23ca671
Merge branch 'main' into 3196--AutoResize
Lukas742 51e72cc
measure cell content `dir` agnostic, types, comments, cleanup
Lukas742 0ed976b
support tree tables
Lukas742 68bafe8
docs
Lukas742 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
4 changes: 2 additions & 2 deletions
4
packages/main/src/components/AnalyticalTable/defaults/Column/Cell.tsx
This file contains hidden or 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
71 changes: 71 additions & 0 deletions
71
packages/main/src/components/AnalyticalTable/hooks/useAutoResize.tsx
This file contains hidden or 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,71 @@ | ||
import { enrichEventWithDetails } from '@ui5/webcomponents-react-base'; | ||
import { DEFAULT_COLUMN_WIDTH } from '../defaults/Column/index.js'; | ||
import type { ReactTableHooks } from '../types/index.js'; | ||
import { CELL_PADDING_PX } from './useDynamicColumnWidths.js'; | ||
|
||
function setResizerProps(...params) { | ||
const [props, { instance, header }] = params; | ||
const { dispatch, virtualRowsRange, rows, webComponentsReactProperties, state } = instance; | ||
const { uniqueId, onAutoResize } = webComponentsReactProperties; | ||
const { autoResizable, id: accessor } = header; | ||
|
||
if (!document || !autoResizable || !rows.length || !virtualRowsRange) { | ||
Lukas742 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return props; | ||
} | ||
|
||
return { | ||
...props, | ||
onDoubleClick: (e) => { | ||
Lukas742 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let largest = getMeasureMax(accessor, uniqueId, virtualRowsRange, state.isRtl); | ||
largest = largest > DEFAULT_COLUMN_WIDTH ? largest : DEFAULT_COLUMN_WIDTH; | ||
onAutoResize( | ||
enrichEventWithDetails(e, { | ||
accessor, | ||
width: largest | ||
}) | ||
); | ||
if (e.defaultPrevented) { | ||
return; | ||
} | ||
dispatch({ | ||
type: 'AUTO_RESIZE', | ||
payload: { [accessor]: largest } | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
function getMeasureMax(accessor, uniqueId, virtualRowsRange, isRtl) { | ||
let maxWidth = 0; | ||
const firstRowQuery = `[data-component-name="AnalyticalTableBodyScrollableContainer"][data-react-id="${uniqueId}"] [data-virtual-row-index="${virtualRowsRange.startIndex}"]`; | ||
const rowsDOM = document.querySelectorAll(`${firstRowQuery}, ${firstRowQuery} ~ [data-virtual-row-index]`); | ||
Lukas742 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const start = virtualRowsRange.startIndex; | ||
const end = virtualRowsRange.endIndex; | ||
|
||
for (let i = 0; i <= end - start; i++) { | ||
Lukas742 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const cellTextElement: HTMLSpanElement = rowsDOM[i]?.querySelector(`[data-id="${accessor}"]`); | ||
let currWidth = 0; | ||
if (!cellTextElement) { | ||
continue; | ||
} | ||
const computedStyle = getComputedStyle(cellTextElement); | ||
currWidth += cellTextElement.scrollWidth; | ||
// cannot use `offsetLeft` for RTL direction | ||
currWidth += !isRtl | ||
? cellTextElement.offsetLeft | ||
: cellTextElement.parentElement.getBoundingClientRect().right - cellTextElement.getBoundingClientRect().right; | ||
Lukas742 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
currWidth += parseFloat(computedStyle.marginInlineEnd); | ||
currWidth += parseFloat(computedStyle.borderInlineEndWidth); | ||
Lukas742 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
currWidth += CELL_PADDING_PX; | ||
|
||
maxWidth = maxWidth > currWidth ? maxWidth : currWidth; | ||
} | ||
|
||
return Math.ceil(maxWidth); | ||
} | ||
|
||
export const useAutoResize = (hooks: ReactTableHooks) => { | ||
hooks.getResizerProps.push(setResizerProps); | ||
}; | ||
|
||
useAutoResize.pluginName = 'useAutoResize'; |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.