@dile/utils 3.4.1 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,13 @@
1
1
  import { html, css, LitElement } from 'lit';
2
2
  import { getCountries } from './countriesData.js';
3
3
  import '@dile/ui/components/select/select.js';
4
+ import { DileNativeValidity } from '@dile/ui/mixins/form/index.js';
5
+
6
+ export class DileCountrySelect extends DileNativeValidity(LitElement) {
7
+ static get formAssociated() {
8
+ return true;
9
+ }
4
10
 
5
- export class DileCountrySelect extends LitElement {
6
11
  static styles = [
7
12
  css`
8
13
  :host {
@@ -16,6 +21,8 @@ export class DileCountrySelect extends LitElement {
16
21
  value: { type: String },
17
22
  name: { type: String },
18
23
  label: { type: String },
24
+ /** Marks the field as required: shows the required indicator in the label and sets aria-required */
25
+ required: { type: Boolean, reflect: true },
19
26
  language: { type: String },
20
27
  errored: { type: Boolean },
21
28
  message: { type: String },
@@ -37,9 +44,23 @@ export class DileCountrySelect extends LitElement {
37
44
  this.hideErrorOnInput = false;
38
45
  this.priorityCountries = '';
39
46
  this.selectChanged = this.selectChanged.bind(this);
47
+ this.internals = this.attachInternals();
48
+ }
49
+
50
+ firstUpdated() {
51
+ this._defaultValue = this.value;
52
+ }
53
+
54
+ /** Called by the browser when the owner form is reset: restores the initial value */
55
+ formResetCallback() {
56
+ super.formResetCallback?.();
57
+ this.value = this._defaultValue;
40
58
  }
41
59
 
42
60
  updated(changedProperties) {
61
+ if (changedProperties.has('value')) {
62
+ this.internals.setFormValue(this.value ?? '');
63
+ }
43
64
  if (changedProperties.has('language') || changedProperties.has('priorityCountries')) {
44
65
  this.requestUpdate();
45
66
  }
@@ -116,6 +137,7 @@ export class DileCountrySelect extends LitElement {
116
137
  name="${this.name}"
117
138
  label="${label}"
118
139
  ?errored=${this.errored}
140
+ ?required=${this.required}
119
141
  ?hideErrorOnInput=${this.hideErrorOnInput}
120
142
  message="${this.message || ''}"
121
143
  @element-changed=${this.selectChanged}
@@ -331,6 +331,16 @@ export class DileDatepicker extends DileCloseOnEscPressed(DileInputIcon) {
331
331
  return { valid: true, date };
332
332
  }
333
333
 
334
+ /**
335
+ * Error of the current value, without changing the component state (used by DileForm).
336
+ * Works regardless of validateDate. An empty value is valid.
337
+ * @returns {string} the error message, or '' when the value is valid
338
+ */
339
+ getValidationError() {
340
+ const result = this.getDateValidation();
341
+ return result.valid ? '' : this.getValidationMessage(result.reason);
342
+ }
343
+
334
344
  /**
335
345
  * Validates the current value, showing or removing the error message.
336
346
  * @returns {boolean} true when the value is valid
@@ -343,7 +353,7 @@ export class DileDatepicker extends DileCloseOnEscPressed(DileInputIcon) {
343
353
  this.message = this._stateBeforeDateError.message ?? '';
344
354
  this._dateErrorShown = false;
345
355
  }
346
- this.internals?.setValidity({});
356
+ this.updateValidity();
347
357
  return true;
348
358
  }
349
359
  if (!this._dateErrorShown) {
@@ -352,7 +362,7 @@ export class DileDatepicker extends DileCloseOnEscPressed(DileInputIcon) {
352
362
  }
353
363
  this.errored = true;
354
364
  this.message = this.getValidationMessage(result.reason);
355
- this.internals?.setValidity({ customError: true }, this.message, this.el);
365
+ this.updateValidity();
356
366
  return false;
357
367
  }
358
368
 
@@ -12,6 +12,11 @@ const PREFIX_PLACEHOLDERS = {
12
12
  en: 'Prefix',
13
13
  };
14
14
 
15
+ const VALIDATION_MESSAGES = {
16
+ en: 'Enter a valid phone number.',
17
+ es: 'Introduce un número de teléfono válido.',
18
+ };
19
+
15
20
  // E.164: up to 15 digits in total, prefix included.
16
21
  const MAX_DIGITS = 15;
17
22
  const MIN_NUMBER_DIGITS = 4;
@@ -32,8 +37,10 @@ export class DileInputPhone extends DileInput {
32
37
  noPrefix: { type: Boolean },
33
38
  /** ISO code of the country of the prefix. Set it to preselect a default prefix. */
34
39
  country: { type: String },
35
- /** Language of the prefix selector: 'es' or 'en' */
40
+ /** Language of the prefix selector and of the validation message: 'es' or 'en' */
36
41
  language: { type: String },
42
+ /** Message returned by getValidationError() for an invalid number. Overrides the default one */
43
+ invalidMessage: { type: String },
37
44
  /** ISO codes of the countries listed first in the prefix selector, separated by "|" */
38
45
  priorityCountries: { type: String, attribute: 'priority-countries' },
39
46
  /** Placeholder of the prefix selector */
@@ -269,6 +276,18 @@ export class DileInputPhone extends DileInput {
269
276
  return Boolean(split) && split.number.length >= MIN_NUMBER_DIGITS;
270
277
  }
271
278
 
279
+ /**
280
+ * Error of the current value, without changing the component state (used by DileForm).
281
+ * Based on isValid(). An empty value is valid.
282
+ * @returns {string} the error message, or '' when the value is valid
283
+ */
284
+ getValidationError() {
285
+ if (this.isValid()) {
286
+ return '';
287
+ }
288
+ return this.invalidMessage || VALIDATION_MESSAGES[this.language] || VALIDATION_MESSAGES.en;
289
+ }
290
+
272
291
  clear() {
273
292
  this._typedNumber = '';
274
293
  this.country = this._initialCountry;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/utils",
3
- "version": "3.4.1",
3
+ "version": "3.5.0",
4
4
  "description": "Utility Components of Diverse Uses Based on the Lit Library and Web Components Standard.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,7 +21,7 @@
21
21
  "homepage": "https://dile-components.com/",
22
22
  "dependencies": {
23
23
  "@dile/iconlib": "^2.0.2",
24
- "@dile/ui": "^3.23.0",
24
+ "@dile/ui": "^3.24.0",
25
25
  "@lion/ui": "^0.27.0",
26
26
  "linkify-string": "^4.1.3",
27
27
  "linkifyjs": "^4.1.3",
@@ -31,5 +31,5 @@
31
31
  "publishConfig": {
32
32
  "access": "public"
33
33
  },
34
- "gitHead": "828a98ebe286539d42d66f8acf6d5efcdb463136"
34
+ "gitHead": "67268dcc3bb2972b55da7d971a20ddbfa0bcbd0d"
35
35
  }