-
Notifications
You must be signed in to change notification settings - Fork 7
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
Column width: localstorage fix + sortable column width #289
base: main
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This solution is probably not the most react-friendly way to do things. I am also not quite sure, why we need to do it that way. Can we implement the resizing like they do it in the example? https://codesandbox.io/p/sandbox/ant-design-resizable-table-columns-qtbr67?file=%2Fsrc%2FApp.tsx |
This comment has been minimized.
This comment has been minimized.
Rewrote resizing of columns as requestedSummary
Screencast.from.27.06.2024.13.00.29.webmDetails
|
setTimeout(() => { | ||
/* Hydrating */ | ||
/* Replace 'auto' width with actual px values */ | ||
const resizeableElements = document.querySelectorAll('.react-resizable'); | ||
const widthsOfResizeableElements = Array.from(resizeableElements).map( | ||
(element: any) => element.getBoundingClientRect().width, | ||
); | ||
/* Get the widths */ | ||
const widths = columns.map((column: any, index: number) => { | ||
/* Check if not resizeable */ | ||
if (notResizeabel.includes(column.key)) return column.width || minWidth; | ||
|
||
return widthsOfResizeableElements.shift() || minWidth; | ||
}); | ||
|
||
/* Replace width */ | ||
newColumns = newColumns.map((column: any, index: number) => { | ||
return { | ||
...column, | ||
width: widths[index], | ||
}; | ||
}); | ||
|
||
/* Update UI */ | ||
setColumnWidths((old) => ({ ...old, [resizingColumn.current]: newWidth })); | ||
setResizeableColumns(newColumns); | ||
}, 1_000); |
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.
This is a workaround to keep antdesigns 'auto' width. It replaces the auto values with the actual pixel values after everything was rendered.
This comment has been minimized.
This comment has been minimized.
{column.title as ReactNode} | ||
const widths = getWidthsOfInnerElements(containerRef.current); | ||
|
||
if (widths.some((w) => w > width)) { |
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.
Maybe this should be adapted in the future, as the total width can be determined by two direct children, that are displayed next to each other (such as the process / folder icon and the title)
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.
yeah, would be good
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.
Changed it to also check for elements that are displayed inline (i.e. use the sum of their width)
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
// components: { | ||
// body: { | ||
// row: DraggableRow, | ||
// }, | ||
// }, |
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.
Should this now be passed as tableProps
?
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.
Just forgot to uncomment it again -> removed comments
setTimeout(() => { | ||
/* Hydrating */ | ||
/* Replace 'auto' width with actual px values */ | ||
const resizeableElements = document.querySelectorAll('.react-resizable'); | ||
const widthsOfResizeableElements = Array.from(resizeableElements).map( | ||
(element: any) => element.getBoundingClientRect().width, | ||
); | ||
/* Get the widths */ | ||
const widths = columns.map((column: any, index: number) => { | ||
/* Check if not resizeable */ | ||
if (notResizeabel.includes(column.key)) return column.width || minWidth; | ||
|
||
return widthsOfResizeableElements.shift() || minWidth; | ||
}); | ||
|
||
/* Replace width */ | ||
newColumns = newColumns.map((column: any, index: number) => { | ||
return { | ||
...column, | ||
width: widths[index], | ||
}; | ||
}); | ||
|
||
const handleMouseMove = useCallback((event: MouseEvent) => { | ||
if (indicatorRef.current) { | ||
indicatorRef.current.style.left = `${event.clientX}px`; | ||
setResizeableColumns(newColumns); | ||
}, 1_000); |
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.
Timeouts should always have a cleanup returned in an effect. Otherwise this could cause errors when the component unmounts before the delay. You can still use a ref as a flag to mark the update, if you are unable to calculate that from the given values instead.
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.
Added timeout clear as cleanup
export const ResizableTitle = (props: any) => { | ||
const { onResize, width, ...restProps } = props; |
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.
Why no destructuring directly in the function declaration? And is it possible to narrow the type down?
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.
Made suggested changes
|
||
type TruncateType = { | ||
width: number | string; | ||
innerRender: () => JSX.Element | string; |
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.
Can it be () => ReactNode
?
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.
✅
{column.title as ReactNode} | ||
const widths = getWidthsOfInnerElements(containerRef.current); | ||
|
||
if (widths.some((w) => w > width)) { |
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.
yeah, would be good
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
✅ Successfully created Preview Deployment. |
Summary