Permalink
Browse files

fix: DatePicker can not change year/month when under control

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 b9992f4a08574efb47b6e6cd80eb1e888b9a1ede
Showing with 17 additions and 5 deletions.
  1. +7 −1 components/date-picker/RangePicker.tsx
  2. +10 −4 components/date-picker/createPicker.tsx
@@ -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';
@@ -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 = {
@@ -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;

3 comments on commit b9992f4

@afc163

This comment has been minimized.

Member

afc163 replied Jul 25, 2018

补一个测试吧。

@afc163

This comment has been minimized.

Member

afc163 replied Jul 25, 2018

@vimplus

This comment has been minimized.

vimplus replied Jul 27, 2018

什么叫受控模式?

Please sign in to comment.