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

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;
@@ -204,7 +256,7 @@ var nf = new Intl.NumberFormat("en-US", { style: "decimal" });
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) {
@@ -329,7 +381,7 @@ var dom = {
329
381
  const win = dom.getWin(ctx);
330
382
  const width = win.innerWidth;
331
383
  const half = roundToPx(7.5);
332
- point.x = wrap(point.x + half, width) - half;
384
+ point.x = wrap2(point.x + half, width) - half;
333
385
  return { hint, point };
334
386
  },
335
387
  createVirtualCursor(ctx) {
@@ -359,15 +411,23 @@ var dom = {
359
411
  }
360
412
  };
361
413
 
414
+ // ../../utilities/core/dist/index.mjs
415
+ var pipe2 = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
416
+ var ua = (v) => isDom() && v.test(navigator.userAgent);
417
+ var isDom = () => !!(typeof window !== "undefined");
418
+ var isSafari = () => ua(/^((?!chrome|android).)*safari/i);
419
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
420
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
421
+
362
422
  // src/number-input.utils.ts
363
423
  var utils = {
364
424
  isValidNumericEvent: (ctx, event) => {
365
425
  var _a, _b;
366
426
  if (event.key == null)
367
427
  return true;
368
- const isModifierKey = event.ctrlKey || event.altKey || event.metaKey;
369
- const isSingleCharacterKey = event.key.length === 1;
370
- if (isModifierKey || !isSingleCharacterKey)
428
+ const isModifier = isModifiedEvent(event);
429
+ const isSingleKey = event.key.length === 1;
430
+ if (isModifier || !isSingleKey)
371
431
  return true;
372
432
  return (_b = (_a = ctx.validateCharacter) == null ? void 0 : _a.call(ctx, event.key)) != null ? _b : utils.isFloatingPoint(event.key);
373
433
  },
@@ -586,15 +646,6 @@ function connect(state, send, normalize = normalizeProp) {
586
646
 
587
647
  // src/number-input.machine.ts
588
648
  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
649
  var { not, and } = guards;
599
650
  function machine(ctx = {}) {
600
651
  return createMachine({