Skip to content
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

refactor(DatePicker): convert to TypeScript, impove docs and tests, close #4578 #4932

Merged
merged 5 commits into from
Sep 23, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,77 @@ import React from 'react';
import { DatePicker } from '@alifd/next';
import { Types } from '@alifd/adaptor-helper';
import moment from 'moment';
import type { DatePickerProps } from '../../types';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是否应该从'@alifd/next/types/date-picker'中引?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只有 demo 要求必须从 @alifd/next/types 引,因为是给用户看的,其他的部分可以引相对路径。


export default {
name: 'DatePicker',
shape: ['normal', {
value: 'range',
label: 'Range Picker'
}],
editor: (shape) => {
shape: [
'normal',
{
value: 'range',
label: 'Range Picker',
},
],
editor: (shape: 'normal' | 'range') => {
return {
props: [{
name: 'state',
label: 'Status',
type: Types.enum,
options: ['normal', 'expanded', 'selected', 'disabled'],
default: 'normal'
}, {
name: 'size',
type: Types.enum,
options: ['large', 'medium', 'small'],
default: 'medium'
}, {
name: 'type',
type: Types.enum,
options: shape === 'range' ? ['dateRange', 'dateTimeRange'] : ['date', 'dateTime'],
default: shape === 'range' ? 'dateRange' : 'date'
}, {
name: 'width',
type: Types.number,
default: 288
}, {
name: 'label',
type: Types.string,
default: ''
}, {
name: 'placeholder',
type: Types.string,
default: shape === 'range' ? 'Start Date - End Date' : 'Please Select Date'
}]
props: [
{
name: 'state',
label: 'Status',
type: Types.enum,
options: ['normal', 'expanded', 'selected', 'disabled'],
default: 'normal',
},
{
name: 'size',
type: Types.enum,
options: ['large', 'medium', 'small'],
default: 'medium',
},
{
name: 'type',
type: Types.enum,
options:
shape === 'range' ? ['dateRange', 'dateTimeRange'] : ['date', 'dateTime'],
default: shape === 'range' ? 'dateRange' : 'date',
},
{
name: 'width',
type: Types.number,
default: 288,
},
{
name: 'label',
type: Types.string,
default: '',
},
{
name: 'placeholder',
type: Types.string,
default: shape === 'range' ? 'Start Date - End Date' : 'Please Select Date',
},
],
};
},
adaptor: ({ shape, state, size, type, width, label, placeholder = '', style = {}, ...others }) => {
adaptor: ({
shape,
state,
size,
type,
width,
label,
placeholder = '',
style = {},
...others
}: any) => {
const now = moment();
style = {
width,
...style,
};

if (shape === 'range') {
return (
return (
<DatePicker.RangePicker
{...others}
visible={state === 'expanded'}
Expand All @@ -65,7 +87,7 @@ export default {
showTime={'dateTimeRange' === type}
locale={{
startPlaceholder: placeholder.split('-')[0],
endPlaceholder: placeholder.split('-')[1]
endPlaceholder: placeholder.split('-')[1],
}}
label={label}
value={state !== 'normal' ? [now.clone(), now.clone().add(1, 'month')] : null}
Expand All @@ -92,31 +114,34 @@ export default {
/>
);
},
content: (shape) => ({
options: [{
name: 'type',
options: shape === 'range' ? ['dateRange', 'dateTimeRange'] : ['date', 'dateTime'],
default: shape === 'range' ? 'dateRange' : 'date'
}, {
name: 'label',
options: ['show', 'hide'],
default: 'hide'
}],
transform: (props, { type , label }) => {
content: (shape: 'normal' | 'range') => ({
options: [
{
name: 'type',
options: shape === 'range' ? ['dateRange', 'dateTimeRange'] : ['date', 'dateTime'],
default: shape === 'range' ? 'dateRange' : 'date',
},
{
name: 'label',
options: ['show', 'hide'],
default: 'hide',
},
],
transform: (props: DatePickerProps, { type, label }: { type: string; label: string }) => {
return {
...props,
type,
label: label === 'show' ? 'Label' : ''
label: label === 'show' ? 'Label' : '',
};
}
},
}),
demoOptions: (demo) => {
demoOptions: (demo: any) => {
const { node } = demo;
const { props = {} } = node;
if (props.state === 'expanded') {
return { ...demo, height: 300 };
}

return demo;
}
},
};
4 changes: 2 additions & 2 deletions components/date-picker/__docs__/demo/accessibility/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { DatePicker } from '@alifd/next';

const { RangePicker, MonthPicker, YearPicker } = DatePicker;
const onChange = val => console.log(val);
const { RangePicker } = DatePicker;
const onChange = (val: unknown) => console.log(val);

ReactDOM.render(
<div>
Expand Down
2 changes: 1 addition & 1 deletion components/date-picker/__docs__/demo/basic/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ReactDOM from 'react-dom';
import { DatePicker } from '@alifd/next';

const { RangePicker, MonthPicker, YearPicker, WeekPicker } = DatePicker;
const onChange = val => console.log(val);
const onChange = (val: unknown) => console.log(val);

ReactDOM.render(
<div>
Expand Down
33 changes: 18 additions & 15 deletions components/date-picker/__docs__/demo/custom-range-picker/index.tsx
Original file line number Diff line number Diff line change
@@ -1,54 +1,57 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { DatePicker } from '@alifd/next';
import { type Moment } from 'moment';
import type { DatePickerProps } from '@alifd/next/types/date-picker';

class CustomRangePicker extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
startValue: null,
endValue: null,
endOpen: false,
};
}
state: {
startValue: Moment | null;
endValue: Moment | null;
endOpen: boolean;
} = {
startValue: null,
endValue: null,
endOpen: false,
};

disabledStartDate = startValue => {
disabledStartDate: DatePickerProps['disabledDate'] = startValue => {
const { endValue } = this.state;
if (!startValue || !endValue) {
return false;
}
return startValue.valueOf() > endValue.valueOf();
};

disabledEndDate = endValue => {
disabledEndDate: DatePickerProps['disabledDate'] = endValue => {
const { startValue } = this.state;
if (!endValue || !startValue) {
return false;
}
return endValue.valueOf() <= startValue.valueOf();
};

onChange = (stateName, value) => {
onChange = (stateName: 'startValue' | 'endValue', value: string | Moment | null) => {
this.setState({
[stateName]: value,
});
};

onStartChange = value => {
onStartChange: DatePickerProps['onChange'] = value => {
this.onChange('startValue', value);
};

onEndChange = value => {
onEndChange: DatePickerProps['onChange'] = value => {
this.onChange('endValue', value);
};

handleStartOpenChange = open => {
handleStartOpenChange: DatePickerProps['onVisibleChange'] = open => {
if (!open) {
this.setState({ endOpen: true });
}
};

handleEndOpenChange = open => {
handleEndOpenChange: DatePickerProps['onVisibleChange'] = open => {
this.setState({ endOpen: open });
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { DatePicker } from '@alifd/next';
import moment from 'moment';
import moment, { type Moment } from 'moment';

const { RangePicker, MonthPicker } = DatePicker;

function onVisibleMonthChange(val, reason) {
function onVisibleMonthChange(val: Moment, reason: string) {
console.log(val.format('L'), reason);
}

Expand Down
2 changes: 1 addition & 1 deletion components/date-picker/__docs__/demo/default/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { RangePicker, MonthPicker, YearPicker } = DatePicker;
const startValue = moment('2017-11-20', 'YYYY-MM-DD', true);
const endValue = moment('2017-12-15', 'YYYY-MM-DD', true);
const timeStamp = moment(1581938105000);
const onChange = val => console.log(val);
const onChange = (val: unknown) => console.log(val);

ReactDOM.render(
<div>
Expand Down
3 changes: 2 additions & 1 deletion components/date-picker/__docs__/demo/disabled-date/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { DatePicker } from '@alifd/next';
import moment from 'moment';
import type { DatePickerProps } from '@alifd/next/types/date-picker';

const { RangePicker, MonthPicker, YearPicker } = DatePicker;
const currentDate = moment();

// Disable all dates before today
const disabledDate = function (date, view) {
const disabledDate: DatePickerProps['disabledDate'] = function (date, view) {
switch (view) {
case 'date':
return date.valueOf() <= currentDate.valueOf();
Expand Down
40 changes: 23 additions & 17 deletions components/date-picker/__docs__/demo/field/index.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { DatePicker, Field, Button } from '@alifd/next';
import { type Moment } from 'moment';

const { RangePicker, YearPicker, MonthPicker } = DatePicker;

class App extends React.Component {
field = new Field(this);

printData = () => {
this.field.validate((err, values) => {
if (err) {
console.error('Error: ', err);
return;
}
this.field.validate(
(
err,
values: { date: Moment; month: Moment; year: Moment; range: [Moment, Moment] }
) => {
if (err) {
console.error('Error: ', err);
return;
}

console.log('datepicker: %s', values.date.format('YYYY-MM-DD'));
console.log('monthpicker: %s', values.month.format('YYYY-MM'));
console.log('yearpicker: %s', values.year.format('YYYY'));
const range = values.range;
console.log(
'rangepicker: [%s, %s]',
range[0] && range[0].format('YYYY-MM-DD'),
range[1] && range[1].format('YYYY-MM-DD')
);
});
console.log('datepicker: %s', values.date.format('YYYY-MM-DD'));
console.log('monthpicker: %s', values.month.format('YYYY-MM'));
console.log('yearpicker: %s', values.year.format('YYYY'));
const range = values.range;
console.log(
'rangepicker: [%s, %s]',
range[0] && range[0].format('YYYY-MM-DD'),
range[1] && range[1].format('YYYY-MM-DD')
);
}
);
};

printError = name => {
printError = (name: string) => {
if (this.field.getError(name)) {
return <span className="error-msg">{this.field.getError(name).join(',')}</span>;
return <span className="error-msg">{this.field.getError(name)!.join(',')}</span>;
}
};

Expand Down
2 changes: 1 addition & 1 deletion components/date-picker/__docs__/demo/format/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ReactDOM from 'react-dom';
import { DatePicker } from '@alifd/next';

const { RangePicker } = DatePicker;
const onChange = val => console.log(val);
const onChange = (val: unknown) => console.log(val);

ReactDOM.render(
<div>
Expand Down
Loading
Loading