cx 24.5.2 → 24.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,441 +1,446 @@
1
- import { VDOM, getContent } from "../../ui/Widget";
2
- import { PureContainer } from "../../ui/PureContainer";
3
- import { ValidationError } from "./ValidationError";
4
- import { HelpText } from "./HelpText";
5
- import { Label } from "./Label";
6
- import { stopPropagation } from "../../util/eventCallbacks";
7
- import { isSelector } from "../../data/isSelector";
8
- import { Localization } from "../../ui/Localization";
9
- import { isPromise } from "../../util/isPromise";
10
- import { Console } from "../../util/Console";
11
- import { parseStyle } from "../../util/parseStyle";
12
- import { FocusManager } from "../../ui/FocusManager";
13
- import { isTouchEvent } from "../../util/isTouchEvent";
14
- import { tooltipMouseLeave, tooltipMouseMove } from "../overlay/tooltip-ops";
15
- import { coalesce } from "../../util/coalesce";
16
- import { isUndefined } from "../../util/isUndefined";
17
- import { shallowEquals } from "../../util/shallowEquals";
18
- import { FieldIcon } from "./FieldIcon";
19
-
20
- export class Field extends PureContainer {
21
- declareData() {
22
- super.declareData(
23
- {
24
- label: undefined,
25
- labelWidth: undefined,
26
- mode: undefined,
27
- viewMode: undefined,
28
- id: undefined,
29
- error: undefined,
30
- inputStyle: { structured: true },
31
- inputClass: { structured: true },
32
- inputAttrs: { structured: true },
33
- emptyText: undefined,
34
- visited: undefined,
35
- autoFocus: undefined,
36
- tabOnEnterKey: undefined,
37
- tabIndex: undefined,
38
- validationParams: { structured: true },
39
- },
40
- ...arguments,
41
- );
42
- }
43
-
44
- init() {
45
- this.inputStyle = parseStyle(this.inputStyle);
46
- super.init();
47
- }
48
-
49
- initComponents(context, instance) {
50
- if (this.validationMode == "tooltip" && isUndefined(this.errorTooltip)) {
51
- this.errorTooltip = {
52
- text: { bind: "$error" },
53
- mod: "error",
54
- ...this.errorTooltip,
55
- };
56
- }
57
-
58
- if (isUndefined(this.help)) {
59
- switch (this.validationMode) {
60
- case "help":
61
- case "help-inline":
62
- this.help = ValidationError;
63
- break;
64
-
65
- case "help-block":
66
- this.help = {
67
- type: ValidationError,
68
- mod: "block",
69
- };
70
- break;
71
- }
72
- }
73
-
74
- if (this.help != null) {
75
- let helpConfig = {};
76
-
77
- if (this.help.isComponentType) helpConfig = this.help;
78
- else if (isSelector(this.help)) helpConfig.text = this.help;
79
- else Object.assign(helpConfig, this.help);
80
-
81
- this.help = HelpText.create(helpConfig);
82
- }
83
-
84
- if (this.label != null) {
85
- let labelConfig = {
86
- mod: this.mod,
87
- disabled: this.disabled,
88
- required: this.required,
89
- asterisk: this.asterisk,
90
- style: this.labelStyle,
91
- class: this.labelClass,
92
- };
93
-
94
- if (this.label.isComponentType) labelConfig = this.label;
95
- else if (isSelector(this.label)) labelConfig.text = this.label;
96
- else Object.assign(labelConfig, this.label);
97
-
98
- this.label = Label.create(labelConfig);
99
- }
100
-
101
- if (this.icon != null) {
102
- let iconConfig = {
103
- className: this.CSS.element(this.baseClass, "icon"),
104
- };
105
- if (isSelector(this.icon)) iconConfig.name = this.icon;
106
- else Object.assign(iconConfig, this.icon);
107
-
108
- this.icon = FieldIcon.create(iconConfig);
109
- }
110
-
111
- return super.initComponents(...arguments, {
112
- label: this.label,
113
- help: this.help,
114
- icon: this.icon,
115
- });
116
- }
117
-
118
- initState(context, instance) {
119
- instance.state = {
120
- inputError: false,
121
- visited: this.visited === true,
122
- };
123
- }
124
-
125
- prepareData(context, instance) {
126
- let { data, state } = instance;
127
- if (!data.id) data.id = "fld-" + instance.id;
128
-
129
- data._disabled = data.disabled;
130
- data._readOnly = data.readOnly;
131
- data._viewMode = data.mode === "view" || data.viewMode;
132
- data._tabOnEnterKey = data.tabOnEnterKey;
133
- instance.parentDisabled = context.parentDisabled;
134
- instance.parentReadOnly = context.parentReadOnly;
135
- instance.parentViewMode = context.parentViewMode;
136
- instance.parentTabOnEnterKey = context.parentTabOnEnterKey;
137
- instance.parentVisited = context.parentVisited;
138
-
139
- if (typeof data.enabled !== "undefined") data._disabled = !data.enabled;
140
-
141
- this.disableOrValidate(context, instance);
142
-
143
- data.inputStyle = parseStyle(data.inputStyle);
144
-
145
- if (this.labelPlacement && this.label) data.mod = [data.mod, "label-placement-" + this.labelPlacement];
146
-
147
- if (this.helpPlacement && this.help) data.mod = [data.mod, "help-placement-" + this.helpPlacement];
148
-
149
- data.empty = this.isEmpty(data);
150
-
151
- super.prepareData(...arguments);
152
- }
153
-
154
- disableOrValidate(context, instance) {
155
- let { data, state } = instance;
156
-
157
- //if the parent is strict and sets some flag to true, it is not allowed to overrule that flag by field settings
158
-
159
- data.disabled = coalesce(
160
- context.parentStrict ? context.parentDisabled : null,
161
- data._disabled,
162
- context.parentDisabled,
163
- );
164
- data.readOnly = coalesce(
165
- context.parentStrict ? context.parentReadOnly : null,
166
- data._readOnly,
167
- context.parentReadOnly,
168
- );
169
- data.viewMode = coalesce(
170
- context.parentStrict ? context.parentViewMode : null,
171
- data._viewMode,
172
- context.parentViewMode,
173
- );
174
- data.tabOnEnterKey = coalesce(
175
- context.parentStrict ? context.parentTabOnEnterKey : null,
176
- data._tabOnEnterKey,
177
- context.parentTabOnEnterKey,
178
- );
179
- data.visited = coalesce(context.parentStrict ? context.parentVisited : null, data.visited, context.parentVisited);
180
-
181
- if (!data.error && !data.disabled && !data.viewMode) this.validate(context, instance);
182
-
183
- if (data.visited && !state.visited) {
184
- //feels hacky but it should be ok since we're in the middle of a new render cycle
185
- state.visited = true;
186
- }
187
-
188
- data.stateMods = {
189
- ...data.stateMods,
190
- disabled: data.disabled,
191
- "edit-mode": !data.viewMode,
192
- "view-mode": data.viewMode,
193
- };
194
- }
195
-
196
- explore(context, instance) {
197
- let { data, state } = instance;
198
-
199
- instance.parentDisabled = context.parentDisabled;
200
- instance.parentReadOnly = context.parentReadOnly;
201
- instance.parentViewMode = context.parentViewMode;
202
- instance.parentTabOnEnterKey = context.parentTabOnEnterKey;
203
- instance.parentVisited = context.parentVisited;
204
-
205
- if (
206
- instance.cache("parentDisabled", context.parentDisabled) ||
207
- instance.cache("parentReadOnly", context.parentReadOnly) ||
208
- instance.cache("parentViewMode", context.parentViewMode) ||
209
- instance.cache("parentTabOnEnterKey", context.parentTabOnEnterKey) ||
210
- instance.cache("parentVisited", context.parentVisited)
211
- ) {
212
- instance.markShouldUpdate(context);
213
- this.disableOrValidate(context, instance);
214
- this.prepareCSS(context, instance);
215
- }
216
-
217
- if (!context.validation)
218
- context.validation = {
219
- errors: [],
220
- };
221
-
222
- if (data.error) {
223
- context.validation.errors.push({
224
- fieldId: data.id,
225
- message: data.error,
226
- visited: state.visited,
227
- type: "error",
228
- });
229
- }
230
-
231
- context.push("lastFieldId", data.id);
232
- super.explore(context, instance);
233
- }
234
-
235
- exploreCleanup(context, instance) {
236
- context.pop("lastFieldId");
237
- }
238
-
239
- isEmpty(data) {
240
- return data.value == null || data.value === this.emptyValue;
241
- }
242
-
243
- validateRequired(context, instance) {
244
- let { data } = instance;
245
- if (this.isEmpty(data)) return this.requiredText;
246
- }
247
-
248
- validate(context, instance) {
249
- let { data, state } = instance;
250
- state = state || {};
251
-
252
- let empty = this.isEmpty(data);
253
-
254
- if (!data.error) {
255
- if (state.inputError) data.error = state.inputError;
256
- else if (state.validating && !empty) data.error = this.validatingText;
257
- else if (
258
- state.validationError &&
259
- data.value === state.lastValidatedValue &&
260
- shallowEquals(data.validationParams, state.lastValidationParams)
261
- )
262
- data.error = state.validationError;
263
- else if (data.required) data.error = this.validateRequired(context, instance);
264
- }
265
-
266
- if (
267
- !empty &&
268
- !state.validating &&
269
- !data.error &&
270
- this.onValidate &&
271
- (!state.previouslyValidated ||
272
- data.value != state.lastValidatedValue ||
273
- data.validationParams != state.lastValidationParams)
274
- ) {
275
- let result = instance.invoke("onValidate", data.value, instance, data.validationParams);
276
- if (isPromise(result)) {
277
- data.error = this.validatingText;
278
- instance.setState({
279
- validating: true,
280
- lastValidatedValue: data.value,
281
- previouslyValidated: true,
282
- lastValidationParams: data.validationParams,
283
- });
284
- result
285
- .then((r) => {
286
- let { data, state } = instance;
287
- let error =
288
- data.value == state.lastValidatedValue &&
289
- shallowEquals(data.validationParams, state.lastValidationParams)
290
- ? r
291
- : this.validatingText; //parameters changed, this will be revalidated
292
-
293
- instance.setState({
294
- validating: false,
295
- validationError: error,
296
- });
297
- })
298
- .catch((e) => {
299
- instance.setState({
300
- validating: false,
301
- validationError: this.validationExceptionText,
302
- });
303
- if (this.onValidationException) instance.invoke("onValidationException", e, instance);
304
- else {
305
- Console.warn("Unhandled validation exception:", e);
306
- }
307
- });
308
- } else {
309
- data.error = result;
310
- }
311
- }
312
- }
313
-
314
- renderLabel(context, instance, key) {
315
- if (instance.components.label) return getContent(instance.components.label.vdom);
316
- }
317
-
318
- renderInput(context, instance, key) {
319
- throw new Error("Not implemented.");
320
- }
321
-
322
- renderHelp(context, instance, key) {
323
- if (instance.components.help) return getContent(instance.components.help.render(context, key));
324
- }
325
-
326
- renderIcon(context, instance, key) {
327
- if (instance.components.icon) return getContent(instance.components.icon.render(context, key));
328
- }
329
-
330
- formatValue(context, { data }) {
331
- return data.text || data.value;
332
- }
333
-
334
- renderValue(context, instance, key) {
335
- let text = this.formatValue(context, instance);
336
- if (text) {
337
- return (
338
- <span
339
- key={key}
340
- onMouseMove={(e) => tooltipMouseMove(e, ...getFieldTooltip(instance))}
341
- onMouseLeave={(e) => tooltipMouseLeave(e, ...getFieldTooltip(instance))}
342
- >
343
- {text}
344
- </span>
345
- );
346
- }
347
- }
348
-
349
- renderContent(context, instance, key) {
350
- let content = this.renderValue(...arguments) || this.renderEmptyText(...arguments);
351
- return this.renderWrap(context, instance, key, content);
352
- }
353
-
354
- renderWrap(context, instance, key, content) {
355
- let { data } = instance;
356
- let interactive = !data.viewMode && !data.disabled;
357
- return (
358
- <div
359
- key={key}
360
- className={data.classNames}
361
- style={data.style}
362
- onMouseDown={interactive ? stopPropagation : null}
363
- onTouchStart={interactive ? stopPropagation : null}
364
- >
365
- {content}
366
- {this.labelPlacement && this.renderLabel(context, instance, "label")}
367
- </div>
368
- );
369
- }
370
-
371
- renderEmptyText(context, { data }, key) {
372
- return (
373
- <span key={key} className={this.CSS.element(this.baseClass, "empty-text")}>
374
- {data.emptyText || <span>&nbsp;</span>}
375
- </span>
376
- );
377
- }
378
-
379
- render(context, instance, key) {
380
- let { data } = instance;
381
- let content = !data.viewMode
382
- ? this.renderInput(context, instance, key)
383
- : this.renderContent(context, instance, key);
384
-
385
- return {
386
- label: !this.labelPlacement && this.renderLabel(context, instance, key),
387
- content: content,
388
- helpSpacer: this.helpSpacer && instance.components.help ? " " : null,
389
- help: !this.helpPlacement && this.renderHelp(context, instance, key),
390
- };
391
- }
392
-
393
- handleKeyDown(e, instance) {
394
- if (this.onKeyDown && instance.invoke("onKeyDown", e, instance) === false) return false;
395
-
396
- if (instance.data.tabOnEnterKey && e.keyCode === 13) {
397
- let target = e.target;
398
- setTimeout(() => {
399
- if (!instance.state.inputError) FocusManager.focusNext(target);
400
- }, 10);
401
- }
402
- }
403
- }
404
-
405
- Field.prototype.validationMode = "tooltip";
406
- Field.prototype.suppressErrorsUntilVisited = false;
407
- Field.prototype.requiredText = "This field is required.";
408
- Field.prototype.autoFocus = false;
409
- Field.prototype.asterisk = false;
410
- Field.prototype.validatingText = "Validation is in progress...";
411
- Field.prototype.validationExceptionText = "Something went wrong during input validation. Check log for more details.";
412
- Field.prototype.helpSpacer = true;
413
- Field.prototype.trackFocus = false; //add cxs-focus on parent element
414
- Field.prototype.labelPlacement = false;
415
- Field.prototype.helpPlacement = false;
416
- Field.prototype.emptyValue = null;
417
- Field.prototype.styled = true;
418
-
419
- //These flags are inheritable and should not be set to false
420
- //Field.prototype.visited = null;
421
- //Field.prototype.disabled = null;
422
- //Field.prototype.readOnly = null;
423
- //Field.prototype.viewMode = null;
424
-
425
- Localization.registerPrototype("cx/widgets/Field", Field);
426
-
427
- export function getFieldTooltip(instance) {
428
- let { widget, data, state } = instance;
429
-
430
- if (widget.errorTooltip && data.error && (!state || state.visited || !widget.suppressErrorsUntilVisited))
431
- return [
432
- instance,
433
- widget.errorTooltip,
434
- {
435
- data: {
436
- $error: data.error,
437
- },
438
- },
439
- ];
440
- return [instance, widget.tooltip];
441
- }
1
+ import { VDOM, getContent } from "../../ui/Widget";
2
+ import { PureContainer } from "../../ui/PureContainer";
3
+ import { ValidationError } from "./ValidationError";
4
+ import { HelpText } from "./HelpText";
5
+ import { Label } from "./Label";
6
+ import { stopPropagation } from "../../util/eventCallbacks";
7
+ import { isSelector } from "../../data/isSelector";
8
+ import { Localization } from "../../ui/Localization";
9
+ import { isPromise } from "../../util/isPromise";
10
+ import { Console } from "../../util/Console";
11
+ import { parseStyle } from "../../util/parseStyle";
12
+ import { FocusManager } from "../../ui/FocusManager";
13
+ import { isTouchEvent } from "../../util/isTouchEvent";
14
+ import { tooltipMouseLeave, tooltipMouseMove } from "../overlay/tooltip-ops";
15
+ import { coalesce } from "../../util/coalesce";
16
+ import { isUndefined } from "../../util/isUndefined";
17
+ import { shallowEquals } from "../../util/shallowEquals";
18
+ import { FieldIcon } from "./FieldIcon";
19
+
20
+ export class Field extends PureContainer {
21
+ declareData() {
22
+ super.declareData(
23
+ {
24
+ label: undefined,
25
+ labelWidth: undefined,
26
+ mode: undefined,
27
+ viewMode: undefined,
28
+ id: undefined,
29
+ error: undefined,
30
+ inputStyle: { structured: true },
31
+ inputClass: { structured: true },
32
+ inputAttrs: { structured: true },
33
+ emptyText: undefined,
34
+ visited: undefined,
35
+ autoFocus: undefined,
36
+ tabOnEnterKey: undefined,
37
+ tabIndex: undefined,
38
+ validationParams: { structured: true },
39
+ },
40
+ ...arguments,
41
+ );
42
+ }
43
+
44
+ init() {
45
+ this.inputStyle = parseStyle(this.inputStyle);
46
+ super.init();
47
+ }
48
+
49
+ initComponents(context, instance) {
50
+ if (this.validationMode == "tooltip" && isUndefined(this.errorTooltip)) {
51
+ this.errorTooltip = {
52
+ text: { bind: "$error" },
53
+ mod: "error",
54
+ ...this.errorTooltip,
55
+ };
56
+ }
57
+
58
+ if (isUndefined(this.help)) {
59
+ switch (this.validationMode) {
60
+ case "help":
61
+ case "help-inline":
62
+ this.help = ValidationError;
63
+ break;
64
+
65
+ case "help-block":
66
+ this.help = {
67
+ type: ValidationError,
68
+ mod: "block",
69
+ };
70
+ break;
71
+ }
72
+ }
73
+
74
+ if (this.help != null) {
75
+ let helpConfig = {};
76
+
77
+ if (this.help.isComponentType) helpConfig = this.help;
78
+ else if (isSelector(this.help)) helpConfig.text = this.help;
79
+ else Object.assign(helpConfig, this.help);
80
+
81
+ this.help = HelpText.create(helpConfig);
82
+ }
83
+
84
+ if (this.label != null) {
85
+ let labelConfig = {
86
+ mod: this.mod,
87
+ disabled: this.disabled,
88
+ required: this.required,
89
+ asterisk: this.asterisk,
90
+ style: this.labelStyle,
91
+ class: this.labelClass,
92
+ };
93
+
94
+ if (this.label.isComponentType) labelConfig = this.label;
95
+ else if (isSelector(this.label)) labelConfig.text = this.label;
96
+ else Object.assign(labelConfig, this.label);
97
+
98
+ this.label = Label.create(labelConfig);
99
+ }
100
+
101
+ if (this.icon != null) {
102
+ let iconConfig = {
103
+ className: this.CSS.element(this.baseClass, "icon"),
104
+ };
105
+ if (isSelector(this.icon)) iconConfig.name = this.icon;
106
+ else Object.assign(iconConfig, this.icon);
107
+
108
+ this.icon = FieldIcon.create(iconConfig);
109
+ }
110
+
111
+ return super.initComponents(...arguments, {
112
+ label: this.label,
113
+ help: this.help,
114
+ icon: this.icon,
115
+ });
116
+ }
117
+
118
+ initState(context, instance) {
119
+ instance.state = {
120
+ inputError: false,
121
+ visited: this.visited === true,
122
+ };
123
+ }
124
+
125
+ prepareData(context, instance) {
126
+ let { data, state } = instance;
127
+ if (!data.id) data.id = "fld-" + instance.id;
128
+
129
+ data._disabled = data.disabled;
130
+ data._readOnly = data.readOnly;
131
+ data._viewMode = data.mode === "view" || data.viewMode;
132
+ data._tabOnEnterKey = data.tabOnEnterKey;
133
+ data.validationValue = this.getValidationValue(data);
134
+ instance.parentDisabled = context.parentDisabled;
135
+ instance.parentReadOnly = context.parentReadOnly;
136
+ instance.parentViewMode = context.parentViewMode;
137
+ instance.parentTabOnEnterKey = context.parentTabOnEnterKey;
138
+ instance.parentVisited = context.parentVisited;
139
+
140
+ if (typeof data.enabled !== "undefined") data._disabled = !data.enabled;
141
+
142
+ this.disableOrValidate(context, instance);
143
+
144
+ data.inputStyle = parseStyle(data.inputStyle);
145
+
146
+ if (this.labelPlacement && this.label) data.mod = [data.mod, "label-placement-" + this.labelPlacement];
147
+
148
+ if (this.helpPlacement && this.help) data.mod = [data.mod, "help-placement-" + this.helpPlacement];
149
+
150
+ data.empty = this.isEmpty(data);
151
+
152
+ super.prepareData(...arguments);
153
+ }
154
+
155
+ disableOrValidate(context, instance) {
156
+ let { data, state } = instance;
157
+
158
+ //if the parent is strict and sets some flag to true, it is not allowed to overrule that flag by field settings
159
+
160
+ data.disabled = coalesce(
161
+ context.parentStrict ? context.parentDisabled : null,
162
+ data._disabled,
163
+ context.parentDisabled,
164
+ );
165
+ data.readOnly = coalesce(
166
+ context.parentStrict ? context.parentReadOnly : null,
167
+ data._readOnly,
168
+ context.parentReadOnly,
169
+ );
170
+ data.viewMode = coalesce(
171
+ context.parentStrict ? context.parentViewMode : null,
172
+ data._viewMode,
173
+ context.parentViewMode,
174
+ );
175
+ data.tabOnEnterKey = coalesce(
176
+ context.parentStrict ? context.parentTabOnEnterKey : null,
177
+ data._tabOnEnterKey,
178
+ context.parentTabOnEnterKey,
179
+ );
180
+ data.visited = coalesce(context.parentStrict ? context.parentVisited : null, data.visited, context.parentVisited);
181
+
182
+ if (!data.error && !data.disabled && !data.viewMode) this.validate(context, instance);
183
+
184
+ if (data.visited && !state.visited) {
185
+ //feels hacky but it should be ok since we're in the middle of a new render cycle
186
+ state.visited = true;
187
+ }
188
+
189
+ data.stateMods = {
190
+ ...data.stateMods,
191
+ disabled: data.disabled,
192
+ "edit-mode": !data.viewMode,
193
+ "view-mode": data.viewMode,
194
+ };
195
+ }
196
+
197
+ explore(context, instance) {
198
+ let { data, state } = instance;
199
+
200
+ instance.parentDisabled = context.parentDisabled;
201
+ instance.parentReadOnly = context.parentReadOnly;
202
+ instance.parentViewMode = context.parentViewMode;
203
+ instance.parentTabOnEnterKey = context.parentTabOnEnterKey;
204
+ instance.parentVisited = context.parentVisited;
205
+
206
+ if (
207
+ instance.cache("parentDisabled", context.parentDisabled) ||
208
+ instance.cache("parentReadOnly", context.parentReadOnly) ||
209
+ instance.cache("parentViewMode", context.parentViewMode) ||
210
+ instance.cache("parentTabOnEnterKey", context.parentTabOnEnterKey) ||
211
+ instance.cache("parentVisited", context.parentVisited)
212
+ ) {
213
+ instance.markShouldUpdate(context);
214
+ this.disableOrValidate(context, instance);
215
+ this.prepareCSS(context, instance);
216
+ }
217
+
218
+ if (!context.validation)
219
+ context.validation = {
220
+ errors: [],
221
+ };
222
+
223
+ if (data.error) {
224
+ context.validation.errors.push({
225
+ fieldId: data.id,
226
+ message: data.error,
227
+ visited: state.visited,
228
+ type: "error",
229
+ });
230
+ }
231
+
232
+ context.push("lastFieldId", data.id);
233
+ super.explore(context, instance);
234
+ }
235
+
236
+ exploreCleanup(context, instance) {
237
+ context.pop("lastFieldId");
238
+ }
239
+
240
+ isEmpty(data) {
241
+ return data.value == null || data.value === this.emptyValue;
242
+ }
243
+
244
+ validateRequired(context, instance) {
245
+ let { data } = instance;
246
+ if (this.isEmpty(data)) return this.requiredText;
247
+ }
248
+
249
+ getValidationValue(data) {
250
+ return data.value;
251
+ }
252
+
253
+ validate(context, instance) {
254
+ let { data, state } = instance;
255
+ state = state || {};
256
+
257
+ let empty = this.isEmpty(data);
258
+
259
+ if (!data.error) {
260
+ if (state.inputError) data.error = state.inputError;
261
+ else if (state.validating && !empty) data.error = this.validatingText;
262
+ else if (
263
+ state.validationError &&
264
+ data.validationValue === state.lastValidatedValue &&
265
+ shallowEquals(data.validationParams, state.lastValidationParams)
266
+ )
267
+ data.error = state.validationError;
268
+ else if (data.required) data.error = this.validateRequired(context, instance);
269
+ }
270
+
271
+ if (
272
+ !empty &&
273
+ !state.validating &&
274
+ !data.error &&
275
+ this.onValidate &&
276
+ (!state.previouslyValidated ||
277
+ data.validationValue != state.lastValidatedValue ||
278
+ data.validationParams != state.lastValidationParams)
279
+ ) {
280
+ let result = instance.invoke("onValidate", data.validationValue, instance, data.validationParams);
281
+ if (isPromise(result)) {
282
+ data.error = this.validatingText;
283
+ instance.setState({
284
+ validating: true,
285
+ lastValidatedValue: data.validationValue,
286
+ previouslyValidated: true,
287
+ lastValidationParams: data.validationParams,
288
+ });
289
+ result
290
+ .then((r) => {
291
+ let { data, state } = instance;
292
+ let error =
293
+ data.validationValue == state.lastValidatedValue &&
294
+ shallowEquals(data.validationParams, state.lastValidationParams)
295
+ ? r
296
+ : this.validatingText; //parameters changed, this will be revalidated
297
+
298
+ instance.setState({
299
+ validating: false,
300
+ validationError: error,
301
+ });
302
+ })
303
+ .catch((e) => {
304
+ instance.setState({
305
+ validating: false,
306
+ validationError: this.validationExceptionText,
307
+ });
308
+ if (this.onValidationException) instance.invoke("onValidationException", e, instance);
309
+ else {
310
+ Console.warn("Unhandled validation exception:", e);
311
+ }
312
+ });
313
+ } else {
314
+ data.error = result;
315
+ }
316
+ }
317
+ }
318
+
319
+ renderLabel(context, instance, key) {
320
+ if (instance.components.label) return getContent(instance.components.label.vdom);
321
+ }
322
+
323
+ renderInput(context, instance, key) {
324
+ throw new Error("Not implemented.");
325
+ }
326
+
327
+ renderHelp(context, instance, key) {
328
+ if (instance.components.help) return getContent(instance.components.help.render(context, key));
329
+ }
330
+
331
+ renderIcon(context, instance, key) {
332
+ if (instance.components.icon) return getContent(instance.components.icon.render(context, key));
333
+ }
334
+
335
+ formatValue(context, { data }) {
336
+ return data.text || data.value;
337
+ }
338
+
339
+ renderValue(context, instance, key) {
340
+ let text = this.formatValue(context, instance);
341
+ if (text) {
342
+ return (
343
+ <span
344
+ key={key}
345
+ onMouseMove={(e) => tooltipMouseMove(e, ...getFieldTooltip(instance))}
346
+ onMouseLeave={(e) => tooltipMouseLeave(e, ...getFieldTooltip(instance))}
347
+ >
348
+ {text}
349
+ </span>
350
+ );
351
+ }
352
+ }
353
+
354
+ renderContent(context, instance, key) {
355
+ let content = this.renderValue(...arguments) || this.renderEmptyText(...arguments);
356
+ return this.renderWrap(context, instance, key, content);
357
+ }
358
+
359
+ renderWrap(context, instance, key, content) {
360
+ let { data } = instance;
361
+ let interactive = !data.viewMode && !data.disabled;
362
+ return (
363
+ <div
364
+ key={key}
365
+ className={data.classNames}
366
+ style={data.style}
367
+ onMouseDown={interactive ? stopPropagation : null}
368
+ onTouchStart={interactive ? stopPropagation : null}
369
+ >
370
+ {content}
371
+ {this.labelPlacement && this.renderLabel(context, instance, "label")}
372
+ </div>
373
+ );
374
+ }
375
+
376
+ renderEmptyText(context, { data }, key) {
377
+ return (
378
+ <span key={key} className={this.CSS.element(this.baseClass, "empty-text")}>
379
+ {data.emptyText || <span>&nbsp;</span>}
380
+ </span>
381
+ );
382
+ }
383
+
384
+ render(context, instance, key) {
385
+ let { data } = instance;
386
+ let content = !data.viewMode
387
+ ? this.renderInput(context, instance, key)
388
+ : this.renderContent(context, instance, key);
389
+
390
+ return {
391
+ label: !this.labelPlacement && this.renderLabel(context, instance, key),
392
+ content: content,
393
+ helpSpacer: this.helpSpacer && instance.components.help ? " " : null,
394
+ help: !this.helpPlacement && this.renderHelp(context, instance, key),
395
+ };
396
+ }
397
+
398
+ handleKeyDown(e, instance) {
399
+ if (this.onKeyDown && instance.invoke("onKeyDown", e, instance) === false) return false;
400
+
401
+ if (instance.data.tabOnEnterKey && e.keyCode === 13) {
402
+ let target = e.target;
403
+ setTimeout(() => {
404
+ if (!instance.state.inputError) FocusManager.focusNext(target);
405
+ }, 10);
406
+ }
407
+ }
408
+ }
409
+
410
+ Field.prototype.validationMode = "tooltip";
411
+ Field.prototype.suppressErrorsUntilVisited = false;
412
+ Field.prototype.requiredText = "This field is required.";
413
+ Field.prototype.autoFocus = false;
414
+ Field.prototype.asterisk = false;
415
+ Field.prototype.validatingText = "Validation is in progress...";
416
+ Field.prototype.validationExceptionText = "Something went wrong during input validation. Check log for more details.";
417
+ Field.prototype.helpSpacer = true;
418
+ Field.prototype.trackFocus = false; //add cxs-focus on parent element
419
+ Field.prototype.labelPlacement = false;
420
+ Field.prototype.helpPlacement = false;
421
+ Field.prototype.emptyValue = null;
422
+ Field.prototype.styled = true;
423
+
424
+ //These flags are inheritable and should not be set to false
425
+ //Field.prototype.visited = null;
426
+ //Field.prototype.disabled = null;
427
+ //Field.prototype.readOnly = null;
428
+ //Field.prototype.viewMode = null;
429
+
430
+ Localization.registerPrototype("cx/widgets/Field", Field);
431
+
432
+ export function getFieldTooltip(instance) {
433
+ let { widget, data, state } = instance;
434
+
435
+ if (widget.errorTooltip && data.error && (!state || state.visited || !widget.suppressErrorsUntilVisited))
436
+ return [
437
+ instance,
438
+ widget.errorTooltip,
439
+ {
440
+ data: {
441
+ $error: data.error,
442
+ },
443
+ },
444
+ ];
445
+ return [instance, widget.tooltip];
446
+ }