Skip to content

Commit 69fc87b

Browse files
authored
Merge pull request #7 from AmauriAires/fix-startDate-and-endDate-props
Allow startDate and endDate to be altered
2 parents 93e69a2 + 9d129b3 commit 69fc87b

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ Almost all the props available for [react-datetime](https://github.com/YouCanBoo
3333

3434
| Name | Type | Default | Description |
3535
|------|------|---------|-------------|
36-
| startDate | `Date` | `new Date()` | This sets the initial start date |
37-
| endDate | `Date` | `new Date()` | This sets the initial end date |
36+
| startDate | `Date` | `new Date()` | This sets a value for the start date |
37+
| endDate | `Date` | `new Date()` | This sets a value for the end date |
3838
| dateFormat | `Boolean` or `String` | `true` | Defines the format for the date. It accepts any `Moment` date format (not in localized format). If `true` the date will be displayed using the defaults for the current `locale`. If `false` the datepicker is disabled and the component can be used as timepicker. |
3939
| timeFormat | `Boolean` or `String` | `true` | Defines the format for the time. It accepts any `Moment` time format (not in localized format). If `true` the time will be displayed using the defaults for the current `locale`. If `false` the timepicker is disabled and the component can be used as datepicker. |
4040
| utc | `boolean` | `false` | When true, start and end time values will be interpreted as UTC. Otherwise they will default to the user's local timezone. |

lib/index.jsx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,24 @@ class DatetimeRangePicker extends Component {
1313
super(props);
1414

1515
this.state = {
16-
start: moment(props.startDate) || moment(),
17-
end: moment(props.endDate) || moment(),
16+
start: null,
17+
end: null,
18+
startDate: null,
19+
endDate: null,
1820
};
1921
}
2022

23+
static getDerivedStateFromProps(nextProps, prevState) {
24+
return nextProps.startDate === prevState.startDate && nextProps.endDate === prevState.endDate
25+
? {}
26+
: {
27+
start: moment(nextProps.startDate) || moment(),
28+
end: moment(nextProps.endDate) || moment(),
29+
startDate: nextProps.startDate,
30+
endDate: nextProps.endDate,
31+
}
32+
}
33+
2134
getInputProps() {
2235
const inputReadOnlyStyle = {
2336
cursor: 'pointer',
@@ -66,7 +79,7 @@ class DatetimeRangePicker extends Component {
6679
return {
6780
...baseProps,
6881
...inputProps,
69-
defaultValue: this.props.startDate,
82+
value: this.state.start,
7083
onBlur: this.props.onStartDateBlur,
7184
onFocus: this.props.onStartDateFocus,
7285
timeConstraints: this.props.startTimeConstraints,
@@ -81,7 +94,7 @@ class DatetimeRangePicker extends Component {
8194
...baseProps,
8295
...inputProps,
8396
onBlur: this.props.onEndDateBlur,
84-
defaultValue: this.props.endDate,
97+
value: this.state.end,
8598
onFocus: this.props.onEndDateFocus,
8699
timeConstraints: this.props.endTimeConstraints,
87100
};
@@ -223,9 +236,11 @@ DatetimeRangePicker.propTypes = {
223236
onStartDateFocus: PropTypes.func,
224237
onStartDateChange: PropTypes.func,
225238
pickerClassName: PropTypes.string,
226-
endDate: PropTypes.oneOfType([PropTypes.instanceOf(Moment), PropTypes.instanceOf(Date)]),
239+
defaultEndDate: PropTypes.oneOfType([PropTypes.instanceOf(moment), PropTypes.instanceOf(Date)]),
240+
endDate: PropTypes.oneOfType([PropTypes.instanceOf(moment), PropTypes.instanceOf(Date)]),
227241
endTimeConstraints: PropTypes.object, // eslint-disable-line
228-
startDate: PropTypes.oneOfType([PropTypes.instanceOf(Moment), PropTypes.instanceOf(Date)]),
242+
startDate: PropTypes.oneOfType([PropTypes.instanceOf(moment), PropTypes.instanceOf(Date)]),
243+
defaultStartDate: PropTypes.oneOfType([PropTypes.instanceOf(moment), PropTypes.instanceOf(Date)]),
229244
startTimeConstraints: PropTypes.object, // eslint-disable-line
230245
dateFormat: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
231246
timeFormat: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-datetime-range-picker",
3-
"description": "Resusable date time range picker",
4-
"version": "1.0.6",
3+
"description": "Reusable date time range picker",
4+
"version": "1.0.7",
55
"author": "Samuel Amoah <sa.am@programmer.net>",
66
"repository": {
77
"type": "git",
@@ -37,7 +37,7 @@
3737
"prop-types": "^15.5.10"
3838
},
3939
"peerDependencies": {
40-
"react": "^15.6.1",
40+
"react": "^16.3.0",
4141
"react-datetime": "^2.8.10"
4242
},
4343
"scripts": {

0 commit comments

Comments
 (0)