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

fix: panel flick in some edge case #439

Merged
merged 3 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/examples/range.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default () => {
allowClear
ref={rangePickerRef}
showTime
style={{ width: 700 }}
style={{ width: 580 }}
ranges={{
ranges: [moment(), moment().add(10, 'day')],
}}
Expand Down
11 changes: 7 additions & 4 deletions src/RangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -894,16 +894,19 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
arrowLeft = startInputDivRef.current.offsetWidth + separatorRef.current.offsetWidth;

// If panelWidth - arrowWidth - arrowMarginLeft < arrowLeft, panel should move to right side.
// If offsetLeft > arrowLeft, arrow position is absolutely right, because arrowLeft is not calculated with arrow margin.
// If arrowOffsetLeft > arrowLeft, arrowMarginLeft = arrowOffsetLeft - arrowLeft
const arrowMarginLeft =
arrowRef.current.offsetLeft > arrowLeft
? arrowRef.current.offsetLeft - arrowLeft
: arrowRef.current.offsetLeft;

if (
panelDivRef.current.offsetWidth &&
arrowRef.current.offsetWidth &&
arrowLeft >
panelDivRef.current.offsetWidth -
arrowRef.current.offsetWidth -
(direction === 'rtl' || arrowRef.current.offsetLeft > arrowLeft
? 0
: arrowRef.current.offsetLeft)
(direction === 'rtl' ? 0 : arrowMarginLeft)
Copy link
Member

Choose a reason for hiding this comment

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

解释一下?

Copy link
Member Author

Choose a reason for hiding this comment

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

这里是暂时没考虑 rtl 的 case,所以先忽略成 0,marginRight 相对来说算起来麻烦一些

) {
panelLeft = arrowLeft;
}
Expand Down
38 changes: 37 additions & 1 deletion tests/range.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1596,7 +1596,7 @@ describe('Picker.Range', () => {
mock.mockRestore();
});

it('panel should be stable: right', () => {
it('panel should be stable: arrow right and panel left', () => {
const mock = spyElementPrototypes(HTMLElement, {
offsetWidth: {
get() {
Expand Down Expand Up @@ -1631,4 +1631,40 @@ describe('Picker.Range', () => {
expect(wrapper.find('.rc-picker-panel-container').getDOMNode().style.marginLeft).toBe('0px');
mock.mockRestore();
});

it('panel should be stable: arrow right and panel right', () => {
const mock = spyElementPrototypes(HTMLElement, {
offsetWidth: {
get() {
if (this.className.includes('range-arrow')) {
return 14;
} else if (this.className.includes('panel-container')) {
return 311;
} else if (this.className.includes('input')) {
return 285;
} else if (this.className.includes('range-separator')) {
return 10;
}
},
},
offsetLeft: {
get() {
if (this.className.includes('range-arrow')) {
return 305;
}
},
},
});
const wrapper = mount(
<MomentRangePicker
allowClear
defaultValue={[moment('1990-09-03'), moment('1989-11-28')]}
clearIcon={<span>X</span>}
suffixIcon={<span>O</span>}
/>,
);
wrapper.openPicker(1);
expect(wrapper.find('.rc-picker-panel-container').getDOMNode().style.marginLeft).toBe('295px');
mock.mockRestore();
});
});