cx 24.0.2 → 24.0.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 (39) hide show
  1. package/dist/manifest.js +713 -710
  2. package/dist/widgets.js +98 -37
  3. package/locale/de-de.js +6 -1
  4. package/locale/en-us.js +5 -1
  5. package/locale/es-es.js +6 -1
  6. package/locale/fr-fr.js +7 -2
  7. package/locale/nl-nl.js +4 -5
  8. package/locale/pt-pt.js +12 -1
  9. package/locale/sr-latn-ba.js +6 -2
  10. package/package.json +1 -1
  11. package/src/data/StructuredSelector.d.ts +1 -1
  12. package/src/data/ops/updateTree.js +1 -1
  13. package/src/data/ops/updateTree.spec.js +16 -14
  14. package/src/ui/Culture.d.ts +0 -3
  15. package/src/ui/DataProxy.d.ts +1 -0
  16. package/src/ui/DataProxy.js +2 -2
  17. package/src/ui/Localization.js +0 -2
  18. package/src/ui/Rescope.js +2 -2
  19. package/src/ui/Text.js +2 -4
  20. package/src/ui/adapter/DataAdapter.js +7 -12
  21. package/src/ui/adapter/GroupAdapter.d.ts +22 -3
  22. package/src/ui/adapter/TreeAdapter.d.ts +23 -3
  23. package/src/widgets/form/DateTimeField.d.ts +86 -86
  24. package/src/widgets/form/DateTimeField.js +569 -572
  25. package/src/widgets/form/Field.js +24 -9
  26. package/src/widgets/form/FieldIcon.js +42 -0
  27. package/src/widgets/form/LookupField.d.ts +174 -174
  28. package/src/widgets/form/LookupField.js +1130 -1131
  29. package/src/widgets/form/MonthField.d.ts +37 -38
  30. package/src/widgets/form/MonthField.js +6 -9
  31. package/src/widgets/form/NumberField.d.ts +2 -2
  32. package/src/widgets/form/NumberField.js +5 -9
  33. package/src/widgets/form/Select.d.ts +31 -35
  34. package/src/widgets/form/Select.js +7 -12
  35. package/src/widgets/form/TextField.d.ts +2 -2
  36. package/src/widgets/form/TextField.js +6 -5
  37. package/src/widgets/grid/Grid.d.ts +6 -6
  38. package/src/widgets/grid/Grid.js +3277 -3277
  39. package/src/widgets/overlay/Dropdown.d.ts +1 -0
@@ -1,572 +1,569 @@
1
- import { Widget, VDOM, getContent } from "../../ui/Widget";
2
- import { Cx } from "../../ui/Cx";
3
- import { Field, getFieldTooltip } from "./Field";
4
- import { DateTimePicker } from "./DateTimePicker";
5
- import { Calendar } from "./Calendar";
6
- import { Culture } from "../../ui/Culture";
7
- import { isTouchEvent } from "../../util/isTouchEvent";
8
- import { isTouchDevice } from "../../util/isTouchDevice";
9
- import { Dropdown } from "../overlay/Dropdown";
10
- import { StringTemplate } from "../../data/StringTemplate";
11
- import { zeroTime } from "../../util/date/zeroTime";
12
- import { dateDiff } from "../../util/date/dateDiff";
13
- import {
14
- tooltipParentWillReceiveProps,
15
- tooltipParentWillUnmount,
16
- tooltipMouseMove,
17
- tooltipMouseLeave,
18
- tooltipParentDidMount,
19
- } from "../overlay/tooltip-ops";
20
- import { KeyCode } from "../../util/KeyCode";
21
- import { Localization } from "../../ui/Localization";
22
- import DropdownIcon from "../icons/drop-down";
23
- import { Icon } from "../Icon";
24
- import ClearIcon from "../icons/clear";
25
- import { stopPropagation } from "../../util/eventCallbacks";
26
- import { Format } from "../../util/Format";
27
- import { TimeList } from "./TimeList";
28
- import { autoFocus } from "../autoFocus";
29
-
30
- export class DateTimeField extends Field {
31
- declareData() {
32
- super.declareData(
33
- {
34
- value: this.emptyValue,
35
- disabled: undefined,
36
- readOnly: undefined,
37
- enabled: undefined,
38
- placeholder: undefined,
39
- required: undefined,
40
- minValue: undefined,
41
- minExclusive: undefined,
42
- maxValue: undefined,
43
- maxExclusive: undefined,
44
- format: undefined,
45
- icon: undefined,
46
- autoOpen: undefined,
47
- },
48
- ...arguments
49
- );
50
- }
51
-
52
- init() {
53
- if (typeof this.hideClear !== "undefined") this.showClear = !this.hideClear;
54
-
55
- if (this.alwaysShowClear) this.showClear = true;
56
-
57
- if (!this.format) {
58
- switch (this.segment) {
59
- case "datetime":
60
- this.format = "datetime;YYYYMMddhhmm";
61
- break;
62
-
63
- case "time":
64
- this.format = "time;hhmm";
65
- break;
66
-
67
- case "date":
68
- this.format = "date;yyyyMMMdd";
69
- break;
70
- }
71
- }
72
- super.init();
73
- }
74
-
75
- prepareData(context, instance) {
76
- let { data } = instance;
77
-
78
- if (data.value) {
79
- let date = new Date(data.value);
80
- if (isNaN(date.getTime())) data.formatted = String(data.value);
81
- else {
82
- // handle utc edge cases
83
- if (this.segment == "date") date = zeroTime(date);
84
- data.formatted = Format.value(date, data.format);
85
- }
86
- data.date = date;
87
- } else data.formatted = "";
88
-
89
- if (data.refDate) data.refDate = zeroTime(new Date(data.refDate));
90
-
91
- if (data.maxValue) data.maxValue = new Date(data.maxValue);
92
-
93
- if (data.minValue) data.minValue = new Date(data.minValue);
94
-
95
- if (this.segment == "date") {
96
- if (data.minValue) data.minValue = zeroTime(data.minValue);
97
-
98
- if (data.maxValue) data.maxValue = zeroTime(data.maxValue);
99
- }
100
-
101
- instance.lastDropdown = context.lastDropdown;
102
-
103
- super.prepareData(context, instance);
104
- }
105
-
106
- validate(context, instance) {
107
- super.validate(context, instance);
108
- var { data, widget } = instance;
109
- if (!data.error && data.date) {
110
- if (isNaN(data.date)) data.error = this.inputErrorText;
111
- else {
112
- let d;
113
- if (data.maxValue) {
114
- d = dateDiff(data.date, data.maxValue);
115
- if (d > 0) data.error = StringTemplate.format(this.maxValueErrorText, data.maxValue);
116
- else if (d == 0 && data.maxExclusive)
117
- data.error = StringTemplate.format(this.maxExclusiveErrorText, data.maxValue);
118
- }
119
- if (data.minValue) {
120
- d = dateDiff(data.date, data.minValue);
121
- if (d < 0) data.error = StringTemplate.format(this.minValueErrorText, data.minValue);
122
- else if (d == 0 && data.minExclusive)
123
- data.error = StringTemplate.format(this.minExclusiveErrorText, data.minValue);
124
- }
125
- if (widget.disabledDaysOfWeek) {
126
- if (widget.disabledDaysOfWeek.includes(data.date.getDay()))
127
- data.error = this.disabledDaysOfWeekErrorText;
128
- }
129
- }
130
- }
131
- }
132
-
133
- renderInput(context, instance, key) {
134
- return (
135
- <DateTimeInput
136
- key={key}
137
- instance={instance}
138
- data={instance.data}
139
- picker={{
140
- value: this.value,
141
- minValue: this.minValue,
142
- maxValue: this.maxValue,
143
- minExclusive: this.minExclusive,
144
- maxExclusive: this.maxExclusive,
145
- maxValueErrorText: this.maxValueErrorText,
146
- maxExclusiveErrorText: this.maxExclusiveErrorText,
147
- minValueErrorText: this.minValueErrorText,
148
- minExclusiveErrorText: this.minExclusiveErrorText,
149
- }}
150
- label={this.labelPlacement && getContent(this.renderLabel(context, instance, "label"))}
151
- help={this.helpPlacement && getContent(this.renderHelp(context, instance, "help"))}
152
- />
153
- );
154
- }
155
-
156
- formatValue(context, { data }) {
157
- return data.value ? data.formatted : null;
158
- }
159
-
160
- parseDate(date, instance) {
161
- if (!date) return null;
162
- if (date instanceof Date) return date;
163
- if (this.onParseInput) {
164
- let result = instance.invoke("onParseInput", date, instance);
165
- if (result !== undefined) return result;
166
- }
167
- date = Culture.getDateTimeCulture().parse(date, { useCurrentDateForDefaults: true });
168
- return date;
169
- }
170
- }
171
-
172
- DateTimeField.prototype.baseClass = "datetimefield";
173
- DateTimeField.prototype.maxValueErrorText = "Select {0:d} or before.";
174
- DateTimeField.prototype.maxExclusiveErrorText = "Select a date before {0:d}.";
175
- DateTimeField.prototype.minValueErrorText = "Select {0:d} or later.";
176
- DateTimeField.prototype.minExclusiveErrorText = "Select a date after {0:d}.";
177
- DateTimeField.prototype.inputErrorText = "Invalid date entered.";
178
- DateTimeField.prototype.disabledDaysOfWeekErrorText = "Selected day of week is not allowed.";
179
-
180
- DateTimeField.prototype.suppressErrorsUntilVisited = true;
181
- DateTimeField.prototype.icon = "calendar";
182
- DateTimeField.prototype.showClear = true;
183
- DateTimeField.prototype.alwaysShowClear = false;
184
- DateTimeField.prototype.reactOn = "enter blur";
185
- DateTimeField.prototype.segment = "datetime";
186
- DateTimeField.prototype.picker = "auto";
187
- DateTimeField.prototype.disabledDaysOfWeek = null;
188
- DateTimeField.prototype.focusInputFirst = false;
189
-
190
- Widget.alias("datetimefield", DateTimeField);
191
- Localization.registerPrototype("cx/widgets/DateTimeField", DateTimeField);
192
-
193
- class DateTimeInput extends VDOM.Component {
194
- constructor(props) {
195
- super(props);
196
- props.instance.component = this;
197
- this.state = {
198
- dropdownOpen: false,
199
- focus: false,
200
- };
201
- }
202
-
203
- getDropdown() {
204
- if (this.dropdown) return this.dropdown;
205
-
206
- let { widget, lastDropdown } = this.props.instance;
207
-
208
- let pickerConfig;
209
-
210
- switch (widget.picker) {
211
- case "calendar":
212
- pickerConfig = {
213
- type: Calendar,
214
- partial: widget.partial,
215
- encoding: widget.encoding,
216
- disabledDaysOfWeek: widget.disabledDaysOfWeek,
217
- focusable: !widget.focusInputFirst,
218
- };
219
- break;
220
-
221
- case "list":
222
- pickerConfig = {
223
- type: TimeList,
224
- style: "height: 300px",
225
- encoding: widget.encoding,
226
- step: widget.step,
227
- format: widget.format,
228
- scrollSelectionIntoView: true,
229
- };
230
- break;
231
-
232
- default:
233
- pickerConfig = {
234
- type: DateTimePicker,
235
- segment: widget.segment,
236
- encoding: widget.encoding,
237
- showSeconds: widget.showSeconds,
238
- };
239
- break;
240
- }
241
-
242
- let dropdown = {
243
- scrollTracking: true,
244
- inline: !isTouchDevice() || !!lastDropdown,
245
- matchWidth: false,
246
- placementOrder: "down down-right down-left up up-right up-left",
247
- touchFriendly: true,
248
- firstChildDefinesHeight: true,
249
- firstChildDefinesWidth: true,
250
- ...widget.dropdownOptions,
251
- type: Dropdown,
252
- relatedElement: this.input,
253
- onFocusOut: (e) => {
254
- this.closeDropdown(e);
255
- },
256
- onMouseDown: stopPropagation,
257
- items: {
258
- ...pickerConfig,
259
- ...this.props.picker,
260
- autoFocus: !widget.focusInputFirst,
261
- tabIndex: widget.focusInputFirst ? -1 : 0,
262
- onKeyDown: (e) => this.onKeyDown(e),
263
- onSelect: (e, calendar, date) => {
264
- e.stopPropagation();
265
- e.preventDefault();
266
- let touch = isTouchEvent(e);
267
- this.closeDropdown(e, () => {
268
- if (date) {
269
- // If a blur event occurs before we re-render the input,
270
- // the old input value is parsed and written to the store.
271
- // We want to prevent that by eagerly updating the input value.
272
- // This can happen if the date field is within a menu.
273
- let newFormattedValue = Format.value(date, this.props.data.format);
274
- this.input.value = newFormattedValue;
275
- }
276
- if (!touch) this.input.focus();
277
- });
278
- },
279
- },
280
- };
281
-
282
- return (this.dropdown = Widget.create(dropdown));
283
- }
284
-
285
- render() {
286
- let { instance, label, help } = this.props;
287
- let { data, widget, state } = instance;
288
- let { CSS, baseClass, suppressErrorsUntilVisited } = widget;
289
-
290
- let insideButton, icon;
291
-
292
- if (!data.readOnly && !data.disabled) {
293
- if (
294
- widget.showClear &&
295
- (((widget.alwaysShowClear || !data.required) && !data.empty) || instance.state.inputError)
296
- )
297
- insideButton = (
298
- <div
299
- className={CSS.element(baseClass, "clear")}
300
- onMouseDown={(e) => {
301
- e.preventDefault();
302
- e.stopPropagation();
303
- }}
304
- onClick={(e) => this.onClearClick(e)}
305
- >
306
- <ClearIcon className={CSS.element(baseClass, "icon")} />
307
- </div>
308
- );
309
- else
310
- insideButton = (
311
- <div className={CSS.element(baseClass, "right-icon")}>
312
- <DropdownIcon className={CSS.element(baseClass, "icon")} />
313
- </div>
314
- );
315
- }
316
-
317
- if (data.icon) {
318
- icon = (
319
- <div className={CSS.element(baseClass, "left-icon")}>
320
- {Icon.render(data.icon, { className: CSS.element(baseClass, "icon") })}
321
- </div>
322
- );
323
- }
324
-
325
- let dropdown = false;
326
- if (this.state.dropdownOpen)
327
- dropdown = (
328
- <Cx
329
- widget={this.getDropdown()}
330
- parentInstance={instance}
331
- options={{ name: "datefield-dropdown" }}
332
- subscribe
333
- />
334
- );
335
-
336
- let empty = this.input ? !this.input.value : data.empty;
337
-
338
- return (
339
- <div
340
- className={CSS.expand(
341
- data.classNames,
342
- CSS.state({
343
- visited: state.visited,
344
- focus: this.state.focus || this.state.dropdownOpen,
345
- icon: !!icon,
346
- empty: empty && !data.placeholder,
347
- error: data.error && (state.visited || !suppressErrorsUntilVisited || !empty),
348
- })
349
- )}
350
- style={data.style}
351
- onMouseDown={this.onMouseDown.bind(this)}
352
- onTouchStart={stopPropagation}
353
- >
354
- <input
355
- id={data.id}
356
- ref={(el) => {
357
- this.input = el;
358
- }}
359
- type="text"
360
- className={CSS.expand(CSS.element(baseClass, "input"), data.inputClass)}
361
- style={data.inputStyle}
362
- defaultValue={data.formatted}
363
- disabled={data.disabled}
364
- readOnly={data.readOnly}
365
- tabIndex={data.tabIndex}
366
- placeholder={data.placeholder}
367
- {...data.inputAttrs}
368
- onInput={(e) => this.onChange(e, "input")}
369
- onChange={(e) => this.onChange(e, "change")}
370
- onKeyDown={(e) => this.onKeyDown(e)}
371
- onBlur={(e) => {
372
- this.onBlur(e);
373
- }}
374
- onFocus={(e) => {
375
- this.onFocus(e);
376
- }}
377
- onMouseMove={(e) => tooltipMouseMove(e, ...getFieldTooltip(this.props.instance))}
378
- onMouseLeave={(e) => tooltipMouseLeave(e, ...getFieldTooltip(this.props.instance))}
379
- />
380
- {icon}
381
- {insideButton}
382
- {dropdown}
383
- {label}
384
- {help}
385
- </div>
386
- );
387
- }
388
-
389
- onMouseDown(e) {
390
- e.stopPropagation();
391
-
392
- if (this.state.dropdownOpen) {
393
- this.closeDropdown(e);
394
- } else {
395
- this.openDropdownOnFocus = true;
396
- }
397
-
398
- //icon click
399
- if (e.target !== this.input) {
400
- e.preventDefault();
401
-
402
- //the field should not focus only in case when dropdown will open and autofocus
403
- if (this.props.instance.widget.focusInputFirst || this.state.dropdownOpen) this.input.focus();
404
-
405
- if (this.state.dropdownOpen) this.closeDropdown(e);
406
- else this.openDropdown(e);
407
- }
408
- }
409
-
410
- onFocus(e) {
411
- let { instance } = this.props;
412
- let { widget } = instance;
413
- if (widget.trackFocus) {
414
- this.setState({
415
- focus: true,
416
- });
417
- }
418
- if (this.openDropdownOnFocus || widget.focusInputFirst) this.openDropdown(e);
419
- }
420
-
421
- onKeyDown(e) {
422
- let { instance } = this.props;
423
- if (instance.widget.handleKeyDown(e, instance) === false) return;
424
-
425
- switch (e.keyCode) {
426
- case KeyCode.enter:
427
- this.onChange(e, "enter");
428
- break;
429
-
430
- case KeyCode.esc:
431
- if (this.state.dropdownOpen) {
432
- e.stopPropagation();
433
- this.closeDropdown(e, () => {
434
- this.input.focus();
435
- });
436
- }
437
- break;
438
-
439
- case KeyCode.left:
440
- case KeyCode.right:
441
- e.stopPropagation();
442
- break;
443
-
444
- case KeyCode.down:
445
- this.openDropdown(e);
446
- e.stopPropagation();
447
- e.preventDefault();
448
- break;
449
- }
450
- }
451
-
452
- onBlur(e) {
453
- if (!this.state.dropdownOpen) this.props.instance.setState({ visited: true });
454
- else if (this.props.instance.widget.focusInputFirst) this.closeDropdown(e);
455
- if (this.state.focus)
456
- this.setState({
457
- focus: false,
458
- });
459
- this.onChange(e, "blur");
460
- }
461
-
462
- closeDropdown(e, callback) {
463
- if (this.state.dropdownOpen) {
464
- if (this.scrollableParents)
465
- this.scrollableParents.forEach((el) => {
466
- el.removeEventListener("scroll", this.updateDropdownPosition);
467
- });
468
-
469
- this.setState({ dropdownOpen: false }, callback);
470
- this.props.instance.setState({ visited: true });
471
- } else if (callback) callback();
472
- }
473
-
474
- openDropdown() {
475
- let { data } = this.props.instance;
476
- this.openDropdownOnFocus = false;
477
-
478
- if (!this.state.dropdownOpen && !(data.disabled || data.readOnly)) {
479
- this.setState({
480
- dropdownOpen: true,
481
- dropdownOpenTime: Date.now(),
482
- });
483
- }
484
- }
485
-
486
- onClearClick(e) {
487
- this.setValue(null);
488
- e.stopPropagation();
489
- e.preventDefault();
490
- }
491
-
492
- UNSAFE_componentWillReceiveProps(props) {
493
- let { data, state } = props.instance;
494
- if (data.formatted !== this.input.value && (data.formatted !== this.props.data.formatted || !state.inputError)) {
495
- this.input.value = data.formatted || "";
496
- props.instance.setState({
497
- inputError: false,
498
- });
499
- }
500
-
501
- tooltipParentWillReceiveProps(this.input, ...getFieldTooltip(this.props.instance));
502
- }
503
-
504
- componentDidMount() {
505
- tooltipParentDidMount(this.input, ...getFieldTooltip(this.props.instance));
506
- autoFocus(this.input, this);
507
- if (this.props.data.autoOpen) this.openDropdown();
508
- }
509
-
510
- componentDidUpdate() {
511
- autoFocus(this.input, this);
512
- }
513
-
514
- componentWillUnmount() {
515
- tooltipParentWillUnmount(this.props.instance);
516
- }
517
-
518
- onChange(e, eventType) {
519
- let { instance, data } = this.props;
520
- let { widget } = instance;
521
-
522
- if (data.disabled || data.readOnly) return;
523
-
524
- if (widget.reactOn.indexOf(eventType) === -1) return;
525
-
526
- if (eventType == "enter") instance.setState({ visited: true });
527
-
528
- this.setValue(e.target.value, data.value);
529
- }
530
-
531
- setValue(text, baseValue) {
532
- let { instance, data } = this.props;
533
- let { widget } = instance;
534
-
535
- let date = widget.parseDate(text, instance);
536
-
537
- instance.setState({
538
- inputError: isNaN(date) && widget.inputErrorText,
539
- });
540
-
541
- if (!isNaN(date)) {
542
- let mixed = new Date(baseValue);
543
- if (date && baseValue && !isNaN(mixed) && widget.partial) {
544
- switch (widget.segment) {
545
- case "date":
546
- mixed.setFullYear(date.getFullYear());
547
- mixed.setMonth(date.getMonth());
548
- mixed.setDate(date.getDate());
549
- break;
550
-
551
- case "time":
552
- mixed.setHours(date.getHours());
553
- mixed.setMinutes(date.getMinutes());
554
- mixed.setSeconds(date.getSeconds());
555
- break;
556
-
557
- default:
558
- mixed = date;
559
- break;
560
- }
561
-
562
- date = mixed;
563
- }
564
-
565
- let encode = widget.encoding || Culture.getDefaultDateEncoding();
566
-
567
- let value = date ? encode(date) : widget.emptyValue;
568
-
569
- if (!instance.set("value", value)) this.input.value = value ? Format.value(date, data.format) : "";
570
- }
571
- }
572
- }
1
+ import { Widget, VDOM, getContent } from "../../ui/Widget";
2
+ import { Cx } from "../../ui/Cx";
3
+ import { Field, getFieldTooltip } from "./Field";
4
+ import { DateTimePicker } from "./DateTimePicker";
5
+ import { Calendar } from "./Calendar";
6
+ import { Culture } from "../../ui/Culture";
7
+ import { isTouchEvent } from "../../util/isTouchEvent";
8
+ import { isTouchDevice } from "../../util/isTouchDevice";
9
+ import { Dropdown } from "../overlay/Dropdown";
10
+ import { StringTemplate } from "../../data/StringTemplate";
11
+ import { zeroTime } from "../../util/date/zeroTime";
12
+ import { dateDiff } from "../../util/date/dateDiff";
13
+ import {
14
+ tooltipParentWillReceiveProps,
15
+ tooltipParentWillUnmount,
16
+ tooltipMouseMove,
17
+ tooltipMouseLeave,
18
+ tooltipParentDidMount,
19
+ } from "../overlay/tooltip-ops";
20
+ import { KeyCode } from "../../util/KeyCode";
21
+ import { Localization } from "../../ui/Localization";
22
+ import DropdownIcon from "../icons/drop-down";
23
+ import { Icon } from "../Icon";
24
+ import ClearIcon from "../icons/clear";
25
+ import { stopPropagation } from "../../util/eventCallbacks";
26
+ import { Format } from "../../util/Format";
27
+ import { TimeList } from "./TimeList";
28
+ import { autoFocus } from "../autoFocus";
29
+
30
+ export class DateTimeField extends Field {
31
+ declareData() {
32
+ super.declareData(
33
+ {
34
+ value: this.emptyValue,
35
+ disabled: undefined,
36
+ readOnly: undefined,
37
+ enabled: undefined,
38
+ placeholder: undefined,
39
+ required: undefined,
40
+ minValue: undefined,
41
+ minExclusive: undefined,
42
+ maxValue: undefined,
43
+ maxExclusive: undefined,
44
+ format: undefined,
45
+ icon: undefined,
46
+ autoOpen: undefined,
47
+ },
48
+ ...arguments,
49
+ );
50
+ }
51
+
52
+ init() {
53
+ if (typeof this.hideClear !== "undefined") this.showClear = !this.hideClear;
54
+
55
+ if (this.alwaysShowClear) this.showClear = true;
56
+
57
+ if (!this.format) {
58
+ switch (this.segment) {
59
+ case "datetime":
60
+ this.format = "datetime;YYYYMMddhhmm";
61
+ break;
62
+
63
+ case "time":
64
+ this.format = "time;hhmm";
65
+ break;
66
+
67
+ case "date":
68
+ this.format = "date;yyyyMMMdd";
69
+ break;
70
+ }
71
+ }
72
+ super.init();
73
+ }
74
+
75
+ prepareData(context, instance) {
76
+ let { data } = instance;
77
+
78
+ if (data.value) {
79
+ let date = new Date(data.value);
80
+ if (isNaN(date.getTime())) data.formatted = String(data.value);
81
+ else {
82
+ // handle utc edge cases
83
+ if (this.segment == "date") date = zeroTime(date);
84
+ data.formatted = Format.value(date, data.format);
85
+ }
86
+ data.date = date;
87
+ } else data.formatted = "";
88
+
89
+ if (data.refDate) data.refDate = zeroTime(new Date(data.refDate));
90
+
91
+ if (data.maxValue) data.maxValue = new Date(data.maxValue);
92
+
93
+ if (data.minValue) data.minValue = new Date(data.minValue);
94
+
95
+ if (this.segment == "date") {
96
+ if (data.minValue) data.minValue = zeroTime(data.minValue);
97
+
98
+ if (data.maxValue) data.maxValue = zeroTime(data.maxValue);
99
+ }
100
+
101
+ instance.lastDropdown = context.lastDropdown;
102
+
103
+ super.prepareData(context, instance);
104
+ }
105
+
106
+ validate(context, instance) {
107
+ super.validate(context, instance);
108
+ var { data, widget } = instance;
109
+ if (!data.error && data.date) {
110
+ if (isNaN(data.date)) data.error = this.inputErrorText;
111
+ else {
112
+ let d;
113
+ if (data.maxValue) {
114
+ d = dateDiff(data.date, data.maxValue);
115
+ if (d > 0) data.error = StringTemplate.format(this.maxValueErrorText, data.maxValue);
116
+ else if (d == 0 && data.maxExclusive)
117
+ data.error = StringTemplate.format(this.maxExclusiveErrorText, data.maxValue);
118
+ }
119
+ if (data.minValue) {
120
+ d = dateDiff(data.date, data.minValue);
121
+ if (d < 0) data.error = StringTemplate.format(this.minValueErrorText, data.minValue);
122
+ else if (d == 0 && data.minExclusive)
123
+ data.error = StringTemplate.format(this.minExclusiveErrorText, data.minValue);
124
+ }
125
+ if (widget.disabledDaysOfWeek) {
126
+ if (widget.disabledDaysOfWeek.includes(data.date.getDay()))
127
+ data.error = this.disabledDaysOfWeekErrorText;
128
+ }
129
+ }
130
+ }
131
+ }
132
+
133
+ renderInput(context, instance, key) {
134
+ return (
135
+ <DateTimeInput
136
+ key={key}
137
+ instance={instance}
138
+ data={instance.data}
139
+ picker={{
140
+ value: this.value,
141
+ minValue: this.minValue,
142
+ maxValue: this.maxValue,
143
+ minExclusive: this.minExclusive,
144
+ maxExclusive: this.maxExclusive,
145
+ maxValueErrorText: this.maxValueErrorText,
146
+ maxExclusiveErrorText: this.maxExclusiveErrorText,
147
+ minValueErrorText: this.minValueErrorText,
148
+ minExclusiveErrorText: this.minExclusiveErrorText,
149
+ }}
150
+ label={this.labelPlacement && getContent(this.renderLabel(context, instance, "label"))}
151
+ help={this.helpPlacement && getContent(this.renderHelp(context, instance, "help"))}
152
+ icon={getContent(this.renderIcon(context, instance, "icon"))}
153
+ />
154
+ );
155
+ }
156
+
157
+ formatValue(context, { data }) {
158
+ return data.value ? data.formatted : null;
159
+ }
160
+
161
+ parseDate(date, instance) {
162
+ if (!date) return null;
163
+ if (date instanceof Date) return date;
164
+ if (this.onParseInput) {
165
+ let result = instance.invoke("onParseInput", date, instance);
166
+ if (result !== undefined) return result;
167
+ }
168
+ date = Culture.getDateTimeCulture().parse(date, { useCurrentDateForDefaults: true });
169
+ return date;
170
+ }
171
+ }
172
+
173
+ DateTimeField.prototype.baseClass = "datetimefield";
174
+ DateTimeField.prototype.maxValueErrorText = "Select {0:d} or before.";
175
+ DateTimeField.prototype.maxExclusiveErrorText = "Select a date before {0:d}.";
176
+ DateTimeField.prototype.minValueErrorText = "Select {0:d} or later.";
177
+ DateTimeField.prototype.minExclusiveErrorText = "Select a date after {0:d}.";
178
+ DateTimeField.prototype.inputErrorText = "Invalid date entered.";
179
+ DateTimeField.prototype.disabledDaysOfWeekErrorText = "Selected day of week is not allowed.";
180
+
181
+ DateTimeField.prototype.suppressErrorsUntilVisited = true;
182
+ DateTimeField.prototype.icon = "calendar";
183
+ DateTimeField.prototype.showClear = true;
184
+ DateTimeField.prototype.alwaysShowClear = false;
185
+ DateTimeField.prototype.reactOn = "enter blur";
186
+ DateTimeField.prototype.segment = "datetime";
187
+ DateTimeField.prototype.picker = "auto";
188
+ DateTimeField.prototype.disabledDaysOfWeek = null;
189
+ DateTimeField.prototype.focusInputFirst = false;
190
+
191
+ Widget.alias("datetimefield", DateTimeField);
192
+ Localization.registerPrototype("cx/widgets/DateTimeField", DateTimeField);
193
+
194
+ class DateTimeInput extends VDOM.Component {
195
+ constructor(props) {
196
+ super(props);
197
+ props.instance.component = this;
198
+ this.state = {
199
+ dropdownOpen: false,
200
+ focus: false,
201
+ };
202
+ }
203
+
204
+ getDropdown() {
205
+ if (this.dropdown) return this.dropdown;
206
+
207
+ let { widget, lastDropdown } = this.props.instance;
208
+
209
+ let pickerConfig;
210
+
211
+ switch (widget.picker) {
212
+ case "calendar":
213
+ pickerConfig = {
214
+ type: Calendar,
215
+ partial: widget.partial,
216
+ encoding: widget.encoding,
217
+ disabledDaysOfWeek: widget.disabledDaysOfWeek,
218
+ focusable: !widget.focusInputFirst,
219
+ };
220
+ break;
221
+
222
+ case "list":
223
+ pickerConfig = {
224
+ type: TimeList,
225
+ style: "height: 300px",
226
+ encoding: widget.encoding,
227
+ step: widget.step,
228
+ format: widget.format,
229
+ scrollSelectionIntoView: true,
230
+ };
231
+ break;
232
+
233
+ default:
234
+ pickerConfig = {
235
+ type: DateTimePicker,
236
+ segment: widget.segment,
237
+ encoding: widget.encoding,
238
+ showSeconds: widget.showSeconds,
239
+ };
240
+ break;
241
+ }
242
+
243
+ let dropdown = {
244
+ scrollTracking: true,
245
+ inline: !isTouchDevice() || !!lastDropdown,
246
+ matchWidth: false,
247
+ placementOrder: "down down-right down-left up up-right up-left",
248
+ touchFriendly: true,
249
+ firstChildDefinesHeight: true,
250
+ firstChildDefinesWidth: true,
251
+ ...widget.dropdownOptions,
252
+ type: Dropdown,
253
+ relatedElement: this.input,
254
+ onFocusOut: (e) => {
255
+ this.closeDropdown(e);
256
+ },
257
+ onMouseDown: stopPropagation,
258
+ items: {
259
+ ...pickerConfig,
260
+ ...this.props.picker,
261
+ autoFocus: !widget.focusInputFirst,
262
+ tabIndex: widget.focusInputFirst ? -1 : 0,
263
+ onKeyDown: (e) => this.onKeyDown(e),
264
+ onSelect: (e, calendar, date) => {
265
+ e.stopPropagation();
266
+ e.preventDefault();
267
+ let touch = isTouchEvent(e);
268
+ this.closeDropdown(e, () => {
269
+ if (date) {
270
+ // If a blur event occurs before we re-render the input,
271
+ // the old input value is parsed and written to the store.
272
+ // We want to prevent that by eagerly updating the input value.
273
+ // This can happen if the date field is within a menu.
274
+ let newFormattedValue = Format.value(date, this.props.data.format);
275
+ this.input.value = newFormattedValue;
276
+ }
277
+ if (!touch) this.input.focus();
278
+ });
279
+ },
280
+ },
281
+ };
282
+
283
+ return (this.dropdown = Widget.create(dropdown));
284
+ }
285
+
286
+ render() {
287
+ let { instance, label, help, icon: iconVDOM } = this.props;
288
+ let { data, widget, state } = instance;
289
+ let { CSS, baseClass, suppressErrorsUntilVisited } = widget;
290
+
291
+ let insideButton, icon;
292
+
293
+ if (!data.readOnly && !data.disabled) {
294
+ if (
295
+ widget.showClear &&
296
+ (((widget.alwaysShowClear || !data.required) && !data.empty) || instance.state.inputError)
297
+ )
298
+ insideButton = (
299
+ <div
300
+ className={CSS.element(baseClass, "clear")}
301
+ onMouseDown={(e) => {
302
+ e.preventDefault();
303
+ e.stopPropagation();
304
+ }}
305
+ onClick={(e) => this.onClearClick(e)}
306
+ >
307
+ <ClearIcon className={CSS.element(baseClass, "icon")} />
308
+ </div>
309
+ );
310
+ else
311
+ insideButton = (
312
+ <div className={CSS.element(baseClass, "right-icon")}>
313
+ <DropdownIcon className={CSS.element(baseClass, "icon")} />
314
+ </div>
315
+ );
316
+ }
317
+
318
+ if (iconVDOM) {
319
+ icon = <div className={CSS.element(baseClass, "left-icon")}>{iconVDOM}</div>;
320
+ }
321
+
322
+ let dropdown = false;
323
+ if (this.state.dropdownOpen)
324
+ dropdown = (
325
+ <Cx
326
+ widget={this.getDropdown()}
327
+ parentInstance={instance}
328
+ options={{ name: "datefield-dropdown" }}
329
+ subscribe
330
+ />
331
+ );
332
+
333
+ let empty = this.input ? !this.input.value : data.empty;
334
+
335
+ return (
336
+ <div
337
+ className={CSS.expand(
338
+ data.classNames,
339
+ CSS.state({
340
+ visited: state.visited,
341
+ focus: this.state.focus || this.state.dropdownOpen,
342
+ icon: !!icon,
343
+ empty: empty && !data.placeholder,
344
+ error: data.error && (state.visited || !suppressErrorsUntilVisited || !empty),
345
+ }),
346
+ )}
347
+ style={data.style}
348
+ onMouseDown={this.onMouseDown.bind(this)}
349
+ onTouchStart={stopPropagation}
350
+ >
351
+ <input
352
+ id={data.id}
353
+ ref={(el) => {
354
+ this.input = el;
355
+ }}
356
+ type="text"
357
+ className={CSS.expand(CSS.element(baseClass, "input"), data.inputClass)}
358
+ style={data.inputStyle}
359
+ defaultValue={data.formatted}
360
+ disabled={data.disabled}
361
+ readOnly={data.readOnly}
362
+ tabIndex={data.tabIndex}
363
+ placeholder={data.placeholder}
364
+ {...data.inputAttrs}
365
+ onInput={(e) => this.onChange(e, "input")}
366
+ onChange={(e) => this.onChange(e, "change")}
367
+ onKeyDown={(e) => this.onKeyDown(e)}
368
+ onBlur={(e) => {
369
+ this.onBlur(e);
370
+ }}
371
+ onFocus={(e) => {
372
+ this.onFocus(e);
373
+ }}
374
+ onMouseMove={(e) => tooltipMouseMove(e, ...getFieldTooltip(this.props.instance))}
375
+ onMouseLeave={(e) => tooltipMouseLeave(e, ...getFieldTooltip(this.props.instance))}
376
+ />
377
+ {icon}
378
+ {insideButton}
379
+ {dropdown}
380
+ {label}
381
+ {help}
382
+ </div>
383
+ );
384
+ }
385
+
386
+ onMouseDown(e) {
387
+ e.stopPropagation();
388
+
389
+ if (this.state.dropdownOpen) {
390
+ this.closeDropdown(e);
391
+ } else {
392
+ this.openDropdownOnFocus = true;
393
+ }
394
+
395
+ //icon click
396
+ if (e.target !== this.input) {
397
+ e.preventDefault();
398
+
399
+ //the field should not focus only in case when dropdown will open and autofocus
400
+ if (this.props.instance.widget.focusInputFirst || this.state.dropdownOpen) this.input.focus();
401
+
402
+ if (this.state.dropdownOpen) this.closeDropdown(e);
403
+ else this.openDropdown(e);
404
+ }
405
+ }
406
+
407
+ onFocus(e) {
408
+ let { instance } = this.props;
409
+ let { widget } = instance;
410
+ if (widget.trackFocus) {
411
+ this.setState({
412
+ focus: true,
413
+ });
414
+ }
415
+ if (this.openDropdownOnFocus || widget.focusInputFirst) this.openDropdown(e);
416
+ }
417
+
418
+ onKeyDown(e) {
419
+ let { instance } = this.props;
420
+ if (instance.widget.handleKeyDown(e, instance) === false) return;
421
+
422
+ switch (e.keyCode) {
423
+ case KeyCode.enter:
424
+ this.onChange(e, "enter");
425
+ break;
426
+
427
+ case KeyCode.esc:
428
+ if (this.state.dropdownOpen) {
429
+ e.stopPropagation();
430
+ this.closeDropdown(e, () => {
431
+ this.input.focus();
432
+ });
433
+ }
434
+ break;
435
+
436
+ case KeyCode.left:
437
+ case KeyCode.right:
438
+ e.stopPropagation();
439
+ break;
440
+
441
+ case KeyCode.down:
442
+ this.openDropdown(e);
443
+ e.stopPropagation();
444
+ e.preventDefault();
445
+ break;
446
+ }
447
+ }
448
+
449
+ onBlur(e) {
450
+ if (!this.state.dropdownOpen) this.props.instance.setState({ visited: true });
451
+ else if (this.props.instance.widget.focusInputFirst) this.closeDropdown(e);
452
+ if (this.state.focus)
453
+ this.setState({
454
+ focus: false,
455
+ });
456
+ this.onChange(e, "blur");
457
+ }
458
+
459
+ closeDropdown(e, callback) {
460
+ if (this.state.dropdownOpen) {
461
+ if (this.scrollableParents)
462
+ this.scrollableParents.forEach((el) => {
463
+ el.removeEventListener("scroll", this.updateDropdownPosition);
464
+ });
465
+
466
+ this.setState({ dropdownOpen: false }, callback);
467
+ this.props.instance.setState({ visited: true });
468
+ } else if (callback) callback();
469
+ }
470
+
471
+ openDropdown() {
472
+ let { data } = this.props.instance;
473
+ this.openDropdownOnFocus = false;
474
+
475
+ if (!this.state.dropdownOpen && !(data.disabled || data.readOnly)) {
476
+ this.setState({
477
+ dropdownOpen: true,
478
+ dropdownOpenTime: Date.now(),
479
+ });
480
+ }
481
+ }
482
+
483
+ onClearClick(e) {
484
+ this.setValue(null);
485
+ e.stopPropagation();
486
+ e.preventDefault();
487
+ }
488
+
489
+ UNSAFE_componentWillReceiveProps(props) {
490
+ let { data, state } = props.instance;
491
+ if (data.formatted !== this.input.value && (data.formatted !== this.props.data.formatted || !state.inputError)) {
492
+ this.input.value = data.formatted || "";
493
+ props.instance.setState({
494
+ inputError: false,
495
+ });
496
+ }
497
+
498
+ tooltipParentWillReceiveProps(this.input, ...getFieldTooltip(this.props.instance));
499
+ }
500
+
501
+ componentDidMount() {
502
+ tooltipParentDidMount(this.input, ...getFieldTooltip(this.props.instance));
503
+ autoFocus(this.input, this);
504
+ if (this.props.data.autoOpen) this.openDropdown();
505
+ }
506
+
507
+ componentDidUpdate() {
508
+ autoFocus(this.input, this);
509
+ }
510
+
511
+ componentWillUnmount() {
512
+ tooltipParentWillUnmount(this.props.instance);
513
+ }
514
+
515
+ onChange(e, eventType) {
516
+ let { instance, data } = this.props;
517
+ let { widget } = instance;
518
+
519
+ if (data.disabled || data.readOnly) return;
520
+
521
+ if (widget.reactOn.indexOf(eventType) === -1) return;
522
+
523
+ if (eventType == "enter") instance.setState({ visited: true });
524
+
525
+ this.setValue(e.target.value, data.value);
526
+ }
527
+
528
+ setValue(text, baseValue) {
529
+ let { instance, data } = this.props;
530
+ let { widget } = instance;
531
+
532
+ let date = widget.parseDate(text, instance);
533
+
534
+ instance.setState({
535
+ inputError: isNaN(date) && widget.inputErrorText,
536
+ });
537
+
538
+ if (!isNaN(date)) {
539
+ let mixed = new Date(baseValue);
540
+ if (date && baseValue && !isNaN(mixed) && widget.partial) {
541
+ switch (widget.segment) {
542
+ case "date":
543
+ mixed.setFullYear(date.getFullYear());
544
+ mixed.setMonth(date.getMonth());
545
+ mixed.setDate(date.getDate());
546
+ break;
547
+
548
+ case "time":
549
+ mixed.setHours(date.getHours());
550
+ mixed.setMinutes(date.getMinutes());
551
+ mixed.setSeconds(date.getSeconds());
552
+ break;
553
+
554
+ default:
555
+ mixed = date;
556
+ break;
557
+ }
558
+
559
+ date = mixed;
560
+ }
561
+
562
+ let encode = widget.encoding || Culture.getDefaultDateEncoding();
563
+
564
+ let value = date ? encode(date) : widget.emptyValue;
565
+
566
+ if (!instance.set("value", value)) this.input.value = value ? Format.value(date, data.format) : "";
567
+ }
568
+ }
569
+ }