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