Repository navigation
Replies: 1 comment
|
Thanks for the detailed proposal, @isBatak. The video and the alternatives you laid out made it easy to follow. We've decided not to add a function You can get order-of-magnitude stepping today by controlling the value. const stepReasons = new Set(["increment-trigger", "decrement-trigger", "keyboard", "wheel"])
const [value, setValue] = useState<number | null>(0.001)
const service = useMachine(numberInput.machine, {
id,
value,
min: 0.0001,
max: 1000,
step: 0.0001,
onValueChange({ value: next, reason }) {
if (reason && stepReasons.has(reason) && value != null && next != null) {
const nextValue = next > value ? value * 10 : value / 10
setValue(Number(nextValue.toPrecision(12)))
return
}
setValue(next)
},
})That gives Closing this for now, but thanks again for the thoughtful write-up. |
Uh oh!
There was an error while loading. Please reload this page.
🚀 Feature request
Allow the
NumberInputstepprop to be dynamic, so the step amount can be calculated from the current value, direction, and step type.This would enable non-linear stepping strategies such as stepping by orders of magnitude while still supporting the existing
smallStepandlargeStepbehavior.🧱 Problem Statement / Justification
NumberInputcurrently accepts a fixed numericstepvalue. This works well for linear stepping:But it cannot represent stepping strategies where the step depends on the current value.
One example is order-of-magnitude stepping:
with decrement going in the opposite direction.
Screen.Recording.2026-09-01.at.10.53.15.mov
The current machine implementation uses
stepas an amount passed toincrementValue/decrementValue:https://github.com/chakra-ui/zag/blob/main/packages/machines/number-input/src/number-input.machine.ts#L373-L382
Because
incrementValueanddecrementValuetreatstepas an amount to add/subtract, the required step can differ by direction for the same current value.For example, from
0.001:A fixed number cannot express this behavior.
smallStepandlargeStepThere is an additional consideration because
NumberInputcurrently derivessmallStepandlargeStepfromstep:https://github.com/chakra-ui/zag/blob/main/packages/machines/number-input/src/number-input.machine.ts
This works when
stepis a number, but would no longer work ifstepis a function.Dynamic stepping therefore needs to account for the type of step being requested as well.
✅ Proposed solution or API
Allow
stepto accept either the existing numeric value or a function:Existing usage remains unchanged:
For dynamic stepping:
For the default step this would produce:
The callback returns the step amount, not the next value, preserving the existing semantics of
incrementValue(value, step)anddecrementValue(value, step).Conceptually, the machine could resolve the step before performing the existing operation:
The same resolver could be used for decrement and for small/large stepping.
Backwards compatibility
The existing numeric API would continue to work:
When
stepis a number,smallStepandlargeStepcould continue to use the current defaults:When
stepis a function, the callback receivestype: "small" | "default" | "large"and is responsible for resolving the appropriate dynamic step.Explicit numeric
smallStep/largeStepvalues could still take precedence if provided.↩️ Alternatives
Make all step props accept functions
Another option would be to make all three properties accept the same dynamic value:
This is more explicit, but requires separate callbacks for what is essentially one stepping strategy.
Introduce
getStepInstead of overloading
step, a separate resolver could be introduced:This makes the distinction between a static step and a dynamic stepping strategy clearer, but adds another prop to the API.
Return the next value
The callback could return the next value directly:
This would provide maximum flexibility, but changes the abstraction from resolving a
stepto controlling the value itself and bypasses the existingincrementValue/decrementValuesemantics.🧢 Your Company/Team
TradeLocker
📝 Additional Information
Relevant implementation:
https://github.com/chakra-ui/zag/blob/main/packages/machines/number-input/src/number-input.machine.ts#L373-L382
All reactions