Skip to content

Commit

Permalink
fix(DatePicker): explicitly update minDate and maxDate
Browse files Browse the repository at this point in the history
Since flatpickr does not automatically update minDate and maxDate,
we need to explicitly do it when the user makes any change in the props
by using set(option, value) method from flatpickr
Aligned to the change in carbon-design-system#4856
  • Loading branch information
Bui Duc Binh committed Jan 7, 2020
1 parent cc80a71 commit fc786c7
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions packages/react/src/components/DatePicker/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,17 +383,23 @@ export default class DatePicker extends Component {
}
}

componentDidUpdate(prevProps) {
const extraProps = ['dateFormat', 'minDate', 'maxDate'];
extraProps.forEach(prop => {
if (prevProps[prop] !== this.props[prop]) {
if (this.cal) {
this.cal.set(prop, this.props[prop]);
} else if (this.inputField) {
this.inputField[prop] = this.props[prop];
}
componentDidUpdate({
dateFormat: prevDateFormat,
minDate: prevMinDate,
maxDate: prevMaxDate,
}) {
const { dateFormat, minDate, maxDate } = this.props;
if (this.cal) {
if (prevDateFormat !== dateFormat) {
this.cal.set({ dateFormat });
}
});
if (prevMinDate !== minDate) {
this.cal.set('minDate', minDate);
}
if (prevMaxDate !== maxDate) {
this.cal.set('maxDate', maxDate);
}
}
}

componentWillUnmount() {
Expand Down

0 comments on commit fc786c7

Please sign in to comment.