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