@zag-js/number-input 0.0.0-dev-20220504084708 → 0.0.0-dev-20220509202935

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.
package/dist/index.js CHANGED
@@ -161,6 +161,30 @@ function observeAttributes(node, attributes, fn) {
161
161
  obs.observe(node, { attributes: true, attributeFilter: attrs });
162
162
  return () => obs.disconnect();
163
163
  }
164
+ function itemById(v, id) {
165
+ return v.find((node) => node.id === id);
166
+ }
167
+ function indexOfId(v, id) {
168
+ const item = itemById(v, id);
169
+ return item ? v.indexOf(item) : -1;
170
+ }
171
+ var getValueText = (item) => {
172
+ var _a, _b;
173
+ return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
174
+ };
175
+ var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
176
+ var wrap = (v, idx) => {
177
+ return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
178
+ };
179
+ function findByText(v, text, currentId) {
180
+ const index = currentId ? indexOfId(v, currentId) : -1;
181
+ let items = currentId ? wrap(v, index) : v;
182
+ const isSingleKey = text.length === 1;
183
+ if (isSingleKey) {
184
+ items = items.filter((item) => item.id !== currentId);
185
+ }
186
+ return items.find((item) => match(getValueText(item), text));
187
+ }
164
188
  function addPointerlockChangeListener(doc, fn) {
165
189
  return addDomEvent(doc, "pointerlockchange", fn, false);
166
190
  }
@@ -201,6 +225,34 @@ function requestPointerLock(doc, handlers = {}) {
201
225
  exit();
202
226
  };
203
227
  }
228
+ function findByTypeahead(_items, options) {
229
+ const { state: state2, activeId, key, timeout = 350 } = options;
230
+ const search = state2.keysSoFar + key;
231
+ const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
232
+ const query2 = isRepeated ? search[0] : search;
233
+ let items = _items.slice();
234
+ const next = findByText(items, query2, activeId);
235
+ function cleanup() {
236
+ clearTimeout(state2.timer);
237
+ state2.timer = -1;
238
+ }
239
+ function update(value) {
240
+ state2.keysSoFar = value;
241
+ cleanup();
242
+ if (value !== "") {
243
+ state2.timer = +setTimeout(() => {
244
+ update("");
245
+ cleanup();
246
+ }, timeout);
247
+ }
248
+ }
249
+ update(search);
250
+ return next;
251
+ }
252
+ findByTypeahead.defaultOptions = {
253
+ keysSoFar: "",
254
+ timer: -1
255
+ };
204
256
 
205
257
  // ../../utilities/number/dist/index.mjs
206
258
  var __defProp2 = Object.defineProperty;
@@ -223,11 +275,11 @@ var __spreadValues2 = (a, b) => {
223
275
  return a;
224
276
  };
225
277
  var __spreadProps2 = (a, b) => __defProps2(a, __getOwnPropDescs2(b));
226
- var nf = new Intl.NumberFormat("en-US", { style: "decimal" });
278
+ var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
227
279
  function formatter(n) {
228
280
  return parseFloat(nf.format(n));
229
281
  }
230
- function wrap(num, max) {
282
+ function wrap2(num, max) {
231
283
  return (num % max + max) % max;
232
284
  }
233
285
  function round(v, t2) {
@@ -245,9 +297,16 @@ function roundToPx(num) {
245
297
  function clamp(v, o) {
246
298
  return Math.min(Math.max(valueOf(v), o.min), o.max);
247
299
  }
248
- function countDecimals(v) {
249
- var _a, _b;
250
- return (_b = (_a = nf.formatToParts(v).find((p) => p.type === "fraction")) == null ? void 0 : _a.value.length) != null ? _b : 0;
300
+ function countDecimals(value) {
301
+ if (!Number.isFinite(value))
302
+ return 0;
303
+ let e = 1;
304
+ let p = 0;
305
+ while (Math.round(value * e) / e !== value) {
306
+ e *= 10;
307
+ p += 1;
308
+ }
309
+ return p;
251
310
  }
252
311
  var increment = (v, s) => formatter(valueOf(v) + s);
253
312
  var decrement = (v, s) => formatter(valueOf(v) - s);
@@ -261,9 +320,7 @@ function valueOf(v) {
261
320
  function getMaxPrecision(o) {
262
321
  var _a;
263
322
  const stepPrecision = countDecimals(o.step);
264
- const val = valueOf(o.value);
265
- const valuePrecision = Number.isNaN(val) ? stepPrecision : Math.max(countDecimals(val), stepPrecision);
266
- return Math.max(valuePrecision, (_a = o.precision) != null ? _a : 0);
323
+ return Math.max(stepPrecision, (_a = o.precision) != null ? _a : 0);
267
324
  }
268
325
  function roundToPrecision(v, o) {
269
326
  return round(v, getMaxPrecision(__spreadProps2(__spreadValues2({}, o), { value: v })));
@@ -352,7 +409,7 @@ var dom = {
352
409
  const win = dom.getWin(ctx);
353
410
  const width = win.innerWidth;
354
411
  const half = roundToPx(7.5);
355
- point.x = wrap(point.x + half, width) - half;
412
+ point.x = wrap2(point.x + half, width) - half;
356
413
  return { hint, point };
357
414
  },
358
415
  createVirtualCursor(ctx) {
@@ -382,15 +439,23 @@ var dom = {
382
439
  }
383
440
  };
384
441
 
442
+ // ../../utilities/core/dist/index.mjs
443
+ var pipe2 = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
444
+ var ua = (v) => isDom() && v.test(navigator.userAgent);
445
+ var isDom = () => !!(typeof window !== "undefined");
446
+ var isSafari = () => ua(/^((?!chrome|android).)*safari/i);
447
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
448
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
449
+
385
450
  // src/number-input.utils.ts
386
451
  var utils = {
387
452
  isValidNumericEvent: (ctx, event) => {
388
453
  var _a, _b;
389
454
  if (event.key == null)
390
455
  return true;
391
- const isModifierKey = event.ctrlKey || event.altKey || event.metaKey;
392
- const isSingleCharacterKey = event.key.length === 1;
393
- if (isModifierKey || !isSingleCharacterKey)
456
+ const isModifier = isModifiedEvent(event);
457
+ const isSingleKey = event.key.length === 1;
458
+ if (isModifier || !isSingleKey)
394
459
  return true;
395
460
  return (_b = (_a = ctx.validateCharacter) == null ? void 0 : _a.call(ctx, event.key)) != null ? _b : utils.isFloatingPoint(event.key);
396
461
  },
@@ -609,15 +674,6 @@ function connect(state, send, normalize = normalizeProp) {
609
674
 
610
675
  // src/number-input.machine.ts
611
676
  var import_core = require("@zag-js/core");
612
-
613
- // ../../utilities/core/dist/index.mjs
614
- var pipe2 = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
615
- var ua = (v) => isDom() && v.test(navigator.userAgent);
616
- var isDom = () => !!(typeof window !== "undefined");
617
- var isSafari = () => ua(/^((?!chrome|android).)*safari/i);
618
- var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
619
-
620
- // src/number-input.machine.ts
621
677
  var { not, and } = import_core.guards;
622
678
  function machine(ctx = {}) {
623
679
  return (0, import_core.createMachine)({
@@ -736,6 +792,7 @@ function machine(ctx = {}) {
736
792
  actions: ["clampValue", "clearHint"]
737
793
  },
738
794
  {
795
+ target: "idle",
739
796
  actions: ["roundValue"]
740
797
  }
741
798
  ]