Skip to content

Commit

Permalink
fix(datetimewidget): fix broken onBlur event handling
Browse files Browse the repository at this point in the history
@rsjf (or our `onBlur` function) did not correctly handle the onBlur event--the input's value would
not save to form state. `onBlur` now calls @rsjf's native `onChange` event.

fix #53470
  • Loading branch information
benforshey committed Sep 30, 2024
1 parent 0363d63 commit bf4c783
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.10.1] - 2024-09-30

### Fixed

- `DateTimeWidget` now registers input through keyboard (typing, copy/paste).

## [1.10.0] - 2024-06-04

### Added
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meditor",
"version": "1.10.0",
"version": "1.10.1",
"description": "mEditor, the model editor",
"directories": {
"example": "examples",
Expand Down
43 changes: 25 additions & 18 deletions packages/app/components/jsonschemaform/widgets/DateTimeWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import Flatpickr from 'react-flatpickr'
import { format, zonedTimeToUtc } from 'date-fns-tz'
import type { WidgetProps } from '@rjsf/utils'

function formatDate(date: string, dateFormatOption: any) {
let dateValue

switch (dateFormatOption) {
case 'Z':
dateValue = zonedTimeToUtc(new Date(date), 'Etc/UTC').toISOString()
break

default:
dateValue = format(new Date(date), 'yyyy-MM-dd HH:mm:ssxxx')

break
}

return dateValue
}

function DateTimeWidget(props: WidgetProps) {
const {
id,
Expand All @@ -28,25 +45,15 @@ function DateTimeWidget(props: WidgetProps) {
disabled={disabled || readonly}
autoFocus={autofocus || false}
defaultValue={value}
onBlur={() => {
onBlur(id, value)
}}
onChange={(_selectedDates, date) => {
let dateValue

switch (dateFormatOption) {
case 'Z':
dateValue = zonedTimeToUtc(
new Date(date),
'Etc/UTC'
).toISOString()
break

default:
dateValue = format(new Date(date), 'yyyy-MM-dd HH:mm:ssxxx')
onBlur={(event: any) => {
// @rsjf (or maybe our onBlur function) does not correctly handle the blur event for this component.
// Instead of onBlur, call onChange if there's a value.
if (event.target.value) {
onChange(formatDate(event.target.value, dateFormatOption))
}

onChange(dateValue)
}}
onChange={(_selectedDates: any, date: string) => {
onChange(formatDate(date, dateFormatOption))
}}
options={{
time_24hr: true,
Expand Down

0 comments on commit bf4c783

Please sign in to comment.