Skip to content

Commit 10feebb

Browse files
authored
Merge pull request #3 from dfdez/fix/default-date-format
Fix default datetime widget format
2 parents 269a14a + 544a3ac commit 10feebb

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/decap-cms-widget-datetime/src/DateTimeControl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class DateTimeControl extends React.Component {
8282
const { field } = this.props;
8383
let inputType = 'datetime-local';
8484
let inputFormat = 'YYYY-MM-DDTHH:mm';
85-
let format = 'YYYY-MM-DDTHH:mm:ss.SSS[Z]';
85+
let format = `YYYY-MM-DDTHH:mm:ss.SSS${this.isUtc ? '[Z]' : 'Z'}`;
8686
let userFormat = field?.get('format');
8787
let dateFormat = field?.get('date_format');
8888
let timeFormat = field?.get('time_format');

packages/decap-cms-widget-datetime/src/__tests__/DateTimeControl.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,16 @@ function setup(propsOverrides = {}) {
3535
}
3636

3737
describe('DateTimeControl', () => {
38+
const mockDate = '2025-01-01T12:00:00.000Z';
39+
3840
beforeEach(() => {
3941
jest.clearAllMocks();
42+
jest.useFakeTimers();
43+
jest.setSystemTime(new Date(mockDate));
44+
});
45+
46+
afterEach(() => {
47+
jest.useRealTimers();
4048
});
4149

4250
test('renders the component with input, now button, and clear button', () => {
@@ -57,4 +65,26 @@ describe('DateTimeControl', () => {
5765
fireEvent.click(clearButton);
5866
expect(props.onChange).toHaveBeenCalledWith('');
5967
});
68+
69+
test('sets value in custom format (local timezone) when input value changes', () => {
70+
const { input, props } = setup({ field: new Map() })
71+
72+
const testDate = '2024-03-15T10:30:00';
73+
74+
fireEvent.change(input, { target: { value: testDate } });
75+
76+
const expectedValue = dayjs(testDate).format('YYYY-MM-DDTHH:mm:ss.SSSZ');
77+
expect(props.onChange).toHaveBeenCalledWith(expectedValue);
78+
});
79+
80+
test('sets value in custom format (UTC) when input value changes', () => {
81+
const { input, props } = setup({ field: new Map([['picker_utc', true]]) });
82+
83+
const testDate = '2024-03-15T10:30:00';
84+
85+
fireEvent.change(input, { target: { value: testDate } });
86+
87+
const expectedValue = dayjs(testDate).format('YYYY-MM-DDTHH:mm:ss.SSS[Z]');
88+
expect(props.onChange).toHaveBeenCalledWith(expectedValue);
89+
});
6090
});

0 commit comments

Comments
 (0)