Topic: MDBDateTimePicker generate infinite loop when manually change date
dartseng premium asked 2 years ago
I have a datetimepicker with a datetime selected fro menus, when i adjust manually the date (for example from 2022-xx-yy to 2032-xx-yy):
*Expected behavior*The datetimepicker calls onChange handler and show the new date
*Actual behavior*The datetimepicker start an infinite loop of call to the onChange handler alternating the old and the new date.
Resources (screenshots, code snippets etc.)
const date2EnStr = date => date?.getFullYear() + (((date?.getMonth() + 1) > 9) ? "-" : "-0")
+ (date?.getMonth() + 1) + ((date?.getDate() > 9) ? "-" : "-0") + date?.getDate() ;
const time2str24 = date => ((date.getHours() < 10) ? "0" : "") + date.getHours() + ((date.getMinutes() < 10) ? ":0" : ":") + date.getMinutes();
const datePlusMinutes = (date, minutes) => moment(date).add(minutes, 'm').toDate();
function DateTimePickerManager(props) {
function onChangeHandler(dateTimeStr) {
//console.log("DateTimePickerManager onChangeHandler: ", dateTimeStr);
props.onChange(dateTimeStr);
}
let dateOptions = {}
let timeOptions = {
format: '24h',
minTime: '00:00',
maxTime: '23:59'
};
if (props.dateOptions) {
dateOptions = {...dateOptions, ...props.dateOptions};
}
return (
<MDBDateTimepicker id={props.id} inline
label=''
defaultDate={props.date}
defaultTime={props.time}
dateFormat="yyyy-mm-dd"
datepickerOptions={dateOptions}
timepickerOptions={timeOptions}
onChange={onChangeHandler}/>
);}
export function App() {
const now = datePlusMinutes(new Date(), 15);
const [currDate, setCurrDate] = useState('');
function onChangeHandler(dateTimeStr) {
console.log("onChangeHandler: ", dateTimeStr);
setCurrDate(dateTimeStr);
}
let dtpDate = (currDate === '') ? date2EnStr(now) : currDate.split(", ")[0];
let dtpTime = (currDate === '') ? time2str24(now) : currDate.split(", ")[1];
let DateOptions = {value: dtpDate};
let TimeOptions = {}
console.log("call DTPManager: ", dtpDate, dtpTime, DateOptions);
return (
<DateTimePickerManager id="dpstart" date={dtpDate} time={dtpTime} onChange={onChangeHandler}
dateOptions={DateOptions}
timeOptions={TimeOptions}/>
);}
Package.json
"dependencies": {
...
"react-moment": "^1.1.2",
"moment-timezone": "^0.5.34"}
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Status
Opened
Specification of the issue
- ForumUser: Premium
- Premium support: Yes
- Technology: MDB React
- MDB Version: MDB5 5.0.0
- Device: ACER PC
- Browser: Chrome
- OS: Windows 10
- Provided sample code: No
- Provided link: No
Wojciech Staniszewski staff commented 2 years ago
We will check this issue. Possible fix will be available in the next release.
dartseng premium commented 2 years ago
I hope I don't have to wait two month for this fix! However there are a lot of problems with this componet, for example:- the onChange callback setted on timepicker options never happens- the onChange callback on datetimepicker runs immediately after a date settings and again after a time settings, I think would be better to runs only once at the end of the setting operation.
The documentation of datetimepicker is not updated on the onChange callback that now has a parameter with date and time timestamp.
Wojciech Staniszewski staff commented 2 years ago
We will update it, thanks for reporting.