Skip to content

Commit 70099b7

Browse files
authored
fix(ui): falling back to UTC timezones in timezone picker (#15841)
We were accidentally falling back to UTC values in the timezone picker when in reality we shouldn't fallback to any timezone unless its specified. Now no timezone will be defaulted to unless its explicitly set as `defaultTimezone`
1 parent 6ae6f10 commit 70099b7

File tree

6 files changed

+77
-2
lines changed

6 files changed

+77
-2
lines changed

packages/ui/src/elements/TimezonePicker/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const TimezonePicker: React.FC<Props> = (props) => {
3030
const selectedTimezone = useMemo(() => {
3131
return options.find((t) => {
3232
const value = typeof t === 'string' ? t : t.value
33-
return value === (selectedTimezoneFromProps || 'UTC')
33+
return value === selectedTimezoneFromProps
3434
})
3535
}, [options, selectedTimezoneFromProps])
3636

test/fields/collections/Date/e2e.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {
1515
saveDocAndAssert,
1616
} from '../../../__helpers/e2e/helpers.js'
1717
import { AdminUrlUtil } from '../../../__helpers/shared/adminUrlUtil.js'
18-
import { initPayloadE2ENoConfig } from '../../../__helpers/shared/initPayloadE2ENoConfig.js'
1918
import { reInitializeDB } from '../../../__helpers/shared/clearAndSeed/reInitializeDB.js'
19+
import { initPayloadE2ENoConfig } from '../../../__helpers/shared/initPayloadE2ENoConfig.js'
2020
import { RESTClient } from '../../../__helpers/shared/rest.js'
2121
import { TEST_TIMEOUT_LONG } from '../../../playwright.config.js'
2222
import { dateFieldsSlug } from '../../slugs.js'
@@ -603,6 +603,28 @@ describe('Date', () => {
603603
// eslint-disable-next-line payload/no-flaky-assertions
604604
expect(existingDoc?.dayAndTimeWithTimezone).toEqual(expectedUTCValue)
605605
})
606+
607+
test('should not show UTC in timezone picker when no defaultTimezone is configured', async () => {
608+
await page.goto(url.create)
609+
610+
const valueContainer = page.locator('#field-dateWithTimezoneNoDefault .rs__value-container')
611+
612+
await expect(valueContainer).toBeVisible()
613+
614+
const singleValue = page.locator('#field-dateWithTimezoneNoDefault .rs__single-value')
615+
616+
await expect(singleValue).toBeHidden()
617+
})
618+
619+
test('should show configured defaultTimezone in timezone picker on create', async () => {
620+
await page.goto(url.create)
621+
622+
const selectedTimezone = page.locator(
623+
'#field-dayAndTimeWithTimezoneRequired .rs__value-container',
624+
)
625+
626+
await expect(selectedTimezone).toContainText('Eastern Time')
627+
})
606628
})
607629

608630
describe('dates with offset timezones', () => {

test/fields/collections/Date/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,22 @@ const DateFields: CollectionConfig = {
230230
],
231231
},
232232
},
233+
{
234+
name: 'dateWithTimezoneNoDefault',
235+
type: 'date',
236+
admin: {
237+
date: {
238+
pickerAppearance: 'dayAndTime',
239+
},
240+
},
241+
timezone: {
242+
supportedTimezones: [
243+
{ label: 'New York', value: 'America/New_York' },
244+
{ label: 'London', value: 'Europe/London' },
245+
{ label: 'UTC', value: 'UTC' },
246+
],
247+
},
248+
},
233249
{
234250
name: 'dateWithTimezoneWithDisabledColumns',
235251
type: 'date',

test/fields/collections/Date/shared.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export const dateDoc: Partial<DateField> = {
3737
// Mixed IANA + offset timezones - 2027-08-12 10:00 AM in America/New_York (UTC-4 in summer) = 14:00 UTC
3838
dateWithMixedTimezones: '2027-08-12T14:00:00.000+00:00',
3939
dateWithMixedTimezones_tz: 'America/New_York',
40+
// Date with timezone but no defaultTimezone configured
41+
dateWithTimezoneNoDefault: '2027-08-12T14:00:00.000+00:00',
42+
dateWithTimezoneNoDefault_tz: 'America/New_York',
4043
// Date with timezone where timezone column is hidden via override
4144
dateWithTimezoneWithDisabledColumns: '2027-08-12T14:00:00.000+00:00',
4245
dateWithTimezoneWithDisabledColumns_tz: 'America/New_York',

test/fields/int.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4812,5 +4812,35 @@ describe('Fields', () => {
48124812
)
48134813
expect(disabledColumnsTzField?.label).toEqual('Date With Timezone With Disabled Columns Tz')
48144814
})
4815+
4816+
it('should not silently default timezone to UTC when no defaultTimezone is configured', async () => {
4817+
const { dateWithTimezoneNoDefault_tz: _, ...dataWithoutNoDefaultTz } = dateDoc
4818+
4819+
const doc = await payload.create({
4820+
collection: dateFieldsSlug,
4821+
data: {
4822+
...dataWithoutNoDefaultTz,
4823+
dateWithTimezoneNoDefault: '2027-08-12T14:00:00.000Z',
4824+
},
4825+
draft: true,
4826+
})
4827+
4828+
expect(doc.dateWithTimezoneNoDefault_tz).toBeFalsy()
4829+
})
4830+
4831+
it('should use configured defaultTimezone when set', async () => {
4832+
const { dateWithMixedTimezones_tz: _, ...dataWithoutMixedTz } = dateDoc
4833+
4834+
const doc = await payload.create({
4835+
collection: dateFieldsSlug,
4836+
data: {
4837+
...dataWithoutMixedTz,
4838+
dateWithMixedTimezones: '2027-08-12T14:00:00.000Z',
4839+
},
4840+
draft: true,
4841+
})
4842+
4843+
expect(doc.dateWithMixedTimezones_tz).toEqual('America/New_York')
4844+
})
48154845
})
48164846
})

test/fields/payload-types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,8 @@ export interface DateField {
12671267
dateWithOffsetTimezone_tz?: ('+05:30' | '-08:00' | '+00:00') | null;
12681268
dateWithMixedTimezones?: string | null;
12691269
dateWithMixedTimezones_tz?: ('America/New_York' | '+05:30' | 'UTC') | null;
1270+
dateWithTimezoneNoDefault?: string | null;
1271+
dateWithTimezoneNoDefault_tz?: ('America/New_York' | 'Europe/London' | 'UTC') | null;
12701272
dateWithTimezoneWithDisabledColumns?: string | null;
12711273
dateWithTimezoneWithDisabledColumns_tz?: SupportedTimezones;
12721274
updatedAt: string;
@@ -3194,6 +3196,8 @@ export interface DateFieldsSelect<T extends boolean = true> {
31943196
dateWithOffsetTimezone_tz?: T;
31953197
dateWithMixedTimezones?: T;
31963198
dateWithMixedTimezones_tz?: T;
3199+
dateWithTimezoneNoDefault?: T;
3200+
dateWithTimezoneNoDefault_tz?: T;
31973201
dateWithTimezoneWithDisabledColumns?: T;
31983202
dateWithTimezoneWithDisabledColumns_tz?: T;
31993203
updatedAt?: T;

0 commit comments

Comments
 (0)