@yamada-ui/number-input 0.1.0
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/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/chunk-L2FQDPVY.mjs +538 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +541 -0
- package/dist/index.mjs +8 -0
- package/dist/number-input.d.ts +48 -0
- package/dist/number-input.js +539 -0
- package/dist/number-input.mjs +8 -0
- package/package.json +80 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,541 @@
|
|
|
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/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
NumberInput: () => NumberInput,
|
|
24
|
+
useNumberInput: () => useNumberInput
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(src_exports);
|
|
27
|
+
|
|
28
|
+
// src/number-input.tsx
|
|
29
|
+
var import_core = require("@yamada-ui/core");
|
|
30
|
+
var import_form_control = require("@yamada-ui/form-control");
|
|
31
|
+
var import_icon = require("@yamada-ui/icon");
|
|
32
|
+
var import_use_counter = require("@yamada-ui/use-counter");
|
|
33
|
+
var import_use_event_listener = require("@yamada-ui/use-event-listener");
|
|
34
|
+
var import_use_interval = require("@yamada-ui/use-interval");
|
|
35
|
+
var import_utils = require("@yamada-ui/utils");
|
|
36
|
+
var import_react = require("react");
|
|
37
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
38
|
+
var isDefaultValidCharacter = (character) => /^[Ee0-9+\-.]$/.test(character);
|
|
39
|
+
var isValidNumericKeyboardEvent = ({ key, ctrlKey, altKey, metaKey }, isValid) => {
|
|
40
|
+
if (key == null)
|
|
41
|
+
return true;
|
|
42
|
+
const isModifierKey = ctrlKey || altKey || metaKey;
|
|
43
|
+
const isSingleCharacterKey = key.length === 1;
|
|
44
|
+
if (!isSingleCharacterKey || isModifierKey)
|
|
45
|
+
return true;
|
|
46
|
+
return isValid(key);
|
|
47
|
+
};
|
|
48
|
+
var getStep = ({ ctrlKey, shiftKey, metaKey }) => {
|
|
49
|
+
let ratio = 1;
|
|
50
|
+
if (metaKey || ctrlKey)
|
|
51
|
+
ratio = 0.1;
|
|
52
|
+
if (shiftKey)
|
|
53
|
+
ratio = 10;
|
|
54
|
+
return ratio;
|
|
55
|
+
};
|
|
56
|
+
var useNumberInput = (props = {}) => {
|
|
57
|
+
var _a;
|
|
58
|
+
const {
|
|
59
|
+
id,
|
|
60
|
+
name,
|
|
61
|
+
inputMode = "decimal",
|
|
62
|
+
pattern = "[0-9]*(.[0-9]+)?",
|
|
63
|
+
required,
|
|
64
|
+
disabled,
|
|
65
|
+
readOnly,
|
|
66
|
+
focusInputOnChange = true,
|
|
67
|
+
clampValueOnBlur = true,
|
|
68
|
+
keepWithinRange = true,
|
|
69
|
+
allowMouseWheel,
|
|
70
|
+
min = Number.MIN_SAFE_INTEGER,
|
|
71
|
+
max = Number.MAX_SAFE_INTEGER,
|
|
72
|
+
precision,
|
|
73
|
+
"aria-invalid": isInvalid,
|
|
74
|
+
...rest
|
|
75
|
+
} = (0, import_form_control.useFormControlProps)(props);
|
|
76
|
+
const isRequired = required;
|
|
77
|
+
const isReadOnly = readOnly;
|
|
78
|
+
const isDisabled = disabled;
|
|
79
|
+
const [isFocused, setFocused] = (0, import_react.useState)(false);
|
|
80
|
+
const isInteractive = !(readOnly || disabled);
|
|
81
|
+
const inputRef = (0, import_react.useRef)(null);
|
|
82
|
+
const inputSelectionRef = (0, import_react.useRef)(null);
|
|
83
|
+
const incrementRef = (0, import_react.useRef)(null);
|
|
84
|
+
const decrementRef = (0, import_react.useRef)(null);
|
|
85
|
+
const onFocus = (0, import_utils.useCallbackRef)(
|
|
86
|
+
(0, import_utils.handlerAll)(rest.onFocus, (ev) => {
|
|
87
|
+
var _a2, _b, _c;
|
|
88
|
+
setFocused(true);
|
|
89
|
+
if (!inputSelectionRef.current)
|
|
90
|
+
return;
|
|
91
|
+
ev.target.selectionStart = (_b = inputSelectionRef.current.start) != null ? _b : (_a2 = ev.currentTarget.value) == null ? void 0 : _a2.length;
|
|
92
|
+
ev.currentTarget.selectionEnd = (_c = inputSelectionRef.current.end) != null ? _c : ev.currentTarget.selectionStart;
|
|
93
|
+
})
|
|
94
|
+
);
|
|
95
|
+
const onBlur = (0, import_utils.useCallbackRef)(
|
|
96
|
+
(0, import_utils.handlerAll)(rest.onBlur, () => {
|
|
97
|
+
setFocused(false);
|
|
98
|
+
if (clampValueOnBlur)
|
|
99
|
+
validateAndClamp();
|
|
100
|
+
})
|
|
101
|
+
);
|
|
102
|
+
const onInvalid = (0, import_utils.useCallbackRef)(rest.onInvalid);
|
|
103
|
+
const isValidCharacter = (0, import_utils.useCallbackRef)((_a = rest.isValidCharacter) != null ? _a : isDefaultValidCharacter);
|
|
104
|
+
const { isMin, isMax, isOut, value, valueAsNumber, setValue, update, cast, ...counter } = (0, import_use_counter.useCounter)({
|
|
105
|
+
min,
|
|
106
|
+
max,
|
|
107
|
+
precision,
|
|
108
|
+
keepWithinRange,
|
|
109
|
+
...rest
|
|
110
|
+
});
|
|
111
|
+
const sanitize = (0, import_react.useCallback)(
|
|
112
|
+
(value2) => value2.split("").filter(isValidCharacter).join(""),
|
|
113
|
+
[isValidCharacter]
|
|
114
|
+
);
|
|
115
|
+
const parse = (0, import_react.useCallback)((value2) => {
|
|
116
|
+
var _a2, _b;
|
|
117
|
+
return (_b = (_a2 = rest.parse) == null ? void 0 : _a2.call(rest, value2)) != null ? _b : value2;
|
|
118
|
+
}, [rest]);
|
|
119
|
+
const format = (0, import_react.useCallback)(
|
|
120
|
+
(value2) => {
|
|
121
|
+
var _a2, _b;
|
|
122
|
+
return ((_b = (_a2 = rest.format) == null ? void 0 : _a2.call(rest, value2)) != null ? _b : value2).toString();
|
|
123
|
+
},
|
|
124
|
+
[rest]
|
|
125
|
+
);
|
|
126
|
+
const increment = (0, import_react.useCallback)(
|
|
127
|
+
(step = ((_b) => (_b = rest.step) != null ? _b : 1)()) => {
|
|
128
|
+
if (isInteractive)
|
|
129
|
+
counter.increment(step);
|
|
130
|
+
},
|
|
131
|
+
[isInteractive, counter, rest.step]
|
|
132
|
+
);
|
|
133
|
+
const decrement = (0, import_react.useCallback)(
|
|
134
|
+
(step = ((_c) => (_c = rest.step) != null ? _c : 1)()) => {
|
|
135
|
+
if (isInteractive)
|
|
136
|
+
counter.decrement(step);
|
|
137
|
+
},
|
|
138
|
+
[isInteractive, counter, rest.step]
|
|
139
|
+
);
|
|
140
|
+
const validateAndClamp = (0, import_react.useCallback)(() => {
|
|
141
|
+
let next = value;
|
|
142
|
+
if (value === "")
|
|
143
|
+
return;
|
|
144
|
+
const valueStartsWithE = /^[eE]/.test(value.toString());
|
|
145
|
+
if (valueStartsWithE) {
|
|
146
|
+
setValue("");
|
|
147
|
+
} else {
|
|
148
|
+
if (valueAsNumber < min)
|
|
149
|
+
next = min;
|
|
150
|
+
if (valueAsNumber > max)
|
|
151
|
+
next = max;
|
|
152
|
+
cast(next);
|
|
153
|
+
}
|
|
154
|
+
}, [cast, max, min, setValue, value, valueAsNumber]);
|
|
155
|
+
const onChange = (0, import_react.useCallback)(
|
|
156
|
+
(event) => {
|
|
157
|
+
const evt = event.nativeEvent;
|
|
158
|
+
if (evt.isComposing)
|
|
159
|
+
return;
|
|
160
|
+
const parsedInput = parse(event.currentTarget.value);
|
|
161
|
+
update(sanitize(parsedInput));
|
|
162
|
+
inputSelectionRef.current = {
|
|
163
|
+
start: event.currentTarget.selectionStart,
|
|
164
|
+
end: event.currentTarget.selectionEnd
|
|
165
|
+
};
|
|
166
|
+
},
|
|
167
|
+
[parse, update, sanitize]
|
|
168
|
+
);
|
|
169
|
+
const onKeyDown = (0, import_react.useCallback)(
|
|
170
|
+
(ev) => {
|
|
171
|
+
var _a2;
|
|
172
|
+
if (ev.nativeEvent.isComposing)
|
|
173
|
+
return;
|
|
174
|
+
if (!isValidNumericKeyboardEvent(ev, isValidCharacter))
|
|
175
|
+
ev.preventDefault();
|
|
176
|
+
const step = getStep(ev) * ((_a2 = rest.step) != null ? _a2 : 1);
|
|
177
|
+
const keyMap = {
|
|
178
|
+
ArrowUp: () => increment(step),
|
|
179
|
+
ArrowDown: () => decrement(step),
|
|
180
|
+
Home: () => update(min),
|
|
181
|
+
End: () => update(max)
|
|
182
|
+
};
|
|
183
|
+
const action = keyMap[ev.key];
|
|
184
|
+
if (!action)
|
|
185
|
+
return;
|
|
186
|
+
ev.preventDefault();
|
|
187
|
+
action(ev);
|
|
188
|
+
},
|
|
189
|
+
[decrement, increment, isValidCharacter, max, min, rest.step, update]
|
|
190
|
+
);
|
|
191
|
+
const { up, down, stop, isSpinning } = useSpinner(increment, decrement);
|
|
192
|
+
useAttributeObserver(incrementRef, ["disabled"], isSpinning, stop);
|
|
193
|
+
useAttributeObserver(decrementRef, ["disabled"], isSpinning, stop);
|
|
194
|
+
const focusInput = (0, import_react.useCallback)(() => {
|
|
195
|
+
if (focusInputOnChange)
|
|
196
|
+
requestAnimationFrame(() => {
|
|
197
|
+
var _a2;
|
|
198
|
+
(_a2 = inputRef.current) == null ? void 0 : _a2.focus();
|
|
199
|
+
});
|
|
200
|
+
}, [focusInputOnChange]);
|
|
201
|
+
const eventUp = (0, import_react.useCallback)(
|
|
202
|
+
(ev) => {
|
|
203
|
+
ev.preventDefault();
|
|
204
|
+
up();
|
|
205
|
+
focusInput();
|
|
206
|
+
},
|
|
207
|
+
[focusInput, up]
|
|
208
|
+
);
|
|
209
|
+
const eventDown = (0, import_react.useCallback)(
|
|
210
|
+
(ev) => {
|
|
211
|
+
ev.preventDefault();
|
|
212
|
+
down();
|
|
213
|
+
focusInput();
|
|
214
|
+
},
|
|
215
|
+
[focusInput, down]
|
|
216
|
+
);
|
|
217
|
+
(0, import_utils.useUpdateEffect)(() => {
|
|
218
|
+
if (valueAsNumber > max) {
|
|
219
|
+
onInvalid == null ? void 0 : onInvalid("rangeOverflow", format(value), valueAsNumber);
|
|
220
|
+
} else if (valueAsNumber < min) {
|
|
221
|
+
onInvalid == null ? void 0 : onInvalid("rangeOverflow", format(value), valueAsNumber);
|
|
222
|
+
}
|
|
223
|
+
}, [valueAsNumber, value, format, onInvalid]);
|
|
224
|
+
(0, import_utils.useSafeLayoutEffect)(() => {
|
|
225
|
+
if (!inputRef.current)
|
|
226
|
+
return;
|
|
227
|
+
const notInSync = inputRef.current.value != value;
|
|
228
|
+
if (!notInSync)
|
|
229
|
+
return;
|
|
230
|
+
const parsedInput = parse(inputRef.current.value);
|
|
231
|
+
setValue(sanitize(parsedInput));
|
|
232
|
+
}, [parse, sanitize]);
|
|
233
|
+
(0, import_use_event_listener.useEventListener)(
|
|
234
|
+
() => inputRef.current,
|
|
235
|
+
"wheel",
|
|
236
|
+
(ev) => {
|
|
237
|
+
var _a2, _b, _c;
|
|
238
|
+
const ownerDocument = (_b = (_a2 = inputRef.current) == null ? void 0 : _a2.ownerDocument) != null ? _b : document;
|
|
239
|
+
const isFocused2 = ownerDocument.activeElement === inputRef.current;
|
|
240
|
+
if (!allowMouseWheel || !isFocused2)
|
|
241
|
+
return;
|
|
242
|
+
ev.preventDefault();
|
|
243
|
+
const step = getStep(ev) * ((_c = rest.step) != null ? _c : 1);
|
|
244
|
+
const direction = Math.sign(ev.deltaY);
|
|
245
|
+
if (direction === -1) {
|
|
246
|
+
increment(step);
|
|
247
|
+
} else if (direction === 1) {
|
|
248
|
+
decrement(step);
|
|
249
|
+
}
|
|
250
|
+
},
|
|
251
|
+
{ passive: false }
|
|
252
|
+
);
|
|
253
|
+
const getInputProps = (0, import_react.useCallback)(
|
|
254
|
+
(props2 = {}, ref = null) => ({
|
|
255
|
+
id,
|
|
256
|
+
name,
|
|
257
|
+
type: "text",
|
|
258
|
+
inputMode,
|
|
259
|
+
pattern,
|
|
260
|
+
required,
|
|
261
|
+
disabled,
|
|
262
|
+
readOnly,
|
|
263
|
+
...(0, import_utils.pickObject)(rest, import_form_control.formControlProperties),
|
|
264
|
+
...(0, import_utils.omitObject)(props2, ["defaultValue"]),
|
|
265
|
+
ref: (0, import_utils.mergeRefs)(inputRef, ref),
|
|
266
|
+
value: format(value),
|
|
267
|
+
"aria-invalid": (0, import_utils.ariaAttr)(isInvalid != null ? isInvalid : isOut),
|
|
268
|
+
autoComplete: "off",
|
|
269
|
+
autoCorrect: "off",
|
|
270
|
+
onChange: (0, import_utils.handlerAll)(props2.onChange, onChange),
|
|
271
|
+
onKeyDown: (0, import_utils.handlerAll)(props2.onKeyDown, onKeyDown),
|
|
272
|
+
onFocus: (0, import_utils.handlerAll)(props2.onFocus, onFocus),
|
|
273
|
+
onBlur: (0, import_utils.handlerAll)(props2.onBlur, onBlur)
|
|
274
|
+
}),
|
|
275
|
+
[
|
|
276
|
+
id,
|
|
277
|
+
name,
|
|
278
|
+
inputMode,
|
|
279
|
+
pattern,
|
|
280
|
+
required,
|
|
281
|
+
disabled,
|
|
282
|
+
readOnly,
|
|
283
|
+
rest,
|
|
284
|
+
format,
|
|
285
|
+
value,
|
|
286
|
+
isInvalid,
|
|
287
|
+
isOut,
|
|
288
|
+
onChange,
|
|
289
|
+
onKeyDown,
|
|
290
|
+
onFocus,
|
|
291
|
+
onBlur
|
|
292
|
+
]
|
|
293
|
+
);
|
|
294
|
+
const getIncrementProps = (0, import_react.useCallback)(
|
|
295
|
+
(props2 = {}, ref = null) => {
|
|
296
|
+
const disabled2 = props2.disabled || keepWithinRange && isMax;
|
|
297
|
+
return {
|
|
298
|
+
required,
|
|
299
|
+
readOnly,
|
|
300
|
+
disabled: disabled2,
|
|
301
|
+
...(0, import_utils.pickObject)(rest, import_form_control.formControlProperties),
|
|
302
|
+
...props2,
|
|
303
|
+
ref: (0, import_utils.mergeRefs)(ref, incrementRef),
|
|
304
|
+
role: "button",
|
|
305
|
+
tabIndex: -1,
|
|
306
|
+
cursor: readOnly ? "not-allowed" : props2.cursor,
|
|
307
|
+
onPointerDown: (0, import_utils.handlerAll)(props2.onPointerDown, (ev) => {
|
|
308
|
+
if (ev.button === 0 && !disabled2)
|
|
309
|
+
eventUp(ev);
|
|
310
|
+
}),
|
|
311
|
+
onPointerLeave: (0, import_utils.handlerAll)(props2.onPointerLeave, stop),
|
|
312
|
+
onPointerUp: (0, import_utils.handlerAll)(props2.onPointerUp, stop)
|
|
313
|
+
};
|
|
314
|
+
},
|
|
315
|
+
[keepWithinRange, isMax, required, readOnly, rest, stop, eventUp]
|
|
316
|
+
);
|
|
317
|
+
const getDecrementProps = (0, import_react.useCallback)(
|
|
318
|
+
(props2 = {}, ref = null) => {
|
|
319
|
+
const disabled2 = props2.disabled || keepWithinRange && isMin;
|
|
320
|
+
return {
|
|
321
|
+
required,
|
|
322
|
+
readOnly,
|
|
323
|
+
disabled: disabled2,
|
|
324
|
+
...(0, import_utils.pickObject)(rest, import_form_control.formControlProperties),
|
|
325
|
+
...props2,
|
|
326
|
+
ref: (0, import_utils.mergeRefs)(ref, decrementRef),
|
|
327
|
+
role: "button",
|
|
328
|
+
tabIndex: -1,
|
|
329
|
+
cursor: readOnly ? "not-allowed" : props2.cursor,
|
|
330
|
+
onPointerDown: (0, import_utils.handlerAll)(props2.onPointerDown, (ev) => {
|
|
331
|
+
if (ev.button === 0 && !disabled2)
|
|
332
|
+
eventDown(ev);
|
|
333
|
+
}),
|
|
334
|
+
onPointerLeave: (0, import_utils.handlerAll)(props2.onPointerLeave, stop),
|
|
335
|
+
onPointerUp: (0, import_utils.handlerAll)(props2.onPointerUp, stop)
|
|
336
|
+
};
|
|
337
|
+
},
|
|
338
|
+
[keepWithinRange, isMin, required, readOnly, rest, stop, eventDown]
|
|
339
|
+
);
|
|
340
|
+
return {
|
|
341
|
+
value: format(value),
|
|
342
|
+
valueAsNumber,
|
|
343
|
+
isFocused,
|
|
344
|
+
isRequired,
|
|
345
|
+
isReadOnly,
|
|
346
|
+
isDisabled,
|
|
347
|
+
getInputProps,
|
|
348
|
+
getIncrementProps,
|
|
349
|
+
getDecrementProps
|
|
350
|
+
};
|
|
351
|
+
};
|
|
352
|
+
var INTERVAL = 50;
|
|
353
|
+
var DELAY = 300;
|
|
354
|
+
var useSpinner = (increment, decrement) => {
|
|
355
|
+
const [isSpinning, setIsSpinning] = (0, import_react.useState)(false);
|
|
356
|
+
const [action, setAction] = (0, import_react.useState)(null);
|
|
357
|
+
const [isOnce, setIsOnce] = (0, import_react.useState)(true);
|
|
358
|
+
const timeoutRef = (0, import_react.useRef)(null);
|
|
359
|
+
const removeTimeout = () => clearTimeout(timeoutRef.current);
|
|
360
|
+
(0, import_use_interval.useInterval)(
|
|
361
|
+
() => {
|
|
362
|
+
if (action === "increment")
|
|
363
|
+
increment();
|
|
364
|
+
if (action === "decrement")
|
|
365
|
+
decrement();
|
|
366
|
+
},
|
|
367
|
+
isSpinning ? INTERVAL : null
|
|
368
|
+
);
|
|
369
|
+
const up = (0, import_react.useCallback)(() => {
|
|
370
|
+
if (isOnce)
|
|
371
|
+
increment();
|
|
372
|
+
timeoutRef.current = setTimeout(() => {
|
|
373
|
+
setIsOnce(false);
|
|
374
|
+
setIsSpinning(true);
|
|
375
|
+
setAction("increment");
|
|
376
|
+
}, DELAY);
|
|
377
|
+
}, [increment, isOnce]);
|
|
378
|
+
const down = (0, import_react.useCallback)(() => {
|
|
379
|
+
if (isOnce)
|
|
380
|
+
decrement();
|
|
381
|
+
timeoutRef.current = setTimeout(() => {
|
|
382
|
+
setIsOnce(false);
|
|
383
|
+
setIsSpinning(true);
|
|
384
|
+
setAction("decrement");
|
|
385
|
+
}, DELAY);
|
|
386
|
+
}, [decrement, isOnce]);
|
|
387
|
+
const stop = (0, import_react.useCallback)(() => {
|
|
388
|
+
setIsOnce(true);
|
|
389
|
+
setIsSpinning(false);
|
|
390
|
+
removeTimeout();
|
|
391
|
+
}, []);
|
|
392
|
+
(0, import_react.useEffect)(() => {
|
|
393
|
+
return () => removeTimeout();
|
|
394
|
+
}, []);
|
|
395
|
+
return { up, down, stop, isSpinning };
|
|
396
|
+
};
|
|
397
|
+
var useAttributeObserver = (ref, attributeFilter, enabled, func) => {
|
|
398
|
+
(0, import_react.useEffect)(() => {
|
|
399
|
+
var _a;
|
|
400
|
+
if (!ref.current || !enabled)
|
|
401
|
+
return;
|
|
402
|
+
const ownerDocument = (_a = ref.current.ownerDocument.defaultView) != null ? _a : window;
|
|
403
|
+
const observer = new ownerDocument.MutationObserver((changes) => {
|
|
404
|
+
for (const { type, attributeName } of changes) {
|
|
405
|
+
if (type === "attributes" && attributeName && attributeFilter.includes(attributeName))
|
|
406
|
+
func();
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
observer.observe(ref.current, { attributes: true, attributeFilter });
|
|
410
|
+
return () => observer.disconnect();
|
|
411
|
+
});
|
|
412
|
+
};
|
|
413
|
+
var [NumberInputContextProvider, useNumberInputContext] = (0, import_utils.createContext)({
|
|
414
|
+
strict: false,
|
|
415
|
+
name: "NumberInputContext"
|
|
416
|
+
});
|
|
417
|
+
var NumberInput = (0, import_core.forwardRef)((props, ref) => {
|
|
418
|
+
const [styles, mergedProps] = (0, import_core.useMultiComponentStyle)("NumberInput", props);
|
|
419
|
+
const {
|
|
420
|
+
className,
|
|
421
|
+
isStepper = true,
|
|
422
|
+
container,
|
|
423
|
+
addon,
|
|
424
|
+
increment,
|
|
425
|
+
decrement,
|
|
426
|
+
onChange,
|
|
427
|
+
...rest
|
|
428
|
+
} = (0, import_core.omitThemeProps)(mergedProps);
|
|
429
|
+
const { getInputProps, getIncrementProps, getDecrementProps } = useNumberInput({
|
|
430
|
+
onChange,
|
|
431
|
+
...rest
|
|
432
|
+
});
|
|
433
|
+
const css = {
|
|
434
|
+
position: "relative",
|
|
435
|
+
zIndex: 0,
|
|
436
|
+
...styles.container
|
|
437
|
+
};
|
|
438
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
439
|
+
NumberInputContextProvider,
|
|
440
|
+
{
|
|
441
|
+
value: { getInputProps, getIncrementProps, getDecrementProps, styles },
|
|
442
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_core.ui.div, { className: (0, import_utils.cx)("ui-number-input", className), __css: css, ...container, children: [
|
|
443
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(NumberInputField, { ...getInputProps(rest, ref) }),
|
|
444
|
+
isStepper ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(NumberInputAddon, { ...addon, children: [
|
|
445
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(NumberIncrementStepper, { ...increment }),
|
|
446
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(NumberDecrementStepper, { ...decrement })
|
|
447
|
+
] }) : null
|
|
448
|
+
] })
|
|
449
|
+
}
|
|
450
|
+
);
|
|
451
|
+
});
|
|
452
|
+
var NumberInputField = (0, import_core.forwardRef)(
|
|
453
|
+
({ className, ...rest }, ref) => {
|
|
454
|
+
const { styles } = useNumberInputContext();
|
|
455
|
+
const css = {
|
|
456
|
+
width: "100%",
|
|
457
|
+
...styles.field
|
|
458
|
+
};
|
|
459
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
460
|
+
import_core.ui.input,
|
|
461
|
+
{
|
|
462
|
+
ref,
|
|
463
|
+
className: (0, import_utils.cx)("ui-number-input-field", className),
|
|
464
|
+
__css: css,
|
|
465
|
+
...rest
|
|
466
|
+
}
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
);
|
|
470
|
+
var NumberInputAddon = (0, import_core.forwardRef)(({ className, ...rest }, ref) => {
|
|
471
|
+
const { styles } = useNumberInputContext();
|
|
472
|
+
const css = {
|
|
473
|
+
display: "flex",
|
|
474
|
+
flexDirection: "column",
|
|
475
|
+
position: "absolute",
|
|
476
|
+
top: "0",
|
|
477
|
+
insetEnd: "0px",
|
|
478
|
+
margin: "1px",
|
|
479
|
+
height: "calc(100% - 2px)",
|
|
480
|
+
zIndex: 1,
|
|
481
|
+
...styles.addon
|
|
482
|
+
};
|
|
483
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
484
|
+
import_core.ui.div,
|
|
485
|
+
{
|
|
486
|
+
ref,
|
|
487
|
+
className: (0, import_utils.cx)("ui-number-input-addon", className),
|
|
488
|
+
"aria-hidden": true,
|
|
489
|
+
__css: css,
|
|
490
|
+
...rest
|
|
491
|
+
}
|
|
492
|
+
);
|
|
493
|
+
});
|
|
494
|
+
var Stepper = (0, import_core.ui)("div", {
|
|
495
|
+
baseStyle: {
|
|
496
|
+
display: "flex",
|
|
497
|
+
justifyContent: "center",
|
|
498
|
+
alignItems: "center",
|
|
499
|
+
flex: 1,
|
|
500
|
+
transitionProperty: "common",
|
|
501
|
+
transitionDuration: "normal",
|
|
502
|
+
userSelect: "none",
|
|
503
|
+
cursor: "pointer",
|
|
504
|
+
lineHeight: "normal"
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
var NumberIncrementStepper = (0, import_core.forwardRef)(
|
|
508
|
+
({ className, children, ...rest }, ref) => {
|
|
509
|
+
const { getIncrementProps, styles } = useNumberInputContext();
|
|
510
|
+
const css = { ...styles.stepper };
|
|
511
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
512
|
+
Stepper,
|
|
513
|
+
{
|
|
514
|
+
className: (0, import_utils.cx)("ui-number-input-stepper", className),
|
|
515
|
+
...getIncrementProps(rest, ref),
|
|
516
|
+
__css: css,
|
|
517
|
+
children: children != null ? children : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icon.ChevronIcon, { __css: { transform: "rotate(180deg)" } })
|
|
518
|
+
}
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
);
|
|
522
|
+
var NumberDecrementStepper = (0, import_core.forwardRef)(
|
|
523
|
+
({ className, children, ...rest }, ref) => {
|
|
524
|
+
const { getDecrementProps, styles } = useNumberInputContext();
|
|
525
|
+
const css = { ...styles.stepper };
|
|
526
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
527
|
+
Stepper,
|
|
528
|
+
{
|
|
529
|
+
className: (0, import_utils.cx)("ui-number-input-stepper", className),
|
|
530
|
+
...getDecrementProps(rest, ref),
|
|
531
|
+
__css: css,
|
|
532
|
+
children: children != null ? children : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icon.ChevronIcon, {})
|
|
533
|
+
}
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
);
|
|
537
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
538
|
+
0 && (module.exports = {
|
|
539
|
+
NumberInput,
|
|
540
|
+
useNumberInput
|
|
541
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as _yamada_ui_core from '@yamada-ui/core';
|
|
2
|
+
import { HTMLUIProps, ThemeProps, CSSUIProps } from '@yamada-ui/core';
|
|
3
|
+
import * as _yamada_ui_utils from '@yamada-ui/utils';
|
|
4
|
+
import { PropGetter } from '@yamada-ui/utils';
|
|
5
|
+
import { UseFormControlProps } from '@yamada-ui/form-control';
|
|
6
|
+
import { UseCounterProps } from '@yamada-ui/use-counter';
|
|
7
|
+
import { InputHTMLAttributes } from 'react';
|
|
8
|
+
|
|
9
|
+
type ValidityState = 'rangeUnderflow' | 'rangeOverflow';
|
|
10
|
+
type UseNumberInputProps = UseFormControlProps<HTMLInputElement> & UseCounterProps & {
|
|
11
|
+
name?: string;
|
|
12
|
+
inputMode?: InputHTMLAttributes<any>['inputMode'];
|
|
13
|
+
pattern?: InputHTMLAttributes<any>['pattern'];
|
|
14
|
+
focusInputOnChange?: boolean;
|
|
15
|
+
clampValueOnBlur?: boolean;
|
|
16
|
+
allowMouseWheel?: boolean;
|
|
17
|
+
onInvalid?: (message: ValidityState, value: string, valueAsNumber: number) => void;
|
|
18
|
+
getAriaValueText?: (value: string | number) => string;
|
|
19
|
+
isValidCharacter?: (value: string) => boolean;
|
|
20
|
+
parse?: (value: string) => string;
|
|
21
|
+
format?: (value: string | number) => string | number;
|
|
22
|
+
};
|
|
23
|
+
declare const useNumberInput: (props?: UseNumberInputProps) => {
|
|
24
|
+
value: string;
|
|
25
|
+
valueAsNumber: number;
|
|
26
|
+
isFocused: boolean;
|
|
27
|
+
isRequired: boolean | undefined;
|
|
28
|
+
isReadOnly: boolean | undefined;
|
|
29
|
+
isDisabled: boolean | undefined;
|
|
30
|
+
getInputProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
|
|
31
|
+
getIncrementProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
|
|
32
|
+
getDecrementProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
|
|
33
|
+
};
|
|
34
|
+
type NumberInputOptions = {
|
|
35
|
+
isStepper?: boolean;
|
|
36
|
+
container?: HTMLUIProps<'div'>;
|
|
37
|
+
addon?: HTMLUIProps<'div'>;
|
|
38
|
+
increment?: NumberIncrementStepperProps;
|
|
39
|
+
decrement?: NumberDecrementStepperProps;
|
|
40
|
+
focusBorderColor?: CSSUIProps<'unresponsive'>['borderColor'];
|
|
41
|
+
errorBorderColor?: CSSUIProps<'unresponsive'>['borderColor'];
|
|
42
|
+
};
|
|
43
|
+
type NumberInputProps = Omit<HTMLUIProps<'input'>, 'disabled' | 'required' | 'readOnly' | 'size' | 'onChange'> & ThemeProps<'NumberInput'> & Omit<UseNumberInputProps, 'disabled' | 'required' | 'readOnly'> & NumberInputOptions;
|
|
44
|
+
declare const NumberInput: _yamada_ui_core.Component<"input", NumberInputProps>;
|
|
45
|
+
type NumberIncrementStepperProps = HTMLUIProps<'div'>;
|
|
46
|
+
type NumberDecrementStepperProps = HTMLUIProps<'div'>;
|
|
47
|
+
|
|
48
|
+
export { NumberInput, NumberInputProps, UseNumberInputProps, useNumberInput };
|