Skip to content

Commit 793c523

Browse files
committed
warning if is invalidate date type
1 parent bf70725 commit 793c523

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

src/PickerPanel.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ function PickerPanel<DateType>(props: PickerPanelProps<DateType>) {
118118
onMouseDown,
119119
} = props as MergedPickerPanelProps<DateType>;
120120

121+
if (process.env.NODE_ENV !== 'production') {
122+
warning(
123+
!value || generateConfig.isValidate(value),
124+
'Invalidate date pass to `value`.',
125+
);
126+
warning(
127+
!value || generateConfig.isValidate(value),
128+
'Invalidate date pass to `defaultValue`.',
129+
);
130+
}
131+
132+
// ============================ State =============================
121133
const { operationRef, panelRef: panelDivRef } = React.useContext(
122134
PanelContext,
123135
);

src/generate/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface GenerateConfig<DateType> {
2222

2323
// Compare
2424
isAfter: (date1: DateType, date2: DateType) => boolean;
25+
isValidate: (date: DateType) => boolean;
2526

2627
locale: {
2728
getWeekFirstDay: (locale: string) => number;

src/generate/moment.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const generateConfig: GenerateConfig<Moment> = {
6262

6363
// Compare
6464
isAfter: (date1, date2) => date1.isAfter(date2),
65+
isValidate: date => date.isValid(),
6566

6667
locale: {
6768
getWeekFirstDay: locale => {

tests/panel.spec.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React from 'react';
22
import MockDate from 'mockdate';
3+
import moment from 'moment';
4+
import { resetWarned } from 'rc-util/lib/warning';
35
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
46
import { mount, getMoment, isSame, MomentPickerPanel } from './util/commonUtil';
57

@@ -369,4 +371,22 @@ describe('Panel', () => {
369371

370372
expect(wrapper.render()).toMatchSnapshot();
371373
});
374+
375+
it('warning with invalidate value', () => {
376+
resetWarned();
377+
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
378+
379+
const invalidateDate = moment('notValidate', 'YYYY', true);
380+
mount(<MomentPickerPanel value={invalidateDate} />);
381+
expect(errSpy).toHaveBeenCalledWith(
382+
'Warning: Invalidate date pass to `value`.',
383+
);
384+
385+
mount(<MomentPickerPanel defaultValue={invalidateDate} />);
386+
expect(errSpy).toHaveBeenCalledWith(
387+
'Warning: Invalidate date pass to `defaultValue`.',
388+
);
389+
390+
errSpy.mockRestore();
391+
});
372392
});

0 commit comments

Comments
 (0)