diff --git a/docs/examples/range.tsx b/docs/examples/range.tsx index 688fd24f5..acecc8322 100644 --- a/docs/examples/range.tsx +++ b/docs/examples/range.tsx @@ -50,11 +50,12 @@ export default () => {

Value: {value ? `${formatDate(value[0])} ~ ${formatDate(value[1])}` : 'null'}

-
+

Basic

{...sharedProps} + style={{width: '100%'}} value={undefined} locale={zhCN} allowClear diff --git a/src/RangePicker.tsx b/src/RangePicker.tsx index 9421e8585..e1919e8eb 100644 --- a/src/RangePicker.tsx +++ b/src/RangePicker.tsx @@ -892,11 +892,16 @@ function InnerRangePicker(props: RangePickerProps) { const panelWidth = panelDivRef.current.offsetWidth; const arrowWidth = arrowRef.current.offsetWidth; + const inputAreaWidth = startInputDivRef.current.offsetWidth * 2 + separatorRef.current.offsetWidth; + if ( panelWidth && arrowWidth && - arrowLeft > panelWidth - arrowWidth - (direction === 'rtl' ? 0 : arrowMarginLeft) + arrowLeft > panelWidth - arrowWidth - (direction === 'rtl' ? 0 : arrowMarginLeft) && + // If input size is too small that panel cannot move to right, let it keep left. + inputAreaWidth >= 2 * panelWidth ) { + panelLeft = arrowLeft; } } diff --git a/tests/range.spec.tsx b/tests/range.spec.tsx index 93777a925..e0ffd7454 100644 --- a/tests/range.spec.tsx +++ b/tests/range.spec.tsx @@ -1807,7 +1807,7 @@ describe('Picker.Range', () => { } else if (this.className.includes('panel-container')) { return 311; } else if (this.className.includes('input')) { - return 285; + return 306; } else if (this.className.includes('range-separator')) { return 10; } @@ -1831,7 +1831,7 @@ describe('Picker.Range', () => { ); openPicker(container, 1); expect(document.querySelector('.rc-picker-panel-container')).toHaveStyle({ - marginLeft: '295px', + marginLeft: '316px', }); mock.mockRestore(); }); @@ -1927,4 +1927,42 @@ describe('Picker.Range', () => { fireEvent.click(document.querySelector('.rc-picker-cell')); expect(document.querySelectorAll('.rc-picker-input')[1]).toHaveClass('rc-picker-input-active'); }); + + it('If input size is too small that panel cannot move to right, let it keep left', () => { + 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 100; + } else if (this.className.includes('range-separator')) { + return 10; + } + }, + }, + offsetLeft: { + get() { + if (this.className.includes('range-arrow')) { + return 305; + } + }, + }, + }); + const { container } = render( + X} + suffixIcon={O} + />, + ); + openPicker(container, 1); + expect(document.querySelector('.rc-picker-panel-container')).toHaveStyle({ + marginLeft: '0px', + }); + mock.mockRestore(); + }); });