Skip to content
This repository was archived by the owner on Mar 8, 2023. It is now read-only.

Commit f3dfe38

Browse files
authored
Merge pull request #113 from strapi/fix/datetime
Fix datetime
2 parents c902e13 + 231f22a commit f3dfe38

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

packages/buffetjs-custom/src/components/DateTime/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React, { useState, useEffect } from 'react';
88
import moment from 'moment';
99
import PropTypes from 'prop-types';
1010
import momentPropTypes from 'react-moment-proptypes';
11-
11+
import { isEmpty, cloneDeep } from 'lodash';
1212
import { DatePicker, TimePicker } from '@buffetjs/core';
1313
import Wrapper from './Wrapper';
1414

@@ -46,18 +46,18 @@ function DateTime({
4646
step,
4747
...rest
4848
}) {
49-
const [timestamp, setTimestamp] = useState('');
49+
const [timestamp, setTimestamp] = useState(null);
5050
const [isInit, setIsInit] = useState(false);
51-
const setTime = time => {
51+
const setData = time => {
5252
const [hour, minute, second] = time.split(':');
5353
const timeObj = {
5454
hour,
5555
minute,
5656
second,
5757
};
58-
const currentDate = timestamp || moment();
58+
const currentDate = isEmpty(timestamp) ? moment() : cloneDeep(timestamp);
59+
currentDate.set('hours', timeObj.hour);
5960

60-
currentDate.set(timeObj);
6161
setDate(currentDate);
6262
};
6363
const setDate = (date, time) => {
@@ -77,7 +77,7 @@ function DateTime({
7777

7878
setDate(newDate);
7979
} else {
80-
setTimestamp('');
80+
setTimestamp(null);
8181
}
8282

8383
setIsInit(true);
@@ -103,11 +103,11 @@ function DateTime({
103103
name="time"
104104
disabled={disabled}
105105
onChange={({ target }) => {
106-
setTime(target.value);
106+
setData(target.value);
107107
}}
108108
seconds={false}
109109
tabIndex={tabIndex}
110-
value={getTimeString(timestamp)}
110+
value={getTimeString(timestamp) || ''}
111111
step={step}
112112
/>
113113
</Wrapper>

packages/buffetjs-custom/src/components/DateTime/tests/index.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,9 @@ describe('<DateTime />', () => {
7575
};
7676
const renderedComponent = mount(<DateTime {...props} />);
7777
const timepicker = renderedComponent.find(TimePicker);
78-
const updatedValue = '10:10:10';
7978

80-
act(() => {
81-
timepicker.props().onChange({ target: { value: updatedValue } });
82-
});
79+
const mock = { target: { value: '10' } };
80+
timepicker.simulate('change', mock);
8381

8482
const expected = value
8583
.set('hour', 10)

0 commit comments

Comments
 (0)