Skip to content

Commit

Permalink
chore(common): add EditorConstructor type whenever possible
Browse files Browse the repository at this point in the history
- this is extension to another PR #1462 that now uses `editorClass` and should be typed to `EditorConstructor` whenever possible (instead of `any` to get rid of the typing issue)
  • Loading branch information
ghiscoding-SE committed Apr 16, 2024
1 parent 38b465c commit 1cd6d7b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/editors/sliderEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class SliderEditor implements Editor {
protected _defaultValue = 0;
protected _isValueTouched = false;
protected _originalValue?: number | string;
protected _cellContainerElm!: HTMLDivElement;
protected _cellContainerElm!: HTMLElement;
protected _editorElm!: HTMLDivElement;
protected _inputElm!: HTMLInputElement;
protected _sliderOptions!: CurrentSliderOption;
Expand Down
21 changes: 14 additions & 7 deletions packages/common/src/extensions/slickCellExternalCopyManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createDomElement, getHtmlStringOutput, stripTags } from '@slickgrid-universal/utils';

import type { Column, Editor, ExcelCopyBufferOption, ExternalCopyClipCommand, OnEventArgs } from '../interfaces/index';
import type { Column, Editor, EditorConstructor, ElementPosition, ExcelCopyBufferOption, ExternalCopyClipCommand, OnEventArgs } from '../interfaces/index';
import { SlickEvent, SlickEventData, SlickEventHandler, type SlickGrid, SlickRange, type SlickDataView, Utils as SlickUtils } from '../core/index';

// using external SlickGrid JS libraries
Expand Down Expand Up @@ -129,13 +129,16 @@ export class SlickCellExternalCopyManager {
if (columnDef) {
if (columnDef.editorClass) {
const tmpP = document.createElement('p');
const editor = new (columnDef as any).editorClass({
const editor = new (columnDef.editorClass as EditorConstructor)({
container: tmpP, // a dummy container
column: columnDef,
event,
position: { top: 0, left: 0 }, // a dummy position required by some editors
position: { top: 0, left: 0 } as unknown as ElementPosition, // a dummy position required by some editors
gridPosition: { top: 0, left: 0 } as unknown as ElementPosition, // a dummy position required by some editors
grid: this._grid,
}) as Editor;
/* istanbul ignore next */ cancelChanges: () => { },
/* istanbul ignore next */ commitChanges: () => { },
});
editor.loadValue(item);
retVal = editor.serializeValue();
editor.destroy();
Expand All @@ -157,11 +160,15 @@ export class SlickCellExternalCopyManager {
// if a custom setter is not defined, we call applyValue of the editor to unserialize
if (columnDef.editorClass) {
const tmpDiv = document.createElement('div');
const editor = new (columnDef as any).editorClass({
const editor = new (columnDef.editorClass as EditorConstructor)({
container: tmpDiv, // a dummy container
column: columnDef,
position: { top: 0, left: 0 }, // a dummy position required by some editors
grid: this._grid
event: null as any,
position: { top: 0, left: 0 } as unknown as ElementPosition, // a dummy position required by some editors
gridPosition: { top: 0, left: 0 } as unknown as ElementPosition, // a dummy position required by some editors
grid: this._grid,
/* istanbul ignore next */ cancelChanges: () => { },
/* istanbul ignore next */ commitChanges: () => { },
}) as Editor;
editor.loadValue(item);
const validationResults = editor.validate(undefined, value);
Expand Down
10 changes: 5 additions & 5 deletions packages/common/src/interfaces/editorArguments.interface.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type { Column, CompositeEditorOption, ElementPosition } from './index';
import type { PositionMethod } from '../enums/positionMethod.type';
import type { SlickDataView, SlickGrid } from '../core/index';
import type { SlickDataView, SlickEventData, SlickGrid } from '../core/index';

export interface EditorArguments {
/** Column Definition */
column: Column;

/** Column MetaData */
columnMetaData: any;
columnMetaData?: any;

/** Editor HTML DOM element container */
container: HTMLDivElement;
container: HTMLElement;

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

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

/** Slick Grid object */
grid: SlickGrid;
Expand All @@ -25,7 +25,7 @@ export interface EditorArguments {
gridPosition: ElementPosition;

/** Item DataContext */
item: any;
item?: any;

/** Editor Position */
position: PositionMethod | ElementPosition;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { emptyElement, getOffset, } from '@slickgrid-universal/common';
import type { Column, CompositeEditorOption, Editor, EditorArguments, EditorValidationResult, ElementPosition, } from '@slickgrid-universal/common';
import type { Column, CompositeEditorOption, Editor, EditorArguments, EditorConstructor, EditorValidationResult, ElementPosition, } from '@slickgrid-universal/common';
import type { HtmlElementPosition } from '@slickgrid-universal/utils';

export interface CompositeEditorArguments extends EditorArguments {
Expand Down Expand Up @@ -71,15 +71,15 @@ export function SlickCompositeEditor(this: any, columns: Column[], containers: A
function editor(this: any, args: EditorArguments) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context: any = this;
let editors: Array<Editor & { args: EditorArguments }> = [];
let editors: Array<Editor & { args: EditorArguments; }> = [];

function init() {
let newArgs: Partial<CompositeEditorArguments> = {};
let newArgs = {} as CompositeEditorArguments;
let idx = 0;
while (idx < columns.length) {
if (columns[idx].editorClass) {
const column = columns[idx];
newArgs = { ...args };
newArgs = { ...args } as CompositeEditorArguments;
newArgs.container = containers[idx];
newArgs.column = column;
newArgs.position = getContainerBox(idx);
Expand All @@ -88,9 +88,9 @@ export function SlickCompositeEditor(this: any, columns: Column[], containers: A
newArgs.compositeEditorOptions = options;
newArgs.formValues = {};

const currentEditor = new (column.editorClass as any)(newArgs) as Editor & { args: EditorArguments };
const currentEditor = new (column.editorClass as EditorConstructor)(newArgs);
options.editors[column.id] = currentEditor; // add every Editor instance refs
editors.push(currentEditor);
editors.push(currentEditor as Editor & { args: EditorArguments; });
}
idx++;
}
Expand Down

0 comments on commit 1cd6d7b

Please sign in to comment.