cx 25.6.2 → 25.6.3

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.
Files changed (103) hide show
  1. package/dist/manifest.js +749 -749
  2. package/dist/widgets.css +5 -0
  3. package/dist/widgets.js +77 -34
  4. package/package.json +1 -1
  5. package/src/charts/Legend.d.ts +45 -45
  6. package/src/charts/LegendEntry.js +128 -128
  7. package/src/charts/LegendEntry.scss +27 -27
  8. package/src/charts/PieChart.d.ts +92 -92
  9. package/src/charts/RangeMarker.js +159 -159
  10. package/src/charts/axis/Axis.d.ts +113 -113
  11. package/src/charts/axis/Axis.js +280 -280
  12. package/src/charts/axis/CategoryAxis.d.ts +30 -30
  13. package/src/charts/axis/CategoryAxis.js +241 -241
  14. package/src/charts/axis/NumericAxis.js +351 -351
  15. package/src/charts/axis/Stack.js +55 -55
  16. package/src/charts/axis/TimeAxis.js +611 -611
  17. package/src/charts/helpers/SnapPointFinder.js +69 -69
  18. package/src/data/Binding.spec.js +69 -69
  19. package/src/data/ExposedValueView.d.ts +19 -19
  20. package/src/data/Expression.js +229 -229
  21. package/src/data/Expression.spec.js +229 -229
  22. package/src/data/StringTemplate.js +92 -92
  23. package/src/data/StringTemplate.spec.js +132 -132
  24. package/src/data/StructuredSelector.js +132 -132
  25. package/src/data/getAccessor.spec.js +11 -11
  26. package/src/data/getSelector.js +49 -49
  27. package/src/hooks/createLocalStorageRef.d.ts +3 -3
  28. package/src/hooks/createLocalStorageRef.js +20 -20
  29. package/src/index.scss +6 -6
  30. package/src/ui/Culture.d.ts +57 -57
  31. package/src/ui/Culture.js +139 -139
  32. package/src/ui/FocusManager.js +171 -171
  33. package/src/ui/Format.js +108 -108
  34. package/src/ui/HoverSync.js +147 -147
  35. package/src/ui/Repeater.d.ts +61 -61
  36. package/src/ui/index.d.ts +42 -42
  37. package/src/ui/layout/ContentPlaceholder.d.ts +19 -19
  38. package/src/ui/layout/ContentPlaceholder.js +105 -105
  39. package/src/ui/layout/ContentPlaceholder.spec.js +579 -579
  40. package/src/ui/layout/LabelsTopLayout.js +134 -134
  41. package/src/util/Format.js +270 -270
  42. package/src/util/date/encodeDate.d.ts +1 -1
  43. package/src/util/date/encodeDate.js +8 -8
  44. package/src/util/date/encodeDateWithTimezoneOffset.d.ts +1 -1
  45. package/src/util/date/index.d.ts +11 -11
  46. package/src/util/date/index.js +11 -11
  47. package/src/util/date/parseDateInvariant.d.ts +3 -3
  48. package/src/util/date/parseDateInvariant.js +20 -20
  49. package/src/util/getSearchQueryPredicate.js +59 -59
  50. package/src/util/index.d.ts +51 -51
  51. package/src/util/index.js +54 -54
  52. package/src/util/isValidIdentifierName.d.ts +1 -1
  53. package/src/util/isValidIdentifierName.js +5 -5
  54. package/src/util/isValidIdentifierName.spec.js +33 -33
  55. package/src/util/scss/add-rules.scss +38 -38
  56. package/src/widgets/CxCredit.scss +37 -37
  57. package/src/widgets/HighlightedSearchText.js +36 -36
  58. package/src/widgets/HighlightedSearchText.scss +18 -18
  59. package/src/widgets/List.scss +91 -91
  60. package/src/widgets/Sandbox.d.ts +18 -16
  61. package/src/widgets/Sandbox.js +65 -63
  62. package/src/widgets/drag-drop/DropZone.js +214 -214
  63. package/src/widgets/form/Calendar.js +618 -618
  64. package/src/widgets/form/Calendar.scss +196 -196
  65. package/src/widgets/form/Checkbox.scss +127 -127
  66. package/src/widgets/form/ColorField.js +397 -397
  67. package/src/widgets/form/ColorField.scss +96 -96
  68. package/src/widgets/form/ColorPicker.scss +283 -283
  69. package/src/widgets/form/DateTimeField.js +576 -576
  70. package/src/widgets/form/DateTimePicker.js +392 -392
  71. package/src/widgets/form/LookupField.d.ts +179 -179
  72. package/src/widgets/form/LookupField.scss +219 -219
  73. package/src/widgets/form/MonthPicker.d.ts +8 -0
  74. package/src/widgets/form/MonthPicker.js +65 -23
  75. package/src/widgets/form/MonthPicker.scss +4 -0
  76. package/src/widgets/form/NumberField.js +459 -459
  77. package/src/widgets/form/NumberField.scss +61 -61
  78. package/src/widgets/form/Radio.scss +121 -121
  79. package/src/widgets/form/Select.scss +99 -99
  80. package/src/widgets/form/Slider.scss +118 -118
  81. package/src/widgets/form/Switch.scss +140 -140
  82. package/src/widgets/form/TextArea.scss +43 -43
  83. package/src/widgets/form/TextField.js +290 -290
  84. package/src/widgets/form/TextField.scss +55 -55
  85. package/src/widgets/form/UploadButton.d.ts +34 -34
  86. package/src/widgets/form/variables.scss +353 -353
  87. package/src/widgets/grid/Grid.d.ts +442 -442
  88. package/src/widgets/grid/GridRow.js +228 -228
  89. package/src/widgets/grid/TreeNode.d.ts +23 -23
  90. package/src/widgets/grid/TreeNode.scss +88 -88
  91. package/src/widgets/grid/variables.scss +133 -133
  92. package/src/widgets/nav/LinkButton.js +128 -128
  93. package/src/widgets/nav/Menu.scss +74 -74
  94. package/src/widgets/overlay/Dropdown.js +612 -612
  95. package/src/widgets/overlay/FlyweightTooltipTracker.js +39 -39
  96. package/src/widgets/overlay/Overlay.d.ts +73 -73
  97. package/src/widgets/overlay/Window.js +202 -202
  98. package/src/widgets/overlay/captureMouse.js +124 -124
  99. package/src/widgets/overlay/createHotPromiseWindowFactory.d.ts +18 -18
  100. package/src/widgets/overlay/createHotPromiseWindowFactory.js +56 -56
  101. package/src/widgets/overlay/index.d.ts +11 -11
  102. package/src/widgets/overlay/index.js +11 -11
  103. package/src/widgets/variables.scss +144 -144
@@ -1,459 +1,459 @@
1
- import { Widget, VDOM, getContent } from "../../ui/Widget";
2
- import { Field, getFieldTooltip } from "./Field";
3
- import { Format } from "../../ui/Format";
4
- import { Culture } from "../../ui/Culture";
5
- import { StringTemplate } from "../../data/StringTemplate";
6
- import {
7
- tooltipParentWillReceiveProps,
8
- tooltipParentWillUnmount,
9
- tooltipMouseMove,
10
- tooltipMouseLeave,
11
- tooltipParentDidMount,
12
- } from "../overlay/tooltip-ops";
13
- import { stopPropagation } from "../../util/eventCallbacks";
14
- import { Localization } from "../../ui/Localization";
15
- import ClearIcon from "../icons/clear";
16
- import { isString } from "../../util/isString";
17
- import { isNumber } from "../../util/isNumber";
18
- import { isDefined } from "../../util/isDefined";
19
- import { getActiveElement } from "../../util/getActiveElement";
20
-
21
- import { enableCultureSensitiveFormatting } from "../../ui/Format";
22
- import { KeyCode } from "../../util/KeyCode";
23
- import { autoFocus } from "../autoFocus";
24
-
25
- enableCultureSensitiveFormatting();
26
-
27
- export class NumberField extends Field {
28
- declareData() {
29
- super.declareData(
30
- {
31
- value: this.emptyValue,
32
- disabled: undefined,
33
- readOnly: undefined,
34
- enabled: undefined,
35
- placeholder: undefined,
36
- required: undefined,
37
- format: undefined,
38
- minValue: undefined,
39
- maxValue: undefined,
40
- constrain: undefined,
41
- minExclusive: undefined,
42
- maxExclusive: undefined,
43
- incrementPercentage: undefined,
44
- increment: undefined,
45
- icon: undefined,
46
- scale: undefined,
47
- offset: undefined,
48
- },
49
- ...arguments,
50
- );
51
- }
52
-
53
- init() {
54
- if (isDefined(this.step)) this.increment = this.step;
55
-
56
- if (isDefined(this.hideClear)) this.showClear = !this.hideClear;
57
-
58
- if (this.alwaysShowClear) this.showClear = true;
59
-
60
- super.init();
61
- }
62
-
63
- prepareData(context, instance) {
64
- let { data, state, cached } = instance;
65
- data.formatted = Format.value(data.value, data.format);
66
-
67
- if (!cached.data || data.value != cached.data.value) state.empty = data.value == null;
68
-
69
- super.prepareData(context, instance);
70
- }
71
-
72
- formatValue(context, { data }) {
73
- return data.formatted;
74
- }
75
-
76
- parseValue(value, instance) {
77
- if (this.onParseInput) {
78
- let result = instance.invoke("onParseInput", value, instance);
79
- if (result !== undefined) return result;
80
- }
81
- return Culture.getNumberCulture().parse(value);
82
- }
83
-
84
- validate(context, instance) {
85
- super.validate(context, instance);
86
-
87
- let { data } = instance;
88
- if (isNumber(data.value) && !data.error) {
89
- if (isNumber(data.minValue)) {
90
- if (data.value < data.minValue)
91
- data.error = StringTemplate.format(this.minValueErrorText, Format.value(data.minValue, data.format));
92
- else if (data.value == data.minValue && data.minExclusive)
93
- data.error = StringTemplate.format(this.minExclusiveErrorText, Format.value(data.minValue, data.format));
94
- }
95
-
96
- if (isNumber(data.maxValue)) {
97
- if (data.value > data.maxValue)
98
- data.error = StringTemplate.format(this.maxValueErrorText, Format.value(data.maxValue, data.format));
99
- else if (data.value == data.maxValue && data.maxExclusive)
100
- data.error = StringTemplate.format(this.maxExclusiveErrorText, Format.value(data.maxValue, data.format));
101
- }
102
- }
103
- }
104
-
105
- renderInput(context, instance, key) {
106
- return (
107
- <Input
108
- key={key}
109
- data={instance.data}
110
- instance={instance}
111
- label={this.labelPlacement && getContent(this.renderLabel(context, instance, "label"))}
112
- help={this.helpPlacement && getContent(this.renderHelp(context, instance, "help"))}
113
- icon={this.renderIcon(context, instance, "icon")}
114
- />
115
- );
116
- }
117
-
118
- validateRequired(context, instance) {
119
- return instance.state.empty && this.requiredText;
120
- }
121
- }
122
-
123
- NumberField.prototype.baseClass = "numberfield";
124
- NumberField.prototype.reactOn = "enter blur";
125
- NumberField.prototype.format = "n";
126
- NumberField.prototype.inputType = "text";
127
-
128
- NumberField.prototype.maxValueErrorText = "Enter {0} or less.";
129
- NumberField.prototype.maxExclusiveErrorText = "Enter a number less than {0}.";
130
- NumberField.prototype.minValueErrorText = "Enter {0} or more.";
131
- NumberField.prototype.minExclusiveErrorText = "Enter a number greater than {0}.";
132
- NumberField.prototype.inputErrorText = "Invalid number entered.";
133
- NumberField.prototype.suppressErrorsUntilVisited = true;
134
-
135
- NumberField.prototype.incrementPercentage = 0.1;
136
- NumberField.prototype.scale = 1;
137
- NumberField.prototype.offset = 0;
138
- NumberField.prototype.snapToIncrement = true;
139
- NumberField.prototype.icon = null;
140
- NumberField.prototype.showClear = false;
141
- NumberField.prototype.alwaysShowClear = false;
142
-
143
- Widget.alias("numberfield", NumberField);
144
- Localization.registerPrototype("cx/widgets/NumberField", NumberField);
145
-
146
- class Input extends VDOM.Component {
147
- constructor(props) {
148
- super(props);
149
- this.state = {
150
- focus: false,
151
- };
152
- }
153
-
154
- render() {
155
- let { data, instance, label, help, icon: iconVDOM } = this.props;
156
- let { widget, state } = instance;
157
- let { CSS, baseClass, suppressErrorsUntilVisited } = widget;
158
-
159
- let icon = iconVDOM && <div className={CSS.element(baseClass, "left-icon")}>{iconVDOM}</div>;
160
-
161
- let insideButton;
162
- if (!data.readOnly && !data.disabled) {
163
- if (
164
- widget.showClear &&
165
- (((widget.alwaysShowClear || !data.required) && !data.empty) || instance.state.inputError)
166
- )
167
- insideButton = (
168
- <div
169
- className={CSS.element(baseClass, "clear")}
170
- onMouseDown={(e) => e.preventDefault()}
171
- onClick={(e) => this.onClearClick(e)}
172
- >
173
- <ClearIcon className={CSS.element(baseClass, "icon")} />
174
- </div>
175
- );
176
- }
177
-
178
- let empty = this.input ? !this.input.value : data.empty;
179
-
180
- return (
181
- <div
182
- className={CSS.expand(
183
- data.classNames,
184
- CSS.state({
185
- visited: state.visited,
186
- focus: this.state.focus,
187
- icon: !!icon,
188
- empty: empty && !data.placeholder,
189
- error: data.error && (state.visited || !suppressErrorsUntilVisited || !empty),
190
- }),
191
- )}
192
- style={data.style}
193
- onMouseDown={stopPropagation}
194
- onTouchStart={stopPropagation}
195
- >
196
- <input
197
- id={data.id}
198
- type={widget.inputType}
199
- className={CSS.expand(CSS.element(baseClass, "input"), data.inputClass)}
200
- defaultValue={data.formatted}
201
- ref={(el) => {
202
- this.input = el;
203
- }}
204
- style={data.inputStyle}
205
- disabled={data.disabled}
206
- readOnly={data.readOnly}
207
- tabIndex={data.tabIndex}
208
- placeholder={data.placeholder}
209
- {...data.inputAttrs}
210
- onMouseMove={(e) => tooltipMouseMove(e, ...getFieldTooltip(this.props.instance))}
211
- onMouseLeave={(e) => tooltipMouseLeave(e, ...getFieldTooltip(this.props.instance))}
212
- onChange={(e) => this.onChange(e, "change")}
213
- onKeyDown={this.onKeyDown.bind(this)}
214
- onBlur={(e) => {
215
- this.onChange(e, "blur");
216
- }}
217
- onFocus={(e) => this.onFocus()}
218
- onWheel={(e) => {
219
- this.onChange(e, "wheel");
220
- }}
221
- onClick={stopPropagation}
222
- />
223
- {insideButton}
224
- {icon}
225
- {label}
226
- {help}
227
- </div>
228
- );
229
- }
230
-
231
- UNSAFE_componentWillReceiveProps(props) {
232
- let { data, state } = props.instance;
233
- if (this.props.data.formatted != data.formatted && !state.inputError) {
234
- this.input.value = props.data.formatted || "";
235
- props.instance.setState({
236
- inputError: false,
237
- });
238
- }
239
- tooltipParentWillReceiveProps(this.input, ...getFieldTooltip(props.instance));
240
- }
241
-
242
- componentDidMount() {
243
- tooltipParentDidMount(this.input, ...getFieldTooltip(this.props.instance));
244
- autoFocus(this.input, this);
245
- }
246
-
247
- componentDidUpdate() {
248
- autoFocus(this.input, this);
249
- }
250
-
251
- componentWillUnmount() {
252
- if (this.input == getActiveElement() && this.input.value != this.props.data.formatted) {
253
- this.onChange({ target: { value: this.input.value } }, "blur");
254
- }
255
- tooltipParentWillUnmount(this.props.instance);
256
- }
257
-
258
- getPreCursorDigits(text, cursor, decimalSeparator) {
259
- let res = "";
260
- for (let i = 0; i < cursor; i++) {
261
- if ("0" <= text[i] && text[i] <= "9") res += text[i];
262
- else if (text[i] == decimalSeparator) res += ".";
263
- else if (text[i] == "-") res += "-";
264
- }
265
- return res;
266
- }
267
-
268
- getLengthWithoutSuffix(text, decimalSeparator) {
269
- let l = text.length;
270
- while (l > 0) {
271
- if ("0" <= text[l - 1] && text[l - 1] <= "9") break;
272
- if (text[l - 1] == decimalSeparator) break;
273
- l--;
274
- }
275
- return l;
276
- }
277
-
278
- getDecimalSeparator(format) {
279
- let text = Format.value(0.11111111, format);
280
- for (let i = text.length - 1; i >= 0; i--) {
281
- if ("0" <= text[i] && text[i] <= "9") continue;
282
- if (text[i] == "," || text[i] == ".") return text[i];
283
- break;
284
- }
285
- return null;
286
- }
287
-
288
- updateCursorPosition(preCursorText) {
289
- if (isString(preCursorText)) {
290
- let cursor = 0;
291
- let preCursor = 0;
292
- let text = this.input.value || "";
293
- while (preCursor < preCursorText.length && cursor < text.length) {
294
- if (text[cursor] == preCursorText[preCursor]) {
295
- cursor++;
296
- preCursor++;
297
- } else {
298
- cursor++;
299
- }
300
- }
301
- this.input.setSelectionRange(cursor, cursor);
302
- }
303
- }
304
-
305
- calculateIncrement(value, strength) {
306
- if (value == 0) return 0.1;
307
- let absValue = Math.abs(value * strength);
308
- let log10 = Math.floor(Math.log10(absValue) + 0.001);
309
- let size = Math.pow(10, log10);
310
- if (absValue / size > 4.999) return 5 * size;
311
- if (absValue / size > 1.999) return 2 * size;
312
- return size;
313
- }
314
-
315
- onClearClick(e) {
316
- this.input.value = "";
317
- let { instance } = this.props;
318
- instance.set("value", instance.widget.emptyValue, { immediate: true });
319
- }
320
-
321
- onKeyDown(e) {
322
- let { instance } = this.props;
323
- if (instance.widget.handleKeyDown(e, instance) === false) return;
324
-
325
- switch (e.keyCode) {
326
- case KeyCode.enter:
327
- this.onChange(e, "enter");
328
- break;
329
-
330
- case KeyCode.left:
331
- case KeyCode.right:
332
- e.stopPropagation();
333
- break;
334
- }
335
- }
336
-
337
- onChange(e, change) {
338
- let { instance, data } = this.props;
339
- let { widget } = instance;
340
-
341
- if (data.required) {
342
- instance.setState({
343
- empty: !e.target.value,
344
- });
345
- }
346
-
347
- if (change == "blur" && this.state.focus) {
348
- this.setState({
349
- focus: false,
350
- });
351
- }
352
-
353
- let immediate = change == "blur" || change == "enter";
354
-
355
- if ((widget.reactOn.indexOf(change) == -1 && !immediate) || data.disabled || data.readOnly) return;
356
-
357
- if (immediate) instance.setState({ visited: true });
358
-
359
- let value = null;
360
-
361
- if (e.target.value) {
362
- let displayValue = widget.parseValue(e.target.value, instance);
363
- if (isNaN(displayValue)) {
364
- instance.setState({
365
- inputError: instance.widget.inputErrorText,
366
- });
367
- return;
368
- }
369
-
370
- value = displayValue * data.scale + data.offset;
371
-
372
- if (change == "wheel") {
373
- e.preventDefault();
374
- let increment =
375
- data.increment != null ? data.increment : this.calculateIncrement(value, data.incrementPercentage);
376
- value = value + (e.deltaY < 0 ? increment : -increment);
377
- if (widget.snapToIncrement) {
378
- value = Math.round(value / increment) * increment;
379
- }
380
-
381
- if (data.minValue != null) {
382
- if (data.minExclusive) {
383
- if (value <= data.minValue) return;
384
- } else {
385
- value = Math.max(value, data.minValue);
386
- }
387
- }
388
-
389
- if (data.maxValue != null) {
390
- if (data.maxExclusive) {
391
- if (value >= data.maxValue) return;
392
- } else {
393
- value = Math.min(value, data.maxValue);
394
- }
395
- }
396
- }
397
-
398
- if (data.constrain) {
399
- if (data.minValue != null) {
400
- if (value < data.minValue) {
401
- value = data.minValue;
402
- }
403
- }
404
-
405
- if (data.maxValue != null) {
406
- if (value > data.maxValue) {
407
- value = data.maxValue;
408
- }
409
- }
410
- }
411
-
412
- let fmt = data.format;
413
- let decimalSeparator = this.getDecimalSeparator(fmt) || Format.value(1.1, "n;1")[1];
414
-
415
- let formatted = Format.value(value, fmt);
416
- // Re-parse to avoid differences between formatted value and value in the store
417
-
418
- value = widget.parseValue(formatted, instance) * data.scale + data.offset;
419
-
420
- // Allow users to type numbers like 100.0003 or -0.05 without interruptions
421
- // If the last typed character is zero or dot (decimal separator), skip processing it
422
- if (
423
- change == "change" &&
424
- this.input.selectionStart == this.input.selectionEnd &&
425
- this.input.selectionEnd >= this.getLengthWithoutSuffix(this.input.value, decimalSeparator) &&
426
- (e.target.value[this.input.selectionEnd - 1] == decimalSeparator ||
427
- (e.target.value.indexOf(decimalSeparator) >= 0 && e.target.value[this.input.selectionEnd - 1] == "0") ||
428
- (this.input.selectionEnd == 2 && e.target.value[0] === "-" && e.target.value[1] === "0"))
429
- )
430
- return;
431
-
432
- if (change != "blur") {
433
- // Format, but keep the correct cursor position
434
- let preCursorText = this.getPreCursorDigits(this.input.value, this.input.selectionStart, decimalSeparator);
435
- this.input.value = formatted;
436
- this.updateCursorPosition(preCursorText);
437
- } else {
438
- this.input.value = formatted;
439
- }
440
- }
441
-
442
- instance.set("value", value, { immediate });
443
-
444
- instance.setState({
445
- inputError: false,
446
- visited: true,
447
- });
448
- }
449
-
450
- onFocus() {
451
- let { instance } = this.props;
452
- let { widget } = instance;
453
- if (widget.trackFocus) {
454
- this.setState({
455
- focus: true,
456
- });
457
- }
458
- }
459
- }
1
+ import { Widget, VDOM, getContent } from "../../ui/Widget";
2
+ import { Field, getFieldTooltip } from "./Field";
3
+ import { Format } from "../../ui/Format";
4
+ import { Culture } from "../../ui/Culture";
5
+ import { StringTemplate } from "../../data/StringTemplate";
6
+ import {
7
+ tooltipParentWillReceiveProps,
8
+ tooltipParentWillUnmount,
9
+ tooltipMouseMove,
10
+ tooltipMouseLeave,
11
+ tooltipParentDidMount,
12
+ } from "../overlay/tooltip-ops";
13
+ import { stopPropagation } from "../../util/eventCallbacks";
14
+ import { Localization } from "../../ui/Localization";
15
+ import ClearIcon from "../icons/clear";
16
+ import { isString } from "../../util/isString";
17
+ import { isNumber } from "../../util/isNumber";
18
+ import { isDefined } from "../../util/isDefined";
19
+ import { getActiveElement } from "../../util/getActiveElement";
20
+
21
+ import { enableCultureSensitiveFormatting } from "../../ui/Format";
22
+ import { KeyCode } from "../../util/KeyCode";
23
+ import { autoFocus } from "../autoFocus";
24
+
25
+ enableCultureSensitiveFormatting();
26
+
27
+ export class NumberField extends Field {
28
+ declareData() {
29
+ super.declareData(
30
+ {
31
+ value: this.emptyValue,
32
+ disabled: undefined,
33
+ readOnly: undefined,
34
+ enabled: undefined,
35
+ placeholder: undefined,
36
+ required: undefined,
37
+ format: undefined,
38
+ minValue: undefined,
39
+ maxValue: undefined,
40
+ constrain: undefined,
41
+ minExclusive: undefined,
42
+ maxExclusive: undefined,
43
+ incrementPercentage: undefined,
44
+ increment: undefined,
45
+ icon: undefined,
46
+ scale: undefined,
47
+ offset: undefined,
48
+ },
49
+ ...arguments,
50
+ );
51
+ }
52
+
53
+ init() {
54
+ if (isDefined(this.step)) this.increment = this.step;
55
+
56
+ if (isDefined(this.hideClear)) this.showClear = !this.hideClear;
57
+
58
+ if (this.alwaysShowClear) this.showClear = true;
59
+
60
+ super.init();
61
+ }
62
+
63
+ prepareData(context, instance) {
64
+ let { data, state, cached } = instance;
65
+ data.formatted = Format.value(data.value, data.format);
66
+
67
+ if (!cached.data || data.value != cached.data.value) state.empty = data.value == null;
68
+
69
+ super.prepareData(context, instance);
70
+ }
71
+
72
+ formatValue(context, { data }) {
73
+ return data.formatted;
74
+ }
75
+
76
+ parseValue(value, instance) {
77
+ if (this.onParseInput) {
78
+ let result = instance.invoke("onParseInput", value, instance);
79
+ if (result !== undefined) return result;
80
+ }
81
+ return Culture.getNumberCulture().parse(value);
82
+ }
83
+
84
+ validate(context, instance) {
85
+ super.validate(context, instance);
86
+
87
+ let { data } = instance;
88
+ if (isNumber(data.value) && !data.error) {
89
+ if (isNumber(data.minValue)) {
90
+ if (data.value < data.minValue)
91
+ data.error = StringTemplate.format(this.minValueErrorText, Format.value(data.minValue, data.format));
92
+ else if (data.value == data.minValue && data.minExclusive)
93
+ data.error = StringTemplate.format(this.minExclusiveErrorText, Format.value(data.minValue, data.format));
94
+ }
95
+
96
+ if (isNumber(data.maxValue)) {
97
+ if (data.value > data.maxValue)
98
+ data.error = StringTemplate.format(this.maxValueErrorText, Format.value(data.maxValue, data.format));
99
+ else if (data.value == data.maxValue && data.maxExclusive)
100
+ data.error = StringTemplate.format(this.maxExclusiveErrorText, Format.value(data.maxValue, data.format));
101
+ }
102
+ }
103
+ }
104
+
105
+ renderInput(context, instance, key) {
106
+ return (
107
+ <Input
108
+ key={key}
109
+ data={instance.data}
110
+ instance={instance}
111
+ label={this.labelPlacement && getContent(this.renderLabel(context, instance, "label"))}
112
+ help={this.helpPlacement && getContent(this.renderHelp(context, instance, "help"))}
113
+ icon={this.renderIcon(context, instance, "icon")}
114
+ />
115
+ );
116
+ }
117
+
118
+ validateRequired(context, instance) {
119
+ return instance.state.empty && this.requiredText;
120
+ }
121
+ }
122
+
123
+ NumberField.prototype.baseClass = "numberfield";
124
+ NumberField.prototype.reactOn = "enter blur";
125
+ NumberField.prototype.format = "n";
126
+ NumberField.prototype.inputType = "text";
127
+
128
+ NumberField.prototype.maxValueErrorText = "Enter {0} or less.";
129
+ NumberField.prototype.maxExclusiveErrorText = "Enter a number less than {0}.";
130
+ NumberField.prototype.minValueErrorText = "Enter {0} or more.";
131
+ NumberField.prototype.minExclusiveErrorText = "Enter a number greater than {0}.";
132
+ NumberField.prototype.inputErrorText = "Invalid number entered.";
133
+ NumberField.prototype.suppressErrorsUntilVisited = true;
134
+
135
+ NumberField.prototype.incrementPercentage = 0.1;
136
+ NumberField.prototype.scale = 1;
137
+ NumberField.prototype.offset = 0;
138
+ NumberField.prototype.snapToIncrement = true;
139
+ NumberField.prototype.icon = null;
140
+ NumberField.prototype.showClear = false;
141
+ NumberField.prototype.alwaysShowClear = false;
142
+
143
+ Widget.alias("numberfield", NumberField);
144
+ Localization.registerPrototype("cx/widgets/NumberField", NumberField);
145
+
146
+ class Input extends VDOM.Component {
147
+ constructor(props) {
148
+ super(props);
149
+ this.state = {
150
+ focus: false,
151
+ };
152
+ }
153
+
154
+ render() {
155
+ let { data, instance, label, help, icon: iconVDOM } = this.props;
156
+ let { widget, state } = instance;
157
+ let { CSS, baseClass, suppressErrorsUntilVisited } = widget;
158
+
159
+ let icon = iconVDOM && <div className={CSS.element(baseClass, "left-icon")}>{iconVDOM}</div>;
160
+
161
+ let insideButton;
162
+ if (!data.readOnly && !data.disabled) {
163
+ if (
164
+ widget.showClear &&
165
+ (((widget.alwaysShowClear || !data.required) && !data.empty) || instance.state.inputError)
166
+ )
167
+ insideButton = (
168
+ <div
169
+ className={CSS.element(baseClass, "clear")}
170
+ onMouseDown={(e) => e.preventDefault()}
171
+ onClick={(e) => this.onClearClick(e)}
172
+ >
173
+ <ClearIcon className={CSS.element(baseClass, "icon")} />
174
+ </div>
175
+ );
176
+ }
177
+
178
+ let empty = this.input ? !this.input.value : data.empty;
179
+
180
+ return (
181
+ <div
182
+ className={CSS.expand(
183
+ data.classNames,
184
+ CSS.state({
185
+ visited: state.visited,
186
+ focus: this.state.focus,
187
+ icon: !!icon,
188
+ empty: empty && !data.placeholder,
189
+ error: data.error && (state.visited || !suppressErrorsUntilVisited || !empty),
190
+ }),
191
+ )}
192
+ style={data.style}
193
+ onMouseDown={stopPropagation}
194
+ onTouchStart={stopPropagation}
195
+ >
196
+ <input
197
+ id={data.id}
198
+ type={widget.inputType}
199
+ className={CSS.expand(CSS.element(baseClass, "input"), data.inputClass)}
200
+ defaultValue={data.formatted}
201
+ ref={(el) => {
202
+ this.input = el;
203
+ }}
204
+ style={data.inputStyle}
205
+ disabled={data.disabled}
206
+ readOnly={data.readOnly}
207
+ tabIndex={data.tabIndex}
208
+ placeholder={data.placeholder}
209
+ {...data.inputAttrs}
210
+ onMouseMove={(e) => tooltipMouseMove(e, ...getFieldTooltip(this.props.instance))}
211
+ onMouseLeave={(e) => tooltipMouseLeave(e, ...getFieldTooltip(this.props.instance))}
212
+ onChange={(e) => this.onChange(e, "change")}
213
+ onKeyDown={this.onKeyDown.bind(this)}
214
+ onBlur={(e) => {
215
+ this.onChange(e, "blur");
216
+ }}
217
+ onFocus={(e) => this.onFocus()}
218
+ onWheel={(e) => {
219
+ this.onChange(e, "wheel");
220
+ }}
221
+ onClick={stopPropagation}
222
+ />
223
+ {insideButton}
224
+ {icon}
225
+ {label}
226
+ {help}
227
+ </div>
228
+ );
229
+ }
230
+
231
+ UNSAFE_componentWillReceiveProps(props) {
232
+ let { data, state } = props.instance;
233
+ if (this.props.data.formatted != data.formatted && !state.inputError) {
234
+ this.input.value = props.data.formatted || "";
235
+ props.instance.setState({
236
+ inputError: false,
237
+ });
238
+ }
239
+ tooltipParentWillReceiveProps(this.input, ...getFieldTooltip(props.instance));
240
+ }
241
+
242
+ componentDidMount() {
243
+ tooltipParentDidMount(this.input, ...getFieldTooltip(this.props.instance));
244
+ autoFocus(this.input, this);
245
+ }
246
+
247
+ componentDidUpdate() {
248
+ autoFocus(this.input, this);
249
+ }
250
+
251
+ componentWillUnmount() {
252
+ if (this.input == getActiveElement() && this.input.value != this.props.data.formatted) {
253
+ this.onChange({ target: { value: this.input.value } }, "blur");
254
+ }
255
+ tooltipParentWillUnmount(this.props.instance);
256
+ }
257
+
258
+ getPreCursorDigits(text, cursor, decimalSeparator) {
259
+ let res = "";
260
+ for (let i = 0; i < cursor; i++) {
261
+ if ("0" <= text[i] && text[i] <= "9") res += text[i];
262
+ else if (text[i] == decimalSeparator) res += ".";
263
+ else if (text[i] == "-") res += "-";
264
+ }
265
+ return res;
266
+ }
267
+
268
+ getLengthWithoutSuffix(text, decimalSeparator) {
269
+ let l = text.length;
270
+ while (l > 0) {
271
+ if ("0" <= text[l - 1] && text[l - 1] <= "9") break;
272
+ if (text[l - 1] == decimalSeparator) break;
273
+ l--;
274
+ }
275
+ return l;
276
+ }
277
+
278
+ getDecimalSeparator(format) {
279
+ let text = Format.value(0.11111111, format);
280
+ for (let i = text.length - 1; i >= 0; i--) {
281
+ if ("0" <= text[i] && text[i] <= "9") continue;
282
+ if (text[i] == "," || text[i] == ".") return text[i];
283
+ break;
284
+ }
285
+ return null;
286
+ }
287
+
288
+ updateCursorPosition(preCursorText) {
289
+ if (isString(preCursorText)) {
290
+ let cursor = 0;
291
+ let preCursor = 0;
292
+ let text = this.input.value || "";
293
+ while (preCursor < preCursorText.length && cursor < text.length) {
294
+ if (text[cursor] == preCursorText[preCursor]) {
295
+ cursor++;
296
+ preCursor++;
297
+ } else {
298
+ cursor++;
299
+ }
300
+ }
301
+ this.input.setSelectionRange(cursor, cursor);
302
+ }
303
+ }
304
+
305
+ calculateIncrement(value, strength) {
306
+ if (value == 0) return 0.1;
307
+ let absValue = Math.abs(value * strength);
308
+ let log10 = Math.floor(Math.log10(absValue) + 0.001);
309
+ let size = Math.pow(10, log10);
310
+ if (absValue / size > 4.999) return 5 * size;
311
+ if (absValue / size > 1.999) return 2 * size;
312
+ return size;
313
+ }
314
+
315
+ onClearClick(e) {
316
+ this.input.value = "";
317
+ let { instance } = this.props;
318
+ instance.set("value", instance.widget.emptyValue, { immediate: true });
319
+ }
320
+
321
+ onKeyDown(e) {
322
+ let { instance } = this.props;
323
+ if (instance.widget.handleKeyDown(e, instance) === false) return;
324
+
325
+ switch (e.keyCode) {
326
+ case KeyCode.enter:
327
+ this.onChange(e, "enter");
328
+ break;
329
+
330
+ case KeyCode.left:
331
+ case KeyCode.right:
332
+ e.stopPropagation();
333
+ break;
334
+ }
335
+ }
336
+
337
+ onChange(e, change) {
338
+ let { instance, data } = this.props;
339
+ let { widget } = instance;
340
+
341
+ if (data.required) {
342
+ instance.setState({
343
+ empty: !e.target.value,
344
+ });
345
+ }
346
+
347
+ if (change == "blur" && this.state.focus) {
348
+ this.setState({
349
+ focus: false,
350
+ });
351
+ }
352
+
353
+ let immediate = change == "blur" || change == "enter";
354
+
355
+ if ((widget.reactOn.indexOf(change) == -1 && !immediate) || data.disabled || data.readOnly) return;
356
+
357
+ if (immediate) instance.setState({ visited: true });
358
+
359
+ let value = null;
360
+
361
+ if (e.target.value) {
362
+ let displayValue = widget.parseValue(e.target.value, instance);
363
+ if (isNaN(displayValue)) {
364
+ instance.setState({
365
+ inputError: instance.widget.inputErrorText,
366
+ });
367
+ return;
368
+ }
369
+
370
+ value = displayValue * data.scale + data.offset;
371
+
372
+ if (change == "wheel") {
373
+ e.preventDefault();
374
+ let increment =
375
+ data.increment != null ? data.increment : this.calculateIncrement(value, data.incrementPercentage);
376
+ value = value + (e.deltaY < 0 ? increment : -increment);
377
+ if (widget.snapToIncrement) {
378
+ value = Math.round(value / increment) * increment;
379
+ }
380
+
381
+ if (data.minValue != null) {
382
+ if (data.minExclusive) {
383
+ if (value <= data.minValue) return;
384
+ } else {
385
+ value = Math.max(value, data.minValue);
386
+ }
387
+ }
388
+
389
+ if (data.maxValue != null) {
390
+ if (data.maxExclusive) {
391
+ if (value >= data.maxValue) return;
392
+ } else {
393
+ value = Math.min(value, data.maxValue);
394
+ }
395
+ }
396
+ }
397
+
398
+ if (data.constrain) {
399
+ if (data.minValue != null) {
400
+ if (value < data.minValue) {
401
+ value = data.minValue;
402
+ }
403
+ }
404
+
405
+ if (data.maxValue != null) {
406
+ if (value > data.maxValue) {
407
+ value = data.maxValue;
408
+ }
409
+ }
410
+ }
411
+
412
+ let fmt = data.format;
413
+ let decimalSeparator = this.getDecimalSeparator(fmt) || Format.value(1.1, "n;1")[1];
414
+
415
+ let formatted = Format.value(value, fmt);
416
+ // Re-parse to avoid differences between formatted value and value in the store
417
+
418
+ value = widget.parseValue(formatted, instance) * data.scale + data.offset;
419
+
420
+ // Allow users to type numbers like 100.0003 or -0.05 without interruptions
421
+ // If the last typed character is zero or dot (decimal separator), skip processing it
422
+ if (
423
+ change == "change" &&
424
+ this.input.selectionStart == this.input.selectionEnd &&
425
+ this.input.selectionEnd >= this.getLengthWithoutSuffix(this.input.value, decimalSeparator) &&
426
+ (e.target.value[this.input.selectionEnd - 1] == decimalSeparator ||
427
+ (e.target.value.indexOf(decimalSeparator) >= 0 && e.target.value[this.input.selectionEnd - 1] == "0") ||
428
+ (this.input.selectionEnd == 2 && e.target.value[0] === "-" && e.target.value[1] === "0"))
429
+ )
430
+ return;
431
+
432
+ if (change != "blur") {
433
+ // Format, but keep the correct cursor position
434
+ let preCursorText = this.getPreCursorDigits(this.input.value, this.input.selectionStart, decimalSeparator);
435
+ this.input.value = formatted;
436
+ this.updateCursorPosition(preCursorText);
437
+ } else {
438
+ this.input.value = formatted;
439
+ }
440
+ }
441
+
442
+ instance.set("value", value, { immediate });
443
+
444
+ instance.setState({
445
+ inputError: false,
446
+ visited: true,
447
+ });
448
+ }
449
+
450
+ onFocus() {
451
+ let { instance } = this.props;
452
+ let { widget } = instance;
453
+ if (widget.trackFocus) {
454
+ this.setState({
455
+ focus: true,
456
+ });
457
+ }
458
+ }
459
+ }