Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use day names instead of numbers for the API
  • Loading branch information
oandregal committed Nov 13, 2025
commit 70f44f9de6b4b0bca308a65cb4e0c4b875148d4c
9 changes: 7 additions & 2 deletions packages/dataviews/src/dataform-controls/date.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import type {
NormalizedField,
} from '../types';
import getCustomValidity from './utils/get-custom-validity';
import { weekStartsOnToNumber } from '../utils/week-starts-on';

const { DateCalendar, DateRangeCalendar } = unlock( componentsPrivateApis );

Expand Down Expand Up @@ -395,7 +396,9 @@ function CalendarDateControl< Item >( {
month={ calendarMonth }
onMonthChange={ setCalendarMonth }
timeZone={ timezoneString || undefined }
weekStartsOn={ field.displayFormat.weekStartsOn }
weekStartsOn={ weekStartsOnToNumber(
field.displayFormat.weekStartsOn
) }
/>
</VStack>
</BaseControl>
Expand Down Expand Up @@ -608,7 +611,9 @@ function CalendarDateRangeControl< Item >( {
month={ calendarMonth }
onMonthChange={ setCalendarMonth }
timeZone={ timezone.string || undefined }
weekStartsOn={ field.displayFormat.weekStartsOn }
weekStartsOn={ weekStartsOnToNumber(
field.displayFormat.weekStartsOn
) }
/>
</VStack>
</BaseControl>
Expand Down
32 changes: 23 additions & 9 deletions packages/dataviews/src/stories/field-types.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,14 @@ export const DateComponent = ( {
Edit: ControlTypes;
asyncElements: boolean;
displayFormatDate?: string;
displayFormatWeekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
displayFormatWeekStartsOn?:
| 'sunday'
| 'monday'
| 'tuesday'
| 'wednesday'
| 'thursday'
| 'friday'
| 'saturday';
} ) => {
const dateFields = useMemo(
() =>
Expand All @@ -852,7 +859,14 @@ export const DateComponent = ( {
) {
const displayFormat: {
date?: string;
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
weekStartsOn?:
| 'sunday'
| 'monday'
| 'tuesday'
| 'wednesday'
| 'thursday'
| 'friday'
| 'saturday';
} = {};
if ( displayFormatDate ) {
displayFormat.date = displayFormatDate;
Expand Down Expand Up @@ -895,13 +909,13 @@ DateComponent.argTypes = {
control: 'select',
options: {
Default: undefined,
Sunday: 0,
Monday: 1,
Tuesday: 2,
Wednesday: 3,
Thursday: 4,
Friday: 5,
Saturday: 6,
Sunday: 'sunday',
Monday: 'monday',
Tuesday: 'tuesday',
Wednesday: 'wednesday',
Thursday: 'thursday',
Friday: 'friday',
Saturday: 'saturday',
},
description:
'Day that the week starts on. Leave as Default to use WordPress default.',
Expand Down
6 changes: 3 additions & 3 deletions packages/dataviews/src/test/normalize-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ describe( 'normalizeFields: default getValue', () => {
).toBeDefined();
expect(
typeof normalizedFields[ 0 ].displayFormat.weekStartsOn
).toBe( 'number' );
).toBe( 'string' );
} );

it( 'preserves custom format when provided', () => {
Expand All @@ -363,14 +363,14 @@ describe( 'normalizeFields: default getValue', () => {
type: 'date',
displayFormat: {
date: 'F j, Y',
weekStartsOn: 1,
weekStartsOn: 'monday',
},
},
];
const normalizedFields = normalizeFields( fields );
expect( normalizedFields[ 0 ].displayFormat.date ).toBe( 'F j, Y' );
expect( normalizedFields[ 0 ].displayFormat.weekStartsOn ).toBe(
1
'monday'
);
} );

Expand Down
11 changes: 9 additions & 2 deletions packages/dataviews/src/types/field-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,20 @@ export type Field< Item > = {
* Format for date fields:
*
* - date: the format string (e.g., 'F j, Y' for WordPress default format like 'March 10, 2023')
* - weekStartsOn: to specify the first day of the week (0 is Sunday).
* - weekStartsOn: to specify the first day of the week ('sunday', 'monday', etc.).
*
* If not provided, defaults to WordPress date format settings.
*/
type DisplayFormatDate = {
date?: string;
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
weekStartsOn?:
| 'sunday'
| 'monday'
| 'tuesday'
| 'wednesday'
| 'thursday'
| 'friday'
| 'saturday';
};

export type NormalizedField< Item > = Omit< Field< Item >, 'Edit' > & {
Expand Down
5 changes: 4 additions & 1 deletion packages/dataviews/src/utils/normalize-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
SINGLE_SELECTION_OPERATORS,
} from '../constants';
import hasElements from './has-elements';
import { numberToWeekStartsOn } from './week-starts-on';

const getValueFromId =
( id: string ) =>
Expand Down Expand Up @@ -216,7 +217,9 @@ export default function normalizeFields< Item >(
field.type === 'date' &&
field.displayFormat?.weekStartsOn !== undefined
? field.displayFormat.weekStartsOn
: getSettings().l10n.startOfWeek,
: numberToWeekStartsOn(
getSettings().l10n.startOfWeek
),
},
};
} );
Expand Down
57 changes: 57 additions & 0 deletions packages/dataviews/src/utils/week-starts-on.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Converts a weekStartsOn string to a number (0-6).
*
* @param day - The day name ('sunday', 'monday', etc.)
* @return The corresponding number (0 for Sunday, 1 for Monday, etc.)
*/
export function weekStartsOnToNumber(
day:
| 'sunday'
| 'monday'
| 'tuesday'
| 'wednesday'
| 'thursday'
| 'friday'
| 'saturday'
): 0 | 1 | 2 | 3 | 4 | 5 | 6 {
const mapping = {
sunday: 0,
monday: 1,
tuesday: 2,
wednesday: 3,
thursday: 4,
friday: 5,
saturday: 6,
} as const;

return mapping[ day ];
}

/**
* Converts a weekStartsOn number (0-6) to a string.
*
* @param day - The day number (0 for Sunday, 1 for Monday, etc.)
* @return The corresponding day name ('sunday', 'monday', etc.)
*/
export function numberToWeekStartsOn(
day: 0 | 1 | 2 | 3 | 4 | 5 | 6
):
| 'sunday'
| 'monday'
| 'tuesday'
| 'wednesday'
| 'thursday'
| 'friday'
| 'saturday' {
const mapping = {
0: 'sunday',
1: 'monday',
2: 'tuesday',
3: 'wednesday',
4: 'thursday',
5: 'friday',
6: 'saturday',
} as const;

return mapping[ day ];
}