cx 24.9.0 → 24.9.2

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