Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- DataViews: add support for activity layout. [#72780](https://github.com/WordPress/gutenberg/pull/72780)
- DataViews: Add grid keyboard navigation. [#72997](https://github.com/WordPress/gutenberg/pull/72997)
- Field API: introduce the `format` prop to format the `date` field type. [#72999](https://github.com/WordPress/gutenberg/pull/72999)
- Field API: fix display format for date. [#73538](https://github.com/WordPress/gutenberg/pull/73538)
- Documentation: improve Edit component. [#73202](https://github.com/WordPress/gutenberg/pull/73202)
- Documentation: surface better the `type` property in the documentation. [#73349](https://github.com/WordPress/gutenberg/pull/73349)
- Documentation: improve DataView's `layout` prop. [#73470](https://github.com/WordPress/gutenberg/pull/73470)
Expand Down
4 changes: 2 additions & 2 deletions packages/dataviews/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,7 @@ Display format configuration for fields. Currently supported for date fields. Th
- Optional.
- Properties:
- `date`: The format string using PHP date format (e.g., 'F j, Y' for 'March 10, 2023'). Optional, defaults to WordPress "Date Format" setting.
- `weekStartsOn`: Specifies the first day of the week for calendar controls. One of `'sunday'`, `'monday'`, `'tuesday'`, `'wednesday'`, `'thursday'`, `'friday'`, `'saturday'`. Optional, defaults to WordPress "Week Starts On" setting.
- `weekStartsOn`: Specifies the first day of the week for calendar controls. One of 0, 1, 2, 3, 4, 5, 6. Optional, defaults to WordPress "Week Starts On" setting, whose value is 0 (Sunday).

Example:

Expand All @@ -1861,7 +1861,7 @@ Example:
label: 'Publish Date',
format: {
date: 'F j, Y',
weekStartsOn: 'monday',
weekStartsOn: 1,
},
}
```
Expand Down
4 changes: 3 additions & 1 deletion packages/dataviews/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { arrowDown, arrowUp } from '@wordpress/icons';
/**
* Internal dependencies
*/
import type { Operator } from './types';
import type { Operator, DayNumber } from './types';

// Filter operators.
export const OPERATOR_IS = 'is';
Expand Down Expand Up @@ -188,3 +188,5 @@ export const LAYOUT_ACTIVITY = 'activity';
// Picker view layouts.
export const LAYOUT_PICKER_GRID = 'pickerGrid';
export const LAYOUT_PICKER_TABLE = 'pickerTable';

export const DAYS_OF_WEEK: DayNumber[] = [ 0, 1, 2, 3, 4, 5, 6 ];
9 changes: 2 additions & 7 deletions packages/dataviews/src/dataform-controls/date.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import type {
NormalizedField,
} from '../types';
import getCustomValidity from './utils/get-custom-validity';
import { weekStartsOnToNumber } from '../field-types/utils/week-starts-on';

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

Expand Down Expand Up @@ -276,9 +275,7 @@ function CalendarDateControl< Item >( {
if ( type === 'date' ) {
// If the field type is date, we've already normalized the format,
// and so it's safe to tell TypeScript to trust us ("as Required<Format>").
weekStartsOn = weekStartsOnToNumber(
( fieldFormat as Required< FormatDate > ).weekStartsOn
);
weekStartsOn = ( fieldFormat as Required< FormatDate > ).weekStartsOn;
}

const fieldValue = getValue( { item: data } );
Expand Down Expand Up @@ -444,9 +441,7 @@ function CalendarDateRangeControl< Item >( {
if ( type === 'date' ) {
// If the field type is date, we've already normalized the format,
// and so it's safe to tell TypeScript to trust us ("as Required<Format>").
weekStartsOn = weekStartsOnToNumber(
( fieldFormat as Required< FormatDate > ).weekStartsOn
);
weekStartsOn = ( fieldFormat as Required< FormatDate > ).weekStartsOn;
}

const onChangeCallback = useCallback(
Expand Down
9 changes: 4 additions & 5 deletions packages/dataviews/src/field-types/date.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { dateI18n, getDate, getSettings } from '@wordpress/date';
*/
import type {
DataViewRenderFieldProps,
DayString,
Field,
FormatDate,
NormalizedField,
Expand All @@ -27,8 +26,8 @@ import {
OPERATOR_IN_THE_PAST,
OPERATOR_OVER,
OPERATOR_BETWEEN,
DAYS_OF_WEEK,
} from '../constants';
import { DAYS_OF_WEEK, numberToWeekStartsOn } from './utils/week-starts-on';
import { getControl } from '../dataform-controls';
import hasElements from './utils/has-elements';
import getValueFromId from './utils/get-value-from-id';
Expand All @@ -44,9 +43,9 @@ function getFormat( field: Field< any > ): Required< FormatDate > {
: getSettings().formats.date,
weekStartsOn:
field.format?.weekStartsOn !== undefined &&
DAYS_OF_WEEK.includes( field.format?.weekStartsOn as DayString )
DAYS_OF_WEEK.includes( field.format?.weekStartsOn )
? field.format.weekStartsOn
: numberToWeekStartsOn( getSettings().l10n.startOfWeek ),
: getSettings().l10n.startOfWeek,
};
}

Expand All @@ -72,7 +71,7 @@ function render( { item, field }: DataViewRenderFieldProps< any > ) {
format = field.format as Required< FormatDate >;
}

return dateI18n( format.weekStartsOn, getDate( value ) );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the bug.

return dateI18n( format.date, getDate( value ) );
}

export default function normalizeField< Item >(
Expand Down
46 changes: 0 additions & 46 deletions packages/dataviews/src/field-types/utils/week-starts-on.ts

This file was deleted.

32 changes: 9 additions & 23 deletions packages/dataviews/src/stories/field-types.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -839,14 +839,7 @@ export const DateComponent = ( {
Edit: ControlTypes;
asyncElements: boolean;
formatDate?: string;
formatWeekStartsOn?:
| 'sunday'
| 'monday'
| 'tuesday'
| 'wednesday'
| 'thursday'
| 'friday'
| 'saturday';
formatWeekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
} ) => {
const dateFields = useMemo(
() =>
Expand All @@ -856,14 +849,7 @@ export const DateComponent = ( {
if ( formatDate || formatWeekStartsOn !== undefined ) {
const format: {
date?: string;
weekStartsOn?:
| 'sunday'
| 'monday'
| 'tuesday'
| 'wednesday'
| 'thursday'
| 'friday'
| 'saturday';
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
} = {};
if ( formatDate ) {
format.date = formatDate;
Expand Down Expand Up @@ -905,13 +891,13 @@ DateComponent.argTypes = {
control: 'select',
options: {
Default: undefined,
Sunday: 'sunday',
Monday: 'monday',
Tuesday: 'tuesday',
Wednesday: 'wednesday',
Thursday: 'thursday',
Friday: 'friday',
Saturday: 'saturday',
Sunday: 0,
Monday: 1,
Tuesday: 2,
Wednesday: 3,
Thursday: 4,
Friday: 5,
Saturday: 6,
},
description:
'Day that the week starts on. Leave as Default to use WordPress default.',
Expand Down
8 changes: 3 additions & 5 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', () => {
expect( typeof normalizedFields[ 0 ].format.date ).toBe( 'string' );
expect( normalizedFields[ 0 ].format.weekStartsOn ).toBeDefined();
expect( typeof normalizedFields[ 0 ].format.weekStartsOn ).toBe(
'string'
'number'
);
} );

Expand All @@ -364,15 +364,13 @@ describe( 'normalizeFields: default getValue', () => {
type: 'date',
format: {
date: 'F j, Y',
weekStartsOn: 'monday',
weekStartsOn: 1,
},
},
];
const normalizedFields = normalizeFields( fields );
expect( normalizedFields[ 0 ].format.date ).toBe( 'F j, Y' );
expect( normalizedFields[ 0 ].format.weekStartsOn ).toBe(
'monday'
);
expect( normalizedFields[ 0 ].format.weekStartsOn ).toBe( 1 );
} );

it( 'adds empty format for non-date field types', () => {
Expand Down
10 changes: 1 addition & 9 deletions packages/dataviews/src/types/field-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,9 @@ export type NormalizedFormat = Required< FormatDate > | {};
*/
export type FormatDate = {
date?: string;
weekStartsOn?: DayString;
weekStartsOn?: DayNumber;
};
export type DayNumber = 0 | 1 | 2 | 3 | 4 | 5 | 6;
export type DayString =
| 'sunday'
| 'monday'
| 'tuesday'
| 'wednesday'
| 'thursday'
| 'friday'
| 'saturday';

type NormalizedFieldBase< Item > = Omit< Field< Item >, 'Edit' > & {
label: string;
Expand Down
Loading