fix: Slider step prop not accepting null value (#26984)

* Fixes #26976 since rc-slider allows step to be null

* Fixes #26976 since rc-slider allows step to be null

* test: Slider step prop should not crash with undefined value
This commit is contained in:
Lim Shang Yi 2020-10-02 22:15:12 +08:00 committed by GitHub
parent 9cd432e52e
commit ef3aabdf7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 1 deletions

View File

@ -40,6 +40,44 @@ describe('Slider', () => {
expect(wrapper.find('.ant-tooltip-content').length).toBe(0); expect(wrapper.find('.ant-tooltip-content').length).toBe(0);
}); });
it('when step is null, thumb can only be slided to the specific mark', () => {
const intentionallyWrongValue = 40;
const marks = {
0: '0',
48: '48',
100: '100',
};
const wrapper = mount(
<Slider marks={marks} defaultValue={intentionallyWrongValue} step={null} tooltipVisible />,
);
expect(wrapper.find('.ant-slider-handle').get(0).props).toHaveProperty('value', 48);
});
it('when step is not null, thumb can be slided to the multiples of step', () => {
const marks = {
0: '0',
48: '48',
100: '100',
};
const wrapper = mount(<Slider marks={marks} defaultValue={49} step={1} tooltipVisible />);
expect(wrapper.find('.ant-slider-handle').get(0).props).toHaveProperty('value', 49);
});
it('when step is undefined, thumb can be slided to the multiples of step', () => {
const marks = {
0: '0',
48: '48',
100: '100',
};
const wrapper = mount(
<Slider marks={marks} defaultValue={49} step={undefined} tooltipVisible />,
);
expect(wrapper.find('.ant-slider-handle').get(0).props).toHaveProperty('value', 49);
});
it('should render in RTL direction', () => { it('should render in RTL direction', () => {
const wrapper = mount( const wrapper = mount(
<ConfigProvider direction="rtl"> <ConfigProvider direction="rtl">
@ -70,4 +108,9 @@ describe('Slider', () => {
mount(<Slider value={value} tooltipVisible />); mount(<Slider value={value} tooltipVisible />);
}); });
}); });
it('step should not crash with undefined value', () => {
[undefined, null].forEach(value => {
mount(<Slider step={value} tooltipVisible />);
});
});
}); });

View File

@ -29,4 +29,21 @@ describe('Slider.typescript', () => {
); );
expect(result).toBeTruthy(); expect(result).toBeTruthy();
}); });
it('step can be null value', () => {
const value = 0;
function onChange(v: number) {
return v;
}
const result = (
<Slider
defaultValue={value}
value={value}
onChange={onChange}
onAfterChange={onChange}
step={null}
/>
);
expect(result).toBeTruthy();
});
}); });

View File

@ -32,7 +32,7 @@ export interface SliderBaseProps {
reverse?: boolean; reverse?: boolean;
min?: number; min?: number;
max?: number; max?: number;
step?: number; step?: null | number;
marks?: SliderMarks; marks?: SliderMarks;
dots?: boolean; dots?: boolean;
included?: boolean; included?: boolean;
@ -143,6 +143,7 @@ const Slider = React.forwardRef<unknown, SliderSingleProps | SliderRangeProps>(
return ( return (
<RcRange <RcRange
{...(restProps as SliderRangeProps)} {...(restProps as SliderRangeProps)}
step={restProps.step!}
className={cls} className={cls}
ref={ref} ref={ref}
handle={(info: HandleGeneratorInfo) => handle={(info: HandleGeneratorInfo) =>
@ -159,6 +160,7 @@ const Slider = React.forwardRef<unknown, SliderSingleProps | SliderRangeProps>(
return ( return (
<RcSlider <RcSlider
{...(restProps as SliderSingleProps)} {...(restProps as SliderSingleProps)}
step={restProps.step!}
className={cls} className={cls}
ref={ref} ref={ref}
handle={(info: HandleGeneratorInfo) => handle={(info: HandleGeneratorInfo) =>