Skip to content

Commit 795bed4

Browse files
committed
fix: solve animation jitter when input area less width than picker panel issue
1 parent 6919d7e commit 795bed4

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

docs/examples/range.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ export default () => {
5050
<div>
5151
<h2>Value: {value ? `${formatDate(value[0])} ~ ${formatDate(value[1])}` : 'null'}</h2>
5252

53-
<div style={{ display: 'flex', flexWrap: 'wrap' }}>
53+
<div style={{ display: 'flex', flexWrap: 'wrap', width: '100%' }}>
5454
<div style={{ margin: '0 8px' }}>
5555
<h3>Basic</h3>
5656
<RangePicker<Moment>
5757
{...sharedProps}
58+
style={{width: '100%'}}
5859
value={undefined}
5960
locale={zhCN}
6061
allowClear

src/RangePicker.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,11 +892,16 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
892892
const panelWidth = panelDivRef.current.offsetWidth;
893893
const arrowWidth = arrowRef.current.offsetWidth;
894894

895+
const inputAreaWidth = startInputDivRef.current.offsetWidth * 2 + separatorRef.current.offsetWidth;
896+
895897
if (
896898
panelWidth &&
897899
arrowWidth &&
898-
arrowLeft > panelWidth - arrowWidth - (direction === 'rtl' ? 0 : arrowMarginLeft)
900+
arrowLeft > panelWidth - arrowWidth - (direction === 'rtl' ? 0 : arrowMarginLeft) &&
901+
// If input size is too small that panel cannot move to right, let it keep left.
902+
inputAreaWidth >= 2 * panelWidth
899903
) {
904+
900905
panelLeft = arrowLeft;
901906
}
902907
}

tests/range.spec.tsx

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ describe('Picker.Range', () => {
18071807
} else if (this.className.includes('panel-container')) {
18081808
return 311;
18091809
} else if (this.className.includes('input')) {
1810-
return 285;
1810+
return 400;
18111811
} else if (this.className.includes('range-separator')) {
18121812
return 10;
18131813
}
@@ -1830,8 +1830,9 @@ describe('Picker.Range', () => {
18301830
/>,
18311831
);
18321832
openPicker(container, 1);
1833+
console.log(document.querySelector('.rc-picker-panel-container').getAttribute("style"))
18331834
expect(document.querySelector('.rc-picker-panel-container')).toHaveStyle({
1834-
marginLeft: '295px',
1835+
marginLeft: '410px',
18351836
});
18361837
mock.mockRestore();
18371838
});
@@ -1927,4 +1928,43 @@ describe('Picker.Range', () => {
19271928
fireEvent.click(document.querySelector('.rc-picker-cell'));
19281929
expect(document.querySelectorAll('.rc-picker-input')[1]).toHaveClass('rc-picker-input-active');
19291930
});
1931+
1932+
it('If input size is too small that panel cannot move to right, let it keep left', () => {
1933+
const mock = spyElementPrototypes(HTMLElement, {
1934+
offsetWidth: {
1935+
get() {
1936+
if (this.className.includes('range-arrow')) {
1937+
return 14;
1938+
} else if (this.className.includes('panel-container')) {
1939+
return 311;
1940+
} else if (this.className.includes('input')) {
1941+
return 100;
1942+
} else if (this.className.includes('range-separator')) {
1943+
return 10;
1944+
}
1945+
},
1946+
},
1947+
offsetLeft: {
1948+
get() {
1949+
if (this.className.includes('range-arrow')) {
1950+
return 305;
1951+
}
1952+
},
1953+
},
1954+
});
1955+
const { container } = render(
1956+
<MomentRangePicker
1957+
allowClear
1958+
defaultValue={[moment('1990-09-03'), moment('1989-11-28')]}
1959+
clearIcon={<span>X</span>}
1960+
suffixIcon={<span>O</span>}
1961+
/>,
1962+
);
1963+
openPicker(container, 1);
1964+
console.log(document.querySelector('.rc-picker-panel-container').getAttribute("style"))
1965+
expect(document.querySelector('.rc-picker-panel-container')).toHaveStyle({
1966+
marginLeft: '0px',
1967+
});
1968+
mock.mockRestore();
1969+
});
19301970
});

0 commit comments

Comments
 (0)