@adaptabletools/adaptable 13.0.0-canary.11 → 13.0.0-canary.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,82 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AdaptableNumberEditor = void 0;
3
+ exports.AdaptableNumberEditor = exports.ReactAdaptableNumberEditor = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const React = tslib_1.__importStar(require("react"));
6
6
  const renderWithAdaptableContext_1 = require("../../../View/renderWithAdaptableContext");
7
7
  const InternalAdaptableNumberEditor_1 = require("./InternalAdaptableNumberEditor");
8
8
  const core_1 = require("@ag-grid-community/core");
9
+ const react_1 = require("react");
10
+ function shouldClearExistingValue(params) {
11
+ return params.eventKey === core_1.KeyCode.BACKSPACE || params.eventKey === core_1.KeyCode.DELETE;
12
+ }
13
+ function isValidChar(char) {
14
+ // allow only digits
15
+ return ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].includes(char);
16
+ // we do NOT allow key shortcuts for starting non-digits (minus sign, decimal separators) as the input[number] handling is very buggy and we would open a can of worms
17
+ }
18
+ function getStartValue(params) {
19
+ if (shouldClearExistingValue(params)) {
20
+ return '';
21
+ }
22
+ if (params.charPress && isValidChar(params.charPress)) {
23
+ return params.charPress;
24
+ }
25
+ return params.value;
26
+ }
27
+ const defaultValueParser = ({ newValue, oldValue: _ }) => {
28
+ return newValue;
29
+ };
30
+ const style = {
31
+ position: 'absolute',
32
+ top: '0px',
33
+ left: '0px',
34
+ right: '0px',
35
+ bottom: '0px',
36
+ };
37
+ exports.ReactAdaptableNumberEditor = (0, react_1.forwardRef)((props, ref) => {
38
+ var _a, _b;
39
+ const [initialValue] = (0, react_1.useState)(() => getStartValue(props));
40
+ const valueRef = (0, react_1.useRef)(initialValue);
41
+ const columnId = props.column.getColId();
42
+ const adaptable = props.api.__adaptable;
43
+ const colValueParser = props.column.getColDef().valueParser;
44
+ const valueParser = typeof colValueParser === 'function' ? colValueParser : defaultValueParser;
45
+ function onValueChange(value) {
46
+ value = valueParser
47
+ ? valueParser(Object.assign(Object.assign({}, props), { oldValue: props.value, newValue: value }))
48
+ : value;
49
+ valueRef.current = value;
50
+ }
51
+ const editorRef = (0, react_1.useRef)(null);
52
+ (0, react_1.useImperativeHandle)(ref, () => {
53
+ return {
54
+ focusIn() {
55
+ var _a;
56
+ (_a = editorRef.current) === null || _a === void 0 ? void 0 : _a.focus();
57
+ },
58
+ // the final value to send to the grid, on completion of editing
59
+ getValue() {
60
+ return valueRef.current;
61
+ },
62
+ };
63
+ });
64
+ const editorElement = (React.createElement(InternalAdaptableNumberEditor_1.InternalAdaptableNumberEditor, { defaultValue: initialValue, showClearButton: (_a = props.showClearButton) !== null && _a !== void 0 ? _a : true, emptyValue: (_b = props.emptyValue) !== null && _b !== void 0 ? _b : '', onValueChange: onValueChange, ref: (editor) => {
65
+ editorRef.current = editor;
66
+ editor === null || editor === void 0 ? void 0 : editor.focus();
67
+ } }));
68
+ function onKeyDown(keyDownEvent) {
69
+ adaptable._emit('CellEditorKeyDown', {
70
+ keyDownEvent,
71
+ cellValue: valueRef.current,
72
+ columnId,
73
+ updateValueCallback: (updatedValue) => {
74
+ editorRef.current.setValue(updatedValue);
75
+ },
76
+ });
77
+ }
78
+ return (React.createElement("div", { style: style, onKeyDown: onKeyDown }, (0, renderWithAdaptableContext_1.renderWithAdaptableContext)(editorElement, adaptable)));
79
+ });
9
80
  /**
10
81
  * Used by default for all `abColDefNumber` columns.
11
82
  *
@@ -29,9 +100,7 @@ const core_1 = require("@ag-grid-community/core");
29
100
  */
30
101
  class AdaptableNumberEditor {
31
102
  constructor() {
32
- this.valueParser = ({ newValue, oldValue: _ }) => {
33
- return newValue;
34
- };
103
+ this.valueParser = defaultValueParser;
35
104
  this.onValueChange = (value) => {
36
105
  this.value = this.valueParser
37
106
  ? this.valueParser(Object.assign(Object.assign({}, this.params), { oldValue: this.params.value, newValue: value }))
@@ -39,7 +108,7 @@ class AdaptableNumberEditor {
39
108
  };
40
109
  }
41
110
  init(params) {
42
- this.value = this.getStartValue(params);
111
+ this.value = getStartValue(params);
43
112
  this.params = params;
44
113
  this.columnId = params.column.getColId();
45
114
  const { valueParser } = params.column.getColDef();
@@ -47,11 +116,10 @@ class AdaptableNumberEditor {
47
116
  this.valueParser = valueParser;
48
117
  }
49
118
  this.el = document.createElement('div');
50
- this.el.style.position = 'absolute';
51
- this.el.style.top = '0px';
52
- this.el.style.left = '0px';
53
- this.el.style.right = '0px';
54
- this.el.style.bottom = '0px';
119
+ Object.keys(style).forEach((key) => {
120
+ //@ts-ignore
121
+ this.el.style[key] = style[key];
122
+ });
55
123
  }
56
124
  /* Component Editor Lifecycle methods */
57
125
  // gets called once when grid ready to insert the element
@@ -92,22 +160,5 @@ class AdaptableNumberEditor {
92
160
  var _a;
93
161
  (_a = this.unmountReactRoot) === null || _a === void 0 ? void 0 : _a.call(this);
94
162
  }
95
- getStartValue(params) {
96
- if (this.shouldClearExistingValue(params)) {
97
- return '';
98
- }
99
- if (params.charPress && this.isValidChar(params.charPress)) {
100
- return params.charPress;
101
- }
102
- return params.value;
103
- }
104
- shouldClearExistingValue(params) {
105
- return params.eventKey === core_1.KeyCode.BACKSPACE || params.eventKey === core_1.KeyCode.DELETE;
106
- }
107
- isValidChar(char) {
108
- // allow only digits
109
- return ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].includes(char);
110
- // we do NOT allow key shortcuts for starting non-digits (minus sign, decimal separators) as the input[number] handling is very buggy and we would open a can of worms
111
- }
112
163
  }
113
164
  exports.AdaptableNumberEditor = AdaptableNumberEditor;