Skip to content

Commit

Permalink
refactor: upgrade date-fns to v2 (rsuite#1064)
Browse files Browse the repository at this point in the history
* refactor: upgrade date-fns to v2

* fix: change useDateFnsPolyfill from declare to constance variable

* fix: update Spanish formatPattern

* fix: control polyfill by global define  '__RSUITE_ENABLE_DATE_FNS_POLYFILL__'

* chore: update package-lock.json

* fix: change ISO week to locale week rsuite#892

* fix: remove polyfill

Co-authored-by: Sleaf <[email protected]>
  • Loading branch information
Sleaf and Sleaf authored Jul 20, 2020
1 parent 4466927 commit cb7f227
Show file tree
Hide file tree
Showing 79 changed files with 409 additions and 316 deletions.
1 change: 0 additions & 1 deletion .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = (api, options) => {

const plugins = [
'lodash',
'date-fns',
['@babel/plugin-proposal-class-properties', { loose: true }],
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator',
Expand Down
11 changes: 8 additions & 3 deletions docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"@rsuite/responsive-nav": "0.0.10",
"color": "^3.1.2",
"date-fns": "^1.30.1",
"date-fns": "^2.13.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-plugin-import": "^2.19.1",
"isomorphic-fetch": "^2.2.1",
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/calendar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { default as dateFns } from 'date-fns';
import * as dateFns from 'date-fns';
import { Calendar, Button, Tag, Popover, Whisper, Badge } from 'rsuite';
import DefaultPage from '@/components/Page';

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-picker/en-US/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const instance = (
label: 'Prev Day',
closeOverlay: false,
value: datePage => {
return dateFns.addDays(dateFns.parse(datePage), -1);
return dateFns.addDays(dateFns.parseISO(datePage), -1);
}
}
]}
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-picker/en-US/disabled.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const DatePickerInstance = props => (
<p> Disable Month</p>
<DatePicker
disabledDate={date => dateFns.isBefore(date, new Date())}
format="YYYY-MM"
format="yyyy-MM"
/>

<p> Disable Time</p>
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/components/date-picker/en-US/format-month.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<!--start-code-->
```js
const DatePickerInstance = props => (
<DatePicker format="YYYY-MM" ranges={[]} />
<DatePicker format="yyyy-MM" ranges={[]} />
);

ReactDOM.render(<DatePickerInstance />);

```
<!--end-code-->
<!--end-code-->
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const ranges = [
];
const App = () => (
<div>
<DatePicker format="DD MMM YYYY hh:mm:ss A" showMeridian ranges={ranges} />
<DatePicker format="dd MMM yyyy hh:mm:ss a" showMeridian ranges={ranges} />
<hr />
<DatePicker format="hh:mm:ss A" showMeridian ranges={ranges} />
<DatePicker format="hh:mm:ss a" showMeridian ranges={ranges} />
</div>
);

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-picker/en-US/format.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
```js
const instance = (
<DatePicker
format="YYYY-MM-DD HH:mm:ss"
format="yyyy-MM-dd HH:mm:ss"
ranges={[
{
label: 'Now',
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-picker/en-US/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { DatePicker } from 'rsuite';
| disabledHours | (hour:number, date:Date) => boolean | Disabled hours |
| disabledMinutes | (minute:number, date:Date) => boolean | Disabled minutes |
| disabledSeconds | (second:number, date:Date) => boolean | Disabled seconds |
| format | string `('YYYY-MM-DD')` | Format date |
| format | string `('yyyy-MM-dd')` | Format date |
| hideHours | (hour:number, date:Date) => boolean | Hidden hours |
| hideMinutes | (minute:number, date:Date) => boolean | Hidden minutes |
| hideSeconds | (second:number, date:Date) => boolean | Hidden seconds |
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-picker/en-US/intl.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const DatePickerIntl = props => (
<div className="field only-date">
<DatePicker
format="YYYY-MM-DD HH:mm:ss"
format="yyyy-MM-dd HH:mm:ss"
locale={{
sunday: 'Su',
monday: 'Mo',
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/components/date-picker/en-US/range.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
```js
const instance = (
<InputGroup style={{ width: 460 }}>
<DatePicker format="YYYY-MM-DD HH:mm:ss" block appearance="subtle" />
<DatePicker format="yyyy-MM-dd HH:mm:ss" block appearance="subtle" />
<InputGroup.Addon></InputGroup.Addon>
<DatePicker format="YYYY-MM-DD HH:mm:ss" block appearance="subtle" />
<DatePicker format="yyyy-MM-dd HH:mm:ss" block appearance="subtle" />
</InputGroup>
);

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-picker/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import dateFns from 'date-fns';
import * as dateFns from 'date-fns';
import { DatePicker, Button, InputGroup } from 'rsuite';
import DefaultPage from '@/components/Page';

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-picker/zh-CN/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const instance = (
label: '前一天',
closeOverlay: false,
value: datePage => {
return dateFns.addDays(dateFns.parse(datePage), -1);
return dateFns.addDays(dateFns.parseISO(datePage), -1);
}
}
]}
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-picker/zh-CN/disabled.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const DatePickerInstance = props => (
<p> 禁用月份</p>
<DatePicker
disabledDate={date => dateFns.isBefore(date, new Date())}
format="YYYY-MM"
format="yyyy-MM"
/>

<p> 禁用时间</p>
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/components/date-picker/zh-CN/format-month.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<!--start-code-->
```js
const DatePickerInstance = props => (
<DatePicker format="YYYY-MM" ranges={[]} />
<DatePicker format="yyyy-MM" ranges={[]} />
);

ReactDOM.render(<DatePickerInstance />);

```
<!--end-code-->
<!--end-code-->
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const ranges = [
];
const App = () => (
<div>
<DatePicker format="YYYY-MM-DD hh:mm:ss A" showMeridian ranges={ranges} />
<DatePicker format="yyyy-MM-dd hh:mm:ss a" showMeridian ranges={ranges} />
<hr />
<DatePicker format="hh:mm:ss A" showMeridian ranges={ranges} />
<DatePicker format="hh:mm:ss a" showMeridian ranges={ranges} />
</div>
);

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-picker/zh-CN/format.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
```js
const instance = (
<DatePicker
format="YYYY-MM-DD HH:mm:ss"
format="yyyy-MM-dd HH:mm:ss"
ranges={[
{
label: 'Now',
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-picker/zh-CN/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { DatePicker } from 'rsuite';
| disabledHours | (hour:number, date:Date) => boolean | 禁用小时 |
| disabledMinutes | (minute:number, date:Date) => boolean | 禁用分钟 |
| disabledSeconds | (second:number, date:Date) => boolean | 禁用秒 |
| format | string `('YYYY-MM-DD')` | 日期显示格式化 |
| format | string `('yyyy-MM-dd')` | 日期显示格式化 |
| hideHours | (hour:number, date:Date) => boolean | 隐藏小时 |
| hideMinutes | (minute:number, date:Date) => boolean | 隐藏分钟 |
| hideSeconds | (second:number, date:Date) => boolean | 隐藏秒 |
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-picker/zh-CN/intl.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const DatePickerIntl = props => (
<div className="field only-date">
<DatePicker
format="YYYY-MM-DD HH:mm:ss"
format="yyyy-MM-dd HH:mm:ss"
locale={{
sunday: '',
monday: '',
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/components/date-picker/zh-CN/range.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
```js
const instance = (
<InputGroup style={{ width: 460 }}>
<DatePicker format="YYYY-MM-DD HH:mm:ss" block appearance="subtle" />
<DatePicker format="yyyy-MM-dd HH:mm:ss" block appearance="subtle" />
<InputGroup.Addon></InputGroup.Addon>
<DatePicker format="YYYY-MM-DD HH:mm:ss" block appearance="subtle" />
<DatePicker format="yyyy-MM-dd HH:mm:ss" block appearance="subtle" />
</InputGroup>
);

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-range-picker/en-US/intl.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const DateRangePickerIntl = props => (
<div className="field only-date">
<DateRangePicker
format="YYYY-MM-DD"
format="yyyy-MM-dd"
locale={{
sunday: 'Su',
monday: 'Mo',
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-range-picker/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import dateFns from 'date-fns';
import * as dateFns from 'date-fns';
import { DateRangePicker, Button, Divider } from 'rsuite';
import DefaultPage from '@/components/Page';

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-range-picker/zh-CN/intl.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const DateRangePickerIntl = props => (
<div className="field only-date">
<DateRangePicker
format="YYYY-MM-DD"
format="yyyy-MM-dd"
locale={{
sunday: '',
monday: '',
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/date-range-picker/zh-CN/value.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class DateRangePickerValue extends React.Component {
constructor(props) {
super(props);
this.state = {
value: [dateFns.parse('2017-02-01'), dateFns.parse('2017-05-20')]
value: [dateFns.parseISO('2017-02-01'), dateFns.parseISO('2017-05-20')]
};
}
render() {
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
MultiCascader
} from 'rsuite';
import Loadable from 'react-loadable';
import dateFns from 'date-fns';
import * as dateFns from 'date-fns';

import DefaultPage from '@/components/Page';
import { useState } from 'react';
Expand Down
21 changes: 3 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"dependencies": {
"@babel/runtime": "^7.8.4",
"classnames": ">=2.0.0",
"date-fns": "^1.30.1",
"date-fns": "^2.13.0",
"dom-lib": "^1.2.1",
"element-resize-event": "^3.0.3",
"lodash": "^4.17.11",
Expand Down Expand Up @@ -100,7 +100,6 @@
"babel-eslint": "^10.0.3",
"babel-loader": "^8.0.0",
"babel-plugin-add-module-exports": "^1.0.2",
"babel-plugin-date-fns": "^0.2.1",
"babel-plugin-istanbul": "^4.1.4",
"babel-plugin-lodash": "^3.3.4",
"babel-plugin-transform-dev": "^2.0.1",
Expand Down
15 changes: 7 additions & 8 deletions src/Calendar/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import TimeDropdown from './TimeDropdown';
import View from './View';
import Header from './Header';
import { getUnhandledProps, defaultProps, prefix, refType } from '../utils';
import { disabledTime, calendarOnlyProps } from '../utils/timeUtils';
import { shouldTime, shouldDate, shouldMonth } from '../utils/formatUtils';
import { addMonths } from 'date-fns';
import { disabledTime, calendarOnlyProps, addMonths } from '../utils/dateUtils';

import { tuple } from '../@types/utils';

Expand All @@ -34,7 +33,7 @@ export interface CalendarProps {
hideHours?: (hour: number, date: Date) => boolean;
hideMinutes?: (minute: number, date: Date) => boolean;
hideSeconds?: (second: number, date: Date) => boolean;
onMoveForword?: (nextPageDate: Date) => void;
onMoveForward?: (nextPageDate: Date) => void;
onMoveBackward?: (nextPageDate: Date) => void;
onSelect?: (date: Date, event: React.MouseEvent<HTMLDivElement>) => void;
onToggleMonthDropdown?: (event: React.MouseEvent) => void;
Expand Down Expand Up @@ -66,7 +65,7 @@ class Calendar extends React.Component<CalendarProps> {
hideHours: PropTypes.func,
hideMinutes: PropTypes.func,
hideSeconds: PropTypes.func,
onMoveForword: PropTypes.func,
onMoveForward: PropTypes.func,
onMoveBackward: PropTypes.func,
onSelect: PropTypes.func,
onToggleMonthDropdown: PropTypes.func,
Expand All @@ -87,9 +86,9 @@ class Calendar extends React.Component<CalendarProps> {

disabledTime = (date: Date) => disabledTime(this.props, date);

handleMoveForword = () => {
const { onMoveForword, pageDate } = this.props;
onMoveForword?.(addMonths(pageDate, 1));
handleMoveForward = () => {
const { onMoveForward, pageDate } = this.props;
onMoveForward?.(addMonths(pageDate, 1));
};

handleMoveBackward = () => {
Expand Down Expand Up @@ -149,7 +148,7 @@ class Calendar extends React.Component<CalendarProps> {
showMeridian={showMeridian}
disabledDate={this.disabledDate}
disabledTime={this.disabledTime}
onMoveForword={this.handleMoveForword}
onMoveForward={this.handleMoveForward}
onMoveBackward={this.handleMoveBackward}
onToggleMonthDropdown={onToggleMonthDropdown}
onToggleTimeDropdown={onToggleTimeDropdown}
Expand Down
6 changes: 3 additions & 3 deletions src/Calendar/CalendarPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ class CalendarPanel extends React.PureComponent<CalendarPanelProps, State> {
className={classes}
isoWeek={isoWeek}
onSelect={this.handleSelect}
format="YYYY-MM-DD"
format="yyyy-MM-dd"
calendarState={showMonth ? 'DROP_MONTH' : null}
pageDate={value}
renderTitle={date => (
<FormattedDate date={date} formatStr={locale.formattedMonthPattern || 'MMMM YYYY'} />
<FormattedDate date={date} formatStr={locale.formattedMonthPattern || 'MMMM yyyy'} />
)}
renderToolbar={this.renderToolbar}
renderCell={renderCell}
onMoveForword={this.handleNextMonth}
onMoveForward={this.handleNextMonth}
onMoveBackward={this.handlePrevMonth}
onToggleMonthDropdown={this.handleToggleMonthDropdown}
onChangePageDate={this.handleChangePageDate}
Expand Down
Loading

0 comments on commit cb7f227

Please sign in to comment.