@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.mjs CHANGED
@@ -138,6 +138,30 @@ function observeAttributes(node, attributes, fn) {
138
138
  obs.observe(node, { attributes: true, attributeFilter: attrs });
139
139
  return () => obs.disconnect();
140
140
  }
141
+ function itemById(v, id) {
142
+ return v.find((node) => node.id === id);
143
+ }
144
+ function indexOfId(v, id) {
145
+ const item = itemById(v, id);
146
+ return item ? v.indexOf(item) : -1;
147
+ }
148
+ var getValueText = (item) => {
149
+ var _a, _b;
150
+ return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
151
+ };
152
+ var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
153
+ var wrap = (v, idx) => {
154
+ return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
155
+ };
156
+ function findByText(v, text, currentId) {
157
+ const index = currentId ? indexOfId(v, currentId) : -1;
158
+ let items = currentId ? wrap(v, index) : v;
159
+ const isSingleKey = text.length === 1;
160
+ if (isSingleKey) {
161
+ items = items.filter((item) => item.id !== currentId);
162
+ }
163
+ return items.find((item) => match(getValueText(item), text));
164
+ }
141
165
  function addPointerlockChangeListener(doc, fn) {
142
166
  return addDomEvent(doc, "pointerlockchange", fn, false);
143
167
  }
@@ -178,6 +202,34 @@ function requestPointerLock(doc, handlers = {}) {
178
202
  exit();
179
203
  };
180
204
  }
205
+ function findByTypeahead(_items, options) {
206
+ const { state: state2, activeId, key, timeout = 350 } = options;
207
+ const search = state2.keysSoFar + key;
208
+ const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
209
+ const query2 = isRepeated ? search[0] : search;
210
+ let items = _items.slice();
211
+ const next = findByText(items, query2, activeId);
212
+ function cleanup() {
213
+ clearTimeout(state2.timer);
214
+ state2.timer = -1;
215
+ }
216
+ function update(value) {
217
+ state2.keysSoFar = value;
218
+ cleanup();
219
+ if (value !== "") {
220
+ state2.timer = +setTimeout(() => {
221
+ update("");
222
+ cleanup();
223
+ }, timeout);
224
+ }
225
+ }
226
+ update(search);
227
+ return next;
228
+ }
229
+ findByTypeahead.defaultOptions = {
230
+ keysSoFar: "",
231
+ timer: -1
232
+ };
181
233
 
182
234
  // ../../utilities/number/dist/index.mjs
183
235
  var __defProp2 = Object.defineProperty;
@@ -200,11 +252,11 @@ var __spreadValues2 = (a, b) => {
200
252
  return a;
201
253
  };
202
254
  var __spreadProps2 = (a, b) => __defProps2(a, __getOwnPropDescs2(b));
203
- var nf = new Intl.NumberFormat("en-US", { style: "decimal" });
255
+ var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
204
256
  function formatter(n) {
205
257
  return parseFloat(nf.format(n));
206
258
  }
207
- function wrap(num, max) {
259
+ function wrap2(num, max) {
208
260
  return (num % max + max) % max;
209
261
  }
210
262
  function round(v, t2) {
@@ -222,9 +274,16 @@ function roundToPx(num) {
222
274
  function clamp(v, o) {
223
275
  return Math.min(Math.max(valueOf(v), o.min), o.max);
224
276
  }
225
- function countDecimals(v) {
226
- var _a, _b;
227
- return (_b = (_a = nf.formatToParts(v).find((p) => p.type === "fraction")) == null ? void 0 : _a.value.length) != null ? _b : 0;
277
+ function countDecimals(value) {
278
+ if (!Number.isFinite(value))
279
+ return 0;
280
+ let e = 1;
281
+ let p = 0;
282
+ while (Math.round(value * e) / e !== value) {
283
+ e *= 10;
284
+ p += 1;
285
+ }
286
+ return p;
228
287
  }
229
288
  var increment = (v, s) => formatter(valueOf(v) + s);
230
289
  var decrement = (v, s) => formatter(valueOf(v) - s);
@@ -238,9 +297,7 @@ function valueOf(v) {
238
297
  function getMaxPrecision(o) {
239
298
  var _a;
240
299
  const stepPrecision = countDecimals(o.step);
241
- const val = valueOf(o.value);
242
- const valuePrecision = Number.isNaN(val) ? stepPrecision : Math.max(countDecimals(val), stepPrecision);
243
- return Math.max(valuePrecision, (_a = o.precision) != null ? _a : 0);
300
+ return Math.max(stepPrecision, (_a = o.precision) != null ? _a : 0);
244
301
  }
245
302
  function roundToPrecision(v, o) {
246
303
  return round(v, getMaxPrecision(__spreadProps2(__spreadValues2({}, o), { value: v })));
@@ -329,7 +386,7 @@ var dom = {
329
386
  const win = dom.getWin(ctx);
330
387
  const width = win.innerWidth;
331
388
  const half = roundToPx(7.5);
332
- point.x = wrap(point.x + half, width) - half;
389
+ point.x = wrap2(point.x + half, width) - half;
333
390
  return { hint, point };
334
391
  },
335
392
  createVirtualCursor(ctx) {
@@ -359,15 +416,23 @@ var dom = {
359
416
  }
360
417
  };
361
418
 
419
+ // ../../utilities/core/dist/index.mjs
420
+ var pipe2 = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
421
+ var ua = (v) => isDom() && v.test(navigator.userAgent);
422
+ var isDom = () => !!(typeof window !== "undefined");
423
+ var isSafari = () => ua(/^((?!chrome|android).)*safari/i);
424
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
425
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
426
+
362
427
  // src/number-input.utils.ts
363
428
  var utils = {
364
429
  isValidNumericEvent: (ctx, event) => {
365
430
  var _a, _b;
366
431
  if (event.key == null)
367
432
  return true;
368
- const isModifierKey = event.ctrlKey || event.altKey || event.metaKey;
369
- const isSingleCharacterKey = event.key.length === 1;
370
- if (isModifierKey || !isSingleCharacterKey)
433
+ const isModifier = isModifiedEvent(event);
434
+ const isSingleKey = event.key.length === 1;
435
+ if (isModifier || !isSingleKey)
371
436
  return true;
372
437
  return (_b = (_a = ctx.validateCharacter) == null ? void 0 : _a.call(ctx, event.key)) != null ? _b : utils.isFloatingPoint(event.key);
373
438
  },
@@ -586,15 +651,6 @@ function connect(state, send, normalize = normalizeProp) {
586
651
 
587
652
  // src/number-input.machine.ts
588
653
  import { choose, createMachine, guards, ref } from "@zag-js/core";
589
-
590
- // ../../utilities/core/dist/index.mjs
591
- var pipe2 = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
592
- var ua = (v) => isDom() && v.test(navigator.userAgent);
593
- var isDom = () => !!(typeof window !== "undefined");
594
- var isSafari = () => ua(/^((?!chrome|android).)*safari/i);
595
- var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
596
-
597
- // src/number-input.machine.ts
598
654
  var { not, and } = guards;
599
655
  function machine(ctx = {}) {
600
656
  return createMachine({
@@ -713,6 +769,7 @@ function machine(ctx = {}) {
713
769
  actions: ["clampValue", "clearHint"]
714
770
  },
715
771
  {
772
+ target: "idle",
716
773
  actions: ["roundValue"]
717
774
  }
718
775
  ]