Skip to content
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

Add controlled selection scrolling options #632

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions packages/core/src/data-editor/data-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,12 @@ export interface DataEditorProps extends Props {
* @group Selection
*/
readonly gridSelection?: GridSelection;
/**
* Scroll position configuration when using {@link gridSelection}.
* See "Controlled selection" example for details.
* @group Selection
*/
readonly gridSelectionScrollOptions?: GridSelectionScrollOptions;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this option, I think we need to workshop the name a bit. Not quite sure I have a better alternative yet... Did you have any other ideas?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a shorter more generic name like: "gridSelectionScroll" works better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

focusScrollBehavior focusBehavior gridSelectionBehavior

One of the things about this is it actually only reflects the gridSelection.current.cell

Copy link
Contributor Author

@frankagathos frankagathos Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer gridSelectionBehavior , or gridSelectionScrollBehavior might also be a good choise

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approved

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still need to do an audit of how this interacts with all the other options, would be helpful to break down how this corresponds with the paddingX/Y options etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jassmith anything oustanding on my end for this to get merged?

/**
* Emitted whenever the grid selection changes. Specifying
* this function will make the grid’s selection controlled, so
Expand Down Expand Up @@ -610,16 +616,26 @@ export interface DataEditorProps extends Props {
readonly scaleToRem?: boolean;
}

type GridSelectionScrollOptions = {
dir?: ScrollDir;
paddingX?: number;
paddingY?: number;
options?: ScrollAlignOptions;
};

type ScrollDir = "horizontal" | "vertical" | "both";
type ScrollAlignOptions = {
hAlign?: "start" | "center" | "end";
vAlign?: "start" | "center" | "end";
};

type ScrollToFn = (
col: number | { amount: number; unit: "cell" | "px" },
row: number | { amount: number; unit: "cell" | "px" },
dir?: "horizontal" | "vertical" | "both",
dir?: ScrollDir,
paddingX?: number,
paddingY?: number,
options?: {
hAlign?: "start" | "center" | "end";
vAlign?: "start" | "center" | "end";
}
options?: ScrollAlignOptions
) => void;

/** @category DataEditor */
Expand Down Expand Up @@ -743,6 +759,7 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
showSearch: showSearchIn,
onVisibleRegionChanged,
gridSelection: gridSelectionOuter,
gridSelectionScrollOptions,
onGridSelectionChange,
minColumnWidth: minColumnWidthIn = 50,
maxColumnWidth: maxColumnWidthIn = 500,
Expand Down Expand Up @@ -3321,10 +3338,12 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
(outCol !== expectedExternalGridSelection.current?.current?.cell[0] ||
outRow !== expectedExternalGridSelection.current?.current?.cell[1])
) {
scrollToRef.current(outCol, outRow);
const { dir, paddingX, paddingY, options } = gridSelectionScrollOptions ?? {};

scrollToRef.current(outCol, outRow, dir, paddingX, paddingY, options);
}
hasJustScrolled.current = false; //only allow skipping a single scroll
}, [outCol, outRow]);
}, [outCol, outRow, gridSelectionScrollOptions]);

const selectionOutOfBounds =
gridSelection.current !== undefined &&
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/docs/examples/controlled-selection.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export const ControlledSelection: React.VFC = () => {
rows: CompactSelection.empty(),
});

const [centerScroll, setCenterScroll] = React.useState(false);
const handleCenterScroll = () => setCenterScroll(s => !s);

return (
<BeautifulWrapper
title="Controlled Selection"
Expand Down Expand Up @@ -82,12 +85,26 @@ export const ControlledSelection: React.VFC = () => {
}));
}}
/>
<div>
<span style={{ fontSize: "14px", display: "flex", alignItems: "center" }}>
<input
type="checkbox"
name="site_name"
checked={centerScroll}
onChange={handleCenterScroll}
/>
<span onClick={handleCenterScroll}>Centered scrolling</span>
</span>
</div>
</Description>
}>
<DataEditor
{...defaultProps}
getCellContent={getCellContent}
gridSelection={selection}
gridSelectionScrollOptions={
centerScroll ? { options: { hAlign: "center", vAlign: "center" } } : undefined
}
onGridSelectionChange={setSelection}
columns={cols}
rows={100}
Expand Down