/
celleditor.ts
1622 lines (1424 loc) · 37.9 KB
/
celleditor.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2019, PhosphorJS Contributors
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
import { IDisposable } from '@lumino/disposable';
import { DataGrid } from './datagrid';
import { SelectionModel } from './selectionmodel';
import { getKeyboardLayout } from '@lumino/keyboard';
import { Signal } from '@lumino/signaling';
import { Notification } from './notification';
import { CellGroup } from './cellgroup';
/**
* A response object returned from cell input validator
*/
export interface ICellInputValidatorResponse {
/**
* Flag indicating cell input is valid or not
*/
valid: boolean;
/**
* Validation error message. Set only when input is invalid
*/
message?: string;
}
/**
* An object which validates cell input values.
*/
export interface ICellInputValidator {
/**
* Validate cell input.
*
* @param cell - The object holding cell configuration data.
*
* @param value - The cell value input.
*
* @returns An object with validation result.
*/
validate(
cell: CellEditor.CellConfig,
value: any
): ICellInputValidatorResponse;
}
/**
* An object returned from cell editor after a successful edit.
*/
export interface ICellEditResponse {
/**
* An object which holds the configuration data for a cell.
*/
cell: CellEditor.CellConfig;
/**
* Value input.
*/
value: any;
/**
* Cursor move direction based on keys pressed to end the edit.
*/
cursorMovement: SelectionModel.CursorMoveDirection;
}
/**
* An object implementing cell editing.
*/
export interface ICellEditor {
/**
* Start editing the cell.
*
* @param cell - The object holding cell configuration data.
*
* @param options - The cell editing options.
*/
edit(cell: CellEditor.CellConfig, options?: ICellEditOptions): void;
/**
* Cancel editing the cell.
*/
cancel(): void;
}
// default validation error message
const DEFAULT_INVALID_INPUT_MESSAGE = 'Invalid input!';
// A type alias for available cell data types
export type CellDataType =
| 'string'
| 'number'
| 'integer'
| 'boolean'
| 'date'
| 'string:option'
| 'number:option'
| 'integer:option'
| 'date:option'
| 'string:dynamic-option'
| 'number:dynamic-option'
| 'integer:dynamic-option'
| 'date:dynamic-option';
/**
* An object containing cell editing options.
*/
export interface ICellEditOptions {
/**
* Cell editor to use for editing.
*
* #### Notes
* This object is only used by cell editor controller.
* If not set, controller picks the most suitable editor
* for the particular cell configuration.
*/
editor?: ICellEditor;
/**
* Cell input validator to use for value validation.
*/
validator?: ICellInputValidator;
/**
* Callback method to call on cell edit commit.
*/
onCommit?: (response: ICellEditResponse) => void;
/**
* Callback method to call on cell edit cancel.
*/
onCancel?: () => void;
}
/**
* A cell input validator object which always returns valid.
*/
export class PassInputValidator implements ICellInputValidator {
/**
* Validate cell input.
*
* @param cell - The object holding cell configuration data.
*
* @param value - The cell value input.
*
* @returns An object with validation result.
*/
validate(
cell: CellEditor.CellConfig,
value: any
): ICellInputValidatorResponse {
return { valid: true };
}
}
/**
* Text cell input validator.
*/
export class TextInputValidator implements ICellInputValidator {
/**
* Validate cell input.
*
* @param cell - The object holding cell configuration data.
*
* @param value - The cell value input.
*
* @returns An object with validation result.
*/
validate(
cell: CellEditor.CellConfig,
value: string
): ICellInputValidatorResponse {
if (value === null) {
return { valid: true };
}
if (typeof value !== 'string') {
return {
valid: false,
message: 'Input must be valid text'
};
}
if (!isNaN(this.minLength) && value.length < this.minLength) {
return {
valid: false,
message: `Text length must be greater than ${this.minLength}`
};
}
if (!isNaN(this.maxLength) && value.length > this.maxLength) {
return {
valid: false,
message: `Text length must be less than ${this.maxLength}`
};
}
if (this.pattern && !this.pattern.test(value)) {
return {
valid: false,
message: `Text doesn't match the required pattern`
};
}
return { valid: true };
}
/**
* Minimum text length
*
* The default is Number.NaN, meaning no minimum constraint
*/
minLength: number = Number.NaN;
/**
* Maximum text length
*
* The default is Number.NaN, meaning no maximum constraint
*/
maxLength: number = Number.NaN;
/**
* Required text pattern as regular expression
*
* The default is null, meaning no pattern constraint
*/
pattern: RegExp | null = null;
}
/**
* Integer cell input validator.
*/
export class IntegerInputValidator implements ICellInputValidator {
/**
* Validate cell input.
*
* @param cell - The object holding cell configuration data.
*
* @param value - The cell value input.
*
* @returns An object with validation result.
*/
validate(
cell: CellEditor.CellConfig,
value: number
): ICellInputValidatorResponse {
if (value === null) {
return { valid: true };
}
if (isNaN(value) || value % 1 !== 0) {
return {
valid: false,
message: 'Input must be valid integer'
};
}
if (!isNaN(this.min) && value < this.min) {
return {
valid: false,
message: `Input must be greater than ${this.min}`
};
}
if (!isNaN(this.max) && value > this.max) {
return {
valid: false,
message: `Input must be less than ${this.max}`
};
}
return { valid: true };
}
/**
* Minimum value
*
* The default is Number.NaN, meaning no minimum constraint
*/
min: number = Number.NaN;
/**
* Maximum value
*
* The default is Number.NaN, meaning no maximum constraint
*/
max: number = Number.NaN;
}
/**
* Real number cell input validator.
*/
export class NumberInputValidator implements ICellInputValidator {
/**
* Validate cell input.
*
* @param cell - The object holding cell configuration data.
*
* @param value - The cell value input.
*
* @returns An object with validation result.
*/
validate(
cell: CellEditor.CellConfig,
value: number
): ICellInputValidatorResponse {
if (value === null) {
return { valid: true };
}
if (isNaN(value)) {
return {
valid: false,
message: 'Input must be valid number'
};
}
if (!isNaN(this.min) && value < this.min) {
return {
valid: false,
message: `Input must be greater than ${this.min}`
};
}
if (!isNaN(this.max) && value > this.max) {
return {
valid: false,
message: `Input must be less than ${this.max}`
};
}
return { valid: true };
}
/**
* Minimum value
*
* The default is Number.NaN, meaning no minimum constraint
*/
min: number = Number.NaN;
/**
* Maximum value
*
* The default is Number.NaN, meaning no maximum constraint
*/
max: number = Number.NaN;
}
/**
* An abstract base class that provides the most of the functionality
* needed by a cell editor. All of the built-in cell editors
* for various cell types are derived from this base class. Custom cell editors
* can be easily implemented by extending this class.
*/
export abstract class CellEditor implements ICellEditor, IDisposable {
/**
* Construct a new cell editor.
*/
constructor() {
this.inputChanged.connect(() => {
this.validate();
});
}
/**
* Whether the cell editor is disposed.
*/
get isDisposed(): boolean {
return this._disposed;
}
/**
* Dispose of the resources held by cell editor.
*/
dispose(): void {
if (this._disposed) {
return;
}
if (this._gridWheelEventHandler) {
this.cell.grid.node.removeEventListener(
'wheel',
this._gridWheelEventHandler
);
this._gridWheelEventHandler = null;
}
this._closeValidityNotification();
this._disposed = true;
this.cell.grid.node.removeChild(this.viewportOccluder);
}
/**
* Start editing the cell.
*
* @param cell - The object holding cell configuration data.
*
* @param options - The cell editing options.
*/
edit(cell: CellEditor.CellConfig, options?: ICellEditOptions): void {
this.cell = cell;
this.onCommit = options && options.onCommit;
this.onCancel = options && options.onCancel;
this.validator =
options && options.validator
? options.validator
: this.createValidatorBasedOnType();
this._gridWheelEventHandler = () => {
this._closeValidityNotification();
this.updatePosition();
};
cell.grid.node.addEventListener('wheel', this._gridWheelEventHandler);
this._addContainer();
this.updatePosition();
this.startEditing();
}
/**
* Cancel editing the cell.
*/
cancel() {
if (this._disposed) {
return;
}
this.dispose();
if (this.onCancel) {
this.onCancel();
}
}
/**
* Start editing the cell. Usually an editor widget is created and
* added to `editorContainer`
*/
protected abstract startEditing(): void;
/**
* Return the current input entered. This method throws exceptions
* if input is invalid. Error message in exception is shown as notification.
*/
protected abstract getInput(): any;
/**
* Whether the value input is valid.
*/
protected get validInput(): boolean {
return this._validInput;
}
/**
* Validate the cell input. Shows validation error notification when input is invalid.
*/
protected validate() {
let value;
try {
value = this.getInput();
} catch (error) {
console.log(`Input error: ${error.message}`);
this.setValidity(false, error.message || DEFAULT_INVALID_INPUT_MESSAGE);
return;
}
if (this.validator) {
const result = this.validator.validate(this.cell, value);
if (result.valid) {
this.setValidity(true);
} else {
this.setValidity(
false,
result.message || DEFAULT_INVALID_INPUT_MESSAGE
);
}
} else {
this.setValidity(true);
}
}
/**
* Set validity flag.
*
* @param valid - Whether the input is valid.
*
* @param message - Notification message to show.
*
* If message is set to empty string (which is the default)
* existing notification popup is removed if any.
*/
protected setValidity(valid: boolean, message: string = '') {
this._validInput = valid;
this._closeValidityNotification();
if (valid) {
this.editorContainer.classList.remove('lm-mod-invalid');
} else {
this.editorContainer.classList.add('lm-mod-invalid');
// show a notification popup
if (message !== '') {
this.validityNotification = new Notification({
target: this.editorContainer,
message: message,
placement: 'bottom',
timeout: 5000
});
this.validityNotification.show();
}
}
}
/**
* Create and return a cell input validator based on configuration of the
* cell being edited. If no suitable validator can be found, it returns undefined.
*/
protected createValidatorBasedOnType(): ICellInputValidator | undefined {
const cell = this.cell;
const metadata = cell.grid.dataModel!.metadata(
'body',
cell.row,
cell.column
);
switch (metadata && metadata.type) {
case 'string':
{
const validator = new TextInputValidator();
if (typeof metadata!.format === 'string') {
const format = metadata!.format;
switch (format) {
case 'email':
validator.pattern = new RegExp(
'^([a-z0-9_.-]+)@([da-z.-]+).([a-z.]{2,6})$'
);
break;
case 'uuid':
validator.pattern = new RegExp(
'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}'
);
break;
case 'uri':
// TODO
break;
case 'binary':
// TODO
break;
}
}
if (metadata!.constraint) {
if (metadata!.constraint.minLength !== undefined) {
validator.minLength = metadata!.constraint.minLength;
}
if (metadata!.constraint.maxLength !== undefined) {
validator.maxLength = metadata!.constraint.maxLength;
}
if (typeof metadata!.constraint.pattern === 'string') {
validator.pattern = new RegExp(metadata!.constraint.pattern);
}
}
return validator;
}
break;
case 'number':
{
const validator = new NumberInputValidator();
if (metadata!.constraint) {
if (metadata!.constraint.minimum !== undefined) {
validator.min = metadata!.constraint.minimum;
}
if (metadata!.constraint.maximum !== undefined) {
validator.max = metadata!.constraint.maximum;
}
}
return validator;
}
break;
case 'integer':
{
const validator = new IntegerInputValidator();
if (metadata!.constraint) {
if (metadata!.constraint.minimum !== undefined) {
validator.min = metadata!.constraint.minimum;
}
if (metadata!.constraint.maximum !== undefined) {
validator.max = metadata!.constraint.maximum;
}
}
return validator;
}
break;
}
return undefined;
}
/**
* Compute cell rectangle and return with other cell properties.
*/
protected getCellInfo(cell: CellEditor.CellConfig): Private.ICellInfo {
const { grid, row, column } = cell;
let data, columnX, rowY, width, height;
const cellGroup = CellGroup.getGroup(grid.dataModel!, 'body', row, column);
if (cellGroup) {
columnX =
grid.headerWidth -
grid.scrollX +
grid.columnOffset('body', cellGroup.c1);
rowY =
grid.headerHeight - grid.scrollY + grid.rowOffset('body', cellGroup.r1);
width = 0;
height = 0;
for (let r = cellGroup.r1; r <= cellGroup.r2; r++) {
height += grid.rowSize('body', r);
}
for (let c = cellGroup.c1; c <= cellGroup.c2; c++) {
width += grid.columnSize('body', c);
}
data = grid.dataModel!.data('body', cellGroup.r1, cellGroup.c1);
} else {
columnX =
grid.headerWidth - grid.scrollX + grid.columnOffset('body', column);
rowY = grid.headerHeight - grid.scrollY + grid.rowOffset('body', row);
width = grid.columnSize('body', column);
height = grid.rowSize('body', row);
data = grid.dataModel!.data('body', row, column);
}
return {
grid: grid,
row: row,
column: column,
data: data,
x: columnX,
y: rowY,
width: width,
height: height
};
}
/**
* Reposition cell editor by moving viewport occluder and cell editor container.
*/
protected updatePosition(): void {
const grid = this.cell.grid;
const cellInfo = this.getCellInfo(this.cell);
const headerHeight = grid.headerHeight;
const headerWidth = grid.headerWidth;
this.viewportOccluder.style.top = headerHeight + 'px';
this.viewportOccluder.style.left = headerWidth + 'px';
this.viewportOccluder.style.width = grid.viewportWidth - headerWidth + 'px';
this.viewportOccluder.style.height =
grid.viewportHeight - headerHeight + 'px';
this.viewportOccluder.style.position = 'absolute';
this.editorContainer.style.left = cellInfo.x - 1 - headerWidth + 'px';
this.editorContainer.style.top = cellInfo.y - 1 - headerHeight + 'px';
this.editorContainer.style.width = cellInfo.width + 1 + 'px';
this.editorContainer.style.height = cellInfo.height + 1 + 'px';
this.editorContainer.style.visibility = 'visible';
this.editorContainer.style.position = 'absolute';
}
/**
* Commit the edited value.
*
* @param cursorMovement - Cursor move direction based on keys pressed to end the edit.
*
* @returns true on valid input, false otherwise.
*/
protected commit(
cursorMovement: SelectionModel.CursorMoveDirection = 'none'
): boolean {
this.validate();
if (!this._validInput) {
return false;
}
let value;
try {
value = this.getInput();
} catch (error) {
console.log(`Input error: ${error.message}`);
return false;
}
this.dispose();
if (this.onCommit) {
this.onCommit({
cell: this.cell,
value: value,
cursorMovement: cursorMovement
});
}
return true;
}
/**
* Create container elements needed to prevent editor widget overflow
* beyond viewport and to position cell editor widget.
*/
private _addContainer() {
this.viewportOccluder = document.createElement('div');
this.viewportOccluder.className = 'lm-DataGrid-cellEditorOccluder';
this.cell.grid.node.appendChild(this.viewportOccluder);
this.editorContainer = document.createElement('div');
this.editorContainer.className = 'lm-DataGrid-cellEditorContainer';
this.viewportOccluder.appendChild(this.editorContainer);
// update mouse event pass-through state based on input validity
this.editorContainer.addEventListener('mouseleave', (event: MouseEvent) => {
this.viewportOccluder.style.pointerEvents = this._validInput
? 'none'
: 'auto';
});
this.editorContainer.addEventListener('mouseenter', (event: MouseEvent) => {
this.viewportOccluder.style.pointerEvents = 'none';
});
}
/**
* Remove validity notification popup.
*/
private _closeValidityNotification() {
if (this.validityNotification) {
this.validityNotification.close();
this.validityNotification = null;
}
}
/**
* A signal emitted when input changes.
*/
protected inputChanged = new Signal<this, void>(this);
/**
* Callback method to call on cell edit commit.
*/
protected onCommit?: (response: ICellEditResponse) => void;
/**
* Callback method to call on cell edit cancel.
*/
protected onCancel?: () => void;
/**
* Cell configuration data for the cell being edited.
*/
protected cell: CellEditor.CellConfig;
/**
* Cell input validator to use for the cell being edited.
*/
protected validator: ICellInputValidator | undefined;
/**
* The div element used to prevent editor widget overflow beyond grid viewport.
*/
protected viewportOccluder: HTMLDivElement;
/**
* The div element used to contain and position editor widget.
*/
protected editorContainer: HTMLDivElement;
/**
* Notification popup used to show validation error messages.
*/
protected validityNotification: Notification | null = null;
/**
* Whether the cell editor is disposed.
*/
private _disposed = false;
/**
* Whether the value input is valid.
*/
private _validInput: boolean = true;
/**
* Grid wheel event handler.
*/
private _gridWheelEventHandler:
| ((this: HTMLElement, ev: WheelEvent) => any)
| null = null;
}
/**
* Abstract base class with shared functionality
* for cell editors which use HTML Input widget as editor.
*/
export abstract class InputCellEditor extends CellEditor {
/**
* Handle the DOM events for the editor.
*
* @param event - The DOM event sent to the editor.
*/
handleEvent(event: Event): void {
switch (event.type) {
case 'keydown':
this._onKeyDown(event as KeyboardEvent);
break;
case 'blur':
this._onBlur(event as FocusEvent);
break;
case 'input':
this._onInput(event);
break;
}
}
/**
* Dispose of the resources held by cell editor.
*/
dispose() {
if (this.isDisposed) {
return;
}
this._unbindEvents();
super.dispose();
}
/**
* Start editing the cell.
*/
protected startEditing() {
this.createWidget();
const cell = this.cell;
const cellInfo = this.getCellInfo(cell);
this.input.value = this.deserialize(cellInfo.data);
this.editorContainer.appendChild(this.input);
this.input.focus();
this.input.select();
this.bindEvents();
}
protected deserialize(value: any): any {
if (value === null || value === undefined) {
return '';
}
return value.toString();
}
protected createWidget() {
const input = document.createElement('input');
input.classList.add('lm-DataGrid-cellEditorWidget');
input.classList.add('lm-DataGrid-cellEditorInput');
input.spellcheck = false;
input.type = this.inputType;
this.input = input;
}
protected bindEvents() {
this.input.addEventListener('keydown', this);
this.input.addEventListener('blur', this);
this.input.addEventListener('input', this);
}
private _unbindEvents() {
this.input.removeEventListener('keydown', this);
this.input.removeEventListener('blur', this);
this.input.removeEventListener('input', this);
}
private _onKeyDown(event: KeyboardEvent) {
switch (getKeyboardLayout().keyForKeydownEvent(event)) {
case 'Enter':
this.commit(event.shiftKey ? 'up' : 'down');
break;
case 'Tab':
this.commit(event.shiftKey ? 'left' : 'right');
event.stopPropagation();
event.preventDefault();
break;
case 'Escape':
this.cancel();
break;
default:
break;
}
}
private _onBlur(event: FocusEvent) {
if (this.isDisposed) {
return;
}
if (!this.commit()) {
event.preventDefault();
event.stopPropagation();
this.input.focus();
}
}
private _onInput(event: Event) {
this.inputChanged.emit(void 0);
}
protected input: HTMLInputElement;
protected abstract inputType: string;
}
/**
* Cell editor for text cells.
*/
export class TextCellEditor extends InputCellEditor {
/**
* Return the current text input entered.
*/
protected getInput(): string | null {
return this.input.value;
}
protected inputType: string = 'text';
}
/**
* Cell editor for real number cells.
*/
export class NumberCellEditor extends InputCellEditor {
/**
* Start editing the cell.
*/
protected startEditing() {
super.startEditing();
this.input.step = 'any';
const cell = this.cell;
const metadata = cell.grid.dataModel!.metadata(
'body',
cell.row,
cell.column
);
const constraint = metadata.constraint;
if (constraint) {
if (constraint.minimum) {
this.input.min = constraint.minimum;
}
if (constraint.maximum) {
this.input.max = constraint.maximum;
}
}
}
/**
* Return the current number input entered. This method throws exception
* if input is invalid.
*/
protected getInput(): number | null {
let value = this.input.value;
if (value.trim() === '') {
return null;
}
const floatValue = parseFloat(value);
if (isNaN(floatValue)) {
throw new Error('Invalid input');
}
return floatValue;
}
protected inputType: string = 'number';
}
/**
* Cell editor for integer cells.
*/
export class IntegerCellEditor extends InputCellEditor {
/**
* Start editing the cell.
*/
protected startEditing() {
super.startEditing();
this.input.step = '1';
const cell = this.cell;
const metadata = cell.grid.dataModel!.metadata(
'body',
cell.row,
cell.column
);
const constraint = metadata.constraint;
if (constraint) {
if (constraint.minimum) {
this.input.min = constraint.minimum;