Skip to content

feat: provideEditorCallback access cell location #1038

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
wants to merge 3 commits 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
36 changes: 24 additions & 12 deletions packages/cells/test/date-picker-cell.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ describe("editor", () => {

it("renders into the dom with correct value", () => {
// @ts-ignore
const Editor = renderer.provideEditor?.(getMockDateCell()).editor;
const Editor = renderer.provideEditor?.({
...getMockDateCell(),
location: [0, 0],
}).editor;
if (Editor === undefined) {
throw new Error("Editor is invalid");
}
Expand All @@ -59,10 +62,10 @@ describe("editor", () => {

it.each([["date"], ["time"], ["datetime-local"]])("renders with correct format", (format: string) => {
// @ts-ignore
const Editor = renderer.provideEditor?.(
getMockDateCell({ data: { format: format } } as DatePickerCell)
// @ts-ignore
).editor;
const Editor = renderer.provideEditor?.({
...getMockDateCell({ data: { format: format } } as DatePickerCell),
location: [0, 0],
}).editor;
if (Editor === undefined) {
throw new Error("Editor is invalid");
}
Expand All @@ -77,10 +80,10 @@ describe("editor", () => {

it("renders textarea when readonly is true", () => {
// @ts-ignore
const Editor = renderer.provideEditor?.(
getMockDateCell({ readonly: true } as DatePickerCell)
// @ts-ignore
).editor;
const Editor = renderer.provideEditor?.({
...getMockDateCell({ readonly: true } as DatePickerCell),
location: [0, 0],
}).editor;
if (Editor === undefined) {
throw new Error("Editor is invalid");
}
Expand All @@ -103,7 +106,10 @@ describe("editor", () => {
};

// @ts-ignore
const Editor = renderer.provideEditor?.(getMockDateCell(extraProps)).editor;
const Editor = renderer.provideEditor?.({
...getMockDateCell(extraProps),
location: [0, 0],
}).editor;
if (Editor === undefined) {
throw new Error("Editor is invalid");
}
Expand All @@ -124,7 +130,10 @@ describe("editor", () => {
const valueAsNumber = 100;

// @ts-ignore
const Editor = renderer.provideEditor?.(getMockDateCell()).editor;
const Editor = renderer.provideEditor?.({
...getMockDateCell(),
location: [0, 0],
}).editor;
if (Editor === undefined) {
throw new Error("Editor is invalid");
}
Expand Down Expand Up @@ -156,7 +165,10 @@ describe("editor", () => {

it('properly sets new date to undefined when value is ""', async () => {
// @ts-ignore
const Editor = renderer.provideEditor?.(getMockDateCell()).editor;
const Editor = renderer.provideEditor?.({
...getMockDateCell(),
location: [0, 0],
}).editor;
if (Editor === undefined) {
throw new Error("Editor is invalid");
}
Expand Down
20 changes: 16 additions & 4 deletions packages/cells/test/multi-select-cell.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,10 @@ describe("Multi Select Editor", () => {

it("renders into the dom with correct value", async () => {
// @ts-ignore
const Editor = renderer.provideEditor?.(getMockCell()).editor;
const Editor = renderer.provideEditor?.({
...getMockCell(),
location: [0, 0],
}).editor;
if (Editor === undefined) {
throw new Error("Editor is invalid");
}
Expand All @@ -285,7 +288,10 @@ describe("Multi Select Editor", () => {
it("allows to select values", async () => {
const mockCell = getMockCell();
// @ts-ignore
const Editor = renderer.provideEditor?.(mockCell).editor;
const Editor = renderer.provideEditor?.({
...mockCell,
location: [0, 0],
}).editor;
if (Editor === undefined) {
throw new Error("Editor is invalid");
}
Expand Down Expand Up @@ -317,7 +323,10 @@ describe("Multi Select Editor", () => {
it("is disabled if readonly", async () => {
const mockCell = getMockCell({ readonly: true });
// @ts-ignore
const Editor = renderer.provideEditor?.(mockCell).editor;
const Editor = renderer.provideEditor?.({
...mockCell,
location: [0, 0],
}).editor;
if (Editor === undefined) {
throw new Error("Editor is invalid");
}
Expand All @@ -334,7 +343,10 @@ describe("Multi Select Editor", () => {
it("allowDuplicates allows to select values multiple times", async () => {
const mockCell = getMockCell({ data: { allowDuplicates: true } } as any);
// @ts-ignore
const Editor = renderer.provideEditor?.(mockCell).editor;
const Editor = renderer.provideEditor?.({
...mockCell,
location: [0, 0],
}).editor;
if (Editor === undefined) {
throw new Error("Editor is invalid");
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,9 @@ export type ProvideEditorCallbackResult<T extends InnerGridCell> =
| ObjectEditorCallbackResult<T>
| undefined;

export type ProvideEditorCallback<T extends InnerGridCell> = (cell: T) => ProvideEditorCallbackResult<T>;
export type ProvideEditorCallback<T extends InnerGridCell> = (
cell: T & { location?: Item }
) => ProvideEditorCallbackResult<T>;

provideEditor?: ProvideEditorCallback<GridCell>;
```
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/data-editor/data-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2998,7 +2998,10 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
let newVal: InnerGridCell | undefined = undefined;
if (cellValue.kind === GridCellKind.Custom) {
const toDelete = getCellRenderer(cellValue);
const editor = toDelete?.provideEditor?.(cellValue);
const editor = toDelete?.provideEditor?.({
...cellValue,
location: [x - rowMarkerOffset, y],
});
if (toDelete?.onDelete !== undefined) {
newVal = toDelete.onDelete(cellValue);
} else if (isObjectEditorCallbackResult(editor)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,13 @@ const DataGridOverlayEditor: React.FunctionComponent<DataGridOverlayEditorProps>

const [editorProvider, useLabel] = React.useMemo((): [ProvideEditorCallbackResult<GridCell>, boolean] | [] => {
if (isInnerOnlyCell(content)) return [];
const external = provideEditor?.(content);
const cellWithLocation = { ...content, location: cell } as GridCell & {
location: Item;
};
const external = provideEditor?.(cellWithLocation);
if (external !== undefined) return [external, false];
return [getCellRenderer(content)?.provideEditor?.(content), false];
}, [content, getCellRenderer, provideEditor]);
return [getCellRenderer(content)?.provideEditor?.(cellWithLocation), false];
}, [cell, content, getCellRenderer, provideEditor]);

const { ref, style: stayOnScreenStyle } = useStayOnScreen();

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/internal/data-grid/data-grid-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,9 @@ export function isObjectEditorCallbackResult<T extends InnerGridCell>(
}

/** @category Renderers */
export type ProvideEditorCallback<T extends InnerGridCell> = (cell: T) => ProvideEditorCallbackResult<T>;
export type ProvideEditorCallback<T extends InnerGridCell> = (
cell: T & { location?: Item }
) => ProvideEditorCallbackResult<T>;

/** @category Cells */
export type ValidatedGridCell = EditableGridCell & {
Expand Down
10 changes: 8 additions & 2 deletions packages/core/test/cells.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ describe("Image cell", () => {

it("Renders its editor (smoke test)", async () => {
const cell = getImgCell();
const Editor = imageCellRenderer.provideEditor?.(cell);
const Editor = imageCellRenderer.provideEditor?.({
...cell,
location: [0, 0],
});
const target = getMockEditorTarget();

assert(!isObjectEditorCallbackResult(Editor));
Expand All @@ -83,7 +86,10 @@ describe("Image cell", () => {

it("Renders a custom editor (smoke test)", async () => {
const cell = getImgCell();
const Editor = imageCellRenderer.provideEditor?.(cell);
const Editor = imageCellRenderer.provideEditor?.({
...cell,
location: [0, 0],
});
assert(Editor !== undefined);
const target = getMockEditorTarget();

Expand Down