Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui-date-time-input): fix DateTimeInput displaying wrong value of its value is changed in a onChange callback #1829

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions packages/ui-date-time-input/src/DateTimeInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class DateTimeInput extends Component<DateTimeInputProps, DateTimeInputState> {
event.persist()
// timeout is needed here because handleDayClick could be called in the same
// frame, and it updates calendarSelectedDate which is read in here.
window.setTimeout(() => {
setTimeout(() => {
if ((event as React.KeyboardEvent).key === 'Enter') {
// user pressed enter, use the selected value in the calendar
this.updateStateBasedOnDateInput(this.state.calendarSelectedDate, event)
Expand Down Expand Up @@ -351,7 +351,14 @@ class DateTimeInput extends Component<DateTimeInputProps, DateTimeInputState> {
newState.message = { text: text, type: 'error' }
}
if (this.areDifferentDates(this.state.iso, newState.iso)) {
this.props.onChange?.(e, newState.iso?.toISOString())
if (typeof this.props.onChange === 'function') {
const newDate = newState.iso?.toISOString()
// Timeout is needed here because users might change value in the
// onChange event lister, which might not execute properly
setTimeout(() => {
this.props.onChange?.(e, newDate)
}, 0)
}
}
this.setState(newState)
}
Expand All @@ -368,7 +375,7 @@ class DateTimeInput extends Component<DateTimeInputProps, DateTimeInputState> {
// happens on the target before the relatedTarget gets focus.
// The timeout gives it a moment for that to happen
if (typeof this.props.onBlur === 'function') {
window.setTimeout(() => {
setTimeout(() => {
this.props.onBlur?.(e)
}, 0)
}
Expand Down Expand Up @@ -439,12 +446,12 @@ class DateTimeInput extends Component<DateTimeInputProps, DateTimeInputState> {
// Please note that this causes one hour of time difference in the affected timezones/dates and to
// fully solve this bug we need to change to something like luxon which handles this properly
if (currDate.clone().format('HH') === '23') {
arr.push(currDate.clone().add({hours: 1}))
arr.push(currDate.clone().add({ hours: 1 }))
} else {
arr.push(currDate.clone())
}

currDate.add({days: 1})
currDate.add({ days: 1 })
}
return arr.map((date) => {
const dateStr = date.toISOString()
Expand Down
Loading