MCPcopy Index your code
hub / github.com/adobe/react-spectrum / snapValueToStep

Function snapValueToStep

packages/react-stately/src/utils/number.ts:43–73  ·  view source on GitHub ↗
(
  value: number,
  min: number | undefined,
  max: number | undefined,
  step: number
)

Source from the content-addressed store, hash-verified

41}
42
43export function snapValueToStep(
44 value: number,
45 min: number | undefined,
46 max: number | undefined,
47 step: number
48): number {
49 min = Number(min);
50 max = Number(max);
51 let remainder = (value - (isNaN(min) ? 0 : min)) % step;
52 let snappedValue = roundToStepPrecision(
53 Math.abs(remainder) * 2 >= step
54 ? value + Math.sign(remainder) * (step - Math.abs(remainder))
55 : value - remainder,
56 step
57 );
58
59 if (!isNaN(min)) {
60 if (snappedValue < min) {
61 snappedValue = min;
62 } else if (!isNaN(max) && snappedValue > max) {
63 snappedValue = min + Math.floor(roundToStepPrecision((max - min) / step, step)) * step;
64 }
65 } else if (!isNaN(max) && snappedValue > max) {
66 snappedValue = Math.floor(roundToStepPrecision(max / step, step)) * step;
67 }
68
69 // correct floating point behavior by rounding to step precision
70 snappedValue = roundToStepPrecision(snappedValue, step);
71
72 return snappedValue;
73}
74
75/* Takes a value and rounds off to the number of digits. */
76export function toFixedNumber(value: number, digits: number, base: number = 10): number {

Callers 13

number.test.tsFile · 0.90
useNumberFieldStateFunction · 0.90
safeNextStepFunction · 0.90
incrementToMaxFunction · 0.90
useSliderStateFunction · 0.90
updateValueFunction · 0.90
incrementThumbFunction · 0.90
decrementThumbFunction · 0.90
setColorFromPointFunction · 0.90
incrementXFunction · 0.90
incrementYFunction · 0.90
decrementXFunction · 0.90

Calls 1

roundToStepPrecisionFunction · 0.85

Tested by

no test coverage detected