Skip to content

Commit be95ee6

Browse files
committed
Allow startDate and endDate to be altered even after the component DatetimeRangePicker is initialized.
1 parent 93e69a2 commit be95ee6

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/index.jsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,19 @@ 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(),
1820
};
1921
}
2022

23+
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)
27+
? {}
28+
: { start: newStartDate, end: newEndDate, startDate: newStartDate, endDate: newEndDate }
29+
}
30+
2131
getInputProps() {
2232
const inputReadOnlyStyle = {
2333
cursor: 'pointer',
@@ -66,7 +76,7 @@ class DatetimeRangePicker extends Component {
6676
return {
6777
...baseProps,
6878
...inputProps,
69-
defaultValue: this.props.startDate,
79+
value: this.state.start,
7080
onBlur: this.props.onStartDateBlur,
7181
onFocus: this.props.onStartDateFocus,
7282
timeConstraints: this.props.startTimeConstraints,
@@ -81,7 +91,7 @@ class DatetimeRangePicker extends Component {
8191
...baseProps,
8292
...inputProps,
8393
onBlur: this.props.onEndDateBlur,
84-
defaultValue: this.props.endDate,
94+
value: this.state.end,
8595
onFocus: this.props.onEndDateFocus,
8696
timeConstraints: this.props.endTimeConstraints,
8797
};
@@ -223,9 +233,9 @@ DatetimeRangePicker.propTypes = {
223233
onStartDateFocus: PropTypes.func,
224234
onStartDateChange: PropTypes.func,
225235
pickerClassName: PropTypes.string,
226-
endDate: PropTypes.oneOfType([PropTypes.instanceOf(Moment), PropTypes.instanceOf(Date)]),
236+
endDate: PropTypes.oneOfType([PropTypes.instanceOf(moment), PropTypes.instanceOf(Date)]),
227237
endTimeConstraints: PropTypes.object, // eslint-disable-line
228-
startDate: PropTypes.oneOfType([PropTypes.instanceOf(Moment), PropTypes.instanceOf(Date)]),
238+
startDate: PropTypes.oneOfType([PropTypes.instanceOf(moment), PropTypes.instanceOf(Date)]),
229239
startTimeConstraints: PropTypes.object, // eslint-disable-line
230240
dateFormat: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
231241
timeFormat: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),

0 commit comments

Comments
 (0)