@dile/ui 3.23.0 → 3.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/components/checkbox/src/DileCheckbox.js +14 -5
  2. package/components/drop-file/src/DileDropFile.js +5 -3
  3. package/components/input/index.js +1 -1
  4. package/components/input/src/DileInput.js +9 -3
  5. package/components/input/src/DileInputMask.js +16 -0
  6. package/components/input/src/DileInputSearch.js +2 -0
  7. package/components/input/src/label-styles.js +15 -0
  8. package/components/input-suggestions/src/DileInputSuggestionsBase.js +6 -3
  9. package/components/number-picker/src/DileNumberPicker.js +6 -3
  10. package/components/number-picker/src/DileNumberPickerElement.js +2 -0
  11. package/components/order-switch/src/DileOrderSwitch.js +18 -2
  12. package/components/otp-input/src/DileOtpInput.js +6 -3
  13. package/components/radio-group/src/DileRadioGroup.js +6 -3
  14. package/components/rating-scale-question/src/DileRatingScaleQuestion.js +35 -3
  15. package/components/select/src/DileSelect.js +12 -4
  16. package/components/select/src/DileSelectAjax.js +8 -3
  17. package/components/select/src/DileSelectBoolean.js +9 -2
  18. package/components/select/src/DileSelectOverlay.js +6 -3
  19. package/components/select/src/DileSelectSearchableBase.js +8 -3
  20. package/components/switch/src/DileSwitch.js +3 -1
  21. package/components/textarea/src/DileTextarea.js +7 -3
  22. package/components/time-picker/src/DileTimePicker.js +680 -126
  23. package/components/wheel-picker/src/DileWheelPicker.js +17 -2
  24. package/lib/time/time.js +171 -0
  25. package/lib/time/time.test.js +63 -0
  26. package/mixins/form/index.js +2 -1
  27. package/mixins/form/src/DileForm.js +240 -1
  28. package/mixins/form/src/DileNativeValidity.js +195 -0
  29. package/mixins/overlay/src/DileOverlay.js +16 -0
  30. package/package.json +2 -2
@@ -0,0 +1,195 @@
1
+ import { FORM_VALIDATION_MESSAGES, isEmptyValue } from './DileForm.js';
2
+
3
+ const FOCUSABLE_SELECTOR = 'input:not([type="hidden"]), select, textarea, button, [tabindex]:not([tabindex="-1"]), [contenteditable="true"]';
4
+
5
+ /**
6
+ *
7
+ * # DileNativeValidity
8
+ *
9
+ * Mixin for form-associated fields (`static formAssociated = true` and `this.internals`).
10
+ * It keeps the native validity of the field up to date, so the field behaves like a native
11
+ * control inside a plain `<form>`:
12
+ *
13
+ * - `valueMissing` when the field is `required` and empty (the field's `isEmpty()` or `isEmptyValue()`).
14
+ * - `customError` with the message of `setCustomValidity()` or the field's `getValidationError()`.
15
+ *
16
+ * It also exposes the standard API: `validity`, `validationMessage`, `willValidate`,
17
+ * `checkValidity()`, `reportValidity()` and `setCustomValidity()`.
18
+ *
19
+ * The validity is updated after every render of the field. The rules of `@dile/validation`
20
+ * are not used here: they need a DileForm with the DileFormValidation mixin.
21
+ *
22
+ * Browsers do not apply `:user-invalid` to custom elements, so the field gets the `user-invalid`
23
+ * attribute (and the `user-invalid` custom state) when it is invalid after the user left it or
24
+ * tried to submit its form. A form reset removes it.
25
+ *
26
+ */
27
+ export const DileNativeValidity = (superclass) => class extends superclass {
28
+
29
+ constructor() {
30
+ super();
31
+ this._customValidityMessage = '';
32
+ this._validityRun = 0;
33
+ this._userInteracted = false;
34
+ this.addEventListener('focusout', (e) => {
35
+ // Focus moving inside the field (its shadow DOM is retargeted to the host) is not leaving it
36
+ if (e.relatedTarget !== this && !this.contains(e.relatedTarget)) {
37
+ this._markUserInteracted();
38
+ }
39
+ });
40
+ this.addEventListener('invalid', () => this._markUserInteracted());
41
+ // A controller runs after every update, even when a subclass does not call super.updated()
42
+ this.addController({
43
+ hostUpdated: () => this.updateValidity(),
44
+ });
45
+ }
46
+
47
+ /** The ValidityState of the field */
48
+ get validity() {
49
+ return this.internals?.validity;
50
+ }
51
+
52
+ /** The message of the current validation error, or '' */
53
+ get validationMessage() {
54
+ return this.internals?.validationMessage ?? '';
55
+ }
56
+
57
+ /** True when the field takes part in the constraint validation of its form */
58
+ get willValidate() {
59
+ return this.internals?.willValidate ?? false;
60
+ }
61
+
62
+ /** Returns true when the field is valid. Otherwise dispatches an `invalid` event and returns false */
63
+ checkValidity() {
64
+ this.updateValidity();
65
+ return this.internals?.checkValidity() ?? true;
66
+ }
67
+
68
+ /** Like checkValidity(), and the browser shows the error message to the user */
69
+ reportValidity() {
70
+ this.updateValidity();
71
+ return this.internals?.reportValidity() ?? true;
72
+ }
73
+
74
+ /** Sets a custom error message, like in native controls. An empty string removes it */
75
+ setCustomValidity(message) {
76
+ this._customValidityMessage = message ? String(message) : '';
77
+ this.updateValidity();
78
+ }
79
+
80
+ /** True when the field is invalid after the user left it or tried to submit the form */
81
+ get userInvalid() {
82
+ return this._userInteracted && this.internals?.validity?.valid === false;
83
+ }
84
+
85
+ /** Called by the browser when the owner form is reset. Subclasses overriding it must call super */
86
+ formResetCallback() {
87
+ super.formResetCallback?.();
88
+ this._userInteracted = false;
89
+ this._updateUserInvalid();
90
+ }
91
+
92
+ _markUserInteracted() {
93
+ if (!this._userInteracted) {
94
+ this._userInteracted = true;
95
+ this._updateUserInvalid();
96
+ }
97
+ }
98
+
99
+ /** Reflects userInvalid in the user-invalid attribute and custom state */
100
+ _updateUserInvalid() {
101
+ const userInvalid = this.userInvalid;
102
+ if (this.hasAttribute('user-invalid') !== userInvalid) {
103
+ this.toggleAttribute('user-invalid', userInvalid);
104
+ }
105
+ const states = this.internals?.states;
106
+ if (states) {
107
+ try {
108
+ userInvalid ? states.add('user-invalid') : states.delete('user-invalid');
109
+ } catch (error) {
110
+ // Old browsers only accept the legacy --state syntax: the attribute is enough there
111
+ }
112
+ }
113
+ }
114
+
115
+ /** Element the browser points to when it reports the error: the inner control of the field */
116
+ get validityAnchor() {
117
+ const el = this.el;
118
+ if (el instanceof HTMLElement) {
119
+ return el;
120
+ }
121
+ return this.shadowRoot?.querySelector(FOCUSABLE_SELECTOR) ?? undefined;
122
+ }
123
+
124
+ /** True when the field is disabled or readonly, so it is not validated */
125
+ get _isValidityBarred() {
126
+ return Boolean(this.disabled || this.isDisabled || this.readonly);
127
+ }
128
+
129
+ /** True when the field has no value, using its isEmpty() method if it has one */
130
+ get _isEmptyForValidity() {
131
+ return typeof this.isEmpty === 'function' ? this.isEmpty() : isEmptyValue(this.value);
132
+ }
133
+
134
+ /** Message of the native valueMissing error, in the language of the field or the document */
135
+ get requiredValidationMessage() {
136
+ const lang = this.closest?.('[lang]')?.getAttribute('lang') || document.documentElement.lang || 'en';
137
+ const messages = FORM_VALIDATION_MESSAGES[lang.slice(0, 2).toLowerCase()] || FORM_VALIDATION_MESSAGES.en;
138
+ return messages.required;
139
+ }
140
+
141
+ /** Recomputes the native validity of the field */
142
+ updateValidity() {
143
+ const internals = this.internals;
144
+ if (!internals || typeof internals.setValidity !== 'function') {
145
+ return;
146
+ }
147
+ const run = ++this._validityRun;
148
+ if (this._isValidityBarred) {
149
+ this._setValidity({});
150
+ return;
151
+ }
152
+ if (this.required && this._isEmptyForValidity) {
153
+ this._setValidity({ valueMissing: true }, this.requiredValidationMessage);
154
+ return;
155
+ }
156
+ if (this._customValidityMessage) {
157
+ this._setValidity({ customError: true }, this._customValidityMessage);
158
+ return;
159
+ }
160
+ const ownError = typeof this.getValidationError === 'function' ? this.getValidationError() : '';
161
+ if (ownError && typeof ownError.then === 'function') {
162
+ ownError.then((message) => {
163
+ if (run === this._validityRun) {
164
+ this._setOwnError(message);
165
+ }
166
+ });
167
+ return;
168
+ }
169
+ this._setOwnError(ownError);
170
+ }
171
+
172
+ _setOwnError(message) {
173
+ if (message) {
174
+ this._setValidity({ customError: true }, String(message));
175
+ } else {
176
+ this._setValidity({});
177
+ }
178
+ }
179
+
180
+ _setValidity(flags, message) {
181
+ const hasError = Object.values(flags).some(Boolean);
182
+ if (!hasError) {
183
+ this.internals.setValidity({});
184
+ } else {
185
+ const text = message || 'Invalid value.';
186
+ try {
187
+ this.internals.setValidity(flags, text, this.validityAnchor);
188
+ } catch (error) {
189
+ // The anchor must be inside the field: fall back to the field itself
190
+ this.internals.setValidity(flags, text);
191
+ }
192
+ }
193
+ this._updateUserInvalid();
194
+ }
195
+ }
@@ -54,6 +54,19 @@ export const DileOverlay = function(superClass) {
54
54
  if (this._overlayClass === 'opened') this.updatePosition();
55
55
  });
56
56
  };
57
+ // Content rendered after open() (asynchronous templates, lazy content...) changes the size
58
+ // of the overlay once it was positioned, so it is positioned again when its size changes.
59
+ this._resizeObserver = typeof ResizeObserver !== 'undefined'
60
+ ? new ResizeObserver(() => {
61
+ if (this._opening || this._overlayClass === 'opened') this.updatePosition();
62
+ })
63
+ : null;
64
+ }
65
+
66
+ disconnectedCallback() {
67
+ super.disconnectedCallback();
68
+ this._removePositionListeners();
69
+ this._resizeObserver?.disconnect();
57
70
  }
58
71
 
59
72
  firstUpdated() {
@@ -89,6 +102,7 @@ export const DileOverlay = function(superClass) {
89
102
  this.cancelDelay();
90
103
  this._opening = false;
91
104
  this._overlayClass = '';
105
+ this._resizeObserver?.disconnect();
92
106
  this.overlay.style.display = 'none';
93
107
  this._dispatchClosed();
94
108
  } else if(this._overlayClass=='opened') {
@@ -133,6 +147,7 @@ export const DileOverlay = function(superClass) {
133
147
  if(!this._opening) {
134
148
  this._opening = true;
135
149
  this.updatePosition();
150
+ this._resizeObserver?.observe(this.overlay);
136
151
  // Close the other overlays, even if they are still opening.
137
152
  if(this.closeOthers) {
138
153
  this.closeOthers();
@@ -156,6 +171,7 @@ export const DileOverlay = function(superClass) {
156
171
  _removePositionListeners() {
157
172
  window.removeEventListener('scroll', this._onScrollResize, { capture: true });
158
173
  window.removeEventListener('resize', this._onScrollResize);
174
+ this._resizeObserver?.disconnect();
159
175
  if (this._rafId) {
160
176
  cancelAnimationFrame(this._rafId);
161
177
  this._rafId = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/ui",
3
- "version": "3.23.0",
3
+ "version": "3.24.0",
4
4
  "description": "UI Core components from dile-components.",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -28,5 +28,5 @@
28
28
  "publishConfig": {
29
29
  "access": "public"
30
30
  },
31
- "gitHead": "828a98ebe286539d42d66f8acf6d5efcdb463136"
31
+ "gitHead": "67268dcc3bb2972b55da7d971a20ddbfa0bcbd0d"
32
32
  }