Skip to content

Commit a43bf9e

Browse files
committed
Use ResizeDetector to fix date pickers autosize width in hidden containers
1 parent 50cb3e4 commit a43bf9e

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

components/dash-core-components/src/fragments/DatePickerRange.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
strAsDate,
2121
} from '../utils/calendar/helpers';
2222
import {captureCSSForPortal} from '../utils/calendar/cssVariables';
23+
import ResizeDetector from '../utils/ResizeDetector';
2324
import '../components/css/datepickers.css';
2425

2526
const DatePickerRange = ({
@@ -104,6 +105,8 @@ const DatePickerRange = ({
104105
const containerRef = useRef<HTMLDivElement>(null);
105106
const startInputRef = useRef<HTMLInputElement | null>(null);
106107
const endInputRef = useRef<HTMLInputElement | null>(null);
108+
const startAutosizeRef = useRef<any>(null);
109+
const endAutosizeRef = useRef<any>(null);
107110
const calendarRef = useRef<CalendarHandle>(null);
108111
const hasPortal = with_portal || with_full_screen_portal;
109112

@@ -128,6 +131,12 @@ const DatePickerRange = ({
128131
setEndInputValue(formatDate(internalEndDate, display_format));
129132
}, [internalEndDate, display_format]);
130133

134+
const handleResize = useCallback(() => {
135+
startAutosizeRef.current?.updateInputWidth?.();
136+
endAutosizeRef.current?.updateInputWidth?.();
137+
}, []);
138+
139+
131140
useEffect(() => {
132141
// Controls when setProps is called. Basically, whenever internal state
133142
// diverges from props (i.e., user interaction)
@@ -313,6 +322,10 @@ const DatePickerRange = ({
313322
);
314323

315324
return (
325+
<ResizeDetector
326+
onResize={handleResize}
327+
targets={[containerRef]}
328+
>
316329
<div className="dash-datepicker" ref={containerRef}>
317330
<Popover.Root
318331
open={!disabled && isCalendarOpen}
@@ -335,6 +348,7 @@ const DatePickerRange = ({
335348
}}
336349
>
337350
<AutosizeInput
351+
ref={startAutosizeRef}
338352
inputRef={node => {
339353
startInputRef.current = node;
340354
}}
@@ -356,6 +370,7 @@ const DatePickerRange = ({
356370
/>
357371
<ArrowIcon className="dash-datepicker-range-arrow" />
358372
<AutosizeInput
373+
ref={endAutosizeRef}
359374
inputRef={node => {
360375
endInputRef.current = node;
361376
}}
@@ -458,6 +473,7 @@ const DatePickerRange = ({
458473
</Popover.Portal>
459474
</Popover.Root>
460475
</div>
476+
</ResizeDetector>
461477
);
462478
};
463479

components/dash-core-components/src/fragments/DatePickerSingle.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
strAsDate,
1515
} from '../utils/calendar/helpers';
1616
import {captureCSSForPortal} from '../utils/calendar/cssVariables';
17+
import ResizeDetector from '../utils/ResizeDetector';
1718
import '../components/css/datepickers.css';
1819

1920
const DatePickerSingle = ({
@@ -63,6 +64,7 @@ const DatePickerSingle = ({
6364

6465
const containerRef = useRef<HTMLDivElement>(null);
6566
const inputRef = useRef<HTMLInputElement | null>(null);
67+
const autosizeRef = useRef<any>(null);
6668
const calendarRef = useRef<CalendarHandle>(null);
6769
const hasPortal = with_portal || with_full_screen_portal;
6870

@@ -87,6 +89,15 @@ const DatePickerSingle = ({
8789
}
8890
}, [internalDate]);
8991

92+
const handleResize = useCallback(() => {
93+
autosizeRef.current?.updateInputWidth?.();
94+
}, []);
95+
96+
97+
useEffect(() => {
98+
autosizeRef.current?.updateInputWidth?.();
99+
}, [inputValue]);
100+
90101
const parseUserInput = useCallback(
91102
(focusCalendar = false) => {
92103
if (inputValue === '') {
@@ -157,6 +168,7 @@ const DatePickerSingle = ({
157168
}
158169

159170
return (
171+
<ResizeDetector onResize={handleResize} targets={[containerRef]}>
160172
<div className="dash-datepicker" ref={containerRef}>
161173
<Popover.Root
162174
open={!disabled && isCalendarOpen}
@@ -179,6 +191,7 @@ const DatePickerSingle = ({
179191
}}
180192
>
181193
<AutosizeInput
194+
ref={autosizeRef}
182195
inputRef={node => {
183196
inputRef.current = node;
184197
}}
@@ -274,6 +287,7 @@ const DatePickerSingle = ({
274287
</Popover.Portal>
275288
</Popover.Root>
276289
</div>
290+
</ResizeDetector>
277291
);
278292
};
279293

0 commit comments

Comments
 (0)