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 28fc801 commit cc80a71
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions packages/react/src/components/DatePicker/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,6 @@ export default class DatePicker extends Component {
};

UNSAFE_componentWillUpdate(nextProps) {
//Explicitly update minDate and maxDate
const extraProps = ['minDate', 'maxDate'];
extraProps.forEach(prop => {
if (nextProps[prop] !== this.props[prop]) {
if (this.cal) {
this.cal.set(prop, nextProps[prop]);
} else if (this.inputField) {
this.inputField[prop] = nextProps[prop];
}
}
});
if (nextProps.value !== this.props.value) {
if (this.cal) {
this.cal.setDate(nextProps.value);
Expand Down Expand Up @@ -394,11 +383,17 @@ export default class DatePicker extends Component {
}
}

componentDidUpdate({ dateFormat: prevDateFormat }) {
const { dateFormat } = this.props;
if (this.cal && prevDateFormat !== dateFormat) {
this.cal.set({ dateFormat });
}
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];
}
}
});
}

componentWillUnmount() {
Expand Down

0 comments on commit cc80a71

Please sign in to comment.