Skip to content

Commit

Permalink
fix: DatePicker can not change year/month when under control
Browse files Browse the repository at this point in the history
React 16.4 call getDerivedStateFromProps on every render

close #11447
close #11440
close #11439
close #11416
close #11405
  • Loading branch information
yesmeck committed Jul 25, 2018
1 parent 36f396f commit b9992f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
8 changes: 7 additions & 1 deletion components/date-picker/RangePicker.tsx
Expand Up @@ -5,6 +5,7 @@ import { polyfill } from 'react-lifecycles-compat';
import RangeCalendar from 'rc-calendar/lib/RangeCalendar';
import RcDatePicker from 'rc-calendar/lib/Picker';
import classNames from 'classnames';
import shallowequal from 'shallowequal';
import Icon from '../icon';
import Tag from '../tag';
import warning from '../_util/warning';
Expand Down Expand Up @@ -77,8 +78,13 @@ class RangePicker extends React.Component<any, RangePickerState> {
const value = nextProps.value || [];
state = {
value,
showDate: getShowDateFromValue(value) || prevState.showDate,
};
if (!shallowequal(nextProps.value, prevState.value)) {
state = {
...state,
showDate: getShowDateFromValue(value) || prevState.showDate,
};
}
}
if (('open' in nextProps) && prevState.open !== nextProps.open) {
state = {
Expand Down
14 changes: 10 additions & 4 deletions components/date-picker/createPicker.tsx
Expand Up @@ -23,14 +23,20 @@ export default function createPicker(TheCalendar: React.ComponentClass): any {
showToday: true,
};

static getDerivedStateFromProps(nextProps: PickerProps) {
static getDerivedStateFromProps(nextProps: PickerProps, prevState: any) {
let state = null;
if ('value' in nextProps) {
return {
state = {
value: nextProps.value,
showDate: nextProps.value,
};
if (nextProps.value !== prevState.value) {
state = {
...state,
showDate: nextProps.value,
};
}
}
return null;
return state;
}

private input: any;
Expand Down

3 comments on commit b9992f4

@afc163
Copy link
Member

@afc163 afc163 commented on b9992f4 Jul 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

补一个测试吧。

@afc163
Copy link
Member

@afc163 afc163 commented on b9992f4 Jul 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vimplus
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

什么叫受控模式?

Please sign in to comment.