diff --git a/src/PickerPanel.tsx b/src/PickerPanel.tsx index bd90c28ba..69823954d 100644 --- a/src/PickerPanel.tsx +++ b/src/PickerPanel.tsx @@ -118,6 +118,18 @@ function PickerPanel(props: PickerPanelProps) { onMouseDown, } = props as MergedPickerPanelProps; + if (process.env.NODE_ENV !== 'production') { + warning( + !value || generateConfig.isValidate(value), + 'Invalidate date pass to `value`.', + ); + warning( + !value || generateConfig.isValidate(value), + 'Invalidate date pass to `defaultValue`.', + ); + } + + // ============================ State ============================= const { operationRef, panelRef: panelDivRef } = React.useContext( PanelContext, ); diff --git a/src/generate/index.ts b/src/generate/index.ts index 3d6e6c128..47daec4fb 100644 --- a/src/generate/index.ts +++ b/src/generate/index.ts @@ -22,6 +22,7 @@ export interface GenerateConfig { // Compare isAfter: (date1: DateType, date2: DateType) => boolean; + isValidate: (date: DateType) => boolean; locale: { getWeekFirstDay: (locale: string) => number; diff --git a/src/generate/moment.ts b/src/generate/moment.ts index 80ba075d7..3e7c057c4 100644 --- a/src/generate/moment.ts +++ b/src/generate/moment.ts @@ -62,6 +62,7 @@ const generateConfig: GenerateConfig = { // Compare isAfter: (date1, date2) => date1.isAfter(date2), + isValidate: date => date.isValid(), locale: { getWeekFirstDay: locale => { diff --git a/tests/panel.spec.tsx b/tests/panel.spec.tsx index 098a9c546..a78a85b46 100644 --- a/tests/panel.spec.tsx +++ b/tests/panel.spec.tsx @@ -1,5 +1,7 @@ import React from 'react'; import MockDate from 'mockdate'; +import moment from 'moment'; +import { resetWarned } from 'rc-util/lib/warning'; import { spyElementPrototypes } from 'rc-util/lib/test/domHook'; import { mount, getMoment, isSame, MomentPickerPanel } from './util/commonUtil'; @@ -369,4 +371,22 @@ describe('Panel', () => { expect(wrapper.render()).toMatchSnapshot(); }); + + it('warning with invalidate value', () => { + resetWarned(); + const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + + const invalidateDate = moment('notValidate', 'YYYY', true); + mount(); + expect(errSpy).toHaveBeenCalledWith( + 'Warning: Invalidate date pass to `value`.', + ); + + mount(); + expect(errSpy).toHaveBeenCalledWith( + 'Warning: Invalidate date pass to `defaultValue`.', + ); + + errSpy.mockRestore(); + }); });