Skip to content

Commit

Permalink
[Slider] Simplify integration with form libraries (#22548)
Browse files Browse the repository at this point in the history
  • Loading branch information
youjingwong authored Sep 11, 2020
1 parent 81c10a5 commit ad558cb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
32 changes: 24 additions & 8 deletions packages/material-ui/src/Slider/Slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,22 @@ const Slider = React.forwardRef(function Slider(props, ref) {
name: 'Slider',
});

const handleChange =
onChange &&
((event, value) => {
if (!(event instanceof Event)) event.persist();

// Redefine target to allow name and value to be read.
// This allows seamless integration with the most popular form libraries.
// https://github.com/mui-org/material-ui/issues/13485#issuecomment-676048492
Object.defineProperty(event, 'target', {
writable: true,
value: { value, name },
});

onChange(event, value);
});

const range = Array.isArray(valueDerived);
let values = range ? valueDerived.slice().sort(asc) : [valueDerived];
values = values.map((value) => clamp(value, min, max));
Expand Down Expand Up @@ -538,8 +554,8 @@ const Slider = React.forwardRef(function Slider(props, ref) {
setValueState(newValue);
setFocusVisible(index);

if (onChange) {
onChange(event, newValue);
if (handleChange) {
handleChange(event, newValue);
}
if (onChangeCommitted) {
onChangeCommitted(event, newValue);
Expand Down Expand Up @@ -625,8 +641,8 @@ const Slider = React.forwardRef(function Slider(props, ref) {
focusThumb({ sliderRef, activeIndex, setActive });
setValueState(newValue);

if (onChange) {
onChange(nativeEvent, newValue);
if (handleChange) {
handleChange(nativeEvent, newValue);
}
});

Expand Down Expand Up @@ -674,8 +690,8 @@ const Slider = React.forwardRef(function Slider(props, ref) {

setValueState(newValue);

if (onChange) {
onChange(event, newValue);
if (handleChange) {
handleChange(event, newValue);
}

const doc = ownerDocument(sliderRef.current);
Expand Down Expand Up @@ -725,8 +741,8 @@ const Slider = React.forwardRef(function Slider(props, ref) {

setValueState(newValue);

if (onChange) {
onChange(event, newValue);
if (handleChange) {
handleChange(event, newValue);
}

const doc = ownerDocument(sliderRef.current);
Expand Down
21 changes: 21 additions & 0 deletions packages/material-ui/src/Slider/Slider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,27 @@ describe('<Slider />', () => {
expect(container.querySelectorAll(`.${classes.mark}[data-index="2"]`).length).to.equal(1);
});

it('should pass "name" and "value" as part of the event.target for onChange', () => {
const handleChange = stub().callsFake((event) => event.target);
const { getByRole } = render(
<Slider onChange={handleChange} name="change-testing" value={3} />,
);
const thumb = getByRole('slider');

act(() => {
thumb.focus();
fireEvent.keyDown(thumb, {
key: 'ArrowRight',
});
});

expect(handleChange.callCount).to.equal(1);
expect(handleChange.firstCall.returnValue).to.deep.equal({
name: 'change-testing',
value: 4,
});
});

describe('prop: ValueLabelComponent', () => {
it('receives the formatted value', () => {
function ValueLabelComponent(props) {
Expand Down

0 comments on commit ad558cb

Please sign in to comment.