@dile/ui 3.6.7 → 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,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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dile/ui",
|
|
3
|
-
"version": "3.
|
|
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": "
|
|
31
|
+
"gitHead": "62016dd8f5a9bd165131bc2058be322eb3aea339"
|
|
32
32
|
}
|