Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Slider] Don't error on minimal changes with readonly value #28472

Merged
merged 7 commits into from
Sep 27, 2021
Merged
24 changes: 8 additions & 16 deletions packages/mui-core/src/SliderUnstyled/SliderUnstyled.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,10 @@ function roundValueToStep(value, step, min) {
return Number(nearest.toFixed(getDecimalPrecision(step)));
}

function setValueIndex({ values, source, newValue, index }) {
// Performance shortcut
if (source[index] === newValue) {
return source;
}

function setValueIndex({ values, newValue, index }) {
const output = values.slice();
output[index] = newValue;
return output;
return output.sort(asc);
}

function focusThumb({ sliderRef, activeIndex, setActive }) {
Expand Down Expand Up @@ -348,10 +343,9 @@ const SliderUnstyled = React.forwardRef(function SliderUnstyled(props, ref) {
const previousValue = newValue;
newValue = setValueIndex({
values,
source: valueDerived,
newValue,
index,
}).sort(asc);
});

let activeIndex = index;

Expand Down Expand Up @@ -381,7 +375,7 @@ const SliderUnstyled = React.forwardRef(function SliderUnstyled(props, ref) {
axis += '-reverse';
}

const getFingerNewValue = ({ finger, move = false, values: values2, source }) => {
const getFingerNewValue = ({ finger, move = false, values: values2 }) => {
const { current: slider } = sliderRef;
const { width, height, bottom, left } = slider.getBoundingClientRect();
let percent;
Expand Down Expand Up @@ -428,10 +422,9 @@ const SliderUnstyled = React.forwardRef(function SliderUnstyled(props, ref) {
const previousValue = newValue;
newValue = setValueIndex({
values: values2,
source,
newValue,
index: activeIndex,
}).sort(asc);
});

// Potentially swap the index if needed.
if (!(disableSwap && move)) {
Expand Down Expand Up @@ -463,7 +456,6 @@ const SliderUnstyled = React.forwardRef(function SliderUnstyled(props, ref) {
finger,
move: true,
values,
source: valueDerived,
});

focusThumb({ sliderRef, activeIndex, setActive });
Expand All @@ -486,7 +478,7 @@ const SliderUnstyled = React.forwardRef(function SliderUnstyled(props, ref) {
return;
}

const { newValue } = getFingerNewValue({ finger, values, source: valueDerived });
const { newValue } = getFingerNewValue({ finger, values });

setActive(-1);
if (nativeEvent.type === 'touchend') {
Expand Down Expand Up @@ -515,7 +507,7 @@ const SliderUnstyled = React.forwardRef(function SliderUnstyled(props, ref) {
touchId.current = touch.identifier;
}
const finger = trackFinger(nativeEvent, touchId);
const { newValue, activeIndex } = getFingerNewValue({ finger, values, source: valueDerived });
const { newValue, activeIndex } = getFingerNewValue({ finger, values });
focusThumb({ sliderRef, activeIndex, setActive });

setValueState(newValue);
Expand Down Expand Up @@ -572,7 +564,7 @@ const SliderUnstyled = React.forwardRef(function SliderUnstyled(props, ref) {
// Avoid text selection
event.preventDefault();
const finger = trackFinger(event, touchId);
const { newValue, activeIndex } = getFingerNewValue({ finger, values, source: valueDerived });
const { newValue, activeIndex } = getFingerNewValue({ finger, values });
focusThumb({ sliderRef, activeIndex, setActive });

setValueState(newValue);
Expand Down
39 changes: 38 additions & 1 deletion packages/mui-core/src/SliderUnstyled/SliderUnstyled.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import * as React from 'react';
import { expect } from 'chai';
import { createClientRender, createMount, describeConformance, screen } from 'test/utils';
import { spy, stub } from 'sinon';
import {
createClientRender,
createMount,
describeConformance,
fireEvent,
screen,
} from 'test/utils';
import SliderUnstyled, { sliderUnstyledClasses as classes } from '@mui/core/SliderUnstyled';

describe('<SliderUnstyled />', () => {
Expand Down Expand Up @@ -93,4 +100,34 @@ describe('<SliderUnstyled />', () => {
expect(screen.getByRole('slider')).to.have.attribute('aria-valuenow', '30');
});
});

[
['readonly range', Object.freeze([1, 2])],
['range', [1, 2]],
].forEach(([valueLabel, value]) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we write the tests on the "styled" version? To be more "end-to-end"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unstyled version is already the end.

it(`calls onChange even if the ${valueLabel} did not change`, () => {
const handleChange = spy();
const { container } = render(
<SliderUnstyled min={0} max={5} onChange={handleChange} value={value} />,
);
stub(container.firstChild, 'getBoundingClientRect').callsFake(() => ({
width: 100,
height: 10,
bottom: 10,
left: 0,
}));

// pixel: 0 20 40 60 80 100
// slider: |---|---|---|---|---|
// values: 0 1 2 3 4 5
// value: ↑ ↑
// mouse: ↑
fireEvent.mouseDown(container.firstChild, {
buttons: 1,
clientX: 41,
});

expect(handleChange.callCount).to.equal(1);
});
});
});