react-datepicker
react-datepicker copied to clipboard
the package may crash application under certain conditions
We have noticed that the crash may occur when changing the timezones.
Occurs when opening Time picker, and based on the code in time.jsx, line 181-184:
This condition may not execute, leaving class variable <this.centerLi> as undefined.
if (isBefore(time, activeTime) || isEqual(time, activeTime)) { this.centerLi = li; }
and then it's being passed as undefined to this method:
static calcCenterPosition = (listHeight, centerLiRef) => { return ( centerLiRef.offsetTop - (listHeight / 2 - centerLiRef.clientHeight / 2) ); };
,
which crashes the application with message:
react-datepicker.min.js:3463 Uncaught TypeError: Cannot read properties of undefined (reading 'offsetTop') at Function.calcCenterPosition (react-datepicker.min.js:3463:1)

Adding optionals, seems to be fixing the issue of APP crash.
static calcCenterPosition = (listHeight, centerLiRef) => { return ( centerLiRef?.offsetTop - (listHeight / 2 - centerLiRef?.clientHeight / 2) ); };
We have tested on latest versions of Chrome, Safari and Firefox.
The PR with proposed fix is here: https://github.com/Hacker0x01/react-datepicker/pull/3702