@zag-js/number-input 0.2.6 → 0.2.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.
@@ -0,0 +1,9 @@
1
+ import {
2
+ machine
3
+ } from "./chunk-7E3ZO263.mjs";
4
+ import "./chunk-2SIS62IB.mjs";
5
+ import "./chunk-GWZPUXGU.mjs";
6
+ import "./chunk-DDNK5RLW.mjs";
7
+ export {
8
+ machine
9
+ };
@@ -0,0 +1,217 @@
1
+ import { StateMachine } from '@zag-js/core';
2
+ import { RequiredBy, DirectionProperty, CommonProperties, Context } from '@zag-js/types';
3
+
4
+ type ValidityState = "rangeUnderflow" | "rangeOverflow";
5
+ type ElementIds = Partial<{
6
+ root: string;
7
+ label: string;
8
+ input: string;
9
+ incrementTrigger: string;
10
+ decrementTrigger: string;
11
+ scrubber: string;
12
+ }>;
13
+ type IntlTranslations = {
14
+ /**
15
+ * Function that returns the human-readable value.
16
+ * It is used to set the `aria-valuetext` property of the input
17
+ */
18
+ valueText?: (value: string) => string;
19
+ /**
20
+ * The label foe the increment button
21
+ */
22
+ incrementLabel: string;
23
+ /**
24
+ * The label for the decrement button
25
+ */
26
+ decrementLabel: string;
27
+ };
28
+ type Value = {
29
+ value: string;
30
+ valueAsNumber: number;
31
+ };
32
+ type PublicContext = DirectionProperty & CommonProperties & {
33
+ /**
34
+ * The ids of the elements in the number input. Useful for composition.
35
+ */
36
+ ids?: ElementIds;
37
+ /**
38
+ * The name attribute of the number input. Useful for form submission.
39
+ */
40
+ name?: string;
41
+ /**
42
+ * The associate form of the input element.
43
+ */
44
+ form?: string;
45
+ /**
46
+ * Whether the number input is disabled.
47
+ */
48
+ disabled?: boolean;
49
+ /**
50
+ * Whether the number input is readonly
51
+ */
52
+ readOnly?: boolean;
53
+ /**
54
+ * Whether the number input value is invalid.
55
+ */
56
+ invalid?: boolean;
57
+ /**
58
+ * The pattern used to check the <input> element's value against
59
+ *
60
+ * @default
61
+ * "[0-9]*(.[0-9]+)?"
62
+ */
63
+ pattern: string;
64
+ /**
65
+ * The value of the input
66
+ */
67
+ value: string;
68
+ /**
69
+ * The minimum value of the number input
70
+ */
71
+ min: number;
72
+ /**
73
+ * The maximum value of the number input
74
+ */
75
+ max: number;
76
+ /**
77
+ * The amount to increment or decrement the value by
78
+ */
79
+ step: number;
80
+ /**
81
+ * Whether to allow mouse wheel to change the value
82
+ */
83
+ allowMouseWheel?: boolean;
84
+ /**
85
+ * Whether to allow the value overflow the min/max range
86
+ * @default true
87
+ */
88
+ allowOverflow: boolean;
89
+ /**
90
+ * Whether the pressed key should be allowed in the input.
91
+ * The default behavior is to allow DOM floating point characters defined by /^[Ee0-9+\-.]$/
92
+ */
93
+ validateCharacter?: (char: string) => boolean;
94
+ /**
95
+ * Whether to clamp the value when the input loses focus (blur)
96
+ * @default true
97
+ */
98
+ clampValueOnBlur: boolean;
99
+ /**
100
+ * Whether to focus input when the value changes
101
+ * @default true
102
+ */
103
+ focusInputOnChange: boolean;
104
+ /**
105
+ * Specifies the localized strings that identifies the accessibility elements and their states
106
+ */
107
+ translations: IntlTranslations;
108
+ /**
109
+ * If using a custom display format, this converts the custom format to a format `parseFloat` understands.
110
+ */
111
+ parse?: (value: string) => string;
112
+ /**
113
+ * If using a custom display format, this converts the default format to the custom format.
114
+ */
115
+ format?: (value: string) => string | number;
116
+ /**
117
+ * Hints at the type of data that might be entered by the user. It also determines
118
+ * the type of keyboard shown to the user on mobile devices
119
+ * @default "decimal"
120
+ */
121
+ inputMode: "text" | "tel" | "numeric" | "decimal";
122
+ /**
123
+ * Function invoked when the value changes
124
+ */
125
+ onChange?: (details: Value) => void;
126
+ /**
127
+ * Function invoked when the value overflows or underflows the min/max range
128
+ */
129
+ onInvalid?: (details: Value & {
130
+ reason: ValidityState;
131
+ }) => void;
132
+ /**
133
+ * Function invoked when the number input is focused
134
+ */
135
+ onFocus?: (details: Value & {
136
+ srcElement: HTMLElement | null;
137
+ }) => void;
138
+ /**
139
+ * The value of the input when it is blurred
140
+ */
141
+ onBlur?: (details: Value) => void;
142
+ /**
143
+ * The minimum number of fraction digits to use. Possible values are from 0 to 20
144
+ */
145
+ minFractionDigits?: number;
146
+ /**
147
+ * The maximum number of fraction digits to use. Possible values are from 0 to 20;
148
+ */
149
+ maxFractionDigits?: number;
150
+ /**
151
+ * Whether to spin the value when the increment/decrement button is pressed
152
+ */
153
+ spinOnPress?: boolean;
154
+ };
155
+ type UserDefinedContext = RequiredBy<PublicContext, "id">;
156
+ type ComputedContext = Readonly<{
157
+ /**
158
+ * @computed
159
+ * The value of the input as a number
160
+ */
161
+ valueAsNumber: number;
162
+ /**
163
+ * @computed
164
+ * Whether the value is at the min
165
+ */
166
+ isAtMin: boolean;
167
+ /**
168
+ * @computed
169
+ * Whether the value is at the max
170
+ */
171
+ isAtMax: boolean;
172
+ /**
173
+ * @computed
174
+ * Whether the value is out of the min/max range
175
+ */
176
+ isOutOfRange: boolean;
177
+ /**
178
+ * @computed
179
+ * Whether the value is empty
180
+ */
181
+ isValueEmpty: boolean;
182
+ /**
183
+ * @computed
184
+ * Whether the increment button is enabled
185
+ */
186
+ canIncrement: boolean;
187
+ /**
188
+ * @computed
189
+ * Whether the decrement button is enabled
190
+ */
191
+ canDecrement: boolean;
192
+ /**
193
+ * @computed
194
+ * The `aria-valuetext` attribute of the input
195
+ */
196
+ valueText: string | undefined;
197
+ /**
198
+ * @computed
199
+ * The formatted value of the input
200
+ */
201
+ formattedValue: string;
202
+ /**
203
+ * @computed
204
+ * Whether the writing direction is RTL
205
+ */
206
+ isRtl: boolean;
207
+ }>;
208
+ type PrivateContext = Context<{}>;
209
+ type MachineContext = PublicContext & PrivateContext & ComputedContext;
210
+ type MachineState = {
211
+ value: "unknown" | "idle" | "focused" | "spinning" | "before:spin" | "scrubbing";
212
+ tags: "focus";
213
+ };
214
+ type State = StateMachine.State<MachineContext, MachineState>;
215
+ type Send = StateMachine.Send<StateMachine.AnyEventObject>;
216
+
217
+ export { MachineContext, MachineState, Send, State, UserDefinedContext };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+
16
+ // src/number-input.types.ts
17
+ var number_input_types_exports = {};
18
+ module.exports = __toCommonJS(number_input_types_exports);
File without changes
@@ -0,0 +1,17 @@
1
+ import { JSX } from '@zag-js/types';
2
+ import { MachineContext } from './number-input.types.js';
3
+ import '@zag-js/core';
4
+
5
+ declare const utils: {
6
+ isValidNumericEvent: (ctx: MachineContext, event: JSX.KeyboardEvent) => boolean;
7
+ isFloatingPoint: (v: string) => boolean;
8
+ sanitize: (ctx: MachineContext, value: string) => string;
9
+ increment: (ctx: MachineContext, step?: number) => string;
10
+ decrement: (ctx: MachineContext, step?: number) => string;
11
+ clamp: (ctx: MachineContext) => string;
12
+ parse: (ctx: MachineContext, value: string) => string;
13
+ format: (ctx: MachineContext, value: string | number) => string | number;
14
+ round: (ctx: MachineContext) => string;
15
+ };
16
+
17
+ export { utils };
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/number-input.utils.ts
21
+ var number_input_utils_exports = {};
22
+ __export(number_input_utils_exports, {
23
+ utils: () => utils
24
+ });
25
+ module.exports = __toCommonJS(number_input_utils_exports);
26
+
27
+ // ../../utilities/dom/src/event.ts
28
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
29
+
30
+ // ../../utilities/number/src/number.ts
31
+ function clamp(v, o) {
32
+ return Math.min(Math.max(valueOf(v), o.min), o.max);
33
+ }
34
+ function countDecimals(value) {
35
+ if (!Number.isFinite(value))
36
+ return 0;
37
+ let e = 1, p = 0;
38
+ while (Math.round(value * e) / e !== value) {
39
+ e *= 10;
40
+ p += 1;
41
+ }
42
+ return p;
43
+ }
44
+ var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
45
+ var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
46
+ function valueOf(v) {
47
+ if (typeof v === "number")
48
+ return v;
49
+ const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
50
+ return !Number.isNaN(num) ? num : 0;
51
+ }
52
+ function formatDecimal(v, o) {
53
+ return new Intl.NumberFormat("en-US", {
54
+ useGrouping: false,
55
+ style: "decimal",
56
+ minimumFractionDigits: o.minFractionDigits,
57
+ maximumFractionDigits: o.maxFractionDigits
58
+ }).format(valueOf(v));
59
+ }
60
+ function decimalOperation(a, op, b) {
61
+ let result = op === "+" ? a + b : a - b;
62
+ if (a % 1 !== 0 || b % 1 !== 0) {
63
+ const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
64
+ a = Math.round(a * multiplier);
65
+ b = Math.round(b * multiplier);
66
+ result = op === "+" ? a + b : a - b;
67
+ result /= multiplier;
68
+ }
69
+ return result;
70
+ }
71
+
72
+ // src/number-input.utils.ts
73
+ var utils = {
74
+ isValidNumericEvent: (ctx, event) => {
75
+ var _a, _b;
76
+ if (event.key == null)
77
+ return true;
78
+ const isModifier = isModifiedEvent(event);
79
+ const isSingleKey = event.key.length === 1;
80
+ if (isModifier || !isSingleKey)
81
+ return true;
82
+ return (_b = (_a = ctx.validateCharacter) == null ? void 0 : _a.call(ctx, event.key)) != null ? _b : utils.isFloatingPoint(event.key);
83
+ },
84
+ isFloatingPoint: (v) => /^[0-9+\-.]$/.test(v),
85
+ sanitize: (ctx, value) => {
86
+ var _a;
87
+ return value.split("").filter((_a = ctx.validateCharacter) != null ? _a : utils.isFloatingPoint).join("");
88
+ },
89
+ increment: (ctx, step) => {
90
+ const value = increment(ctx.value, step != null ? step : ctx.step);
91
+ return formatDecimal(clamp(value, ctx), ctx);
92
+ },
93
+ decrement: (ctx, step) => {
94
+ const value = decrement(ctx.value, step != null ? step : ctx.step);
95
+ return formatDecimal(clamp(value, ctx), ctx);
96
+ },
97
+ clamp: (ctx) => {
98
+ return formatDecimal(clamp(ctx.value, ctx), ctx);
99
+ },
100
+ parse: (ctx, value) => {
101
+ var _a, _b;
102
+ return (_b = (_a = ctx.parse) == null ? void 0 : _a.call(ctx, value)) != null ? _b : value;
103
+ },
104
+ format: (ctx, value) => {
105
+ var _a, _b;
106
+ const _val = value.toString();
107
+ return (_b = (_a = ctx.format) == null ? void 0 : _a.call(ctx, _val)) != null ? _b : _val;
108
+ },
109
+ round: (ctx) => {
110
+ return formatDecimal(ctx.value, ctx);
111
+ }
112
+ };
113
+ // Annotate the CommonJS export names for ESM import in node:
114
+ 0 && (module.exports = {
115
+ utils
116
+ });
@@ -0,0 +1,7 @@
1
+ import {
2
+ utils
3
+ } from "./chunk-GWZPUXGU.mjs";
4
+ import "./chunk-DDNK5RLW.mjs";
5
+ export {
6
+ utils
7
+ };
package/package.json CHANGED
@@ -1,10 +1,7 @@
1
1
  {
2
2
  "name": "@zag-js/number-input",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "Core logic for the number-input widget implemented as a state machine",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "types": "dist/index.d.ts",
8
5
  "keywords": [
9
6
  "js",
10
7
  "machine",
@@ -29,20 +26,33 @@
29
26
  "url": "https://github.com/chakra-ui/zag/issues"
30
27
  },
31
28
  "dependencies": {
32
- "@zag-js/anatomy": "0.1.2",
33
- "@zag-js/core": "0.2.3",
34
- "@zag-js/types": "0.3.1"
29
+ "@zag-js/anatomy": "0.1.3",
30
+ "@zag-js/core": "0.2.4",
31
+ "@zag-js/types": "0.3.2"
35
32
  },
36
33
  "devDependencies": {
37
- "@zag-js/dom-utils": "0.2.1",
38
- "@zag-js/form-utils": "0.2.2",
39
- "@zag-js/number-utils": "0.2.1",
40
- "@zag-js/utils": "0.3.1"
34
+ "clean-package": "2.2.0",
35
+ "@zag-js/dom-utils": "0.2.2",
36
+ "@zag-js/form-utils": "0.2.3",
37
+ "@zag-js/number-utils": "0.2.2",
38
+ "@zag-js/utils": "0.3.2"
39
+ },
40
+ "clean-package": "../../../clean-package.config.json",
41
+ "main": "dist/index.js",
42
+ "module": "dist/index.mjs",
43
+ "types": "dist/index.d.ts",
44
+ "exports": {
45
+ ".": {
46
+ "types": "./dist/index.d.ts",
47
+ "import": "./dist/index.mjs",
48
+ "require": "./dist/index.js"
49
+ },
50
+ "./package.json": "./package.json"
41
51
  },
42
52
  "scripts": {
43
- "build-fast": "tsup src/index.ts --format=esm,cjs",
53
+ "build-fast": "tsup src",
44
54
  "start": "pnpm build --watch",
45
- "build": "tsup src/index.ts --format=esm,cjs --dts",
55
+ "build": "tsup src --dts",
46
56
  "test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
47
57
  "lint": "eslint src --ext .ts,.tsx",
48
58
  "test-ci": "pnpm test --ci --runInBand",