@dile/ui 3.6.6 → 3.7.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.
@@ -3,6 +3,7 @@ export { DileInputIcon } from './src/DileInputIcon.js';
3
3
  export { DileInputInteger } from './src/DileInputInteger.js';
4
4
  export { DileInputMessage } from './src/DileInputMessage.js';
5
5
  export { DileInputMoney } from './src/DileInputMoney.js';
6
+ export { DileInputNumber } from './src/DileInputNumber.js';
6
7
  export { DileInputNumberMask } from './src/DileInputNumberMask.js';
7
8
  export { DileInputPercentage } from './src/DileInputPercentage.js';
8
9
  export { DileInputSearch } from './src/DileInputSearch.js';
@@ -0,0 +1,5 @@
1
+ import { DileInputNumber } from './src/DileInputNumber.js';
2
+
3
+ if (!customElements.get('dile-input-number')) {
4
+ window.customElements.define('dile-input-number', DileInputNumber);
5
+ }
@@ -0,0 +1,96 @@
1
+ import { describe, it, expect, afterEach } from 'vitest';
2
+ import './input-number.js';
3
+
4
+ describe('dile-input-number', () => {
5
+ afterEach(() => {
6
+ document.body.innerHTML = '';
7
+ });
8
+
9
+ async function renderInputNumber(content) {
10
+ document.body.innerHTML = content;
11
+ const el = document.body.querySelector('dile-input-number');
12
+ await el.updateComplete;
13
+ return el;
14
+ }
15
+
16
+ it('normalizes comma separator to dot by default', async () => {
17
+ const el = await renderInputNumber('<dile-input-number name="amount"></dile-input-number>');
18
+ const input = el.shadowRoot.querySelector('input');
19
+
20
+ input.value = '12,75';
21
+ input.dispatchEvent(new Event('input'));
22
+ await el.updateComplete;
23
+
24
+ expect(el.value).toBe('12.75');
25
+ expect(input.value).toBe('12.75');
26
+ });
27
+
28
+ it('normalizes dot separator to comma when configured', async () => {
29
+ const el = await renderInputNumber('<dile-input-number name="amount" decimalSeparator=","></dile-input-number>');
30
+ const input = el.shadowRoot.querySelector('input');
31
+
32
+ input.value = '12.75';
33
+ input.dispatchEvent(new Event('input'));
34
+ await el.updateComplete;
35
+
36
+ expect(el.value).toBe('12,75');
37
+ expect(input.value).toBe('12,75');
38
+ });
39
+
40
+ it('applies min, max and decimals on blur', async () => {
41
+ const el = await renderInputNumber('<dile-input-number name="amount" min="1" max="10" decimals="2"></dile-input-number>');
42
+ const input = el.shadowRoot.querySelector('input');
43
+
44
+ input.value = '10.987';
45
+ input.dispatchEvent(new Event('blur'));
46
+ await el.updateComplete;
47
+
48
+ expect(el.value).toBe('10.00');
49
+ expect(input.value).toBe('10.00');
50
+ });
51
+
52
+ it('applies step rounding and decimal separator formatting on blur', async () => {
53
+ const el = await renderInputNumber('<dile-input-number name="amount" decimalSeparator="," step="0.25" decimals="2"></dile-input-number>');
54
+ const input = el.shadowRoot.querySelector('input');
55
+
56
+ input.value = '1.37';
57
+ input.dispatchEvent(new Event('blur'));
58
+ await el.updateComplete;
59
+
60
+ expect(el.value).toBe('1,25');
61
+ expect(input.value).toBe('1,25');
62
+ });
63
+
64
+ it('normalizes separator only on blur when normalizeOn is blur', async () => {
65
+ const el = await renderInputNumber('<dile-input-number name="amount" decimalSeparator="," normalizeOn="blur"></dile-input-number>');
66
+ const input = el.shadowRoot.querySelector('input');
67
+
68
+ input.value = '12.75';
69
+ input.dispatchEvent(new Event('input'));
70
+ await el.updateComplete;
71
+
72
+ expect(el.value).toBe('12.75');
73
+ expect(input.value).toBe('12.75');
74
+
75
+ input.dispatchEvent(new Event('blur'));
76
+ await el.updateComplete;
77
+
78
+ expect(el.value).toBe('12,75');
79
+ expect(input.value).toBe('12,75');
80
+ });
81
+
82
+ it('falls back to input normalization when normalizeOn is invalid', async () => {
83
+ const el = await renderInputNumber('<dile-input-number name="amount" decimalSeparator="," normalizeOn="foo"></dile-input-number>');
84
+ const input = el.shadowRoot.querySelector('input');
85
+
86
+ await el.updateComplete;
87
+ expect(el.normalizeOn).toBe('input');
88
+
89
+ input.value = '12.75';
90
+ input.dispatchEvent(new Event('input'));
91
+ await el.updateComplete;
92
+
93
+ expect(el.value).toBe('12,75');
94
+ expect(input.value).toBe('12,75');
95
+ });
96
+ });
@@ -0,0 +1,282 @@
1
+ import { css } from 'lit';
2
+ import { DileInput } from './DileInput.js';
3
+
4
+ export class DileInputNumber extends DileInput {
5
+ static styles = [
6
+ DileInput.styles,
7
+ css`
8
+ :host {
9
+ display: block;
10
+ }
11
+ input {
12
+ text-align: var(--dile-input-number-text-align, var(--dile-input-text-align, right));
13
+ }
14
+ `
15
+ ];
16
+
17
+ static get properties() {
18
+ return {
19
+ decimalSeparator: { type: String },
20
+ allowNegative: { type: Boolean },
21
+ min: { type: Number },
22
+ max: { type: Number },
23
+ step: { type: String },
24
+ decimals: { type: Number },
25
+ normalizeOn: { type: String },
26
+ };
27
+ }
28
+
29
+ constructor() {
30
+ super();
31
+ this.type = 'text';
32
+ this.decimalSeparator = '.';
33
+ this.allowNegative = true;
34
+ this.min = null;
35
+ this.max = null;
36
+ this.step = 'any';
37
+ this.decimals = null;
38
+ this.normalizeOn = 'input';
39
+ this.changed = false;
40
+ }
41
+
42
+ firstUpdated() {
43
+ super.firstUpdated();
44
+ this.syncNativeInputConstraints();
45
+ }
46
+
47
+ get safeDecimalSeparator() {
48
+ return this.decimalSeparator === ',' ? ',' : '.';
49
+ }
50
+
51
+ get alternateDecimalSeparator() {
52
+ return this.safeDecimalSeparator === ',' ? '.' : ',';
53
+ }
54
+
55
+ sanitizeValue(value, options = {}) {
56
+ const normalizeSeparator = options.normalizeSeparator !== false;
57
+ if (value === null || value === undefined) {
58
+ return '';
59
+ }
60
+
61
+ const decimalChars = normalizeSeparator
62
+ ? [this.safeDecimalSeparator]
63
+ : [this.safeDecimalSeparator, this.alternateDecimalSeparator];
64
+ let text = value.toString();
65
+
66
+ if (normalizeSeparator) {
67
+ text = text.replaceAll(this.alternateDecimalSeparator, this.safeDecimalSeparator);
68
+ }
69
+
70
+ let sanitized = '';
71
+ let hasSeparator = false;
72
+
73
+ for (const char of text) {
74
+ if (char >= '0' && char <= '9') {
75
+ sanitized += char;
76
+ continue;
77
+ }
78
+ if (decimalChars.includes(char) && !hasSeparator) {
79
+ sanitized += char;
80
+ hasSeparator = true;
81
+ continue;
82
+ }
83
+ if (char === '-' && this.allowNegative && sanitized.length === 0) {
84
+ sanitized += char;
85
+ }
86
+ }
87
+
88
+ return sanitized;
89
+ }
90
+
91
+ normalizeForForm(value) {
92
+ if (!value) {
93
+ return '';
94
+ }
95
+ return value
96
+ .toString()
97
+ .replace(this.safeDecimalSeparator, '.')
98
+ .replace(this.alternateDecimalSeparator, '.');
99
+ }
100
+
101
+ get normalizeOnBlur() {
102
+ return this.safeNormalizeOn === 'blur';
103
+ }
104
+
105
+ get safeNormalizeOn() {
106
+ return this.normalizeOn === 'blur' ? 'blur' : 'input';
107
+ }
108
+
109
+ toNumber(value) {
110
+ if (value === '' || value === '-' || value === null || value === undefined) {
111
+ return NaN;
112
+ }
113
+ return parseFloat(this.normalizeForForm(value));
114
+ }
115
+
116
+ hasMinConstraint() {
117
+ return this.min !== null && this.min !== undefined && !isNaN(this.min);
118
+ }
119
+
120
+ hasMaxConstraint() {
121
+ return this.max !== null && this.max !== undefined && !isNaN(this.max);
122
+ }
123
+
124
+ hasDecimalsConstraint() {
125
+ return this.decimals !== null && this.decimals !== undefined && !isNaN(this.decimals) && this.decimals >= 0;
126
+ }
127
+
128
+ hasStepConstraint() {
129
+ if (this.step === null || this.step === undefined || this.step === '' || this.step === 'any') {
130
+ return false;
131
+ }
132
+ const numericStep = parseFloat(this.step);
133
+ return !isNaN(numericStep) && numericStep > 0;
134
+ }
135
+
136
+ roundFloating(value) {
137
+ return parseFloat(value.toFixed(12));
138
+ }
139
+
140
+ applyStep(number) {
141
+ if (!this.hasStepConstraint()) {
142
+ return number;
143
+ }
144
+ const numericStep = parseFloat(this.step);
145
+ const base = this.hasMinConstraint() ? this.min : 0;
146
+ const stepped = base + Math.round((number - base) / numericStep) * numericStep;
147
+ return this.roundFloating(stepped);
148
+ }
149
+
150
+ clamp(number) {
151
+ let result = number;
152
+ if (this.hasMinConstraint() && result < this.min) {
153
+ result = this.min;
154
+ }
155
+ if (this.hasMaxConstraint() && result > this.max) {
156
+ result = this.max;
157
+ }
158
+ return result;
159
+ }
160
+
161
+ formatNumber(number) {
162
+ if (this.hasDecimalsConstraint()) {
163
+ return number.toFixed(this.decimals).replace('.', this.safeDecimalSeparator);
164
+ }
165
+ return number.toString().replace('.', this.safeDecimalSeparator);
166
+ }
167
+
168
+ normalizeAndConstrain(value) {
169
+ const sanitized = this.sanitizeValue(value);
170
+ const parsed = this.toNumber(sanitized);
171
+
172
+ if (isNaN(parsed)) {
173
+ return '';
174
+ }
175
+
176
+ let constrained = this.clamp(parsed);
177
+ constrained = this.applyStep(constrained);
178
+ constrained = this.clamp(constrained);
179
+
180
+ if (this.hasDecimalsConstraint()) {
181
+ constrained = parseFloat(constrained.toFixed(this.decimals));
182
+ }
183
+
184
+ return this.formatNumber(constrained);
185
+ }
186
+
187
+ syncNativeInputConstraints() {
188
+ if (!this.el) {
189
+ return;
190
+ }
191
+
192
+ this.el.setAttribute('inputmode', 'decimal');
193
+
194
+ if (this.hasMinConstraint()) {
195
+ this.el.setAttribute('min', this.min.toString());
196
+ } else {
197
+ this.el.removeAttribute('min');
198
+ }
199
+
200
+ if (this.hasMaxConstraint()) {
201
+ this.el.setAttribute('max', this.max.toString());
202
+ } else {
203
+ this.el.removeAttribute('max');
204
+ }
205
+
206
+ if (this.hasStepConstraint()) {
207
+ this.el.setAttribute('step', this.step.toString());
208
+ } else {
209
+ this.el.setAttribute('step', 'any');
210
+ }
211
+ }
212
+
213
+ _input(e) {
214
+ const sanitized = this.sanitizeValue(e.target.value, {
215
+ normalizeSeparator: !this.normalizeOnBlur,
216
+ });
217
+ if (sanitized !== e.target.value) {
218
+ e.target.value = sanitized;
219
+ }
220
+ this.value = sanitized;
221
+
222
+ if (this.hideErrorOnInput && this.errored) {
223
+ this.clearError();
224
+ }
225
+ }
226
+
227
+ doBlur(e) {
228
+ const num = this.normalizeAndConstrain(e.target.value);
229
+
230
+ if (num !== e.target.value || this.changed) {
231
+ this.value = num;
232
+ this.emmitChange();
233
+ this.changed = false;
234
+ }
235
+ }
236
+
237
+ updated(changedProperties) {
238
+ super.updated(changedProperties);
239
+
240
+ if (changedProperties.has('normalizeOn') && this.normalizeOn !== this.safeNormalizeOn) {
241
+ this.normalizeOn = this.safeNormalizeOn;
242
+ return;
243
+ }
244
+
245
+ if (changedProperties.has('decimalSeparator') && this.value) {
246
+ const sanitized = this.sanitizeValue(this.value);
247
+ if (sanitized !== this.value) {
248
+ this.value = sanitized;
249
+ return;
250
+ }
251
+ }
252
+
253
+ if (changedProperties.has('value') && this.value) {
254
+ this.changed = true;
255
+ }
256
+
257
+ if (changedProperties.has('min') || changedProperties.has('max') || changedProperties.has('step') || changedProperties.has('decimals')) {
258
+ if (this.value) {
259
+ const normalized = this.normalizeAndConstrain(this.value);
260
+ if (normalized !== this.value) {
261
+ this.value = normalized;
262
+ return;
263
+ }
264
+ }
265
+ this.syncNativeInputConstraints();
266
+ }
267
+
268
+ if (changedProperties.has('value') || changedProperties.has('decimalSeparator')) {
269
+ this.internals.setFormValue(this.normalizeForForm(this.value));
270
+ }
271
+
272
+ if (changedProperties.has('decimalSeparator') || changedProperties.has('normalizeOn')) {
273
+ this.syncNativeInputConstraints();
274
+ }
275
+ }
276
+
277
+ computeValue(value) {
278
+ return this.sanitizeValue(value, {
279
+ normalizeSeparator: !this.normalizeOnBlur,
280
+ });
281
+ }
282
+ }
@@ -19,22 +19,52 @@ export class DileInputSearch extends LitElement {
19
19
  }
20
20
  div:focus-within {
21
21
  border-color: var(--dile-input-focus-border-color, var(--dile-link-color, #6af));
22
+ /* Outline adicional para mayor visibilidad */
23
+ outline: 2px solid var(--dile-input-focus-border-color, var(--dile-link-color, #6af));
24
+ outline-offset: 2px;
22
25
  }
23
26
  input {
24
27
  flex-grow: 1;
25
28
  border: none;
26
- outline: none;
27
29
  color: var(--dile-input-color, #303030);
28
30
  font-size: var(--dile-input-font-size, 1em);
29
31
  line-height: var(--dile-input-line-height, 1.5em);
30
32
  background-color: var(--dile-input-background-color, #fff);
33
+ padding: 2px 4px;
34
+ }
35
+ input:focus {
36
+ outline: none;
31
37
  }
32
38
  input::placeholder {
33
- color: var(--dile-input-placeholder-color, #ccc);
39
+ /* WCAG AA contrast: #666 sobre #fff = 7.5:1 */
40
+ color: var(--dile-input-placeholder-color, #666);
34
41
  }
35
- dile-icon {
42
+ .icon-button {
43
+ background: none;
44
+ border: none;
45
+ cursor: pointer;
46
+ padding: 4px;
36
47
  margin-left: 1px;
37
48
  display: flex;
49
+ align-items: center;
50
+ justify-content: center;
51
+ border-radius: 3px;
52
+ color: var(--dile-input-color, #303030);
53
+ font-size: 1em;
54
+ }
55
+ .icon-button:focus {
56
+ outline: 2px solid var(--dile-input-focus-border-color, var(--dile-link-color, #6af));
57
+ outline-offset: 1px;
58
+ }
59
+ .icon-button:hover:not(:disabled) {
60
+ background-color: var(--dile-input-hover-background, rgba(0,0,0,0.05));
61
+ }
62
+ .icon-button:disabled {
63
+ cursor: not-allowed;
64
+ opacity: 0.5;
65
+ }
66
+ dile-icon {
67
+ display: flex;
38
68
  }
39
69
  :host([disabled]) {
40
70
  opacity: 0.5;
@@ -42,6 +72,12 @@ export class DileInputSearch extends LitElement {
42
72
  .errored {
43
73
  border-color: var(--dile-input-error-border-color, #c00);
44
74
  }
75
+ .error-message {
76
+ color: var(--dile-input-error-border-color, #c00);
77
+ font-size: 0.875em;
78
+ margin-top: 4px;
79
+ padding: 4px 0;
80
+ }
45
81
  `
46
82
  ];
47
83
 
@@ -72,6 +108,10 @@ export class DileInputSearch extends LitElement {
72
108
  }
73
109
 
74
110
  render() {
111
+ const iconLabel = this.value.length
112
+ ? 'Limpiar búsqueda'
113
+ : 'Buscar';
114
+
75
115
  return html`
76
116
  <div class="${this.errored ? "errored" : ""}">
77
117
  <input
@@ -83,12 +123,29 @@ export class DileInputSearch extends LitElement {
83
123
  .value="${this.value}"
84
124
  ?disabled="${this.disabled}"
85
125
  ?readonly="${this.readOnly}"
126
+ aria-label="${this.name || 'Búsqueda'}"
127
+ ?aria-invalid="${this.errored}"
128
+ aria-describedby="${this.errored ? 'error-msg' : null}"
86
129
  >
87
- <dile-icon
130
+ <button
88
131
  @click=${this.iconClick}
89
- .icon="${this.value.length ? clearIcon : searchIcon}"
90
- ></dile-icon>
132
+ @keydown=${this.iconKeydown}
133
+ class="icon-button"
134
+ aria-label="${iconLabel}"
135
+ type="button"
136
+ ?disabled="${this.disabled}"
137
+ >
138
+ <dile-icon
139
+ .icon="${this.value.length ? clearIcon : searchIcon}"
140
+ aria-hidden="true"
141
+ ></dile-icon>
142
+ </button>
91
143
  </div>
144
+ ${this.errored ? html`
145
+ <div id="error-msg" class="error-message" role="alert">
146
+ <slot name="error"></slot>
147
+ </div>
148
+ ` : ''}
92
149
  `;
93
150
  }
94
151
 
@@ -107,6 +164,15 @@ export class DileInputSearch extends LitElement {
107
164
  this.input.focus();
108
165
  }
109
166
 
167
+ iconKeydown(e) {
168
+ // Permitir Space y Enter para activar el icono
169
+ if (e.key === ' ' || e.key === 'Enter') {
170
+ e.preventDefault();
171
+ this.clear();
172
+ this.input.focus();
173
+ }
174
+ }
175
+
110
176
  dispatchSearch(key) {
111
177
  this.dispatchEvent(new CustomEvent('dile-input-search', {
112
178
  bubbles: true,
@@ -137,6 +137,17 @@ export const DileForm = (superclass) => class extends superclass {
137
137
  }
138
138
  }
139
139
 
140
+ clearField(name) {
141
+ const node = this.getNodeElement(name);
142
+ if (node) {
143
+ if (typeof node.clear === "function") {
144
+ node.clear();
145
+ } else {
146
+ node.value = '';
147
+ }
148
+ }
149
+ }
150
+
140
151
  showError(name, error) {
141
152
  const parts = name.split('.');
142
153
 
@@ -82,10 +82,6 @@ export const DileOverlay = function(superClass) {
82
82
  * closes the overlay
83
83
  */
84
84
  close() {
85
- this.dispatchEvent(new CustomEvent('overlay-closed', {
86
- bubbles: true,
87
- composed: true,
88
- }));
89
85
  if(!this._opening && this._overlayClass=='opened') {
90
86
  this._overlayClass = '';
91
87
  this._removePositionListeners();
@@ -93,6 +89,10 @@ export const DileOverlay = function(superClass) {
93
89
  this.delayId = setTimeout(() => {
94
90
  this.overlay.style.display = 'none'
95
91
  }, 500);
92
+ this.dispatchEvent(new CustomEvent('overlay-closed', {
93
+ bubbles: true,
94
+ composed: true,
95
+ }));
96
96
  }
97
97
  }
98
98
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/ui",
3
- "version": "3.6.6",
3
+ "version": "3.7.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": "3bc0a2459a5ced3e7105ce061f93f07a9c798759"
31
+ "gitHead": "62016dd8f5a9bd165131bc2058be322eb3aea339"
32
32
  }