refactor: update lifecycle method for Calendar

This commit is contained in:
zy410419243 2019-03-07 16:33:59 +08:00
parent 4dc533c202
commit 4601144f3f

View File

@ -7,6 +7,7 @@ import enUS from './locale/en_US';
import LocaleReceiver from '../locale-provider/LocaleReceiver'; import LocaleReceiver from '../locale-provider/LocaleReceiver';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider'; import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import interopDefault from '../_util/interopDefault'; import interopDefault from '../_util/interopDefault';
import { polyfill } from 'react-lifecycles-compat';
export { HeaderProps } from './Header'; export { HeaderProps } from './Header';
@ -48,7 +49,7 @@ export interface CalendarState {
mode?: CalendarMode; mode?: CalendarMode;
} }
export default class Calendar extends React.Component<CalendarProps, CalendarState> { class Calendar extends React.Component<CalendarProps, CalendarState> {
static defaultProps = { static defaultProps = {
locale: {}, locale: {},
fullscreen: true, fullscreen: true,
@ -74,6 +75,13 @@ export default class Calendar extends React.Component<CalendarProps, CalendarSta
onChange: PropTypes.func, onChange: PropTypes.func,
}; };
static getDerivedStateFromProps(nextProps: CalendarProps) {
if ('value' in nextProps) {
return { value: nextProps.value! };
}
return null;
}
prefixCls?: string; prefixCls?: string;
constructor(props: CalendarProps) { constructor(props: CalendarProps) {
@ -92,15 +100,10 @@ export default class Calendar extends React.Component<CalendarProps, CalendarSta
}; };
} }
componentWillReceiveProps(nextProps: CalendarProps) { componentDidUpdate(prevProps: CalendarProps) {
if ('value' in nextProps) { if ('mode' in this.props && this.props.mode !== prevProps.mode) {
this.setState({ this.setState({
value: nextProps.value!, mode: this.props.mode!,
});
}
if ('mode' in nextProps && nextProps.mode !== this.props.mode) {
this.setState({
mode: nextProps.mode!,
}); });
} }
} }
@ -283,3 +286,7 @@ export default class Calendar extends React.Component<CalendarProps, CalendarSta
); );
} }
} }
polyfill(Calendar);
export default Calendar;