-
Notifications
You must be signed in to change notification settings - Fork 17
feat: make tables resizeable #823
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,18 @@ | ||
| .date-range { | ||
| &__input { | ||
| min-width: 190px; | ||
| height: 28px; | ||
| padding: 5px 8px; | ||
|
|
||
| color: var(--g-color-text-primary); | ||
| border: 1px solid var(--g-color-line-generic); | ||
| border-radius: var(--g-border-radius-m); | ||
| outline: none; | ||
| background: transparent; | ||
| } | ||
|
|
||
| &__input:focus, | ||
| &__input:focus-visible { | ||
| border: 1px solid var(--g-color-line-generic-hover); | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -1,10 +1,4 @@ | ||
| .ydb-node-host-wrapper { | ||
| &__host-wrapper { | ||
| display: flex; | ||
|
|
||
| width: 330px; | ||
| } | ||
|
|
||
| &__host { | ||
| overflow: hidden; | ||
| } | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| .ydb-query-result-table { | ||
| &__cell { | ||
| width: 100%; | ||
| @include cell-container; | ||
| } | ||
|
|
||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| .ydb-resizeable-data-table { | ||
| display: flex; | ||
|
|
||
| width: max-content; | ||
|
|
||
| // padding for easier resize of the last column | ||
| padding-right: 20px; | ||
| } |
43 changes: 43 additions & 0 deletions
43
src/components/ResizeableDataTable/ResizeableDataTable.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,43 @@ | ||
| import type {DataTableProps, Settings} from '@gravity-ui/react-data-table'; | ||
| import DataTable, {updateColumnsWidth} from '@gravity-ui/react-data-table'; | ||
|
|
||
| import {cn} from '../../utils/cn'; | ||
| import {useTableResize} from '../../utils/hooks/useTableResize'; | ||
|
|
||
| import './ResizeableDataTable.scss'; | ||
|
|
||
| const b = cn('ydb-resizeable-data-table'); | ||
|
|
||
| export interface ResizeableDataTableProps<T> extends Omit<DataTableProps<T>, 'theme' | 'onResize'> { | ||
| columnsWidthLSKey?: string; | ||
| wrapperClassName?: string; | ||
| } | ||
|
|
||
| export function ResizeableDataTable<T>({ | ||
| columnsWidthLSKey, | ||
| columns, | ||
| settings, | ||
| wrapperClassName, | ||
| ...props | ||
| }: ResizeableDataTableProps<T>) { | ||
| const [tableColumnsWidth, setTableColumnsWidth] = useTableResize(columnsWidthLSKey); | ||
|
|
||
| const updatedColumns = updateColumnsWidth(columns, tableColumnsWidth); | ||
|
|
||
| const newSettings: Settings = { | ||
| ...settings, | ||
| defaultResizeable: true, | ||
| }; | ||
|
|
||
| return ( | ||
| <div className={b(null, wrapperClassName)}> | ||
| <DataTable | ||
| theme="yandex-cloud" | ||
| columns={updatedColumns} | ||
| onResize={setTableColumnsWidth} | ||
| settings={newSettings} | ||
| {...props} | ||
| /> | ||
| </div> | ||
| ); | ||
| } |
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,106 @@ | ||
| import React from 'react'; | ||
|
|
||
| import {b} from './shared'; | ||
| import {calculateColumnWidth, rafThrottle} from './utils'; | ||
|
|
||
| interface ResizeHandlerProps { | ||
| maxWidth?: number; | ||
| minWidth?: number; | ||
| getCurrentColumnWidth: () => number | undefined; | ||
| onResize?: (width: number) => void; | ||
| } | ||
|
|
||
| export function ResizeHandler({ | ||
|
Member
Author
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. There and in other virtual table diffs some parts are copied from |
||
| minWidth, | ||
| maxWidth, | ||
| getCurrentColumnWidth, | ||
| onResize, | ||
| }: ResizeHandlerProps) { | ||
| const elementRef = React.useRef<HTMLElement>(null); | ||
|
|
||
| const [resizing, setResizing] = React.useState(false); | ||
|
|
||
| React.useEffect(() => { | ||
| const element = elementRef.current; | ||
|
|
||
| if (!element) { | ||
| return undefined; | ||
| } | ||
|
|
||
| let mouseXPosition: number | undefined; | ||
| let initialColumnWidth: number | undefined; | ||
| let currentColumnWidth: number | undefined; | ||
|
|
||
| const onMouseMove = rafThrottle((e: MouseEvent) => { | ||
| restrictMouseEvent(e); | ||
|
|
||
| if (typeof mouseXPosition !== 'number' || typeof initialColumnWidth !== 'number') { | ||
| return; | ||
| } | ||
|
|
||
| const xDiff = e.clientX - mouseXPosition; | ||
|
|
||
| const newWidth = calculateColumnWidth(initialColumnWidth + xDiff, minWidth, maxWidth); | ||
|
|
||
| if (newWidth === currentColumnWidth) { | ||
| return; | ||
| } | ||
|
|
||
| currentColumnWidth = newWidth; | ||
|
|
||
| onResize?.(currentColumnWidth); | ||
| }); | ||
|
|
||
| const onMouseUp = (e: MouseEvent) => { | ||
| restrictMouseEvent(e); | ||
|
|
||
| if (currentColumnWidth !== undefined) { | ||
| onResize?.(currentColumnWidth); | ||
| } | ||
|
|
||
| setResizing(false); | ||
| mouseXPosition = undefined; | ||
|
|
||
| document.removeEventListener('mousemove', onMouseMove); | ||
| document.removeEventListener('mouseup', onMouseUp); | ||
| }; | ||
|
|
||
| const onMouseDown = (e: MouseEvent) => { | ||
| initialColumnWidth = getCurrentColumnWidth(); | ||
|
|
||
| restrictMouseEvent(e); | ||
|
|
||
| mouseXPosition = e.clientX; | ||
|
|
||
| setResizing(true); | ||
|
|
||
| document.addEventListener('mousemove', onMouseMove); | ||
| document.addEventListener('mouseup', onMouseUp); | ||
| }; | ||
|
|
||
| element.addEventListener('mousedown', onMouseDown); | ||
|
|
||
| return () => { | ||
| element.removeEventListener('mousedown', onMouseDown); | ||
| document.removeEventListener('mousemove', onMouseMove); | ||
| document.removeEventListener('mouseup', onMouseUp); | ||
| }; | ||
| }, [onResize, minWidth, maxWidth, getCurrentColumnWidth]); | ||
|
|
||
| return ( | ||
| <span | ||
| ref={elementRef} | ||
| className={b('resize-handler', {resizing})} | ||
| // Prevent sort trigger on resize | ||
| onClick={(e) => restrictMouseEvent(e)} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| // Prevent sort trigger and text selection on resize | ||
| function restrictMouseEvent< | ||
| T extends {preventDefault: VoidFunction; stopPropagation: VoidFunction}, | ||
| >(e: T) { | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| } | ||
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,31 @@ | ||
| import type {ColumnWidthByName} from '@gravity-ui/react-data-table'; | ||
|
|
||
| import {useTableResize} from '../../utils/hooks/useTableResize'; | ||
|
|
||
| import type {VirtualTableProps} from './VirtualTable'; | ||
| import {VirtualTable} from './VirtualTable'; | ||
| import type {Column} from './types'; | ||
|
|
||
| function updateColumnsWidth<T>(columns: Column<T>[], columnsWidthSetup: ColumnWidthByName) { | ||
| return columns.map((column) => { | ||
| return {...column, width: columnsWidthSetup[column.name] ?? column.width}; | ||
| }); | ||
| } | ||
|
|
||
| interface ResizeableVirtualTableProps<T> extends Omit<VirtualTableProps<T>, 'onColumnsResize'> { | ||
| columnsWidthLSKey: string; | ||
| } | ||
|
|
||
| export function ResizeableVirtualTable<T>({ | ||
| columnsWidthLSKey, | ||
| columns, | ||
| ...props | ||
| }: ResizeableVirtualTableProps<T>) { | ||
| const [tableColumnsWidth, setTableColumnsWidth] = useTableResize(columnsWidthLSKey); | ||
|
|
||
| const updatedColumns = updateColumnsWidth(columns, tableColumnsWidth); | ||
|
|
||
| return ( | ||
| <VirtualTable columns={updatedColumns} onColumnsResize={setTableColumnsWidth} {...props} /> | ||
| ); | ||
| } |
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.
These changes fix light outline, which doesn't correspond to dark mode and makes control height more, than others
Before:

After:
