Skip to content

Commit 81c1040

Browse files
authored
fix: fix rangepicker panel position (#377)
* fix: fix rangepicker panel position * chore: code clean * test: add test case * chore: code clean
1 parent 6123222 commit 81c1040

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/RangePicker.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,13 +893,17 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
893893
// Arrow offset
894894
arrowLeft = startInputDivRef.current.offsetWidth + separatorRef.current.offsetWidth;
895895

896+
// If panelWidth - arrowWidth - arrowMarginLeft < arrowLeft, panel should move to right side.
897+
// If offsetLeft > arrowLeft, arrow position is absolutely right, because arrowLeft is not calculated with arrow margin.
896898
if (
897899
panelDivRef.current.offsetWidth &&
898900
arrowRef.current.offsetWidth &&
899901
arrowLeft >
900902
panelDivRef.current.offsetWidth -
901903
arrowRef.current.offsetWidth -
902-
(direction === 'rtl' ? 0 : arrowRef.current.offsetLeft)
904+
(direction === 'rtl' || arrowRef.current.offsetLeft > arrowLeft
905+
? 0
906+
: arrowRef.current.offsetLeft)
903907
) {
904908
panelLeft = arrowLeft;
905909
}

tests/range.spec.tsx

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,4 +1536,76 @@ describe('Picker.Range', () => {
15361536
wrapper.simulate('mousedown');
15371537
expect(handleMouseDown).toHaveBeenCalled();
15381538
});
1539+
1540+
it('panel should be stable: left', () => {
1541+
const mock = spyElementPrototypes(HTMLElement, {
1542+
offsetWidth: {
1543+
get() {
1544+
if (this.className.includes('range-arrow')) {
1545+
return 14;
1546+
} else if (this.className.includes('panel-container')) {
1547+
return 312;
1548+
} else if (this.className.includes('input')) {
1549+
return 236;
1550+
} else if (this.className.includes('range-separator')) {
1551+
return 10;
1552+
}
1553+
},
1554+
},
1555+
offsetLeft: {
1556+
get() {
1557+
if (this.className.includes('range-arrow')) {
1558+
return 16;
1559+
}
1560+
},
1561+
},
1562+
});
1563+
const wrapper = mount(
1564+
<MomentRangePicker
1565+
allowClear
1566+
defaultValue={[moment('1990-09-03'), moment('1989-11-28')]}
1567+
clearIcon={<span>X</span>}
1568+
suffixIcon={<span>O</span>}
1569+
/>,
1570+
);
1571+
wrapper.openPicker(1);
1572+
expect(wrapper.find('.rc-picker-panel-container').getDOMNode().style.marginLeft).toBe('0px');
1573+
mock.mockRestore();
1574+
});
1575+
1576+
it('panel should be stable: right', () => {
1577+
const mock = spyElementPrototypes(HTMLElement, {
1578+
offsetWidth: {
1579+
get() {
1580+
if (this.className.includes('range-arrow')) {
1581+
return 14;
1582+
} else if (this.className.includes('panel-container')) {
1583+
return 312;
1584+
} else if (this.className.includes('input')) {
1585+
return 236;
1586+
} else if (this.className.includes('range-separator')) {
1587+
return 10;
1588+
}
1589+
},
1590+
},
1591+
offsetLeft: {
1592+
get() {
1593+
if (this.className.includes('range-arrow')) {
1594+
return 262;
1595+
}
1596+
},
1597+
},
1598+
});
1599+
const wrapper = mount(
1600+
<MomentRangePicker
1601+
allowClear
1602+
defaultValue={[moment('1990-09-03'), moment('1989-11-28')]}
1603+
clearIcon={<span>X</span>}
1604+
suffixIcon={<span>O</span>}
1605+
/>,
1606+
);
1607+
wrapper.openPicker(1);
1608+
expect(wrapper.find('.rc-picker-panel-container').getDOMNode().style.marginLeft).toBe('0px');
1609+
mock.mockRestore();
1610+
});
15391611
});

0 commit comments

Comments
 (0)