Skip to content

Commit de86636

Browse files
committed
fix: fix property type 'date' when backend returns an iso string
1 parent ca204b9 commit de86636

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/frontend/components/property-type/datetime/edit.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ import { PropertyLabel } from '../utils/property-label/index.js'
77
import allowOverride from '../../../hoc/allow-override.js'
88
import { useTranslation } from '../../../hooks/index.js'
99
import { PropertyType } from '../../../../backend/index.js'
10+
import { stripTimeFromISO } from './strip-time-from-iso.js'
1011

11-
const formatDate = (val:string|null, propertyType: PropertyType) => {
12-
if (val) return (propertyType === 'date' ? `${val}T00:00:00` : val)
13-
return ''
12+
const formatDate = (date: string | Date | null, propertyType: PropertyType) => {
13+
if (!date) return ''
14+
15+
if (propertyType !== 'date') return date
16+
17+
return `${stripTimeFromISO(date)}T00:00:00`
1418
}
1519

1620
const Edit: React.FC<EditPropertyProps> = (props) => {
@@ -26,7 +30,10 @@ const Edit: React.FC<EditPropertyProps> = (props) => {
2630
value={value}
2731
disabled={property.isDisabled}
2832
onChange={(date) => {
29-
onChange(property.path, property.type === 'date' ? date?.substring(0, 10) ?? date : date)
33+
onChange(
34+
property.path,
35+
property.type === 'date' ? stripTimeFromISO(date) ?? date : date,
36+
)
3037
}}
3138
propertyType={property.type}
3239
{...property.props}

src/frontend/components/property-type/datetime/map-value.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { formatDateProperty } from '@adminjs/design-system'
22

33
import { PropertyType } from '../../../../backend/adapters/property/base-property.js'
4+
import { stripTimeFromISO } from './strip-time-from-iso.js'
45

56
export default (value: Date, propertyType: PropertyType): string => {
67
if (!value) {
78
return ''
89
}
9-
const date = propertyType === 'date' ? new Date(`${value}T00:00:00`) : new Date(value)
10+
const date = propertyType === 'date' ? new Date(`${stripTimeFromISO(value)}T00:00:00`) : new Date(value)
1011
if (date) {
1112
return formatDateProperty(date, propertyType)
1213
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const stripTimeFromISO = (date: string | Date | null): string | null => {
2+
if (date === null) return null
3+
4+
if (typeof date === 'string') {
5+
return date.replace(/T\d{2}:\d{2}:\d{2}\.\d{3}Z$/, '')
6+
}
7+
8+
return date.toISOString().replace(/T\d{2}:\d{2}:\d{2}\.\d{3}Z$/, '')
9+
}

0 commit comments

Comments
 (0)