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

fix: column.editor and gridOptions.editorFactory type changed #995

Merged
merged 7 commits into from
Mar 11, 2024
Merged
3 changes: 2 additions & 1 deletion src/models/column.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
CellMenuOption,
CustomTooltipOption,
Editor,
EditorConstructor,
EditorValidator,
Formatter,
FormatterResultWithHtml,
Expand Down Expand Up @@ -84,7 +85,7 @@ export interface Column<TData = any> {
disableTooltip?: boolean;

/** Any inline editor function that implements Editor for the cell value or ColumnEditor */
editor?: Editor | { model?: Editor; } | null;
editor?: Editor /*| { model?: Editor; }*/ | EditorConstructor | null;
ghiscoding marked this conversation as resolved.
Show resolved Hide resolved

/** Editor number fixed decimal places */
editorFixedDecimalPlaces?: number;
Expand Down
10 changes: 9 additions & 1 deletion src/models/editor.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { EditorArguments } from './editorArguments.interface';
import type { EditorValidationResult } from './editorValidationResult.interface';

import type { GridOption } from '../models/gridOption.interface';
Copy link
Collaborator

Choose a reason for hiding this comment

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

after the CI ran I see that the build is failing, 1 of the reason is that you have duplicate import, you can remove line 3 since GridOption is imported in line 4 as well so you have duplicate imports.

import type { Column } from '../models/column.interface';
manfredwippel marked this conversation as resolved.
Show resolved Hide resolved
/**
* SlickGrid Editor interface, more info can be found on the SlickGrid repo
* https://github.com/6pac/SlickGrid/wiki/Writing-custom-cell-editors
Expand Down Expand Up @@ -86,3 +87,10 @@ export interface Editor {
*/
validate: (targetElm?: HTMLElement, options?: any) => EditorValidationResult;
}

export type EditorConstructor = {
new <TData = any, C extends Column<TData> = Column<TData>, O extends GridOption<C> = GridOption<C>>(args?: EditorArguments<TData, C, O>): Editor;

/** static flag used in makeActiveCellEditable*/
ghiscoding marked this conversation as resolved.
Show resolved Hide resolved
suppressClearOnEdit?: boolean;
};
7 changes: 4 additions & 3 deletions src/models/editorArguments.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import { Column, ElementPosition, PositionMethod } from './index';
import type { SlickDataView } from '../slick.dataview';
import type { SlickGrid } from '../slick.grid';
import type { GridOption } from '../models/gridOption.interface';
manfredwippel marked this conversation as resolved.
Show resolved Hide resolved

export interface EditorArguments {
export interface EditorArguments<TData = any, C extends Column<TData> = Column<TData>, O extends GridOption<C> = GridOption<C>> {
/** Column Definition */
column: Column;

Expand All @@ -17,13 +18,13 @@ export interface EditorArguments {
container: HTMLDivElement;

/** Slick DataView */
dataView: SlickDataView;
dataView?: SlickDataView;

/** Event that was triggered */
event: Event;

/** Slick Grid object */
grid: SlickGrid;
grid: SlickGrid<TData, C, O>;

/** Grid Position */
gridPosition: ElementPosition;
Expand Down
4 changes: 2 additions & 2 deletions src/models/gridOption.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Column as BaseColumn, CellMenuOption, ColumnPickerOption, ColumnReorderFunction, ContextMenuOption, CustomTooltipOption, EditCommand, Editor, ExcelCopyBufferOption, Formatter, GridMenuOption, ItemMetadata, } from './index';
import type { Column as BaseColumn, CellMenuOption, ColumnPickerOption, ColumnReorderFunction, ContextMenuOption, CustomTooltipOption, EditCommand, Editor, EditorConstructor, ExcelCopyBufferOption, Formatter, GridMenuOption, ItemMetadata, } from './index';
import type { SlickEditorLock } from '../slick.core';

export interface CellViewportRange {
Expand Down Expand Up @@ -121,7 +121,7 @@ export interface GridOption<C extends BaseColumn = BaseColumn> {
editCommandHandler?: (item: any, column: C, command: EditCommand) => void;

/** Editor classes factory */
editorFactory?: null | { getEditor: (col: C) => Editor; };
editorFactory?: null | { getEditor: (col: C) => Editor | EditorConstructor; };

/** a global singleton editor lock. */
editorLock?: SlickEditorLock;
Expand Down
30 changes: 19 additions & 11 deletions src/slick.grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ import type {
SlickPlugin,
MenuCommandItemCallbackArgs,
OnClickEventArgs,
EditorConstructor,
EditorArguments,
ghiscoding marked this conversation as resolved.
Show resolved Hide resolved
} from './models/index';
import {
type BasePubSub,
Expand Down Expand Up @@ -3841,19 +3843,19 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
this._options.defaultFormatter) as Formatter;
}

protected getEditor(row: number, cell: number): Editor | undefined {
protected getEditor(row: number, cell: number): Editor | EditorConstructor | null | undefined {
ghiscoding marked this conversation as resolved.
Show resolved Hide resolved
const column = this.columns[cell];
const rowMetadata = (this.data as CustomDataView<TData>)?.getItemMetadata?.(row);
const columnMetadata = rowMetadata?.columns;

if (columnMetadata?.[column.id]?.editor !== undefined) {
return columnMetadata[column.id].editor as Editor;
return columnMetadata[column.id].editor;
}
if (columnMetadata?.[cell]?.editor !== undefined) {
return columnMetadata[cell].editor as Editor;
return columnMetadata[cell].editor;
}

return (column.editor || (this._options?.editorFactory?.getEditor(column))) as Editor;
return (column.editor || (this._options?.editorFactory?.getEditor(column)));
}

protected getDataItemValueForColumn(item: TData, columnDef: C) {
Expand Down Expand Up @@ -5901,11 +5903,11 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
}


editActiveCell(editor: Editor, preClickModeOn?: boolean | null, e?: Event) {
editActiveCell(editor: Editor | EditorConstructor, preClickModeOn?: boolean | null, e?: Event) {
ghiscoding marked this conversation as resolved.
Show resolved Hide resolved
this.makeActiveCellEditable(editor, preClickModeOn, e);
}

protected makeActiveCellEditable(editor?: Editor, preClickModeOn?: boolean | null, e?: Event | SlickEvent_) {
protected makeActiveCellEditable(editor?: Editor | EditorConstructor, preClickModeOn?: boolean | null, e?: Event | SlickEvent_) {
if (!this.activeCellNode) {
return;
}
Expand All @@ -5931,29 +5933,35 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
this.getEditorLock()?.activate(this.editController as EditController);
this.activeCellNode.classList.add('editable');

const useEditor: any = editor || this.getEditor(this.activeRow, this.activeCell);
const useEditor = editor || this.getEditor(this.activeRow, this.activeCell);

// don't clear the cell if a custom editor is passed through
if (!editor && !useEditor.suppressClearOnEdit) {
if (!editor && !useEditor?.suppressClearOnEdit) {
Utils.emptyElement(this.activeCellNode);
}

let metadata = (this.data as CustomDataView<TData>)?.getItemMetadata?.(this.activeRow);
metadata = metadata?.columns as any;
const columnMetaData = metadata && (metadata[columnDef.id as keyof ItemMetadata] || (metadata as any)[this.activeCell]);

this.currentEditor = new useEditor({
//the editor must be constructable
ghiscoding marked this conversation as resolved.
Show resolved Hide resolved
if (typeof useEditor !== 'function') {
ghiscoding marked this conversation as resolved.
Show resolved Hide resolved
return;
}
const editorArgs: EditorArguments<TData, C, O> = {
grid: this,
gridPosition: this.absBox(this._container),
position: this.absBox(this.activeCellNode),
container: this.activeCellNode,
column: columnDef,
columnMetaData,
item: item || {},
event: e,
event: e as Event,
ghiscoding marked this conversation as resolved.
Show resolved Hide resolved
commitChanges: this.commitEditAndSetFocus.bind(this),
cancelChanges: this.cancelEditAndSetFocus.bind(this)
});
};

this.currentEditor = new useEditor(editorArgs);

if (item && this.currentEditor) {
this.currentEditor.loadValue(item);
Expand Down