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

Function changeToCloseValue

src/Slider.tsx:357–410  ·  view source on GitHub ↗
(newValue: number, e?: React.MouseEvent)

Source from the content-addressed store, hash-verified

355 * Else it will replace the value in the values array.
356 */
357 const changeToCloseValue = (newValue: number, e?: React.MouseEvent) => {
358 if (!disabled) {
359 // Create new values
360 const cloneNextValues = [...rawValues];
361
362 let valueIndex = 0;
363 let valueBeforeIndex = 0; // Record the index which value < newValue
364 let valueDist = mergedMax - mergedMin;
365
366 rawValues.forEach((val, index) => {
367 const dist = Math.abs(newValue - val);
368 if (dist <= valueDist) {
369 valueDist = dist;
370 valueIndex = index;
371 }
372
373 if (val < newValue) {
374 valueBeforeIndex = index;
375 }
376 });
377
378 let focusIndex = valueIndex;
379
380 if (rangeEditable && valueDist !== 0 && (!maxCount || rawValues.length < maxCount)) {
381 cloneNextValues.splice(valueBeforeIndex + 1, 0, newValue);
382 focusIndex = valueBeforeIndex + 1;
383 } else {
384 cloneNextValues[valueIndex] = newValue;
385 }
386
387 // Fill value to match default 2 (only when `rawValues` is empty)
388 if (rangeEnabled && !rawValues.length && count === undefined) {
389 cloneNextValues.push(newValue);
390 }
391
392 const nextValue = getTriggerValue(cloneNextValues);
393 onBeforeChange?.(nextValue);
394 triggerChange(cloneNextValues);
395
396 if (e) {
397 (document.activeElement as HTMLElement)?.blur?.();
398 handlesRef.current.focus(focusIndex);
399 onStartDrag(e, focusIndex, cloneNextValues);
400 } else {
401 // https://github.com/ant-design/ant-design/issues/49997
402 onAfterChange?.(nextValue);
403 warning(
404 !onAfterChange,
405 '[rc-slider] `onAfterChange` is deprecated. Please use `onChangeComplete` instead.',
406 );
407 onChangeComplete?.(nextValue);
408 }
409 }
410 };
411
412 // ============================ Click =============================
413 const onSliderMouseDown: React.MouseEventHandler<HTMLDivElement> = (e) => {

Callers 1

onSliderMouseDownFunction · 0.85

Calls 1

getTriggerValueFunction · 0.85

Tested by

no test coverage detected