-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathcustom-inputEditor.ts
132 lines (108 loc) · 3.51 KB
/
custom-inputEditor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import type { Column, ColumnEditor, Editor, EditorValidator, EditorValidationResult } from './../modules/angular-slickgrid';
/*
* An example of a 'detached' editor.
* KeyDown events are also handled to provide handling for Tab, Shift-Tab, Esc and Ctrl-Enter.
*/
export class CustomInputEditor implements Editor {
private _lastInputEvent?: KeyboardEvent;
inputElm!: HTMLInputElement;
defaultValue: any;
constructor(private args: any) {
this.init();
}
/** Get Column Definition object */
get columnDef(): Column {
return this.args?.column ?? {};
}
/** Get Column Editor object */
get columnEditor(): ColumnEditor {
return this.columnDef?.editor ?? {};
}
get hasAutoCommitEdit(): boolean {
return this.args.grid.getOptions().autoCommitEdit ?? false;
}
/** Get the Validator function, can be passed in Editor property or Column Definition */
get validator(): EditorValidator | undefined {
return this.columnEditor.validator || this.columnDef.validator;
}
init(): void {
const placeholder = this.columnEditor?.placeholder || '';
const title = this.columnEditor?.title || '';
this.inputElm = document.createElement('input');
this.inputElm.type = 'text';
this.inputElm.className = 'editor-text';
this.inputElm.placeholder = placeholder;
this.inputElm.title = title;
this.args.container.appendChild(this.inputElm);
this.inputElm.addEventListener('keydown', this.onKeydown.bind(this));
// the lib does not get the focus out event for some reason
// so register it here
if (this.hasAutoCommitEdit) {
this.inputElm.addEventListener('focusout', this.save.bind(this));
}
window.setTimeout(() => {
this.inputElm.focus();
this.inputElm.select();
}, 50);
}
onKeydown(event: KeyboardEvent) {
this._lastInputEvent = event;
if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
event.stopImmediatePropagation();
}
}
destroy() {
this.inputElm.removeEventListener('keydown', this.onKeydown.bind(this));
this.inputElm.removeEventListener('focusout', this.save.bind(this));
this.inputElm.remove();
}
focus() {
this.inputElm.focus();
}
getValue() {
return this.inputElm.value;
}
setValue(val: string) {
this.inputElm.value = val;
}
loadValue(item: any) {
this.defaultValue = item[this.args.column.field] || '';
this.inputElm.value = this.defaultValue;
this.inputElm.defaultValue = this.defaultValue;
this.inputElm.select();
}
serializeValue() {
return this.inputElm.value;
}
applyValue(item: any, state: any) {
const validation = this.validate(state);
item[this.args.column.field] = validation && validation.valid ? state : '';
}
isValueChanged() {
const lastKeyEvent = this._lastInputEvent?.key;
if (this.columnEditor?.alwaysSaveOnEnterKey && lastKeyEvent === 'Enter') {
return true;
}
return !(this.inputElm.value === '' && this.defaultValue === null) && this.inputElm.value !== this.defaultValue;
}
save() {
const validation = this.validate();
if (validation?.valid) {
if (this.hasAutoCommitEdit) {
this.args.grid.getEditorLock().commitCurrentEdit();
} else {
this.args.commitChanges();
}
}
}
validate(inputValue?: any): EditorValidationResult {
if (this.validator) {
const value = inputValue !== undefined ? inputValue : this.inputElm?.value;
return this.validator(value, this.args);
}
return {
valid: true,
msg: null,
};
}
}