-
Notifications
You must be signed in to change notification settings - Fork 61
[WC-1784] DG: Column drag and drop enhancements #1989
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
Open
yordan-st
wants to merge
12
commits into
iobuhov_next
Choose a base branch
from
feat/WC-1784_DG-coldrag-enhance
base: iobuhov_next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
54f32d0
refactor: migrate event controllers from DI to React Context for simp…
iobuhov b99c478
refactor: migrate event handlers to container
iobuhov b754be7
refactor: extract drag & drop state and logic to mobx
yordan-st 54fd9ee
refactor: update components to use new state management
yordan-st 262d891
refactor: remove obsolete tests and snapshots, create for new component
yordan-st b4a467a
refactor: rewrite columnreszier to use injection hooks
yordan-st b731bac
refactor: enhance ColumnResizer test structure and update snapshot
yordan-st 838cc47
refactor: fix failing test
yordan-st fa90eba
feat: enhance drag-and-drop functionality with DragHandle component
yordan-st e90088b
refactor: fix lint errors
yordan-st bb74b16
refactor: standardize use of brandi
yordan-st 4851062
refactor: improve naming, consistency and clean up
yordan-st 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
63 changes: 63 additions & 0 deletions
63
packages/pluggableWidgets/datagrid-web/src/components/ColumnContainer.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,63 @@ | ||
| import classNames from "classnames"; | ||
| import { ReactElement } from "react"; | ||
| import { ColumnHeader } from "./ColumnHeader"; | ||
| import { useColumn, useColumnsStore, useDatagridConfig, useColumnHeaderVM } from "../model/hooks/injection-hooks"; | ||
| import { ColumnResizerProps } from "./ColumnResizer"; | ||
| import { observer } from "mobx-react-lite"; | ||
|
|
||
| export interface ColumnContainerProps { | ||
| isLast?: boolean; | ||
| resizer: ReactElement<ColumnResizerProps>; | ||
| } | ||
|
|
||
| export const ColumnContainer = observer(function ColumnContainer(props: ColumnContainerProps): ReactElement { | ||
| const { columnsFilterable, id: gridId } = useDatagridConfig(); | ||
| const { columnFilters } = useColumnsStore(); | ||
| const { canSort, columnId, setHeaderElementRef, columnIndex, canResize, sortDir, header } = useColumn(); | ||
| const vm = useColumnHeaderVM(); | ||
| const caption = header.trim(); | ||
|
|
||
| return ( | ||
| <div | ||
| aria-sort={getAriaSort(canSort, sortDir)} | ||
| className={classNames("th", { | ||
| [`drop-${vm.dropTarget?.[1]}`]: columnId === vm.dropTarget?.[0], | ||
| dragging: columnId === vm.dragging?.[1], | ||
| "dragging-over-self": columnId === vm.dragging?.[1] && !vm.dropTarget | ||
| })} | ||
| role="columnheader" | ||
| style={!canSort ? { cursor: "unset" } : undefined} | ||
| title={caption} | ||
| ref={ref => setHeaderElementRef(ref)} | ||
| data-column-id={columnId} | ||
| onDrop={vm.handleOnDrop} | ||
| onDragEnter={vm.handleDragEnter} | ||
| onDragOver={vm.handleDragOver} | ||
| > | ||
| <div className={classNames("column-container")} id={`${gridId}-column${columnId}`}> | ||
| <ColumnHeader /> | ||
| {columnsFilterable && ( | ||
| <div className="filter" style={{ pointerEvents: vm.dragging ? "none" : undefined }}> | ||
| {columnFilters[columnIndex]?.renderFilterWidgets()} | ||
| </div> | ||
| )} | ||
| </div> | ||
| {canResize ? props.resizer : null} | ||
| </div> | ||
| ); | ||
| }); | ||
|
|
||
| function getAriaSort(canSort: boolean, sortDir: string | undefined): "ascending" | "descending" | "none" | undefined { | ||
| if (!canSort) { | ||
| return undefined; | ||
| } | ||
|
|
||
| switch (sortDir) { | ||
| case "asc": | ||
| return "ascending"; | ||
| case "desc": | ||
| return "descending"; | ||
| default: | ||
| return "none"; | ||
| } | ||
| } |
102 changes: 102 additions & 0 deletions
102
packages/pluggableWidgets/datagrid-web/src/components/ColumnHeader.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,102 @@ | ||
| import classNames from "classnames"; | ||
| import { DragEventHandler, DragEvent, HTMLAttributes, KeyboardEvent, MouseEvent, ReactElement, ReactNode } from "react"; | ||
| import { FaArrowsAltV } from "./icons/FaArrowsAltV"; | ||
| import { FaLongArrowAltDown } from "./icons/FaLongArrowAltDown"; | ||
| import { FaLongArrowAltUp } from "./icons/FaLongArrowAltUp"; | ||
| import { useColumn, useColumnHeaderVM } from "../model/hooks/injection-hooks"; | ||
| import { observer } from "mobx-react-lite"; | ||
|
|
||
| interface DragHandleProps { | ||
| draggable: boolean; | ||
| onDragStart?: DragEventHandler<HTMLSpanElement>; | ||
| onDragEnd?: DragEventHandler<HTMLSpanElement>; | ||
| } | ||
|
|
||
| export const ColumnHeader = observer(function ColumnHeader(): ReactElement { | ||
| const { header, canSort, alignment, toggleSort } = useColumn(); | ||
|
Collaborator
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. toggleSort will break. |
||
| const caption = header.trim(); | ||
| const sortProps = canSort ? getSortProps(toggleSort) : null; | ||
| const vm = useColumnHeaderVM(); | ||
|
|
||
| return ( | ||
| <div | ||
| className={classNames("column-header", { clickable: canSort }, `align-column-${alignment}`)} | ||
| style={{ pointerEvents: vm.dragging ? "none" : undefined }} | ||
| {...sortProps} | ||
| aria-label={canSort ? "sort " + caption : caption} | ||
| > | ||
| {vm.isDraggable && ( | ||
| <DragHandle draggable={vm.isDraggable} onDragStart={vm.handleDragStart} onDragEnd={vm.handleDragEnd} /> | ||
| )} | ||
| <span className="column-caption">{caption.length > 0 ? caption : "\u00a0"}</span> | ||
| {canSort ? <SortIcon /> : null} | ||
| </div> | ||
| ); | ||
| }); | ||
|
|
||
| function DragHandle({ draggable, onDragStart, onDragEnd }: DragHandleProps): ReactElement { | ||
| const handleMouseDown = (e: MouseEvent<HTMLSpanElement>): void => { | ||
| // Only stop propagation, don't prevent default - we need default for drag to work | ||
| e.stopPropagation(); | ||
| }; | ||
|
|
||
| const handleClick = (e: MouseEvent<HTMLSpanElement>): void => { | ||
| // Stop click events from bubbling to prevent sorting | ||
| e.stopPropagation(); | ||
| e.preventDefault(); | ||
| }; | ||
|
|
||
| const handleDragStart = (e: DragEvent<HTMLSpanElement>): void => { | ||
| // Don't stop propagation here - let the drag start properly | ||
| if (onDragStart) { | ||
| onDragStart(e); | ||
| } | ||
| }; | ||
|
|
||
| const handleDragEnd = (e: DragEvent<HTMLSpanElement>): void => { | ||
| if (onDragEnd) { | ||
| onDragEnd(e); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <span | ||
| className="drag-handle" | ||
| draggable={draggable} | ||
| onDragStart={handleDragStart} | ||
| onDragEnd={handleDragEnd} | ||
| onMouseDown={handleMouseDown} | ||
| onClick={handleClick} | ||
| > | ||
| ⠿ | ||
| </span> | ||
| ); | ||
| } | ||
|
|
||
| function SortIcon(): ReactNode { | ||
| const column = useColumn(); | ||
| switch (column.sortDir) { | ||
| case "asc": | ||
| return <FaLongArrowAltUp />; | ||
| case "desc": | ||
| return <FaLongArrowAltDown />; | ||
| default: | ||
| return <FaArrowsAltV />; | ||
| } | ||
| } | ||
|
|
||
| function getSortProps(toggleSort: () => void): HTMLAttributes<HTMLDivElement> { | ||
| return { | ||
| onClick: () => { | ||
| toggleSort(); | ||
| }, | ||
| onKeyDown: (e: KeyboardEvent<HTMLDivElement>) => { | ||
| if (e.key === "Enter" || e.key === " ") { | ||
| e.preventDefault(); | ||
| toggleSort(); | ||
| } | ||
| }, | ||
| role: "button", | ||
| tabIndex: 0 | ||
| }; | ||
| } | ||
28 changes: 11 additions & 17 deletions
28
packages/pluggableWidgets/datagrid-web/src/components/ColumnResizer.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
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.
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.