Skip to content

Commit 59b6c18

Browse files
committed
Added defaultStartDate and defaultEndDate.
1 parent be95ee6 commit 59b6c18

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ 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+
| defaultStartDate | `Date` | `new Date()` | This sets the default/initial start date |
37+
| defaultEndDate | `Date` | `new Date()` | This sets the default/initial end date |
38+
| startDate | `Date` | `new Date()` | This sets a new value for the start date |
39+
| endDate | `Date` | `new Date()` | This sets a new value for the end date |
3840
| 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. |
3941
| 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. |
4042
| 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: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@ class DatetimeRangePicker extends Component {
1515
this.state = {
1616
start: moment(props.startDate) || moment(),
1717
end: moment(props.endDate) || moment(),
18-
startDate: moment(props.startDate) || moment(),
19-
endDate: moment(props.endDate) || moment(),
18+
startDate: props.startDate,
19+
endDate: props.endDate,
2020
};
2121
}
2222

2323
static getDerivedStateFromProps(nextProps, prevState) {
24-
const newStartDate = moment(nextProps.startDate);
25-
const newEndDate = moment(nextProps.endDate);
26-
return newStartDate.isSame(prevState.startDate) && newEndDate.isSame(prevState.endDate)
24+
return nextProps.startDate === prevState.startDate && nextProps.endDate === prevState.endDate
2725
? {}
28-
: { start: newStartDate, end: newEndDate, startDate: newStartDate, endDate: newEndDate }
26+
: {
27+
start: moment(nextProps.startDate),
28+
end: moment(nextProps.endDate),
29+
startDate: nextProps.startDate,
30+
endDate: nextProps.endDate,
31+
}
2932
}
3033

3134
getInputProps() {
@@ -76,6 +79,7 @@ class DatetimeRangePicker extends Component {
7679
return {
7780
...baseProps,
7881
...inputProps,
82+
defaultValue: this.props.defaultStartDate,
7983
value: this.state.start,
8084
onBlur: this.props.onStartDateBlur,
8185
onFocus: this.props.onStartDateFocus,
@@ -91,6 +95,7 @@ class DatetimeRangePicker extends Component {
9195
...baseProps,
9296
...inputProps,
9397
onBlur: this.props.onEndDateBlur,
98+
defaultValue: this.props.defaultEndDate,
9499
value: this.state.end,
95100
onFocus: this.props.onEndDateFocus,
96101
timeConstraints: this.props.endTimeConstraints,
@@ -233,9 +238,11 @@ DatetimeRangePicker.propTypes = {
233238
onStartDateFocus: PropTypes.func,
234239
onStartDateChange: PropTypes.func,
235240
pickerClassName: PropTypes.string,
241+
defaultEndDate: PropTypes.oneOfType([PropTypes.instanceOf(moment), PropTypes.instanceOf(Date)]),
236242
endDate: PropTypes.oneOfType([PropTypes.instanceOf(moment), PropTypes.instanceOf(Date)]),
237243
endTimeConstraints: PropTypes.object, // eslint-disable-line
238244
startDate: PropTypes.oneOfType([PropTypes.instanceOf(moment), PropTypes.instanceOf(Date)]),
245+
defaultStartDate: PropTypes.oneOfType([PropTypes.instanceOf(moment), PropTypes.instanceOf(Date)]),
239246
startTimeConstraints: PropTypes.object, // eslint-disable-line
240247
dateFormat: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
241248
timeFormat: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),

0 commit comments

Comments
 (0)