Skip to content

refactor(DatePicker2): convert to TypeScript, impove docs and tests, close #4579 #5024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/date-picker2/__docs__/demo/basic/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import ReactDOM from 'react-dom';

import { type Dayjs } from 'dayjs';
import { DatePicker2 } from '@alifd/next';

const { MonthPicker, YearPicker, WeekPicker, QuarterPicker } = DatePicker2;

const onChange = (date, dateStr) => console.log(date, dateStr);
const onChange = (date: Dayjs, dateStr: string) => console.log(date, dateStr);

function App() {
return (
Expand Down
6 changes: 3 additions & 3 deletions components/date-picker2/__docs__/demo/default-panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React from 'react';
import ReactDOM from 'react-dom';

import { DatePicker2 } from '@alifd/next';
import dayjs from 'dayjs';
import dayjs, { type Dayjs } from 'dayjs';

const { RangePicker } = DatePicker2;

const onChange = (date, dateStr) => console.log(date, dateStr);
const onPanelChange = (value, mode) => console.log(value.format('YYYY-MM-DD'), mode);
const onChange = (date: Dayjs | Dayjs[], dateStr: string | string[]) => console.log(date, dateStr);
const onPanelChange = (value: Dayjs, mode: string) => console.log(value.format('YYYY-MM-DD'), mode);

function App() {
return (
Expand Down
4 changes: 2 additions & 2 deletions components/date-picker2/__docs__/demo/disabledDate/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { DatePicker2 } from '@alifd/next';
import dayjs from 'dayjs';
import dayjs, { type Dayjs } from 'dayjs';

const { RangePicker, MonthPicker, YearPicker } = DatePicker2;
const currentDate = dayjs();

// Disable all dates before today
const disabledDate = function (date, mode) {
const disabledDate = function (date: Dayjs, mode: string) {
switch (mode) {
case 'date':
return date.valueOf() <= currentDate.valueOf();
Expand Down
3 changes: 2 additions & 1 deletion components/date-picker2/__docs__/demo/format/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { type Dayjs } from 'dayjs';

import { DatePicker2 } from '@alifd/next';

const { RangePicker } = DatePicker2;

const defaultVal = '2020-02-02';
const defaultRangeVal = ['2020-01-01', '2020-02-02'];
function customizeFormatter(v) {
function customizeFormatter(v: Dayjs) {
return `DATE: ${v.format('YYYY/MM/DD')}`;
}

Expand Down
4 changes: 2 additions & 2 deletions components/date-picker2/__docs__/demo/preset/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { DatePicker2 } from '@alifd/next';
import dayjs from 'dayjs';

const { RangePicker } = DatePicker2;
const RangePreset = {
const RangePreset: object = {
今天: [dayjs(), dayjs()],
本月: [dayjs().startOf('month'), dayjs().endOf('month')],
};
const datePreset = {
const datePreset: object = {
此刻: () => dayjs(),
};

Expand Down
3 changes: 2 additions & 1 deletion components/date-picker2/__docs__/demo/range-picker/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { type Dayjs } from 'dayjs';

import { DatePicker2 } from '@alifd/next';

const { RangePicker } = DatePicker2;

const onChange = (date, dateStr) => console.log(date, dateStr);
const onChange = (date: Dayjs | Dayjs[], dateStr: string | string[]) => console.log(date, dateStr);

const App = () => (
<div className="app">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import ReactDOM from 'react-dom';
import dayjs, { type Dayjs } from 'dayjs';

import React, { useState } from 'react';
import { DatePicker2 } from '@alifd/next';

const { RangePicker } = DatePicker2;

const App = () => {
const [dates, setDates] = useState(null);
const [value, setValue] = useState(null);
const disabledDate = current => {
const [dates, setDates] = useState([dayjs(), dayjs()]);
const [value, setValue] = useState(dayjs());
const disabledDate = (current: Dayjs) => {
if (!dates) {
return false;
}
Expand All @@ -22,7 +23,7 @@ const App = () => {
value={dates || value}
disabledDate={disabledDate}
onCalendarChange={val => {
setDates(val);
setDates(val!);
}}
onChange={val => {
setValue(val);
Expand Down
7 changes: 4 additions & 3 deletions components/date-picker2/__docs__/demo/show-time/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { DatePicker2 } from '@alifd/next';
import { type Dayjs } from 'dayjs';

const { RangePicker } = DatePicker2;
const onChange = value => console.log('onChange: ', value);
const onOk = value => console.log('onOK: ', value.format('YYYY-MM-DD HH:mm:ss'));
const onRangeOk = value => console.log('onOk: [%s, %s]', ...value);
const onChange = (value: Dayjs | Dayjs[]) => console.log('onChange: ', value);
const onOk = (value: Dayjs) => console.log('onOK: ', value.format('YYYY-MM-DD HH:mm:ss'));
const onRangeOk = (value: Dayjs[]) => console.log('onOk: [%s, %s]', ...value);

const defaultTimeValue = '09:00:00';

Expand Down
10 changes: 6 additions & 4 deletions components/date-picker2/__docs__/demo/size/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { DatePicker2, Radio } from '@alifd/next';
import { type Dayjs } from 'dayjs';

const { useState } = React;
const RadioGroup = Radio.Group;
Expand All @@ -23,23 +24,24 @@ const { RangePicker } = DatePicker2;

function Demo() {
const [size, setSize] = useState('medium');
const onChange = val => console.log(val);
const onChange = (val: Dayjs | Dayjs[]) => console.log(val);

return (
<div>
<RadioGroup
shape="button"
// @ts-expect-error RadioGroup上不存在属性 type
type="primary"
defaultValue={size}
dataSource={dataSource}
onChange={v => setSize(v)}
onChange={v => setSize(v as string)}
/>
<br />
<br />
<DatePicker2 size={size} onChange={onChange} />
<DatePicker2 size={size as 'large' | 'medium' | 'small'} onChange={onChange} />
<br />
<br />
<RangePicker size={size} onChange={onChange} />
<RangePicker size={size as 'large' | 'medium' | 'small'} onChange={onChange} />
<br />
<br />
</div>
Expand Down
Loading