Skip to content

Commit af06f65

Browse files
authored
add inputRef.current safety check (#3413)
1 parent 88c9849 commit af06f65

File tree

1 file changed

+20
-17
lines changed
  • packages/@react-spectrum/datepicker/src

1 file changed

+20
-17
lines changed

packages/@react-spectrum/datepicker/src/Input.tsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,32 @@ function Input(props, ref) {
3737
// not cause a layout shift.
3838
let [reservePadding, setReservePadding] = useValueEffect(false);
3939
let onResize = useCallback(() => setReservePadding(function *(reservePadding) {
40-
if (reservePadding) {
41-
// Try to collapse padding if the content is clipped.
42-
if (inputRef.current.scrollWidth > inputRef.current.offsetWidth) {
43-
let width = inputRef.current.parentElement.offsetWidth;
44-
yield false;
40+
if (inputRef.current) {
41+
if (reservePadding) {
42+
// Try to collapse padding if the content is clipped.
43+
if (inputRef.current.scrollWidth > inputRef.current.offsetWidth) {
44+
let width = inputRef.current.parentElement.offsetWidth;
45+
yield false;
4546

46-
// If removing padding causes a layout shift, add it back.
47-
if (inputRef.current.parentElement.offsetWidth !== width) {
48-
yield true;
47+
// If removing padding causes a layout shift, add it back.
48+
if (inputRef.current.parentElement.offsetWidth !== width) {
49+
yield true;
50+
}
4951
}
50-
}
51-
} else {
52-
// Try to add padding if the content is not clipped.
53-
if (inputRef.current.offsetWidth >= inputRef.current.scrollWidth) {
54-
let width = inputRef.current.parentElement.offsetWidth;
55-
yield true;
52+
} else {
53+
// Try to add padding if the content is not clipped.
54+
if (inputRef.current.offsetWidth >= inputRef.current.scrollWidth) {
55+
let width = inputRef.current.parentElement.offsetWidth;
56+
yield true;
5657

57-
// If adding padding does not change the width (i.e. width is constrained), remove it again.
58-
if (inputRef.current.parentElement.offsetWidth === width) {
59-
yield false;
58+
// If adding padding does not change the width (i.e. width is constrained), remove it again.
59+
if (inputRef.current.parentElement.offsetWidth === width) {
60+
yield false;
61+
}
6062
}
6163
}
6264
}
65+
6366
}), [inputRef, setReservePadding]);
6467

6568
useLayoutEffect(onResize, [onResize]);

0 commit comments

Comments
 (0)