Skip to content

Commit

Permalink
warning if is invalidate date type
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Nov 30, 2019
1 parent bf70725 commit 793c523
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/PickerPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ function PickerPanel<DateType>(props: PickerPanelProps<DateType>) {
onMouseDown,
} = props as MergedPickerPanelProps<DateType>;

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,
);
Expand Down
1 change: 1 addition & 0 deletions src/generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface GenerateConfig<DateType> {

// Compare
isAfter: (date1: DateType, date2: DateType) => boolean;
isValidate: (date: DateType) => boolean;

locale: {
getWeekFirstDay: (locale: string) => number;
Expand Down
1 change: 1 addition & 0 deletions src/generate/moment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const generateConfig: GenerateConfig<Moment> = {

// Compare
isAfter: (date1, date2) => date1.isAfter(date2),
isValidate: date => date.isValid(),

locale: {
getWeekFirstDay: locale => {
Expand Down
20 changes: 20 additions & 0 deletions tests/panel.spec.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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(<MomentPickerPanel value={invalidateDate} />);
expect(errSpy).toHaveBeenCalledWith(
'Warning: Invalidate date pass to `value`.',
);

mount(<MomentPickerPanel defaultValue={invalidateDate} />);
expect(errSpy).toHaveBeenCalledWith(
'Warning: Invalidate date pass to `defaultValue`.',
);

errSpy.mockRestore();
});
});

1 comment on commit 793c523

@vercel
Copy link

@vercel vercel bot commented on 793c523 Nov 30, 2019

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.