Skip to content

Commit 2c556b2

Browse files
Ensure components return Date objects with 00:00:00 time OpusCapita#56 (OpusCapita#57)
API changes ==== All public components return Date objects with 00:00:00 time in order to insure consistent output.
1 parent d470dea commit 2c556b2

File tree

9 files changed

+29
-13
lines changed

9 files changed

+29
-13
lines changed

src/client/components/DateInput/DateInput.DOCUMENTATION.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Allows select date using mouse or keyboard.
44

55
Based on configured to OpusCapita defaults [react-day-picker](https://github.com/gpbl/react-day-picker)
66

7+
**Important:** `DatePicker` returns `Date` object with 00:00:00 time.
8+
79
### Props Reference
810

911
| Name | Type | Description |
@@ -29,7 +31,7 @@ Based on configured to OpusCapita defaults [react-day-picker](https://github.com
2931
value={_scope.state.value}
3032
dateFormat="dd/MM/yyyy"
3133
locale="de"
32-
onChange={_scope.handleChange.bind(_scope)}
34+
onChange={_scope.handleChange}
3335
/>
3436

3537
<hr />
@@ -39,7 +41,7 @@ Based on configured to OpusCapita defaults [react-day-picker](https://github.com
3941
dateFormat="dd/MM/yyyy"
4042
disabled={true}
4143
locale="en"
42-
onChange={_scope.handleChange.bind(_scope)}
44+
onChange={_scope.handleChange}
4345
/>
4446

4547
<hr />
@@ -49,7 +51,7 @@ Based on configured to OpusCapita defaults [react-day-picker](https://github.com
4951
dateFormat="dd/MM/yyyy"
5052
locale="fi"
5153
onBlur={(e) => console.log('Blur!', e)}
52-
onChange={_scope.handleChange.bind(_scope)}
54+
onChange={_scope.handleChange}
5355
onFocus={(e) => console.log('Focus!', e)}
5456
showToLeft={true}
5557
showToTop={true}

src/client/components/DateInput/DateInput.SCOPE.react.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DateInputScope extends Component {
1717
};
1818
}
1919

20-
handleChange(value) {
20+
handleChange = (value) => {
2121
this.setState({ value });
2222
}
2323

src/client/components/DateInput/DateInput.react.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import dayjs from '../../dayjs';
1010
import getMessage from '../translations';
1111
import isEqual from 'lodash/isEqual';
1212
import { Portal } from 'react-portal';
13-
import { getCoords, splitProps } from '../utils';
13+
import { getCoords, splitProps, zeroTime } from '../utils';
1414

1515
let springPreset = presets.gentle;
1616
let easeOutCubic = (t) => (--t) * t * t + 1; // eslint-disable-line no-param-reassign
@@ -170,7 +170,7 @@ class DateInput extends Component {
170170
};
171171

172172
handleDateChange = value => {
173-
this.props.onChange(value);
173+
this.props.onChange(zeroTime(value));
174174
this.setState({ error: null });
175175
};
176176

src/client/components/DatePicker/DatePicker.DOCUMENTATION.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ DatePicker shows the calendar button. When you click it, calendar appears. Allow
44

55
Based on configured to OpusCapita defaults [react-day-picker](https://github.com/gpbl/react-day-picker)
66

7+
**Important:** `DatePicker` returns `Date` object with 00:00:00 time.
8+
79
### Props Reference
810

911
| Name | Type | Description |

src/client/components/DatePicker/DatePicker.react.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import DayPicker from '../DayPicker';
55
import { spring, presets, Motion } from 'react-motion';
66
import isEqual from 'lodash/isEqual';
77
import { Portal } from 'react-portal';
8-
import { getCoords, splitProps } from '../utils';
8+
import { getCoords, splitProps, zeroTime } from '../utils';
99

1010
let springPreset = presets.gentle;
1111
let easeOutCubic = (t) => (--t) * t * t + 1; // eslint-disable-line no-param-reassign
@@ -82,7 +82,7 @@ class DatePicker extends Component {
8282
};
8383

8484
handleDateChange = value => {
85-
this.props.onChange(value);
85+
this.props.onChange(zeroTime(value));
8686
};
8787

8888
handleBodyClick = event => {

src/client/components/DateRangeInput/DateRangeInput.DOCUMENTATION.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Allows select date range using mouse.
44

5+
**Important:** `DatePicker` returns `Date` object with 00:00:00 time.
6+
57
### Props Reference
68

79
| Name | Type | Description |

src/client/components/DateRangeInput/DateRangeInput.react.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import dayjs from '../../dayjs';
1010
import { spring, presets, Motion } from 'react-motion';
1111
import getMessage from '../translations';
1212
import { Portal } from 'react-portal';
13-
import { getCoords, splitProps } from '../utils';
13+
import { getCoords, splitProps, zeroTime } from '../utils';
1414

1515
let springPreset = presets.gentle;
1616
let easeOutCubic = (t) => (--t) * t * t + 1; // eslint-disable-line no-param-reassign
@@ -186,7 +186,9 @@ class DateRangeInput extends Component {
186186
this.props.onChange(normalizedRange);
187187
};
188188

189-
handleDayClick = day => {
189+
handleDayClick = dayValue => {
190+
const day = zeroTime(dayValue);
191+
190192
let from = this.props.value[0];
191193
let to = this.props.value[1];
192194

src/client/components/DayPicker/DayPicker.react.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import ReactDayPicker from 'react-day-picker/lib/src/DayPicker';
44
import "react-day-picker/lib/style.css";
55
import localeUtils from '../../dayjs/reactDayPickerUtils';
66
import getMessage from '../translations';
7-
import { splitProps } from '../utils';
7+
import { splitProps, zeroTime } from '../utils';
88
import './DayPicker.less';
99

1010
function Caption(props) {
@@ -107,7 +107,8 @@ class DayPicker extends Component {
107107
};
108108
}
109109

110-
handleDateChange = (date, captionIndex) => {
110+
handleDateChange = (dateValue, captionIndex) => {
111+
const date = zeroTime(dateValue);
111112
if (this.props.isRange) {
112113
let range = this.props.selectedDays[1];
113114
let fromChanged = captionIndex === 0;
@@ -128,7 +129,7 @@ class DayPicker extends Component {
128129
};
129130

130131
handleTodayClick = () => {
131-
this.props.onChange(new Date());
132+
this.props.onChange(zeroTime(new Date()));
132133
};
133134

134135
render() {

src/client/components/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ export const splitProps = (props, specificPropNames = []) => {
3434
return [commonProps, specificProps];
3535
}, [{}, {}]);
3636
}
37+
38+
export const zeroTime = date => {
39+
if (date instanceof Date) {
40+
date.setHours(0, 0, 0);
41+
}
42+
return date;
43+
}

0 commit comments

Comments
 (0)