Skip to content

Commit f5da7a6

Browse files
authored
fix(#19879): focusing the wrong handle when using range (#621)
* fix(#19879): focusing the wrong handle when using range * chore(allowCross): add test case where allowCross is true
1 parent e6059d4 commit f5da7a6

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

assets/index.less

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@
5959
background-color: #fff;
6060
touch-action: pan-x;
6161

62-
&:focus {
62+
&-dragging&-dragging&-dragging {
6363
border-color: tint(@primary-color, 20%);
6464
box-shadow: 0 0 0 5px tint(@primary-color, 50%);
65+
}
66+
67+
&:focus {
6568
outline: none;
6669
}
6770

src/Range.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,16 +411,18 @@ class Range extends React.Component {
411411
if (disabled || tabIndex[i] === null) {
412412
_tabIndex = null;
413413
}
414+
const dragging = handle === i;
414415
return handleGenerator({
415416
className: classNames({
416417
[handleClassName]: true,
417418
[`${handleClassName}-${i + 1}`]: true,
419+
[`${handleClassName}-dragging`]: dragging,
418420
}),
419421
prefixCls,
420422
vertical,
423+
dragging,
421424
offset: offsets[i],
422425
value: v,
423-
dragging: handle === i,
424426
index: i,
425427
tabIndex: _tabIndex,
426428
min,

tests/Range.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,53 @@ describe('Range', () => {
183183
expect(wrapper.instance().getSlider().state.bounds[1]).toBe(40);
184184
});
185185

186+
it('should render correctly when allowCross', () => {
187+
class CustomizedRange extends React.Component { // eslint-disable-line
188+
constructor(props) {
189+
super(props);
190+
this.state = {
191+
value: [20, 40],
192+
};
193+
}
194+
onChange = (value) => {
195+
this.setState({
196+
value,
197+
});
198+
}
199+
getSlider() {
200+
return this.refs.slider;
201+
}
202+
render() {
203+
return <Range ref="slider" onChange={this.onChange} value={this.state.value} />;
204+
}
205+
}
206+
const map = {};
207+
document.addEventListener = jest.fn().mockImplementation((event, cb) => {
208+
map[event] = cb;
209+
});
210+
211+
const mockRect = (wrapper) => {
212+
wrapper.instance().getSlider().sliderRef.getBoundingClientRect = () => ({
213+
left: 0,
214+
width: 100,
215+
});
216+
};
217+
218+
const container = document.createElement('div');
219+
document.body.appendChild(container);
220+
221+
const wrapper = mount(<CustomizedRange />, { attachTo: container });
222+
mockRect(wrapper);
223+
224+
expect(wrapper.instance().getSlider().state.bounds).toEqual([20, 40]);
225+
226+
wrapper.find('.rc-slider').simulate('mouseDown', { button: 0, pageX: 0, pageY: 0 });
227+
map.mousemove({ type: 'mousemove', pageX: 60, pageY: 0 });
228+
229+
expect(wrapper.instance().getSlider().state.bounds).toEqual([40, 60]);
230+
expect(wrapper.find('.rc-slider-handle-2').at(1).getDOMNode().className).toContain('rc-slider-handle-dragging');
231+
});
232+
186233
it('should keep pushable with pushable s defalutValue when not allowCross and setState', () => {
187234
class CustomizedRange extends React.Component { // eslint-disable-line
188235
state = {

0 commit comments

Comments
 (0)