Skip to content

Commit

Permalink
fix(ui-date-time-input): fix DateTimeInput displaying wrong value of …
Browse files Browse the repository at this point in the history
…its value is changed in a onChange callback

Closes: INSTUI-4406

I could only repropduce it in CodeSandbox, but according to React documentation setState is not
synchronous so the results are not deterministic
TEST PLAN:
Make a DateTimeInput whose value changes in an onChange listerer on CodeSandbox. It should update
properly
  • Loading branch information
matyasf committed Dec 17, 2024
1 parent 4e07f9a commit 69e9d24
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/ui-date-time-input/src/DateTimeInput/index.tsx
Original file line number Diff line number Diff line change
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
window.setTimeout(() => {
this.props.onChange?.(e, newDate)
}, 0)
}
}
this.setState(newState)
}
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

0 comments on commit 69e9d24

Please sign in to comment.