MCPcopy
hub / github.com/react-component/slider / offsetValue

Function offsetValue

src/hooks/useOffset.ts:94–171  ·  view source on GitHub ↗
(values, offset, valueIndex, mode = 'unit')

Source from the content-addressed store, hash-verified

92 // ========================== Offset ==========================
93 // Single Value
94 const offsetValue: OffsetValue = (values, offset, valueIndex, mode = 'unit') => {
95 if (typeof offset === 'number') {
96 let nextValue: number;
97 const originValue = values[valueIndex];
98
99 // Only used for `dist` mode
100 const targetDistValue = originValue + offset;
101
102 // Compare next step value & mark value which is best match
103 let potentialValues: number[] = [];
104 markList.forEach((mark) => {
105 potentialValues.push(mark.value);
106 });
107
108 // Min & Max
109 potentialValues.push(min, max);
110
111 // In case origin value is align with mark but not with step
112 potentialValues.push(formatStepValue(originValue));
113
114 // Put offset step value also
115 const sign = offset > 0 ? 1 : -1;
116
117 if (mode === 'unit') {
118 potentialValues.push(formatStepValue(originValue + sign * step));
119 } else {
120 potentialValues.push(formatStepValue(targetDistValue));
121 }
122
123 // Find close one
124 potentialValues = potentialValues
125 .filter((val) => val !== null)
126 // Remove reverse value
127 .filter((val) => (offset < 0 ? val <= originValue : val >= originValue));
128
129 if (mode === 'unit') {
130 // `unit` mode can not contain itself
131 potentialValues = potentialValues.filter((val) => val !== originValue);
132 }
133
134 const compareValue = mode === 'unit' ? originValue : targetDistValue;
135
136 nextValue = potentialValues[0];
137 let valueDist = Math.abs(nextValue - compareValue);
138
139 potentialValues.forEach((potentialValue) => {
140 const dist = Math.abs(potentialValue - compareValue);
141 if (dist < valueDist) {
142 nextValue = potentialValue;
143 valueDist = dist;
144 }
145 });
146
147 // Out of range will back to range
148 if (nextValue === undefined) {
149 return offset < 0 ? min : max;
150 }
151

Callers 2

offsetChangedValueFunction · 0.85
offsetValuesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected