@spectrum-web-components/number-field 0.3.13 → 0.4.1-devmode.7

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,417 +1,380 @@
1
- /*
2
- Copyright 2020 Adobe. All rights reserved.
3
- This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License. You may obtain a copy
5
- of the License at http://www.apache.org/licenses/LICENSE-2.0
6
-
7
- Unless required by applicable law or agreed to in writing, software distributed under
8
- the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- OF ANY KIND, either express or implied. See the License for the specific language
10
- governing permissions and limitations under the License.
11
- */
12
- import { __decorate, __rest } from "tslib";
13
- import { html, } from '@spectrum-web-components/base';
14
- import { property, query, } from '@spectrum-web-components/base/src/decorators.js';
15
- import { streamingListener } from '@spectrum-web-components/base/src/streaming-listener.js';
16
- import { NumberFormatter, NumberParser } from '@internationalized/number';
17
- import '@spectrum-web-components/icons-ui/icons/sp-icon-chevron75.js';
18
- import '@spectrum-web-components/action-button/sp-action-button.js';
19
- import { isAndroid, isIPhone, } from '@spectrum-web-components/shared/src/platform.js';
20
- import { TextfieldBase } from '@spectrum-web-components/textfield';
21
- import chevronStyles from '@spectrum-web-components/icon/src/spectrum-icon-chevron.css.js';
22
- import styles from './number-field.css.js';
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __decorateClass = (decorators, target, key, kind) => {
4
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
5
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
6
+ if (decorator = decorators[i])
7
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
8
+ if (kind && result)
9
+ __defProp(target, key, result);
10
+ return result;
11
+ };
12
+ import {
13
+ html
14
+ } from "@spectrum-web-components/base";
15
+ import {
16
+ property,
17
+ query
18
+ } from "@spectrum-web-components/base/src/decorators.js";
19
+ import { streamingListener } from "@spectrum-web-components/base/src/streaming-listener.js";
20
+ import { NumberFormatter, NumberParser } from "@internationalized/number";
21
+ import "@spectrum-web-components/icons-ui/icons/sp-icon-chevron75.js";
22
+ import "@spectrum-web-components/action-button/sp-action-button.js";
23
+ import {
24
+ isAndroid,
25
+ isIPhone
26
+ } from "@spectrum-web-components/shared/src/platform.js";
27
+ import { TextfieldBase } from "@spectrum-web-components/textfield";
28
+ import chevronStyles from "@spectrum-web-components/icon/src/spectrum-icon-chevron.css.js";
29
+ import styles from "./number-field.css.js";
23
30
  export const FRAMES_PER_CHANGE = 5;
24
- export const indeterminatePlaceholder = '-';
31
+ export const indeterminatePlaceholder = "-";
25
32
  export const remapMultiByteCharacters = {
26
- '1': '1',
27
- '2': '2',
28
- '3': '3',
29
- '4': '4',
30
- '5': '5',
31
- '6': '6',
32
- '7': '7',
33
- '8': '8',
34
- '9': '9',
35
- '0': '0',
36
- '、': ',',
37
- ',': ',',
38
- '。': '.',
39
- '.': '.',
40
- '%': '%',
41
- '+': '+',
42
- ー: '-',
33
+ "\uFF11": "1",
34
+ "\uFF12": "2",
35
+ "\uFF13": "3",
36
+ "\uFF14": "4",
37
+ "\uFF15": "5",
38
+ "\uFF16": "6",
39
+ "\uFF17": "7",
40
+ "\uFF18": "8",
41
+ "\uFF19": "9",
42
+ "\uFF10": "0",
43
+ "\u3001": ",",
44
+ "\uFF0C": ",",
45
+ "\u3002": ".",
46
+ "\uFF0E": ".",
47
+ "\uFF05": "%",
48
+ "\uFF0B": "+",
49
+ \u30FC: "-"
43
50
  };
44
- /**
45
- * @element sp-number-field
46
- * @slot help-text - default or non-negative help text to associate to your form element
47
- * @slot negative-help-text - negative help text to associate to your form element when `invalid`
48
- */
49
51
  export class NumberField extends TextfieldBase {
50
- constructor() {
51
- super(...arguments);
52
- this.focused = false;
53
- this._forcedUnit = '';
54
- /**
55
- * An `<sp-number-field>` element will process its numeric value with
56
- * `new Intl.NumberFormat(this.resolvedLanguage, this.formatOptions).format(this.valueAsNumber)`
57
- * in order to prepare it for visual delivery in the input. In order to customize this
58
- * processing supply your own `Intl.NumberFormatOptions` object here.
59
- *
60
- * See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat
61
- */
62
- this.formatOptions = {};
63
- /**
64
- * Whether the stepper UI is hidden or not.
65
- */
66
- this.hideStepper = false;
67
- this.indeterminate = false;
68
- this.keyboardFocused = false;
69
- this.resolvedLanguage = document.documentElement.lang || navigator.language;
70
- this.stepperActive = false;
71
- this.stepModifier = 10;
72
- this._value = NaN;
73
- this._trackingValue = '';
74
- this.changeCount = 0;
75
- this.wasIndeterminate = false;
76
- }
77
- static get styles() {
78
- return [...super.styles, styles, chevronStyles];
79
- }
80
- set value(rawValue) {
81
- const value = this.validateInput(rawValue);
82
- if (value === this.value) {
83
- return;
84
- }
85
- const oldValue = this._value;
86
- this._value = value;
87
- this.requestUpdate('value', oldValue);
88
- }
89
- get value() {
90
- return this._value;
91
- }
92
- get inputValue() {
93
- return this.indeterminate
94
- ? this.formattedValue
95
- : this.inputElement.value;
96
- }
97
- /**
98
- * Retreive the value of the element parsed to a Number.
99
- */
100
- get valueAsString() {
101
- return this._value.toString();
102
- }
103
- set valueAsString(value) {
104
- this.value = this.numberParser.parse(value);
105
- }
106
- get formattedValue() {
107
- if (isNaN(this.value))
108
- return '';
109
- return (this.numberFormatter.format(this.value) +
110
- (this.focused ? '' : this._forcedUnit));
111
- }
112
- convertValueToNumber(value) {
113
- return this.numberParser.parse(value);
114
- }
115
- get _step() {
116
- var _a;
117
- if (typeof this.step !== 'undefined') {
118
- return this.step;
119
- }
120
- if (((_a = this.formatOptions) === null || _a === void 0 ? void 0 : _a.style) === 'percent') {
121
- return 0.01;
122
- }
123
- return 1;
124
- }
125
- handlePointerdown(event) {
126
- if (event.button !== 0) {
127
- event.preventDefault();
128
- return;
129
- }
130
- this.stepperActive = true;
131
- this.buttons.setPointerCapture(event.pointerId);
132
- const stepUpRect = this.buttons.children[0].getBoundingClientRect();
133
- const stepDownRect = this.buttons.children[1].getBoundingClientRect();
134
- this.findChange = (event) => {
135
- if (event.clientX >= stepUpRect.x &&
136
- event.clientY >= stepUpRect.y &&
137
- event.clientX <= stepUpRect.x + stepUpRect.width &&
138
- event.clientY <= stepUpRect.y + stepUpRect.height) {
139
- this.change = (event) => this.increment(event.shiftKey ? this.stepModifier : 1);
140
- }
141
- else if (event.clientX >= stepDownRect.x &&
142
- event.clientY >= stepDownRect.y &&
143
- event.clientX <= stepDownRect.x + stepDownRect.width &&
144
- event.clientY <= stepDownRect.y + stepDownRect.height) {
145
- this.change = (event) => this.decrement(event.shiftKey ? this.stepModifier : 1);
146
- }
147
- };
148
- this.findChange(event);
149
- this.startChange(event);
150
- }
151
- startChange(event) {
152
- this.changeCount = 0;
153
- this.doChange(event);
154
- this.safty = setTimeout(() => {
155
- this.doNextChange(event);
156
- }, 400);
157
- }
158
- doChange(event) {
159
- this.change(event);
160
- }
161
- handlePointermove(event) {
162
- this.findChange(event);
163
- }
164
- handlePointerup(event) {
165
- this.buttons.releasePointerCapture(event.pointerId);
166
- cancelAnimationFrame(this.nextChange);
167
- clearTimeout(this.safty);
168
- this.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
169
- this.stepperActive = false;
170
- }
171
- doNextChange(event) {
172
- this.changeCount += 1;
173
- if (this.changeCount % FRAMES_PER_CHANGE === 0) {
174
- this.doChange(event);
175
- }
176
- return requestAnimationFrame(() => {
177
- this.nextChange = this.doNextChange(event);
178
- });
179
- }
180
- stepBy(count) {
181
- if (this.disabled || this.readonly) {
182
- return;
183
- }
184
- const min = typeof this.min !== 'undefined' ? this.min : 0;
185
- let value = this.value;
186
- value += count * this._step;
187
- if (isNaN(this.value)) {
188
- this.value = min;
189
- }
190
- else {
191
- this.value = value;
192
- }
193
- this.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
194
- this.indeterminate = false;
195
- this.focus();
196
- }
197
- increment(factor = 1) {
198
- this.stepBy(1 * factor);
199
- }
200
- decrement(factor = 1) {
201
- this.stepBy(-1 * factor);
202
- }
203
- handleKeydown(event) {
204
- switch (event.code) {
205
- case 'ArrowUp':
206
- event.preventDefault();
207
- this.increment(event.shiftKey ? this.stepModifier : 1);
208
- this.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
209
- break;
210
- case 'ArrowDown':
211
- event.preventDefault();
212
- this.decrement(event.shiftKey ? this.stepModifier : 1);
213
- this.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
214
- break;
215
- }
216
- }
217
- onScroll(event) {
52
+ constructor() {
53
+ super(...arguments);
54
+ this.focused = false;
55
+ this._forcedUnit = "";
56
+ this.formatOptions = {};
57
+ this.hideStepper = false;
58
+ this.indeterminate = false;
59
+ this.keyboardFocused = false;
60
+ this.resolvedLanguage = document.documentElement.lang || navigator.language;
61
+ this.stepperActive = false;
62
+ this.stepModifier = 10;
63
+ this._value = NaN;
64
+ this._trackingValue = "";
65
+ this.changeCount = 0;
66
+ this.wasIndeterminate = false;
67
+ }
68
+ static get styles() {
69
+ return [...super.styles, styles, chevronStyles];
70
+ }
71
+ set value(rawValue) {
72
+ const value = this.validateInput(rawValue);
73
+ if (value === this.value) {
74
+ return;
75
+ }
76
+ const oldValue = this._value;
77
+ this._value = value;
78
+ this.requestUpdate("value", oldValue);
79
+ }
80
+ get value() {
81
+ return this._value;
82
+ }
83
+ get inputValue() {
84
+ return this.indeterminate ? this.formattedValue : this.inputElement.value;
85
+ }
86
+ get valueAsString() {
87
+ return this._value.toString();
88
+ }
89
+ set valueAsString(value) {
90
+ this.value = this.numberParser.parse(value);
91
+ }
92
+ get formattedValue() {
93
+ if (isNaN(this.value))
94
+ return "";
95
+ return this.numberFormatter.format(this.value) + (this.focused ? "" : this._forcedUnit);
96
+ }
97
+ convertValueToNumber(value) {
98
+ return this.numberParser.parse(value);
99
+ }
100
+ get _step() {
101
+ var _a;
102
+ if (typeof this.step !== "undefined") {
103
+ return this.step;
104
+ }
105
+ if (((_a = this.formatOptions) == null ? void 0 : _a.style) === "percent") {
106
+ return 0.01;
107
+ }
108
+ return 1;
109
+ }
110
+ handlePointerdown(event) {
111
+ if (event.button !== 0) {
112
+ event.preventDefault();
113
+ return;
114
+ }
115
+ this.stepperActive = true;
116
+ this.buttons.setPointerCapture(event.pointerId);
117
+ const stepUpRect = this.buttons.children[0].getBoundingClientRect();
118
+ const stepDownRect = this.buttons.children[1].getBoundingClientRect();
119
+ this.findChange = (event2) => {
120
+ if (event2.clientX >= stepUpRect.x && event2.clientY >= stepUpRect.y && event2.clientX <= stepUpRect.x + stepUpRect.width && event2.clientY <= stepUpRect.y + stepUpRect.height) {
121
+ this.change = (event3) => this.increment(event3.shiftKey ? this.stepModifier : 1);
122
+ } else if (event2.clientX >= stepDownRect.x && event2.clientY >= stepDownRect.y && event2.clientX <= stepDownRect.x + stepDownRect.width && event2.clientY <= stepDownRect.y + stepDownRect.height) {
123
+ this.change = (event3) => this.decrement(event3.shiftKey ? this.stepModifier : 1);
124
+ }
125
+ };
126
+ this.findChange(event);
127
+ this.startChange(event);
128
+ }
129
+ startChange(event) {
130
+ this.changeCount = 0;
131
+ this.doChange(event);
132
+ this.safty = setTimeout(() => {
133
+ this.doNextChange(event);
134
+ }, 400);
135
+ }
136
+ doChange(event) {
137
+ this.change(event);
138
+ }
139
+ handlePointermove(event) {
140
+ this.findChange(event);
141
+ }
142
+ handlePointerup(event) {
143
+ this.buttons.releasePointerCapture(event.pointerId);
144
+ cancelAnimationFrame(this.nextChange);
145
+ clearTimeout(this.safty);
146
+ this.dispatchEvent(new Event("change", { bubbles: true, composed: true }));
147
+ this.stepperActive = false;
148
+ }
149
+ doNextChange(event) {
150
+ this.changeCount += 1;
151
+ if (this.changeCount % FRAMES_PER_CHANGE === 0) {
152
+ this.doChange(event);
153
+ }
154
+ return requestAnimationFrame(() => {
155
+ this.nextChange = this.doNextChange(event);
156
+ });
157
+ }
158
+ stepBy(count) {
159
+ if (this.disabled || this.readonly) {
160
+ return;
161
+ }
162
+ const min = typeof this.min !== "undefined" ? this.min : 0;
163
+ let value = this.value;
164
+ value += count * this._step;
165
+ if (isNaN(this.value)) {
166
+ this.value = min;
167
+ } else {
168
+ this.value = value;
169
+ }
170
+ this.dispatchEvent(new Event("input", { bubbles: true, composed: true }));
171
+ this.indeterminate = false;
172
+ this.focus();
173
+ }
174
+ increment(factor = 1) {
175
+ this.stepBy(1 * factor);
176
+ }
177
+ decrement(factor = 1) {
178
+ this.stepBy(-1 * factor);
179
+ }
180
+ handleKeydown(event) {
181
+ switch (event.code) {
182
+ case "ArrowUp":
218
183
  event.preventDefault();
219
- const direction = event.shiftKey
220
- ? event.deltaX / Math.abs(event.deltaX)
221
- : event.deltaY / Math.abs(event.deltaY);
222
- if (direction !== 0 && !isNaN(direction)) {
223
- this.stepBy(direction * (event.shiftKey ? this.stepModifier : 1));
224
- }
225
- }
226
- onFocus() {
227
- super.onFocus();
228
- this._trackingValue = this.inputValue;
229
- this.keyboardFocused = !this.readonly && true;
230
- this.addEventListener('wheel', this.onScroll, { passive: false });
231
- }
232
- onBlur() {
233
- super.onBlur();
234
- this.keyboardFocused = !this.readonly && false;
235
- this.removeEventListener('wheel', this.onScroll);
236
- }
237
- handleFocusin() {
238
- this.focused = !this.readonly && true;
239
- this.keyboardFocused = !this.readonly && true;
240
- }
241
- handleFocusout() {
242
- this.focused = !this.readonly && false;
243
- this.keyboardFocused = !this.readonly && false;
244
- }
245
- handleChange() {
246
- const value = this.convertValueToNumber(this.inputValue);
247
- if (this.wasIndeterminate) {
248
- this.wasIndeterminate = false;
249
- this.indeterminateValue = undefined;
250
- if (isNaN(value)) {
251
- this.indeterminate = true;
252
- return;
253
- }
254
- }
255
- this.value = value;
256
- super.handleChange();
257
- }
258
- handleInput() {
259
- if (this.indeterminate) {
260
- this.wasIndeterminate = true;
261
- this.indeterminateValue = this.value;
262
- this.inputElement.value = this.inputElement.value.replace(indeterminatePlaceholder, '');
263
- }
264
- const { value: originalValue, selectionStart } = this.inputElement;
265
- const value = originalValue
266
- .split('')
267
- .map((char) => remapMultiByteCharacters[char] || char)
268
- .join('');
269
- if (this.numberParser.isValidPartialNumber(value)) {
270
- const valueAsNumber = this.convertValueToNumber(value);
271
- if (!value && this.indeterminateValue) {
272
- this.indeterminate = true;
273
- this._value = this.indeterminateValue;
274
- }
275
- else {
276
- this.indeterminate = false;
277
- this._value = this.validateInput(valueAsNumber);
278
- }
279
- this._trackingValue = value;
280
- this.inputElement.value = value;
281
- return;
282
- }
283
- const currentLength = value.length;
284
- const previousLength = this._trackingValue.length;
285
- const nextSelectStart = (selectionStart || currentLength) -
286
- (currentLength - previousLength);
287
- this.inputElement.value = this.indeterminate
288
- ? indeterminatePlaceholder
289
- : this._trackingValue;
290
- this.inputElement.setSelectionRange(nextSelectStart, nextSelectStart);
291
- }
292
- validateInput(value) {
293
- if (typeof this.min !== 'undefined') {
294
- value = Math.max(this.min, value);
295
- }
296
- if (typeof this.max !== 'undefined') {
297
- value = Math.min(this.max, value);
184
+ this.increment(event.shiftKey ? this.stepModifier : 1);
185
+ this.dispatchEvent(new Event("change", { bubbles: true, composed: true }));
186
+ break;
187
+ case "ArrowDown":
188
+ event.preventDefault();
189
+ this.decrement(event.shiftKey ? this.stepModifier : 1);
190
+ this.dispatchEvent(new Event("change", { bubbles: true, composed: true }));
191
+ break;
192
+ }
193
+ }
194
+ onScroll(event) {
195
+ event.preventDefault();
196
+ const direction = event.shiftKey ? event.deltaX / Math.abs(event.deltaX) : event.deltaY / Math.abs(event.deltaY);
197
+ if (direction !== 0 && !isNaN(direction)) {
198
+ this.stepBy(direction * (event.shiftKey ? this.stepModifier : 1));
199
+ }
200
+ }
201
+ onFocus() {
202
+ super.onFocus();
203
+ this._trackingValue = this.inputValue;
204
+ this.keyboardFocused = !this.readonly && true;
205
+ this.addEventListener("wheel", this.onScroll, { passive: false });
206
+ }
207
+ onBlur() {
208
+ super.onBlur();
209
+ this.keyboardFocused = !this.readonly && false;
210
+ this.removeEventListener("wheel", this.onScroll);
211
+ }
212
+ handleFocusin() {
213
+ this.focused = !this.readonly && true;
214
+ this.keyboardFocused = !this.readonly && true;
215
+ }
216
+ handleFocusout() {
217
+ this.focused = !this.readonly && false;
218
+ this.keyboardFocused = !this.readonly && false;
219
+ }
220
+ handleChange() {
221
+ const value = this.convertValueToNumber(this.inputValue);
222
+ if (this.wasIndeterminate) {
223
+ this.wasIndeterminate = false;
224
+ this.indeterminateValue = void 0;
225
+ if (isNaN(value)) {
226
+ this.indeterminate = true;
227
+ return;
228
+ }
229
+ }
230
+ this.value = value;
231
+ super.handleChange();
232
+ }
233
+ handleInput() {
234
+ if (this.indeterminate) {
235
+ this.wasIndeterminate = true;
236
+ this.indeterminateValue = this.value;
237
+ this.inputElement.value = this.inputElement.value.replace(indeterminatePlaceholder, "");
238
+ }
239
+ const { value: originalValue, selectionStart } = this.inputElement;
240
+ const value = originalValue.split("").map((char) => remapMultiByteCharacters[char] || char).join("");
241
+ if (this.numberParser.isValidPartialNumber(value)) {
242
+ const valueAsNumber = this.convertValueToNumber(value);
243
+ if (!value && this.indeterminateValue) {
244
+ this.indeterminate = true;
245
+ this._value = this.indeterminateValue;
246
+ } else {
247
+ this.indeterminate = false;
248
+ this._value = this.validateInput(valueAsNumber);
249
+ }
250
+ this._trackingValue = value;
251
+ this.inputElement.value = value;
252
+ return;
253
+ }
254
+ const currentLength = value.length;
255
+ const previousLength = this._trackingValue.length;
256
+ const nextSelectStart = (selectionStart || currentLength) - (currentLength - previousLength);
257
+ this.inputElement.value = this.indeterminate ? indeterminatePlaceholder : this._trackingValue;
258
+ this.inputElement.setSelectionRange(nextSelectStart, nextSelectStart);
259
+ }
260
+ validateInput(value) {
261
+ if (typeof this.min !== "undefined") {
262
+ value = Math.max(this.min, value);
263
+ }
264
+ if (typeof this.max !== "undefined") {
265
+ value = Math.min(this.max, value);
266
+ }
267
+ if (this.step) {
268
+ const min = typeof this.min !== "undefined" ? this.min : 0;
269
+ const moduloStep = (value - min) % this.step;
270
+ const fallsOnStep = moduloStep === 0;
271
+ if (!fallsOnStep) {
272
+ const overUnder = Math.round(moduloStep / this.step);
273
+ if (overUnder === 1) {
274
+ value += this.step - moduloStep;
275
+ } else {
276
+ value -= moduloStep;
298
277
  }
299
- // Step shouldn't validate when 0...
300
- if (this.step) {
301
- const min = typeof this.min !== 'undefined' ? this.min : 0;
302
- const moduloStep = (value - min) % this.step;
303
- const fallsOnStep = moduloStep === 0;
304
- if (!fallsOnStep) {
305
- const overUnder = Math.round(moduloStep / this.step);
306
- if (overUnder === 1) {
307
- value += this.step - moduloStep;
308
- }
309
- else {
310
- value -= moduloStep;
311
- }
312
- }
313
- if (typeof this.max !== 'undefined') {
314
- while (value > this.max) {
315
- value -= this.step;
316
- }
317
- }
278
+ }
279
+ if (typeof this.max !== "undefined") {
280
+ while (value > this.max) {
281
+ value -= this.step;
318
282
  }
319
- return value;
320
- }
321
- get displayValue() {
322
- const indeterminateValue = this.focused ? '' : indeterminatePlaceholder;
323
- return this.indeterminate ? indeterminateValue : this.formattedValue;
324
- }
325
- clearNumberFormatterCache() {
326
- this._numberFormatter = undefined;
327
- this._numberParser = undefined;
328
- }
329
- get numberFormatter() {
330
- if (!this._numberFormatter || !this._numberFormatterFocused) {
331
- const _a = this.formatOptions, { style, unit,
332
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
333
- unitDisplay } = _a, formatOptionsNoUnit = __rest(_a, ["style", "unit", "unitDisplay"]);
334
- if (style !== 'unit') {
335
- formatOptionsNoUnit.style = style;
336
- }
337
- this._numberFormatterFocused = new NumberFormatter(this.resolvedLanguage, formatOptionsNoUnit);
338
- try {
339
- this._numberFormatter = new NumberFormatter(this.resolvedLanguage, this.formatOptions);
340
- this._forcedUnit = '';
341
- this._numberFormatter.format(1);
342
- }
343
- catch (error) {
344
- if (style === 'unit') {
345
- this._forcedUnit = unit;
346
- }
347
- this._numberFormatter = this._numberFormatterFocused;
348
- }
283
+ }
284
+ }
285
+ return value;
286
+ }
287
+ get displayValue() {
288
+ const indeterminateValue = this.focused ? "" : indeterminatePlaceholder;
289
+ return this.indeterminate ? indeterminateValue : this.formattedValue;
290
+ }
291
+ clearNumberFormatterCache() {
292
+ this._numberFormatter = void 0;
293
+ this._numberParser = void 0;
294
+ }
295
+ get numberFormatter() {
296
+ if (!this._numberFormatter || !this._numberFormatterFocused) {
297
+ const {
298
+ style,
299
+ unit,
300
+ unitDisplay,
301
+ ...formatOptionsNoUnit
302
+ } = this.formatOptions;
303
+ if (style !== "unit") {
304
+ formatOptionsNoUnit.style = style;
305
+ }
306
+ this._numberFormatterFocused = new NumberFormatter(this.resolvedLanguage, formatOptionsNoUnit);
307
+ try {
308
+ this._numberFormatter = new NumberFormatter(this.resolvedLanguage, this.formatOptions);
309
+ this._forcedUnit = "";
310
+ this._numberFormatter.format(1);
311
+ } catch (error) {
312
+ if (style === "unit") {
313
+ this._forcedUnit = unit;
349
314
  }
350
- return this.focused
351
- ? this._numberFormatterFocused
352
- : this._numberFormatter;
353
- }
354
- get numberParser() {
355
- if (!this._numberParser || !this._numberParserFocused) {
356
- const _a = this.formatOptions, { style, unit,
357
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
358
- unitDisplay } = _a, formatOptionsNoUnit = __rest(_a, ["style", "unit", "unitDisplay"]);
359
- if (style !== 'unit') {
360
- formatOptionsNoUnit.style = style;
361
- }
362
- this._numberParserFocused = new NumberParser(this.resolvedLanguage, formatOptionsNoUnit);
363
- try {
364
- this._numberParser = new NumberParser(this.resolvedLanguage, this.formatOptions);
365
- this._forcedUnit = '';
366
- this._numberParser.parse('0');
367
- }
368
- catch (error) {
369
- if (style === 'unit') {
370
- this._forcedUnit = unit;
371
- }
372
- this._numberParser = this._numberParserFocused;
373
- }
315
+ this._numberFormatter = this._numberFormatterFocused;
316
+ }
317
+ }
318
+ return this.focused ? this._numberFormatterFocused : this._numberFormatter;
319
+ }
320
+ get numberParser() {
321
+ if (!this._numberParser || !this._numberParserFocused) {
322
+ const {
323
+ style,
324
+ unit,
325
+ unitDisplay,
326
+ ...formatOptionsNoUnit
327
+ } = this.formatOptions;
328
+ if (style !== "unit") {
329
+ formatOptionsNoUnit.style = style;
330
+ }
331
+ this._numberParserFocused = new NumberParser(this.resolvedLanguage, formatOptionsNoUnit);
332
+ try {
333
+ this._numberParser = new NumberParser(this.resolvedLanguage, this.formatOptions);
334
+ this._forcedUnit = "";
335
+ this._numberParser.parse("0");
336
+ } catch (error) {
337
+ if (style === "unit") {
338
+ this._forcedUnit = unit;
374
339
  }
375
- return this.focused ? this._numberParserFocused : this._numberParser;
376
- }
377
- renderField() {
378
- this.autocomplete = 'off';
379
- return html `
340
+ this._numberParser = this._numberParserFocused;
341
+ }
342
+ }
343
+ return this.focused ? this._numberParserFocused : this._numberParser;
344
+ }
345
+ renderField() {
346
+ this.autocomplete = "off";
347
+ return html`
380
348
  ${super.renderField()}
381
- ${this.hideStepper
382
- ? html ``
383
- : html `
349
+ ${this.hideStepper ? html`` : html`
384
350
  <span
385
351
  class="buttons"
386
352
  @focusin=${this.handleFocusin}
387
353
  @focusout=${this.handleFocusout}
388
354
  ${streamingListener({
389
- start: ['pointerdown', this.handlePointerdown],
390
- streamInside: [
391
- [
392
- 'pointermove',
393
- 'pointerenter',
394
- 'pointerleave',
395
- 'pointerover',
396
- 'pointerout',
397
- ],
398
- this.handlePointermove,
399
- ],
400
- end: [
401
- ['pointerup', 'pointercancel'],
402
- this.handlePointerup,
403
- ],
404
- })}
355
+ start: ["pointerdown", this.handlePointerdown],
356
+ streamInside: [
357
+ [
358
+ "pointermove",
359
+ "pointerenter",
360
+ "pointerleave",
361
+ "pointerover",
362
+ "pointerout"
363
+ ],
364
+ this.handlePointermove
365
+ ],
366
+ end: [
367
+ ["pointerup", "pointercancel"],
368
+ this.handlePointerup
369
+ ]
370
+ })}
405
371
  >
406
372
  <sp-action-button
407
373
  class="stepUp"
408
374
  label="Increment"
409
375
  tabindex="-1"
410
376
  ?focused=${this.focused}
411
- ?disabled=${this.disabled ||
412
- this.readonly ||
413
- (typeof this.max !== 'undefined' &&
414
- this.value === this.max)}
377
+ ?disabled=${this.disabled || this.readonly || typeof this.max !== "undefined" && this.value === this.max}
415
378
  ?quiet=${this.quiet}
416
379
  >
417
380
  <sp-icon-chevron75
@@ -424,10 +387,7 @@ export class NumberField extends TextfieldBase {
424
387
  label="Decrement"
425
388
  tabindex="-1"
426
389
  ?focused=${this.focused}
427
- ?disabled=${this.disabled ||
428
- this.readonly ||
429
- (typeof this.min !== 'undefined' &&
430
- this.value === this.min)}
390
+ ?disabled=${this.disabled || this.readonly || typeof this.min !== "undefined" && this.value === this.min}
431
391
  ?quiet=${this.quiet}
432
392
  >
433
393
  <sp-icon-chevron75
@@ -438,114 +398,102 @@ export class NumberField extends TextfieldBase {
438
398
  </span>
439
399
  `}
440
400
  `;
441
- }
442
- update(changes) {
443
- if (changes.has('formatOptions') || changes.has('resolvedLanguage')) {
444
- this.clearNumberFormatterCache();
401
+ }
402
+ update(changes) {
403
+ if (changes.has("formatOptions") || changes.has("resolvedLanguage")) {
404
+ this.clearNumberFormatterCache();
405
+ }
406
+ if (changes.has("value") || changes.has("max") || changes.has("min")) {
407
+ const value = this.numberParser.parse(this.formattedValue.replace(this._forcedUnit, ""));
408
+ this.value = value;
409
+ }
410
+ super.update(changes);
411
+ }
412
+ willUpdate() {
413
+ this.multiline = false;
414
+ }
415
+ firstUpdated(changes) {
416
+ super.firstUpdated(changes);
417
+ this.addEventListener("keydown", this.handleKeydown);
418
+ }
419
+ updated(changes) {
420
+ if (changes.has("min") || changes.has("formatOptions")) {
421
+ let inputMode = "numeric";
422
+ const hasNegative = typeof this.min !== "undefined" && this.min < 0;
423
+ const { maximumFractionDigits } = this.formatOptions;
424
+ const hasDecimals = maximumFractionDigits && maximumFractionDigits > 0;
425
+ if (isIPhone()) {
426
+ if (hasNegative) {
427
+ inputMode = "text";
428
+ } else if (hasDecimals) {
429
+ inputMode = "decimal";
445
430
  }
446
- super.update(changes);
447
- }
448
- willUpdate() {
449
- this.multiline = false;
450
- }
451
- firstUpdated(changes) {
452
- super.firstUpdated(changes);
453
- this.addEventListener('keydown', this.handleKeydown);
454
- }
455
- updated(changes) {
456
- if (changes.has('value') ||
457
- changes.has('max') ||
458
- changes.has('min') ||
459
- changes.has('min')) {
460
- const value = this.numberParser.parse(this.inputValue.replace(this._forcedUnit, ''));
461
- this.value = value;
431
+ } else if (isAndroid()) {
432
+ if (hasNegative) {
433
+ inputMode = "numeric";
434
+ } else if (hasDecimals) {
435
+ inputMode = "decimal";
462
436
  }
463
- if (changes.has('min') || changes.has('formatOptions')) {
464
- let inputMode = 'numeric';
465
- const hasNegative = typeof this.min !== 'undefined' && this.min < 0;
466
- const { maximumFractionDigits } = this.formatOptions;
467
- const hasDecimals = maximumFractionDigits && maximumFractionDigits > 0;
468
- /* c8 ignore next 18 */
469
- if (isIPhone()) {
470
- // iPhone doesn't have a minus sign in either numeric or decimal.
471
- // Note this is only for iPhone, not iPad, which always has both
472
- // minus and decimal in numeric.
473
- if (hasNegative) {
474
- inputMode = 'text';
475
- }
476
- else if (hasDecimals) {
477
- inputMode = 'decimal';
478
- }
479
- }
480
- else if (isAndroid()) {
481
- // Android numeric has both a decimal point and minus key.
482
- // decimal does not have a minus key.
483
- if (hasNegative) {
484
- inputMode = 'numeric';
485
- }
486
- else if (hasDecimals) {
487
- inputMode = 'decimal';
488
- }
489
- }
490
- this.inputElement.inputMode = inputMode;
437
+ }
438
+ this.inputElement.inputMode = inputMode;
439
+ }
440
+ }
441
+ connectedCallback() {
442
+ super.connectedCallback();
443
+ this.resolveLanguage();
444
+ }
445
+ disconnectedCallback() {
446
+ this.resolveLanguage();
447
+ super.disconnectedCallback();
448
+ }
449
+ resolveLanguage() {
450
+ const queryThemeEvent = new CustomEvent("sp-language-context", {
451
+ bubbles: true,
452
+ composed: true,
453
+ detail: {
454
+ callback: (lang) => {
455
+ this.resolvedLanguage = lang;
491
456
  }
492
- }
493
- connectedCallback() {
494
- super.connectedCallback();
495
- this.resolveLanguage();
496
- }
497
- disconnectedCallback() {
498
- this.resolveLanguage();
499
- super.disconnectedCallback();
500
- }
501
- resolveLanguage() {
502
- const queryThemeEvent = new CustomEvent('sp-language-context', {
503
- bubbles: true,
504
- composed: true,
505
- detail: {
506
- callback: (lang) => {
507
- this.resolvedLanguage = lang;
508
- },
509
- },
510
- cancelable: true,
511
- });
512
- this.dispatchEvent(queryThemeEvent);
513
- }
457
+ },
458
+ cancelable: true
459
+ });
460
+ this.dispatchEvent(queryThemeEvent);
461
+ }
514
462
  }
515
- __decorate([
516
- query('.buttons')
517
- ], NumberField.prototype, "buttons", void 0);
518
- __decorate([
519
- property({ type: Boolean, reflect: true })
520
- ], NumberField.prototype, "focused", void 0);
521
- __decorate([
522
- property({ type: Object, attribute: 'format-options' })
523
- ], NumberField.prototype, "formatOptions", void 0);
524
- __decorate([
525
- property({ type: Boolean, reflect: true, attribute: 'hide-stepper' })
526
- ], NumberField.prototype, "hideStepper", void 0);
527
- __decorate([
528
- property({ type: Boolean, reflect: true })
529
- ], NumberField.prototype, "indeterminate", void 0);
530
- __decorate([
531
- property({ type: Boolean, reflect: true, attribute: 'keyboard-focused' })
532
- ], NumberField.prototype, "keyboardFocused", void 0);
533
- __decorate([
534
- property({ type: Number })
535
- ], NumberField.prototype, "max", void 0);
536
- __decorate([
537
- property({ type: Number })
538
- ], NumberField.prototype, "min", void 0);
539
- __decorate([
540
- property({ attribute: false })
541
- ], NumberField.prototype, "resolvedLanguage", void 0);
542
- __decorate([
543
- property({ type: Number })
544
- ], NumberField.prototype, "step", void 0);
545
- __decorate([
546
- property({ type: Number, reflect: true, attribute: 'step-modifier' })
547
- ], NumberField.prototype, "stepModifier", void 0);
548
- __decorate([
549
- property({ type: Number })
550
- ], NumberField.prototype, "value", null);
551
- //# sourceMappingURL=NumberField.js.map
463
+ __decorateClass([
464
+ query(".buttons")
465
+ ], NumberField.prototype, "buttons", 2);
466
+ __decorateClass([
467
+ property({ type: Boolean, reflect: true })
468
+ ], NumberField.prototype, "focused", 2);
469
+ __decorateClass([
470
+ property({ type: Object, attribute: "format-options" })
471
+ ], NumberField.prototype, "formatOptions", 2);
472
+ __decorateClass([
473
+ property({ type: Boolean, reflect: true, attribute: "hide-stepper" })
474
+ ], NumberField.prototype, "hideStepper", 2);
475
+ __decorateClass([
476
+ property({ type: Boolean, reflect: true })
477
+ ], NumberField.prototype, "indeterminate", 2);
478
+ __decorateClass([
479
+ property({ type: Boolean, reflect: true, attribute: "keyboard-focused" })
480
+ ], NumberField.prototype, "keyboardFocused", 2);
481
+ __decorateClass([
482
+ property({ type: Number })
483
+ ], NumberField.prototype, "max", 2);
484
+ __decorateClass([
485
+ property({ type: Number })
486
+ ], NumberField.prototype, "min", 2);
487
+ __decorateClass([
488
+ property({ attribute: false })
489
+ ], NumberField.prototype, "resolvedLanguage", 2);
490
+ __decorateClass([
491
+ property({ type: Number })
492
+ ], NumberField.prototype, "step", 2);
493
+ __decorateClass([
494
+ property({ type: Number, reflect: true, attribute: "step-modifier" })
495
+ ], NumberField.prototype, "stepModifier", 2);
496
+ __decorateClass([
497
+ property({ type: Number })
498
+ ], NumberField.prototype, "value", 1);
499
+ //# sourceMappingURL=NumberField.js.map