cx 23.4.5 → 23.5.1

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,171 +1,171 @@
1
- import { isSelfOrDescendant, findFirst, findFirstChild, isFocusable, closestParent } from "../util/DOM";
2
- import { batchUpdates } from "./batchUpdates";
3
- import { SubscriberList } from "../util/SubscriberList";
4
- import { isTouchEvent } from "../util/isTouchEvent";
5
- import { getActiveElement } from "../util/getActiveElement";
6
-
7
- /*
8
- * Purpose of FocusManager is to provide focusout notifications.
9
- * IE and Firefox do not provide relatedTarget info in blur events which makes it impossible
10
- * to determine if focus went outside or stayed inside the component.
11
- */
12
-
13
- let subscribers = new SubscriberList(),
14
- timerInterval = 300,
15
- timerId = null;
16
-
17
- let lastActiveElement = null;
18
- let pending = false;
19
-
20
- export class FocusManager {
21
- static subscribe(callback) {
22
- let unsubscribe = subscribers.subscribe(callback);
23
- checkTimer();
24
- return unsubscribe;
25
- }
26
-
27
- static onFocusOut(el, callback) {
28
- let active = isSelfOrDescendant(el, getActiveElement());
29
- return this.subscribe((focusedEl) => {
30
- if (!active) active = isSelfOrDescendant(el, getActiveElement());
31
- else if (!isSelfOrDescendant(el, focusedEl)) {
32
- active = false;
33
- callback(focusedEl);
34
- }
35
- });
36
- }
37
-
38
- static oneFocusOut(el, callback) {
39
- this.nudge();
40
- let off = this.subscribe((focusedEl) => {
41
- if (!isSelfOrDescendant(el, focusedEl)) {
42
- callback(focusedEl);
43
- off();
44
- }
45
- });
46
- return off;
47
- }
48
-
49
- static nudge() {
50
- if (typeof document !== "undefined" && getActiveElement() !== lastActiveElement) {
51
- if (!pending) {
52
- pending = true;
53
- setTimeout(function () {
54
- pending = false;
55
- if (getActiveElement() !== lastActiveElement) {
56
- lastActiveElement = getActiveElement();
57
- batchUpdates(() => {
58
- subscribers.notify(lastActiveElement);
59
- });
60
- checkTimer();
61
- }
62
- }, 0);
63
- }
64
- }
65
- }
66
-
67
- static focus(el) {
68
- el.focus();
69
- this.nudge();
70
- }
71
-
72
- static focusFirst(el) {
73
- let focusable = findFirst(el, isFocusable);
74
- if (focusable) this.focus(focusable);
75
- return focusable;
76
- }
77
-
78
- static focusFirstChild(el) {
79
- let focusable = findFirstChild(el, isFocusable);
80
- if (focusable) this.focus(focusable);
81
- return focusable;
82
- }
83
-
84
- static focusNext(el) {
85
- let next = el,
86
- skip = true;
87
- do {
88
- if (!skip) {
89
- let focusable = this.focusFirst(next);
90
- if (focusable) return focusable;
91
- }
92
-
93
- if (next.nextSibling) {
94
- next = next.nextSibling;
95
- skip = false;
96
- } else {
97
- next = next.parentNode;
98
- skip = true;
99
- }
100
- } while (next);
101
- }
102
-
103
- static setInterval(interval) {
104
- timerInterval = interval;
105
- checkTimer();
106
- }
107
- }
108
-
109
- export function oneFocusOut(component, el, callback) {
110
- if (!component.oneFocusOut)
111
- component.oneFocusOut = FocusManager.oneFocusOut(el, (focus) => {
112
- delete component.oneFocusOut;
113
- callback(focus);
114
- });
115
- }
116
-
117
- export function offFocusOut(component) {
118
- if (component.oneFocusOut) {
119
- component.oneFocusOut();
120
- delete component.oneFocusOut;
121
- }
122
- }
123
-
124
- export function preventFocus(e) {
125
- //Focus can be prevented only on mousedown event. On touchstart this will not work
126
- //preventDefault cannot be used as it prevents scrolling
127
- if (e.type !== "mousedown") return;
128
-
129
- e.preventDefault();
130
-
131
- unfocusElement(e.currentTarget, false);
132
- }
133
-
134
- function checkTimer() {
135
- let shouldRun = !subscribers.isEmpty();
136
-
137
- if (shouldRun && !timerId)
138
- timerId = setInterval(() => {
139
- FocusManager.nudge();
140
- }, timerInterval);
141
-
142
- if (!shouldRun && timerId) {
143
- clearInterval(timerId);
144
- timerId = null;
145
- }
146
- }
147
-
148
- export function preventFocusOnTouch(e, force = false) {
149
- if (force || isTouchEvent()) preventFocus(e);
150
- }
151
-
152
- export function unfocusElement(target = null, unfocusParentOverlay = true) {
153
- const activeElement = getActiveElement();
154
- if (!target) target = activeElement;
155
-
156
- if (unfocusParentOverlay) {
157
- let focusableOverlayContainer = closestParent(target, (el) => el.dataset.focusableOverlayContainer);
158
- if (focusableOverlayContainer) target = focusableOverlayContainer;
159
- }
160
-
161
- //find the closest focusable parent of the target element and focus it instead
162
- let focusableParent = closestParent(
163
- target,
164
- (el) => isFocusable(el) && (!unfocusParentOverlay || el.dataset.focusableOverlayContainer)
165
- );
166
-
167
- if (focusableParent && focusableParent !== document.body) focusableParent.focus();
168
- else activeElement.blur();
169
-
170
- FocusManager.nudge();
171
- }
1
+ import { isSelfOrDescendant, findFirst, findFirstChild, isFocusable, closestParent } from "../util/DOM";
2
+ import { batchUpdates } from "./batchUpdates";
3
+ import { SubscriberList } from "../util/SubscriberList";
4
+ import { isTouchEvent } from "../util/isTouchEvent";
5
+ import { getActiveElement } from "../util/getActiveElement";
6
+
7
+ /*
8
+ * Purpose of FocusManager is to provide focusout notifications.
9
+ * IE and Firefox do not provide relatedTarget info in blur events which makes it impossible
10
+ * to determine if focus went outside or stayed inside the component.
11
+ */
12
+
13
+ let subscribers = new SubscriberList(),
14
+ timerInterval = 300,
15
+ timerId = null;
16
+
17
+ let lastActiveElement = null;
18
+ let pending = false;
19
+
20
+ export class FocusManager {
21
+ static subscribe(callback) {
22
+ let unsubscribe = subscribers.subscribe(callback);
23
+ checkTimer();
24
+ return unsubscribe;
25
+ }
26
+
27
+ static onFocusOut(el, callback) {
28
+ let active = isSelfOrDescendant(el, getActiveElement());
29
+ return this.subscribe((focusedEl) => {
30
+ if (!active) active = isSelfOrDescendant(el, getActiveElement());
31
+ else if (!isSelfOrDescendant(el, focusedEl)) {
32
+ active = false;
33
+ callback(focusedEl);
34
+ }
35
+ });
36
+ }
37
+
38
+ static oneFocusOut(el, callback) {
39
+ this.nudge();
40
+ let off = this.subscribe((focusedEl) => {
41
+ if (!isSelfOrDescendant(el, focusedEl)) {
42
+ callback(focusedEl);
43
+ off();
44
+ }
45
+ });
46
+ return off;
47
+ }
48
+
49
+ static nudge() {
50
+ if (typeof document !== "undefined" && getActiveElement() !== lastActiveElement) {
51
+ if (!pending) {
52
+ pending = true;
53
+ setTimeout(function () {
54
+ pending = false;
55
+ if (getActiveElement() !== lastActiveElement) {
56
+ lastActiveElement = getActiveElement();
57
+ batchUpdates(() => {
58
+ subscribers.notify(lastActiveElement);
59
+ });
60
+ checkTimer();
61
+ }
62
+ }, 0);
63
+ }
64
+ }
65
+ }
66
+
67
+ static focus(el) {
68
+ el.focus();
69
+ this.nudge();
70
+ }
71
+
72
+ static focusFirst(el) {
73
+ let focusable = findFirst(el, isFocusable);
74
+ if (focusable) this.focus(focusable);
75
+ return focusable;
76
+ }
77
+
78
+ static focusFirstChild(el) {
79
+ let focusable = findFirstChild(el, isFocusable);
80
+ if (focusable) this.focus(focusable);
81
+ return focusable;
82
+ }
83
+
84
+ static focusNext(el) {
85
+ let next = el,
86
+ skip = true;
87
+ do {
88
+ if (!skip) {
89
+ let focusable = this.focusFirst(next);
90
+ if (focusable) return focusable;
91
+ }
92
+
93
+ if (next.nextSibling) {
94
+ next = next.nextSibling;
95
+ skip = false;
96
+ } else {
97
+ next = next.parentNode;
98
+ skip = true;
99
+ }
100
+ } while (next);
101
+ }
102
+
103
+ static setInterval(interval) {
104
+ timerInterval = interval;
105
+ checkTimer();
106
+ }
107
+ }
108
+
109
+ export function oneFocusOut(component, el, callback) {
110
+ if (!component.oneFocusOut)
111
+ component.oneFocusOut = FocusManager.oneFocusOut(el, (focus) => {
112
+ delete component.oneFocusOut;
113
+ callback(focus);
114
+ });
115
+ }
116
+
117
+ export function offFocusOut(component) {
118
+ if (component.oneFocusOut) {
119
+ component.oneFocusOut();
120
+ delete component.oneFocusOut;
121
+ }
122
+ }
123
+
124
+ export function preventFocus(e) {
125
+ //Focus can be prevented only on mousedown event. On touchstart this will not work
126
+ //preventDefault cannot be used as it prevents scrolling
127
+ if (e.type !== "mousedown") return;
128
+
129
+ e.preventDefault();
130
+
131
+ unfocusElement(e.currentTarget, false);
132
+ }
133
+
134
+ function checkTimer() {
135
+ let shouldRun = !subscribers.isEmpty();
136
+
137
+ if (shouldRun && !timerId)
138
+ timerId = setInterval(() => {
139
+ FocusManager.nudge();
140
+ }, timerInterval);
141
+
142
+ if (!shouldRun && timerId) {
143
+ clearInterval(timerId);
144
+ timerId = null;
145
+ }
146
+ }
147
+
148
+ export function preventFocusOnTouch(e, force = false) {
149
+ if (force || isTouchEvent()) preventFocus(e);
150
+ }
151
+
152
+ export function unfocusElement(target = null, unfocusParentOverlay = true) {
153
+ const activeElement = getActiveElement();
154
+ if (!target) target = activeElement;
155
+
156
+ if (unfocusParentOverlay) {
157
+ let focusableOverlayContainer = closestParent(target, (el) => el.dataset?.focusableOverlayContainer);
158
+ if (focusableOverlayContainer) target = focusableOverlayContainer;
159
+ }
160
+
161
+ //find the closest focusable parent of the target element and focus it instead
162
+ let focusableParent = closestParent(
163
+ target,
164
+ (el) => isFocusable(el) && (!unfocusParentOverlay || el.dataset?.focusableOverlayContainer)
165
+ );
166
+
167
+ if (focusableParent && focusableParent !== document.body) focusableParent.focus();
168
+ else activeElement.blur();
169
+
170
+ FocusManager.nudge();
171
+ }
@@ -1,83 +1,86 @@
1
- import * as Cx from "../../core";
2
- import { FieldProps } from "./Field";
3
-
4
- export interface DateTimeFieldProps extends FieldProps {
5
- /** Selected date. This should be a Date object or a valid date string consumable by Date.parse function. */
6
- value?: Cx.Prop<string | Date>;
7
-
8
- /** Defaults to false. Used to make the field read-only. */
9
- readOnly?: Cx.BooleanProp;
10
-
11
- /** The opposite of `disabled`. */
12
- enabled?: Cx.BooleanProp;
13
-
14
- /** Default text displayed when the field is empty. */
15
- placeholder?: Cx.StringProp;
16
-
17
- /** Minimum date value. This should be a Date object or a valid date string consumable by Date.parse function. */
18
- minValue?: Cx.Prop<string | Date>;
19
-
20
- /** Set to `true` to disallow the `minValue`. Default value is `false`. */
21
- minExclusive?: Cx.BooleanProp;
22
-
23
- /** Maximum date value. This should be a Date object or a valid date string consumable by Date.parse function. */
24
- maxValue?: Cx.Prop<string | Date>;
25
-
26
- /** Set to `true` to disallow the `maxValue`. Default value is `false`. */
27
- maxExclusive?: Cx.BooleanProp;
28
-
29
- /** Date format used to display the selected date. See Formatting for more details. */
30
- format?: Cx.StringProp;
31
-
32
- /** Base CSS class to be applied to the field. Defaults to `datefield`. */
33
- baseClass?: string;
34
-
35
- /** Maximum value error text. */
36
- maxValueErrorText?: string;
37
-
38
- /** Maximum exclusive value error text. */
39
- maxExclusiveErrorText?: string;
40
-
41
- /** Minimum value error text. */
42
- minValueErrorText?: string;
43
-
44
- /** Minimum exclusive value error text. */
45
- minExclusiveErrorText?: string;
46
-
47
- /** Error message used to indicate wrong user input, e.g. invalid date entered. */
48
- inputErrorText?: string;
49
-
50
- /** Name of the icon to be put on the left side of the input. */
51
- icon?: string;
52
-
53
- /** Set to false to hide the clear button. It can be used interchangeably with the hideClear property. Default value is true. */
54
- showClear?: boolean;
55
-
56
- /**
57
- * Set to `true` to display the clear button even if `required` is set. Default is `false`.
58
- */
59
- alwaysShowClear?: boolean;
60
-
61
- /** Set to true to hide the clear button. It can be used interchangeably with the showClear property. Default value is false. */
62
- hideClear?: boolean;
63
-
64
- /** Determines which segment of date/time is used. Default value is `datetime`. */
65
- segment?: "date" | "time" | "datetime";
66
-
67
- /** Set to `true` to indicate that only one segment of the selected date is affected. */
68
- partial?: boolean;
69
-
70
- /** The function that will be used to convert Date objects before writing data to the store.
71
- * Default implementation is Date.toISOString.
72
- * See also Culture.setDefaultDateEncoding.
73
- */
74
- encoding?: (date: Date) => any;
75
-
76
- /** Defines which days of week should be displayed as disabled, i.e. `[0, 6]` will make Sunday and Saturday unselectable. */
77
- disabledDaysOfWeek?: number[];
78
-
79
- /** Set to true to focus the input field instead of the picker first. */
80
- focusInputFirst?: boolean;
81
- }
82
-
83
- export class DateTimeField extends Cx.Widget<DateTimeFieldProps> {}
1
+ import * as Cx from "../../core";
2
+ import { FieldProps } from "./Field";
3
+
4
+ export interface DateTimeFieldProps extends FieldProps {
5
+ /** Selected date. This should be a Date object or a valid date string consumable by Date.parse function. */
6
+ value?: Cx.Prop<string | Date>;
7
+
8
+ /** Defaults to false. Used to make the field read-only. */
9
+ readOnly?: Cx.BooleanProp;
10
+
11
+ /** The opposite of `disabled`. */
12
+ enabled?: Cx.BooleanProp;
13
+
14
+ /** Default text displayed when the field is empty. */
15
+ placeholder?: Cx.StringProp;
16
+
17
+ /** Minimum date value. This should be a Date object or a valid date string consumable by Date.parse function. */
18
+ minValue?: Cx.Prop<string | Date>;
19
+
20
+ /** Set to `true` to disallow the `minValue`. Default value is `false`. */
21
+ minExclusive?: Cx.BooleanProp;
22
+
23
+ /** Maximum date value. This should be a Date object or a valid date string consumable by Date.parse function. */
24
+ maxValue?: Cx.Prop<string | Date>;
25
+
26
+ /** Set to `true` to disallow the `maxValue`. Default value is `false`. */
27
+ maxExclusive?: Cx.BooleanProp;
28
+
29
+ /** Date format used to display the selected date. See Formatting for more details. */
30
+ format?: Cx.StringProp;
31
+
32
+ /** Base CSS class to be applied to the field. Defaults to `datefield`. */
33
+ baseClass?: string;
34
+
35
+ /** Maximum value error text. */
36
+ maxValueErrorText?: string;
37
+
38
+ /** Maximum exclusive value error text. */
39
+ maxExclusiveErrorText?: string;
40
+
41
+ /** Minimum value error text. */
42
+ minValueErrorText?: string;
43
+
44
+ /** Minimum exclusive value error text. */
45
+ minExclusiveErrorText?: string;
46
+
47
+ /** Error message used to indicate wrong user input, e.g. invalid date entered. */
48
+ inputErrorText?: string;
49
+
50
+ /** Name of the icon to be put on the left side of the input. */
51
+ icon?: string;
52
+
53
+ /** Set to false to hide the clear button. It can be used interchangeably with the hideClear property. Default value is true. */
54
+ showClear?: boolean;
55
+
56
+ /**
57
+ * Set to `true` to display the clear button even if `required` is set. Default is `false`.
58
+ */
59
+ alwaysShowClear?: boolean;
60
+
61
+ /** Set to true to hide the clear button. It can be used interchangeably with the showClear property. Default value is false. */
62
+ hideClear?: boolean;
63
+
64
+ /** Determines which segment of date/time is used. Default value is `datetime`. */
65
+ segment?: "date" | "time" | "datetime";
66
+
67
+ /** Set to `true` to indicate that only one segment of the selected date is affected. */
68
+ partial?: boolean;
69
+
70
+ /** The function that will be used to convert Date objects before writing data to the store.
71
+ * Default implementation is Date.toISOString.
72
+ * See also Culture.setDefaultDateEncoding.
73
+ */
74
+ encoding?: (date: Date) => any;
75
+
76
+ /** Defines which days of week should be displayed as disabled, i.e. `[0, 6]` will make Sunday and Saturday unselectable. */
77
+ disabledDaysOfWeek?: number[];
78
+
79
+ /** Set to true to focus the input field instead of the picker first. */
80
+ focusInputFirst?: boolean;
81
+
82
+ /** Set to true to enable seconds segment in the picker. */
83
+ showSeconds?: boolean;
84
+ }
85
+
86
+ export class DateTimeField extends Cx.Widget<DateTimeFieldProps> {}