-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: Drag & Drop fast follow for card style rows #991
Changes from all commits
dfc1be0
f4bc9de
bdda5b3
bce35af
420f710
13deb4d
b2cc979
c140013
a8831c6
26f73f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { observer } from "mobx-react"; | ||
import { ReactElement, useContext, useRef } from "react"; | ||
import { ReactElement, useContext, useRef, useCallback } from "react"; | ||
import { | ||
defaultRenderFn, | ||
headerRenderFn, | ||
|
@@ -31,7 +31,7 @@ import { | |
import { Css, Palette } from "src/Css"; | ||
import { AnyObject } from "src/types"; | ||
import { isFunction } from "src/utils"; | ||
import { Icon } from "src"; | ||
import { useDebouncedCallback } from "use-debounce"; | ||
|
||
interface RowProps<R extends Kinded> { | ||
as: RenderAs; | ||
|
@@ -93,8 +93,13 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement { | |
const rowStyleCellCss = maybeApplyFunction(row as any, rowStyle?.cellCss); | ||
const levelIndent = style.levels && style.levels[level]?.rowIndent; | ||
|
||
const containerCss = { | ||
...Css.add("transition", "padding 0.25s ease-in-out").$, | ||
...(rs.isDraggedOver === DraggedOver.Above && Css.ptPx(25).$), | ||
...(rs.isDraggedOver === DraggedOver.Below && Css.pbPx(25).$), | ||
}; | ||
|
||
const rowCss = { | ||
...Css.add("transition", "padding 0.5s ease-in-out").$, | ||
...(!reservedRowKinds.includes(row.kind) && style.nonHeaderRowCss), | ||
// Optionally include the row hover styles, by default they should be turned on. | ||
...(showRowHoverColor && { | ||
|
@@ -117,8 +122,6 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement { | |
[`:hover > .${revealOnRowHoverClass} > *`]: Css.visible.$, | ||
}, | ||
...(isLastKeptRow && Css.addIn("&>*", style.keptLastRowCss).$), | ||
...(rs.isDraggedOver === DraggedOver.Above && Css.add("paddingTop", "35px").$), | ||
...(rs.isDraggedOver === DraggedOver.Below && Css.add("paddingBottom", "35px").$), | ||
}; | ||
|
||
let currentColspan = 1; | ||
|
@@ -131,36 +134,16 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement { | |
// used to render the whole row when dragging with the handle | ||
const ref = useRef<HTMLTableRowElement>(null); | ||
|
||
return ( | ||
<RowTag | ||
css={rowCss} | ||
{...others} | ||
data-gridrow | ||
{...getCount(row.id)} | ||
// these events are necessary to get the dragged-over row for the drop event | ||
// and spacer styling | ||
onDrop={(evt) => onDrop?.(row, evt)} | ||
onDragEnter={(evt) => onDragEnter?.(row, evt)} | ||
onDragOver={(evt) => onDragOver?.(row, evt)} | ||
ref={ref} | ||
> | ||
{/* {row.draggable && ( | ||
<div | ||
draggable={row.draggable} | ||
onDragStart={(evt) => { | ||
// show the whole row being dragged when dragging with the handle | ||
ref.current && evt.dataTransfer.setDragImage(ref.current, 0, 0); | ||
return onDragStart?.(row, evt); | ||
}} | ||
onDragEnd={(evt) => onDragEnd?.(row, evt)} | ||
onDrop={(evt) => onDrop?.(row, evt)} | ||
onDragEnter={(evt) => onDragEnter?.(row, evt)} | ||
onDragOver={(evt) => onDragOver?.(row, evt)} | ||
css={Css.mh100.ma.$} | ||
> | ||
<Icon icon="drag" /> | ||
</div> | ||
)} */} | ||
// debounce drag over callback to avoid excessive re-renders | ||
const dragOverCallback = useCallback( | ||
(row: GridDataRow<R>, evt: React.DragEvent<HTMLElement>) => onDragOver?.(row, evt), | ||
[onDragOver], | ||
); | ||
// when the event is not called, we still need to call preventDefault | ||
const onDragOverDebounced = useDebouncedCallback(dragOverCallback, 100); | ||
|
||
const RowContent = () => ( | ||
<RowTag css={rowCss} {...others} data-gridrow {...getCount(row.id)}> | ||
{isKeptGroupRow ? ( | ||
<KeptGroupRow as={as} style={style} columnSizes={columnSizes} row={row} colSpan={columns.length} /> | ||
) : ( | ||
|
@@ -227,7 +210,7 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement { | |
onDragEnd, | ||
onDrop, | ||
onDragEnter, | ||
onDragOver, | ||
onDragOver: onDragOverDebounced, | ||
}); | ||
|
||
// Only use the `numExpandedColumns` as the `colspan` when rendering the "Expandable Header" | ||
|
@@ -372,6 +355,27 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement { | |
)} | ||
</RowTag> | ||
); | ||
|
||
return row.draggable ? ( | ||
<div | ||
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. Wrapping rows in this container seems to be the only sane way to make card style rows work with drag & drop. It doesn't seem to have broken anything wrt rows/tables, at least as far as Beam is concerned. But this is somewhat risky in terms of breaking implementations that rely on Beam. Let me know if this is likely to mess something up. 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. Huh, isn't Ah, I get it, both dnd & cards were using padding, but when dragging we want the dnd padding to be "on top" / separate from the card padding? I'm okay-ish with this as a short-term fix, but yeah let's try and only ad the extra div is dnd is actively being used, just to be extra sure we don't mess existing callers. Afaiu does dnd use padding to provide the "spacer" effect? Kinda wondering if we could use a psuedo element for that (like the cc @bdow for thoughts. 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. The padding for the spacer goes inside the border of the card, so instead of looking like it's going above or below the row, it looks like it's going inside the card. I will look into the before/after pseudo-elements but I'm pretty sure they won't handle events properly. I could make the container conditional to draggable rows, but theoretically it should be able to work either way. 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.
Yeah, makes sense...
Yeah. Ngl I'm surprised it still works, but I get it, because we're like hand-calcing so much of the column widths/etc; even if it just comes down to DOM aesthetics of avoiding the extra div 🤷 dunno, seems simple enough to avoid/make conditional. 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. conditional it is 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. @stephenh should only affect draggable rows now 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. I was also thinking the pseudo elements would be the solution here instead of a wrapping element. I'm surprised they don't. 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. @bsholmes nice, thank you! |
||
css={containerCss} | ||
// these events are necessary to get the dragged-over row for the drop event | ||
// and spacer styling | ||
onDrop={(evt) => onDrop?.(row, evt)} | ||
onDragEnter={(evt) => onDragEnter?.(row, evt)} | ||
onDragOver={(evt) => { | ||
// when the event isn't called due to debounce, we still need to | ||
// call preventDefault for the drop event to fire | ||
evt.preventDefault(); | ||
onDragOverDebounced(row, evt); | ||
}} | ||
ref={ref} | ||
> | ||
{RowContent()} | ||
</div> | ||
) : ( | ||
<>{RowContent()}</> | ||
); | ||
} | ||
|
||
/** | ||
|
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.
question: I'm a little late to the game here, but wouldn't
draggedRow
anddroppedRow
be the same row?It is probably too late to go back and update the implementation at this point, but just spit-balling here; Could we have gotten away with a callback like:
This would give the user the information that a certain row was moved to
newIndex
.Or many even:
If the developer only ever needs to just update their
row
property, this might make their implementations simpler.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.
draggedRow would be the row that was dragged initially, droppedRow would be the row we just dropped the draggedRow onto. We could potentially simplify this callback if we only want it to support reordering, but potentially it could support other kinds of drag & drop behavior and in those cases we want the handler to have info about both rows.
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.
Oh, yes. That is a bit confusing of a name then. I would've assume the row being "dropped" is the row I was dragging. Maybe
targetRow
is a better name?Interesting point about other drag and drop behaviors. Are we being asked to support dropping into another row in order to make the
draggedRow
a child of thetarget/droppedRow
?I'd be tempted to support multiple callbacks for that... like
onReorder
for the current use case, and .... I dunno what else? Would we want to allow rows to drop into each other to nest them or something? I'd be tempted to solution only the problem in front of us instead of envision future scenarios.I'm fine with leaving it as is for now.