@proyecto-viviana/solid-stately 0.2.4 → 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.
Files changed (82) hide show
  1. package/LICENSE +21 -0
  2. package/dist/autocomplete/createAutocompleteState.d.ts +2 -1
  3. package/dist/checkbox/createCheckboxGroupState.d.ts +10 -1
  4. package/dist/collections/types.d.ts +11 -0
  5. package/dist/color/getColorChannels.d.ts +20 -0
  6. package/dist/data/createAsyncList.d.ts +111 -0
  7. package/dist/data/createListData.d.ts +65 -0
  8. package/dist/data/createTreeData.d.ts +61 -0
  9. package/dist/data/index.d.ts +3 -0
  10. package/dist/datepicker/index.d.ts +10 -0
  11. package/dist/grid/types.d.ts +5 -1
  12. package/dist/index.d.ts +6 -1
  13. package/dist/index.js +3737 -2697
  14. package/dist/index.js.map +1 -7
  15. package/dist/menu/index.d.ts +8 -0
  16. package/dist/radio/createRadioGroupState.d.ts +10 -1
  17. package/dist/select/createSelectState.d.ts +17 -0
  18. package/dist/selection/index.d.ts +11 -0
  19. package/dist/toast/createToastState.d.ts +7 -1
  20. package/dist/toggle/createToggleGroupState.d.ts +45 -0
  21. package/dist/toggle/index.d.ts +1 -0
  22. package/dist/tree/TreeCollection.d.ts +3 -2
  23. package/package.json +6 -5
  24. package/src/autocomplete/createAutocompleteState.ts +10 -11
  25. package/src/calendar/createDateFieldState.ts +24 -1
  26. package/src/checkbox/createCheckboxGroupState.ts +42 -6
  27. package/src/collections/ListCollection.ts +152 -146
  28. package/src/collections/createListState.ts +266 -264
  29. package/src/collections/createMenuState.ts +106 -106
  30. package/src/collections/createSelectionState.ts +336 -336
  31. package/src/collections/index.ts +46 -46
  32. package/src/collections/types.ts +181 -169
  33. package/src/color/Color.ts +951 -951
  34. package/src/color/createColorAreaState.ts +293 -293
  35. package/src/color/createColorFieldState.ts +292 -292
  36. package/src/color/createColorSliderState.ts +241 -241
  37. package/src/color/createColorWheelState.ts +211 -211
  38. package/src/color/getColorChannels.ts +34 -0
  39. package/src/color/index.ts +47 -47
  40. package/src/color/types.ts +127 -127
  41. package/src/combobox/createComboBoxState.ts +703 -703
  42. package/src/combobox/index.ts +13 -13
  43. package/src/data/createAsyncList.ts +377 -0
  44. package/src/data/createListData.ts +298 -0
  45. package/src/data/createTreeData.ts +433 -0
  46. package/src/data/index.ts +25 -0
  47. package/src/datepicker/index.ts +36 -0
  48. package/src/disclosure/createDisclosureState.ts +4 -4
  49. package/src/dnd/createDragState.ts +153 -153
  50. package/src/dnd/createDraggableCollectionState.ts +165 -165
  51. package/src/dnd/createDropState.ts +212 -212
  52. package/src/dnd/createDroppableCollectionState.ts +357 -357
  53. package/src/dnd/index.ts +76 -76
  54. package/src/dnd/types.ts +317 -317
  55. package/src/form/createFormValidationState.ts +389 -389
  56. package/src/form/index.ts +15 -15
  57. package/src/grid/types.ts +5 -0
  58. package/src/index.ts +49 -0
  59. package/src/menu/index.ts +19 -0
  60. package/src/numberfield/createNumberFieldState.ts +427 -383
  61. package/src/numberfield/index.ts +5 -5
  62. package/src/overlays/createOverlayTriggerState.ts +67 -67
  63. package/src/overlays/index.ts +5 -5
  64. package/src/radio/createRadioGroupState.ts +44 -6
  65. package/src/searchfield/createSearchFieldState.ts +62 -62
  66. package/src/searchfield/index.ts +5 -5
  67. package/src/select/createSelectState.ts +290 -181
  68. package/src/select/index.ts +5 -5
  69. package/src/selection/index.ts +28 -0
  70. package/src/slider/createSliderState.ts +211 -211
  71. package/src/slider/index.ts +6 -6
  72. package/src/tabs/createTabListState.ts +37 -11
  73. package/src/toast/createToastState.d.ts +6 -1
  74. package/src/toast/createToastState.ts +8 -1
  75. package/src/toggle/createToggleGroupState.ts +127 -0
  76. package/src/toggle/index.ts +6 -0
  77. package/src/tooltip/createTooltipTriggerState.ts +183 -183
  78. package/src/tooltip/index.ts +6 -6
  79. package/src/tree/TreeCollection.ts +208 -175
  80. package/src/tree/createTreeState.ts +392 -392
  81. package/src/tree/index.ts +13 -13
  82. package/src/tree/types.ts +174 -174
@@ -1,383 +1,427 @@
1
- /**
2
- * State management for NumberField.
3
- * Based on @react-stately/numberfield useNumberFieldState.
4
- */
5
-
6
- import { createSignal, createMemo, type Accessor } from 'solid-js';
7
- import { access, type MaybeAccessor } from '../utils';
8
-
9
- export interface NumberFieldStateProps {
10
- /** The current value (controlled). */
11
- value?: number;
12
- /** The default value (uncontrolled). */
13
- defaultValue?: number;
14
- /** Handler called when the value changes. */
15
- onChange?: (value: number) => void;
16
- /** The minimum value. */
17
- minValue?: number;
18
- /** The maximum value. */
19
- maxValue?: number;
20
- /** The step value for increment/decrement. */
21
- step?: number;
22
- /** Whether the field is disabled. */
23
- isDisabled?: boolean;
24
- /** Whether the field is read-only. */
25
- isReadOnly?: boolean;
26
- /** The locale for number formatting. */
27
- locale?: string;
28
- /** Number format options. */
29
- formatOptions?: Intl.NumberFormatOptions;
30
- }
31
-
32
- export interface NumberFieldState {
33
- /** The current input value as a string. */
34
- inputValue: Accessor<string>;
35
- /** The current numeric value. */
36
- numberValue: Accessor<number>;
37
- /** Whether the value can be incremented. */
38
- canIncrement: Accessor<boolean>;
39
- /** Whether the value can be decremented. */
40
- canDecrement: Accessor<boolean>;
41
- /** Whether the field is disabled. */
42
- isDisabled: Accessor<boolean>;
43
- /** Whether the field is read-only. */
44
- isReadOnly: Accessor<boolean>;
45
- /** The minimum value. */
46
- minValue: Accessor<number | undefined>;
47
- /** The maximum value. */
48
- maxValue: Accessor<number | undefined>;
49
- /** Set the input value. */
50
- setInputValue: (value: string) => void;
51
- /** Validate a partial input value. */
52
- validate: (value: string) => boolean;
53
- /** Commit the current input value. */
54
- commit: () => void;
55
- /** Increment the value by step. */
56
- increment: () => void;
57
- /** Decrement the value by step. */
58
- decrement: () => void;
59
- /** Set to maximum value. */
60
- incrementToMax: () => void;
61
- /** Set to minimum value. */
62
- decrementToMin: () => void;
63
- }
64
-
65
- /**
66
- * Handles decimal operations to avoid floating point errors.
67
- */
68
- function handleDecimalOperation(
69
- operator: '+' | '-',
70
- value1: number,
71
- value2: number
72
- ): number {
73
- // Find the number of decimal places
74
- const getDecimals = (n: number) => {
75
- const str = String(n);
76
- const idx = str.indexOf('.');
77
- return idx === -1 ? 0 : str.length - idx - 1;
78
- };
79
-
80
- const decimals = Math.max(getDecimals(value1), getDecimals(value2));
81
- const multiplier = Math.pow(10, decimals);
82
-
83
- const int1 = Math.round(value1 * multiplier);
84
- const int2 = Math.round(value2 * multiplier);
85
-
86
- const result = operator === '+' ? int1 + int2 : int1 - int2;
87
- return result / multiplier;
88
- }
89
-
90
- /**
91
- * Clamps a value between min and max.
92
- */
93
- function clamp(value: number, min?: number, max?: number): number {
94
- let result = value;
95
- if (min != null && result < min) result = min;
96
- if (max != null && result > max) result = max;
97
- return result;
98
- }
99
-
100
- /**
101
- * Snaps a value to the nearest step.
102
- */
103
- function snapToStep(value: number, step: number, min?: number): number {
104
- const base = min ?? 0;
105
- const diff = value - base;
106
- const steps = Math.round(diff / step);
107
- return handleDecimalOperation('+', base, steps * step);
108
- }
109
-
110
- /**
111
- * Creates state for a number field.
112
- */
113
- export function createNumberFieldState(
114
- props: MaybeAccessor<NumberFieldStateProps>
115
- ): NumberFieldState {
116
- const getProps = () => access(props);
117
-
118
- // Get locale and formatter
119
- const locale = () => getProps().locale ?? 'en-US';
120
- const formatOptions = () => getProps().formatOptions ?? {};
121
-
122
- // Create number formatter
123
- const formatter = createMemo(() => {
124
- return new Intl.NumberFormat(locale(), formatOptions());
125
- });
126
-
127
- // Create number parser (simplified - real implementation would be more robust)
128
- const parseNumber = (value: string): number => {
129
- if (!value || value === '' || value === '-') return NaN;
130
-
131
- // Handle locale-specific decimal separators
132
- const opts = formatOptions();
133
- const testNumber = formatter().format(1.1);
134
- const decimalSeparator = testNumber.charAt(1);
135
-
136
- // Normalize the input
137
- let normalized = value;
138
- if (decimalSeparator !== '.') {
139
- normalized = normalized.replace(decimalSeparator, '.');
140
- }
141
-
142
- // Remove grouping separators and currency symbols
143
- normalized = normalized.replace(/[^\d.\-]/g, '');
144
-
145
- const parsed = parseFloat(normalized);
146
- return parsed;
147
- };
148
-
149
- // Format a number to string
150
- const formatNumber = (value: number): string => {
151
- if (isNaN(value)) return '';
152
- return formatter().format(value);
153
- };
154
-
155
- // Determine step value
156
- const step = createMemo(() => {
157
- const p = getProps();
158
- if (p.step != null) return p.step;
159
- // Default step for percent is 0.01
160
- if (p.formatOptions?.style === 'percent') return 0.01;
161
- return 1;
162
- });
163
-
164
- // Internal signals
165
- const [inputValue, setInputValueInternal] = createSignal<string>('');
166
- const [numberValue, setNumberValue] = createSignal<number>(NaN);
167
-
168
- // Initialize from props
169
- const initValue = () => {
170
- const p = getProps();
171
- const initial = p.value ?? p.defaultValue;
172
- if (initial != null) {
173
- setNumberValue(initial);
174
- setInputValueInternal(formatNumber(initial));
175
- }
176
- };
177
-
178
- // Call init on first access
179
- let initialized = false;
180
- const ensureInitialized = () => {
181
- if (!initialized) {
182
- initialized = true;
183
- initValue();
184
- }
185
- };
186
-
187
- // Controlled mode: sync with props.value
188
- const actualNumberValue = createMemo(() => {
189
- ensureInitialized();
190
- const p = getProps();
191
- if (p.value !== undefined) {
192
- return p.value;
193
- }
194
- return numberValue();
195
- });
196
-
197
- // Validate partial input
198
- const validate = (value: string): boolean => {
199
- if (value === '' || value === '-') return true;
200
-
201
- // Allow partial decimal input like "1."
202
- const opts = formatOptions();
203
- const testNumber = formatter().format(1.1);
204
- const decimalSeparator = testNumber.charAt(1);
205
-
206
- // Check if it's a valid partial number
207
- const pattern = new RegExp(
208
- `^-?\\d*${decimalSeparator === '.' ? '\\.' : decimalSeparator}?\\d*$`
209
- );
210
- return pattern.test(value);
211
- };
212
-
213
- // Set input value with validation
214
- const setInputValue = (value: string) => {
215
- ensureInitialized();
216
- setInputValueInternal(value);
217
- };
218
-
219
- // Commit the current input value
220
- const commit = () => {
221
- ensureInitialized();
222
- const p = getProps();
223
- const input = inputValue();
224
-
225
- if (input === '' || input === '-') {
226
- // Clear value
227
- setNumberValue(NaN);
228
- setInputValueInternal('');
229
- return;
230
- }
231
-
232
- let parsed = parseNumber(input);
233
-
234
- if (isNaN(parsed)) {
235
- // Invalid input - revert to current value
236
- setInputValueInternal(formatNumber(actualNumberValue()));
237
- return;
238
- }
239
-
240
- // Clamp and snap
241
- parsed = clamp(parsed, p.minValue, p.maxValue);
242
- parsed = snapToStep(parsed, step(), p.minValue);
243
-
244
- // Update state
245
- setNumberValue(parsed);
246
- setInputValueInternal(formatNumber(parsed));
247
-
248
- // Notify change
249
- if (p.value === undefined) {
250
- p.onChange?.(parsed);
251
- } else {
252
- p.onChange?.(parsed);
253
- }
254
- };
255
-
256
- // Check if can increment
257
- const canIncrement = createMemo(() => {
258
- ensureInitialized();
259
- const p = getProps();
260
- if (p.isDisabled || p.isReadOnly) return false;
261
-
262
- const current = actualNumberValue();
263
- if (isNaN(current)) return true; // Can start from min
264
-
265
- if (p.maxValue == null) return true;
266
- return handleDecimalOperation('+', current, step()) <= p.maxValue;
267
- });
268
-
269
- // Check if can decrement
270
- const canDecrement = createMemo(() => {
271
- ensureInitialized();
272
- const p = getProps();
273
- if (p.isDisabled || p.isReadOnly) return false;
274
-
275
- const current = actualNumberValue();
276
- if (isNaN(current)) return true; // Can start from max
277
-
278
- if (p.minValue == null) return true;
279
- return handleDecimalOperation('-', current, step()) >= p.minValue;
280
- });
281
-
282
- // Increment by step
283
- const increment = () => {
284
- ensureInitialized();
285
- const p = getProps();
286
- if (p.isDisabled || p.isReadOnly) return;
287
-
288
- let current = actualNumberValue();
289
-
290
- if (isNaN(current)) {
291
- // Start from min or 0
292
- current = p.minValue ?? 0;
293
- } else {
294
- // Snap and increment
295
- current = snapToStep(current, step(), p.minValue);
296
- current = handleDecimalOperation('+', current, step());
297
- }
298
-
299
- // Clamp
300
- current = clamp(current, p.minValue, p.maxValue);
301
-
302
- // Update
303
- setNumberValue(current);
304
- setInputValueInternal(formatNumber(current));
305
- p.onChange?.(current);
306
- };
307
-
308
- // Decrement by step
309
- const decrement = () => {
310
- ensureInitialized();
311
- const p = getProps();
312
- if (p.isDisabled || p.isReadOnly) return;
313
-
314
- let current = actualNumberValue();
315
-
316
- if (isNaN(current)) {
317
- // Start from max or 0
318
- current = p.maxValue ?? 0;
319
- } else {
320
- // Snap and decrement
321
- current = snapToStep(current, step(), p.minValue);
322
- current = handleDecimalOperation('-', current, step());
323
- }
324
-
325
- // Clamp
326
- current = clamp(current, p.minValue, p.maxValue);
327
-
328
- // Update
329
- setNumberValue(current);
330
- setInputValueInternal(formatNumber(current));
331
- p.onChange?.(current);
332
- };
333
-
334
- // Set to max
335
- const incrementToMax = () => {
336
- ensureInitialized();
337
- const p = getProps();
338
- if (p.isDisabled || p.isReadOnly) return;
339
-
340
- if (p.maxValue == null) return;
341
-
342
- const snapped = snapToStep(p.maxValue, step(), p.minValue);
343
- setNumberValue(snapped);
344
- setInputValueInternal(formatNumber(snapped));
345
- p.onChange?.(snapped);
346
- };
347
-
348
- // Set to min
349
- const decrementToMin = () => {
350
- ensureInitialized();
351
- const p = getProps();
352
- if (p.isDisabled || p.isReadOnly) return;
353
-
354
- if (p.minValue == null) return;
355
-
356
- setNumberValue(p.minValue);
357
- setInputValueInternal(formatNumber(p.minValue));
358
- p.onChange?.(p.minValue);
359
- };
360
-
361
- return {
362
- get inputValue() {
363
- ensureInitialized();
364
- return inputValue;
365
- },
366
- get numberValue() {
367
- return actualNumberValue;
368
- },
369
- canIncrement,
370
- canDecrement,
371
- isDisabled: () => getProps().isDisabled ?? false,
372
- isReadOnly: () => getProps().isReadOnly ?? false,
373
- minValue: () => getProps().minValue,
374
- maxValue: () => getProps().maxValue,
375
- setInputValue,
376
- validate,
377
- commit,
378
- increment,
379
- decrement,
380
- incrementToMax,
381
- decrementToMin,
382
- };
383
- }
1
+ /**
2
+ * State management for NumberField.
3
+ * Based on @react-stately/numberfield useNumberFieldState.
4
+ */
5
+
6
+ import { createSignal, createMemo, type Accessor } from 'solid-js';
7
+ import { access, type MaybeAccessor } from '../utils';
8
+
9
+ export interface NumberFieldStateProps {
10
+ /** The current value (controlled). */
11
+ value?: number;
12
+ /** The default value (uncontrolled). */
13
+ defaultValue?: number;
14
+ /** Handler called when the value changes. */
15
+ onChange?: (value: number) => void;
16
+ /** The minimum value. */
17
+ minValue?: number;
18
+ /** The maximum value. */
19
+ maxValue?: number;
20
+ /** The step value for increment/decrement. */
21
+ step?: number;
22
+ /** Whether the field is disabled. */
23
+ isDisabled?: boolean;
24
+ /** Whether the field is read-only. */
25
+ isReadOnly?: boolean;
26
+ /** The locale for number formatting. */
27
+ locale?: string;
28
+ /** Number format options. */
29
+ formatOptions?: Intl.NumberFormatOptions;
30
+ }
31
+
32
+ export interface NumberFieldState {
33
+ /** The current input value as a string. */
34
+ inputValue: Accessor<string>;
35
+ /** The current numeric value. */
36
+ numberValue: Accessor<number>;
37
+ /** Whether the value can be incremented. */
38
+ canIncrement: Accessor<boolean>;
39
+ /** Whether the value can be decremented. */
40
+ canDecrement: Accessor<boolean>;
41
+ /** Whether the field is disabled. */
42
+ isDisabled: Accessor<boolean>;
43
+ /** Whether the field is read-only. */
44
+ isReadOnly: Accessor<boolean>;
45
+ /** The minimum value. */
46
+ minValue: Accessor<number | undefined>;
47
+ /** The maximum value. */
48
+ maxValue: Accessor<number | undefined>;
49
+ /** Set the input value. */
50
+ setInputValue: (value: string) => void;
51
+ /** Validate a partial input value. */
52
+ validate: (value: string) => boolean;
53
+ /** Commit the current input value. */
54
+ commit: () => void;
55
+ /** Increment the value by step. */
56
+ increment: () => void;
57
+ /** Decrement the value by step. */
58
+ decrement: () => void;
59
+ /** Set to maximum value. */
60
+ incrementToMax: () => void;
61
+ /** Set to minimum value. */
62
+ decrementToMin: () => void;
63
+ }
64
+
65
+ /**
66
+ * Handles decimal operations to avoid floating point errors.
67
+ */
68
+ function handleDecimalOperation(
69
+ operator: '+' | '-',
70
+ value1: number,
71
+ value2: number
72
+ ): number {
73
+ // Find the number of decimal places
74
+ const getDecimals = (n: number) => {
75
+ const str = String(n);
76
+ const idx = str.indexOf('.');
77
+ return idx === -1 ? 0 : str.length - idx - 1;
78
+ };
79
+
80
+ const decimals = Math.max(getDecimals(value1), getDecimals(value2));
81
+ const multiplier = Math.pow(10, decimals);
82
+
83
+ const int1 = Math.round(value1 * multiplier);
84
+ const int2 = Math.round(value2 * multiplier);
85
+
86
+ const result = operator === '+' ? int1 + int2 : int1 - int2;
87
+ return result / multiplier;
88
+ }
89
+
90
+ /**
91
+ * Clamps a value between min and max.
92
+ */
93
+ function clamp(value: number, min?: number, max?: number): number {
94
+ let result = value;
95
+ if (min != null && result < min) result = min;
96
+ if (max != null && result > max) result = max;
97
+ return result;
98
+ }
99
+
100
+ /**
101
+ * Snaps a value to the nearest step.
102
+ */
103
+ function snapToStep(value: number, step: number, min?: number): number {
104
+ const base = min ?? 0;
105
+ const diff = value - base;
106
+ const steps = Math.round(diff / step);
107
+ return handleDecimalOperation('+', base, steps * step);
108
+ }
109
+
110
+ function isValidStep(step: number | undefined): step is number {
111
+ return step != null && !isNaN(step) && step > 0;
112
+ }
113
+
114
+ /**
115
+ * Creates state for a number field.
116
+ */
117
+ export function createNumberFieldState(
118
+ props: MaybeAccessor<NumberFieldStateProps>
119
+ ): NumberFieldState {
120
+ const getProps = () => access(props);
121
+
122
+ // Get locale and formatter
123
+ const locale = () => getProps().locale ?? 'en-US';
124
+ const formatOptions = () => getProps().formatOptions ?? {};
125
+
126
+ // Create number formatter
127
+ const formatter = createMemo(() => {
128
+ return new Intl.NumberFormat(locale(), formatOptions());
129
+ });
130
+
131
+ // Create number parser (simplified - real implementation would be more robust)
132
+ const parseNumber = (value: string): number => {
133
+ if (!value || value === '' || value === '-') return NaN;
134
+
135
+ // Handle locale-specific decimal separators
136
+ const opts = formatOptions();
137
+ const testNumber = formatter().format(1.1);
138
+ const decimalSeparator = testNumber.charAt(1);
139
+
140
+ // Normalize the input
141
+ let normalized = value;
142
+ if (decimalSeparator !== '.') {
143
+ normalized = normalized.replace(decimalSeparator, '.');
144
+ }
145
+
146
+ // Remove grouping separators and currency symbols
147
+ normalized = normalized.replace(/[^\d.\-]/g, '');
148
+
149
+ const parsed = parseFloat(normalized);
150
+ if (isNaN(parsed)) return parsed;
151
+
152
+ if (opts.style === 'percent') {
153
+ return parsed / 100;
154
+ }
155
+
156
+ return parsed;
157
+ };
158
+
159
+ // Format a number to string
160
+ const formatNumber = (value: number): string => {
161
+ if (isNaN(value)) return '';
162
+ return formatter().format(value);
163
+ };
164
+
165
+ // Determine step value
166
+ const hasCustomStep = createMemo(() => isValidStep(getProps().step));
167
+
168
+ const step = createMemo(() => {
169
+ const p = getProps();
170
+ if (hasCustomStep()) return p.step as number;
171
+ // Default step for percent is 0.01
172
+ if (p.formatOptions?.style === 'percent') return 0.01;
173
+ return 1;
174
+ });
175
+
176
+ // Internal signals
177
+ const [inputValue, setInputValueInternal] = createSignal<string>('');
178
+ const [numberValue, setNumberValue] = createSignal<number>(NaN);
179
+
180
+ const applyConstraints = (value: number): number => {
181
+ const p = getProps();
182
+ if (isNaN(value)) return NaN;
183
+
184
+ if (hasCustomStep()) {
185
+ return clamp(snapToStep(value, step(), p.minValue), p.minValue, p.maxValue);
186
+ }
187
+
188
+ return clamp(value, p.minValue, p.maxValue);
189
+ };
190
+
191
+ // Initialize from props
192
+ const initValue = () => {
193
+ const p = getProps();
194
+ const initial = p.value ?? p.defaultValue;
195
+ if (initial != null) {
196
+ const constrained = applyConstraints(initial);
197
+ setNumberValue(constrained);
198
+ setInputValueInternal(formatNumber(constrained));
199
+ }
200
+ };
201
+
202
+ // Call init on first access
203
+ let initialized = false;
204
+ const ensureInitialized = () => {
205
+ if (!initialized) {
206
+ initialized = true;
207
+ initValue();
208
+ }
209
+ };
210
+
211
+ // Controlled mode: sync with props.value
212
+ const actualNumberValue = createMemo(() => {
213
+ ensureInitialized();
214
+ const p = getProps();
215
+ if (p.value !== undefined) {
216
+ return applyConstraints(p.value);
217
+ }
218
+ return numberValue();
219
+ });
220
+
221
+ let lastControlledValue: number | undefined = undefined;
222
+ const syncControlledValue = () => {
223
+ const p = getProps();
224
+ if (p.value === undefined) {
225
+ lastControlledValue = undefined;
226
+ return;
227
+ }
228
+
229
+ const constrained = applyConstraints(p.value);
230
+ if (lastControlledValue === undefined || !Object.is(lastControlledValue, constrained)) {
231
+ lastControlledValue = constrained;
232
+ setNumberValue(constrained);
233
+ setInputValueInternal(formatNumber(constrained));
234
+ }
235
+ };
236
+
237
+ const parsedInputValue = () => {
238
+ ensureInitialized();
239
+ syncControlledValue();
240
+ return parseNumber(inputValue());
241
+ };
242
+
243
+ // Validate partial input
244
+ const validate = (value: string): boolean => {
245
+ if (value === '' || value === '-') return true;
246
+
247
+ // Allow partial decimal input like "1."
248
+ const opts = formatOptions();
249
+ const testNumber = formatter().format(1.1);
250
+ const decimalSeparator = testNumber.charAt(1);
251
+
252
+ // Check if it's a valid partial number
253
+ const pattern = new RegExp(
254
+ `^-?\\d*${decimalSeparator === '.' ? '\\.' : decimalSeparator}?\\d*$`
255
+ );
256
+ return pattern.test(value);
257
+ };
258
+
259
+ // Set input value with validation
260
+ const setInputValue = (value: string) => {
261
+ ensureInitialized();
262
+ syncControlledValue();
263
+ setInputValueInternal(value);
264
+ };
265
+
266
+ // Commit the current input value
267
+ const commit = () => {
268
+ ensureInitialized();
269
+ syncControlledValue();
270
+ const p = getProps();
271
+ const input = inputValue();
272
+
273
+ if (input === '' || input === '-') {
274
+ // Clear value
275
+ setNumberValue(NaN);
276
+ setInputValueInternal(p.value === undefined ? '' : formatNumber(actualNumberValue()));
277
+ p.onChange?.(NaN);
278
+ return;
279
+ }
280
+
281
+ let parsed = parseNumber(input);
282
+
283
+ if (isNaN(parsed)) {
284
+ // Invalid input - revert to current value
285
+ setInputValueInternal(formatNumber(actualNumberValue()));
286
+ return;
287
+ }
288
+
289
+ // Clamp and optionally snap to custom step.
290
+ parsed = applyConstraints(parsed);
291
+
292
+ // Update state
293
+ setNumberValue(parsed);
294
+ setInputValueInternal(formatNumber(parsed));
295
+
296
+ p.onChange?.(parsed);
297
+ };
298
+
299
+ // Check if can increment
300
+ const canIncrement = createMemo(() => {
301
+ ensureInitialized();
302
+ const p = getProps();
303
+ if (p.isDisabled || p.isReadOnly) return false;
304
+
305
+ const current = parsedInputValue();
306
+ if (isNaN(current)) return true; // Can start from min
307
+
308
+ if (p.maxValue == null) return true;
309
+ return (
310
+ snapToStep(current, step(), p.minValue) > current ||
311
+ handleDecimalOperation('+', current, step()) <= p.maxValue
312
+ );
313
+ });
314
+
315
+ // Check if can decrement
316
+ const canDecrement = createMemo(() => {
317
+ ensureInitialized();
318
+ const p = getProps();
319
+ if (p.isDisabled || p.isReadOnly) return false;
320
+
321
+ const current = parsedInputValue();
322
+ if (isNaN(current)) return true; // Can start from max
323
+
324
+ if (p.minValue == null) return true;
325
+ return (
326
+ snapToStep(current, step(), p.minValue) < current ||
327
+ handleDecimalOperation('-', current, step()) >= p.minValue
328
+ );
329
+ });
330
+
331
+ const safeNextStep = (operation: '+' | '-', minOrMaxValue: number = 0): number => {
332
+ const p = getProps();
333
+ const parsed = parsedInputValue();
334
+
335
+ if (isNaN(parsed)) {
336
+ const base = isNaN(minOrMaxValue) ? 0 : minOrMaxValue;
337
+ return clamp(snapToStep(base, step(), p.minValue), p.minValue, p.maxValue);
338
+ }
339
+
340
+ const snapped = snapToStep(parsed, step(), p.minValue);
341
+ if ((operation === '+' && snapped > parsed) || (operation === '-' && snapped < parsed)) {
342
+ return clamp(snapped, p.minValue, p.maxValue);
343
+ }
344
+
345
+ const next = handleDecimalOperation(operation, parsed, step());
346
+ return clamp(snapToStep(next, step(), p.minValue), p.minValue, p.maxValue);
347
+ };
348
+
349
+ // Increment by step
350
+ const increment = () => {
351
+ ensureInitialized();
352
+ syncControlledValue();
353
+ const p = getProps();
354
+ if (p.isDisabled || p.isReadOnly) return;
355
+
356
+ const current = safeNextStep('+', p.minValue);
357
+ setNumberValue(current);
358
+ setInputValueInternal(formatNumber(current));
359
+ p.onChange?.(current);
360
+ };
361
+
362
+ // Decrement by step
363
+ const decrement = () => {
364
+ ensureInitialized();
365
+ syncControlledValue();
366
+ const p = getProps();
367
+ if (p.isDisabled || p.isReadOnly) return;
368
+
369
+ const current = safeNextStep('-', p.maxValue);
370
+ setNumberValue(current);
371
+ setInputValueInternal(formatNumber(current));
372
+ p.onChange?.(current);
373
+ };
374
+
375
+ // Set to max
376
+ const incrementToMax = () => {
377
+ ensureInitialized();
378
+ syncControlledValue();
379
+ const p = getProps();
380
+ if (p.isDisabled || p.isReadOnly) return;
381
+
382
+ if (p.maxValue == null) return;
383
+
384
+ const snapped = snapToStep(p.maxValue, step(), p.minValue);
385
+ setNumberValue(snapped);
386
+ setInputValueInternal(formatNumber(snapped));
387
+ p.onChange?.(snapped);
388
+ };
389
+
390
+ // Set to min
391
+ const decrementToMin = () => {
392
+ ensureInitialized();
393
+ syncControlledValue();
394
+ const p = getProps();
395
+ if (p.isDisabled || p.isReadOnly) return;
396
+
397
+ if (p.minValue == null) return;
398
+
399
+ setNumberValue(p.minValue);
400
+ setInputValueInternal(formatNumber(p.minValue));
401
+ p.onChange?.(p.minValue);
402
+ };
403
+
404
+ return {
405
+ get inputValue() {
406
+ ensureInitialized();
407
+ syncControlledValue();
408
+ return inputValue;
409
+ },
410
+ get numberValue() {
411
+ return actualNumberValue;
412
+ },
413
+ canIncrement,
414
+ canDecrement,
415
+ isDisabled: () => getProps().isDisabled ?? false,
416
+ isReadOnly: () => getProps().isReadOnly ?? false,
417
+ minValue: () => getProps().minValue,
418
+ maxValue: () => getProps().maxValue,
419
+ setInputValue,
420
+ validate,
421
+ commit,
422
+ increment,
423
+ decrement,
424
+ incrementToMax,
425
+ decrementToMin,
426
+ };
427
+ }