cx 23.4.4 → 23.5.0

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,571 +1,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
- />
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
- };
238
- break;
239
- }
240
-
241
- let dropdown = {
242
- scrollTracking: true,
243
- inline: !isTouchDevice() || !!lastDropdown,
244
- matchWidth: false,
245
- placementOrder: "down down-right down-left up up-right up-left",
246
- touchFriendly: true,
247
- firstChildDefinesHeight: true,
248
- firstChildDefinesWidth: true,
249
- ...widget.dropdownOptions,
250
- type: Dropdown,
251
- relatedElement: this.input,
252
- onFocusOut: (e) => {
253
- this.closeDropdown(e);
254
- },
255
- onMouseDown: stopPropagation,
256
- items: {
257
- ...pickerConfig,
258
- ...this.props.picker,
259
- autoFocus: !widget.focusInputFirst,
260
- tabIndex: widget.focusInputFirst ? -1 : 0,
261
- onKeyDown: (e) => this.onKeyDown(e),
262
- onSelect: (e, calendar, date) => {
263
- e.stopPropagation();
264
- e.preventDefault();
265
- let touch = isTouchEvent(e);
266
- this.closeDropdown(e, () => {
267
- if (date) {
268
- // If a blur event occurs before we re-render the input,
269
- // the old input value is parsed and written to the store.
270
- // We want to prevent that by eagerly updating the input value.
271
- // This can happen if the date field is within a menu.
272
- let newFormattedValue = Format.value(date, this.props.data.format);
273
- this.input.value = newFormattedValue;
274
- }
275
- if (!touch) this.input.focus();
276
- });
277
- },
278
- },
279
- };
280
-
281
- return (this.dropdown = Widget.create(dropdown));
282
- }
283
-
284
- render() {
285
- let { instance, label, help } = this.props;
286
- let { data, widget, state } = instance;
287
- let { CSS, baseClass, suppressErrorsUntilVisited } = widget;
288
-
289
- let insideButton, icon;
290
-
291
- if (!data.readOnly && !data.disabled) {
292
- if (
293
- widget.showClear &&
294
- (((widget.alwaysShowClear || !data.required) && !data.empty) || instance.state.inputError)
295
- )
296
- insideButton = (
297
- <div
298
- className={CSS.element(baseClass, "clear")}
299
- onMouseDown={(e) => {
300
- e.preventDefault();
301
- e.stopPropagation();
302
- }}
303
- onClick={(e) => this.onClearClick(e)}
304
- >
305
- <ClearIcon className={CSS.element(baseClass, "icon")} />
306
- </div>
307
- );
308
- else
309
- insideButton = (
310
- <div className={CSS.element(baseClass, "right-icon")}>
311
- <DropdownIcon className={CSS.element(baseClass, "icon")} />
312
- </div>
313
- );
314
- }
315
-
316
- if (data.icon) {
317
- icon = (
318
- <div className={CSS.element(baseClass, "left-icon")}>
319
- {Icon.render(data.icon, { className: CSS.element(baseClass, "icon") })}
320
- </div>
321
- );
322
- }
323
-
324
- let dropdown = false;
325
- if (this.state.dropdownOpen)
326
- dropdown = (
327
- <Cx
328
- widget={this.getDropdown()}
329
- parentInstance={instance}
330
- options={{ name: "datefield-dropdown" }}
331
- subscribe
332
- />
333
- );
334
-
335
- let empty = this.input ? !this.input.value : data.empty;
336
-
337
- return (
338
- <div
339
- className={CSS.expand(
340
- data.classNames,
341
- CSS.state({
342
- visited: state.visited,
343
- focus: this.state.focus || this.state.dropdownOpen,
344
- icon: !!icon,
345
- empty: empty && !data.placeholder,
346
- error: data.error && (state.visited || !suppressErrorsUntilVisited || !empty),
347
- })
348
- )}
349
- style={data.style}
350
- onMouseDown={this.onMouseDown.bind(this)}
351
- onTouchStart={stopPropagation}
352
- >
353
- <input
354
- id={data.id}
355
- ref={(el) => {
356
- this.input = el;
357
- }}
358
- type="text"
359
- className={CSS.expand(CSS.element(baseClass, "input"), data.inputClass)}
360
- style={data.inputStyle}
361
- defaultValue={data.formatted}
362
- disabled={data.disabled}
363
- readOnly={data.readOnly}
364
- tabIndex={data.tabIndex}
365
- placeholder={data.placeholder}
366
- {...data.inputAttrs}
367
- onInput={(e) => this.onChange(e, "input")}
368
- onChange={(e) => this.onChange(e, "change")}
369
- onKeyDown={(e) => this.onKeyDown(e)}
370
- onBlur={(e) => {
371
- this.onBlur(e);
372
- }}
373
- onFocus={(e) => {
374
- this.onFocus(e);
375
- }}
376
- onMouseMove={(e) => tooltipMouseMove(e, ...getFieldTooltip(this.props.instance))}
377
- onMouseLeave={(e) => tooltipMouseLeave(e, ...getFieldTooltip(this.props.instance))}
378
- />
379
- {icon}
380
- {insideButton}
381
- {dropdown}
382
- {label}
383
- {help}
384
- </div>
385
- );
386
- }
387
-
388
- onMouseDown(e) {
389
- e.stopPropagation();
390
-
391
- if (this.state.dropdownOpen) {
392
- this.closeDropdown(e);
393
- } else {
394
- this.openDropdownOnFocus = true;
395
- }
396
-
397
- //icon click
398
- if (e.target !== this.input) {
399
- e.preventDefault();
400
-
401
- //the field should not focus only in case when dropdown will open and autofocus
402
- if (this.props.instance.widget.focusInputFirst || this.state.dropdownOpen) this.input.focus();
403
-
404
- if (this.state.dropdownOpen) this.closeDropdown(e);
405
- else this.openDropdown(e);
406
- }
407
- }
408
-
409
- onFocus(e) {
410
- let { instance } = this.props;
411
- let { widget } = instance;
412
- if (widget.trackFocus) {
413
- this.setState({
414
- focus: true,
415
- });
416
- }
417
- if (this.openDropdownOnFocus || widget.focusInputFirst) this.openDropdown(e);
418
- }
419
-
420
- onKeyDown(e) {
421
- let { instance } = this.props;
422
- if (instance.widget.handleKeyDown(e, instance) === false) return;
423
-
424
- switch (e.keyCode) {
425
- case KeyCode.enter:
426
- this.onChange(e, "enter");
427
- break;
428
-
429
- case KeyCode.esc:
430
- if (this.state.dropdownOpen) {
431
- e.stopPropagation();
432
- this.closeDropdown(e, () => {
433
- this.input.focus();
434
- });
435
- }
436
- break;
437
-
438
- case KeyCode.left:
439
- case KeyCode.right:
440
- e.stopPropagation();
441
- break;
442
-
443
- case KeyCode.down:
444
- this.openDropdown(e);
445
- e.stopPropagation();
446
- e.preventDefault();
447
- break;
448
- }
449
- }
450
-
451
- onBlur(e) {
452
- if (!this.state.dropdownOpen) this.props.instance.setState({ visited: true });
453
- else if (this.props.instance.widget.focusInputFirst) this.closeDropdown(e);
454
- if (this.state.focus)
455
- this.setState({
456
- focus: false,
457
- });
458
- this.onChange(e, "blur");
459
- }
460
-
461
- closeDropdown(e, callback) {
462
- if (this.state.dropdownOpen) {
463
- if (this.scrollableParents)
464
- this.scrollableParents.forEach((el) => {
465
- el.removeEventListener("scroll", this.updateDropdownPosition);
466
- });
467
-
468
- this.setState({ dropdownOpen: false }, callback);
469
- this.props.instance.setState({ visited: true });
470
- } else if (callback) callback();
471
- }
472
-
473
- openDropdown() {
474
- let { data } = this.props.instance;
475
- this.openDropdownOnFocus = false;
476
-
477
- if (!this.state.dropdownOpen && !(data.disabled || data.readOnly)) {
478
- this.setState({
479
- dropdownOpen: true,
480
- dropdownOpenTime: Date.now(),
481
- });
482
- }
483
- }
484
-
485
- onClearClick(e) {
486
- this.setValue(null);
487
- e.stopPropagation();
488
- e.preventDefault();
489
- }
490
-
491
- UNSAFE_componentWillReceiveProps(props) {
492
- let { data, state } = props.instance;
493
- if (data.formatted !== this.input.value && (data.formatted !== this.props.data.formatted || !state.inputError)) {
494
- this.input.value = data.formatted || "";
495
- props.instance.setState({
496
- inputError: false,
497
- });
498
- }
499
-
500
- tooltipParentWillReceiveProps(this.input, ...getFieldTooltip(this.props.instance));
501
- }
502
-
503
- componentDidMount() {
504
- tooltipParentDidMount(this.input, ...getFieldTooltip(this.props.instance));
505
- autoFocus(this.input, this);
506
- if (this.props.data.autoOpen) this.openDropdown();
507
- }
508
-
509
- componentDidUpdate() {
510
- autoFocus(this.input, this);
511
- }
512
-
513
- componentWillUnmount() {
514
- tooltipParentWillUnmount(this.props.instance);
515
- }
516
-
517
- onChange(e, eventType) {
518
- let { instance, data } = this.props;
519
- let { widget } = instance;
520
-
521
- if (data.disabled || data.readOnly) return;
522
-
523
- if (widget.reactOn.indexOf(eventType) === -1) return;
524
-
525
- if (eventType == "enter") instance.setState({ visited: true });
526
-
527
- this.setValue(e.target.value, data.value);
528
- }
529
-
530
- setValue(text, baseValue) {
531
- let { instance, data } = this.props;
532
- let { widget } = instance;
533
-
534
- let date = widget.parseDate(text, instance);
535
-
536
- instance.setState({
537
- inputError: isNaN(date) && widget.inputErrorText,
538
- });
539
-
540
- if (!isNaN(date)) {
541
- let mixed = new Date(baseValue);
542
- if (date && baseValue && !isNaN(mixed) && widget.partial) {
543
- switch (widget.segment) {
544
- case "date":
545
- mixed.setFullYear(date.getFullYear());
546
- mixed.setMonth(date.getMonth());
547
- mixed.setDate(date.getDate());
548
- break;
549
-
550
- case "time":
551
- mixed.setHours(date.getHours());
552
- mixed.setMinutes(date.getMinutes());
553
- mixed.setSeconds(date.getSeconds());
554
- break;
555
-
556
- default:
557
- mixed = date;
558
- break;
559
- }
560
-
561
- date = mixed;
562
- }
563
-
564
- let encode = widget.encoding || Culture.getDefaultDateEncoding();
565
-
566
- let value = date ? encode(date) : widget.emptyValue;
567
-
568
- if (!instance.set("value", value)) this.input.value = value ? Format.value(date, data.format) : "";
569
- }
570
- }
571
- }
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
+ }