Skip to content

Commit

Permalink
refactor(DatePicker): fix comment issues
Browse files Browse the repository at this point in the history
  • Loading branch information
eternalsky committed Oct 21, 2024
1 parent b86d0fe commit 4c37bf5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
23 changes: 8 additions & 15 deletions components/date-picker/__tests__/index-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ describe('DatePicker', () => {

it('should hide seconds', () => {
cy.mount(<DatePicker defaultVisible showTime={{ format: 'HH:mm' }} />);
// assert(!wrapper.find('.next-time-picker-menu-hour').length);
cy.get('.next-time-picker-menu-hour').should('not.exist');
});

Expand Down Expand Up @@ -143,7 +142,9 @@ describe('DatePicker', () => {
cy.mount(
<DatePicker
onChange={val => {
handleChange((val as Moment).format('YYYY-MM-DD HH:mm:ss'));
handleChange(
moment.isMoment(val) ? val.format('YYYY-MM-DD HH:mm:ss') : null
);
}}
showTime
defaultVisible
Expand All @@ -152,8 +153,8 @@ describe('DatePicker', () => {
cy.get('.next-date-picker-panel-input input').eq(0).type('2017-11-11');
cy.get('.next-date-picker-panel-input input').eq(0).blur();
cy.wrap(handleChange).should('be.calledWith', '2017-11-11 00:00:00');
cy.get('.next-date-picker-panel-input input').eq(1).clear();
cy.get('.next-date-picker-panel-input input').eq(1).type('11:11:11');
cy.get('.next-date-picker-panel-input input').eq(1).focus();
cy.get('.next-date-picker-panel-input input').eq(1).triggerInputChange('11:11:11');
cy.get('.next-date-picker-panel-input input').eq(1).blur();
cy.wrap(handleChange).should('be.calledWith', '2017-11-11 11:11:11');
});
Expand Down Expand Up @@ -295,7 +296,7 @@ describe('DatePicker', () => {
showTime={{ format: 'HH:mm:ss' }}
defaultVisible
defaultValue={moment().hours(0).minutes(0).seconds(0)}
popupProps={{ animation: false }}
popupProps={{ animation: false, autoFocus: false }}
/>
);

Expand Down Expand Up @@ -326,10 +327,7 @@ describe('DatePicker', () => {
describe('with date string', () => {
it('should defaultValue as string', () => {
cy.mount(<DatePicker defaultValue="2018-01-23" />);

// assert(wrapper.find('.next-date-picker-input input').instance().value === '2018-01-23');
cy.get('.next-date-picker-input input').should('have.value', '2018-01-23');
// assert(wrapper.find('i.next-input-clear-icon').length === 1);
cy.get('i.next-input-clear-icon').should('exist');
});

Expand All @@ -341,7 +339,6 @@ describe('DatePicker', () => {
});

it('should set defaultValue as string for TimePicker', () => {
// const onChange = cy.spy();
const handleChange = cy.spy();
cy.mount(
<DatePicker
Expand Down Expand Up @@ -400,7 +397,6 @@ describe('YearPicker', () => {

it('should set hasClear to false', () => {
cy.mount(<YearPicker defaultValue={startYear} hasClear={false} />);
// assert(!wrapper.find('i.next-input-clear-icon').length);
cy.get('i.next-input-clear-icon').should('not.exist');
});

Expand Down Expand Up @@ -582,7 +578,6 @@ describe('MonthPicker', () => {

it('should set hasClear to false', () => {
cy.mount(<MonthPicker defaultValue={startMonth} hasClear={false} />);
// assert(!wrapper.find('i.next-input-clear-icon').length);
cy.get('i.next-input-clear-icon').should('not.exist');
});

Expand Down Expand Up @@ -672,7 +667,6 @@ describe('MonthPicker', () => {
});

it('should input disabled date in picker', () => {
// const onChange = cy.spy();
const onChange = cy.spy();
cy.mount(
<MonthPicker onChange={onChange} disabledDate={disabledDate} defaultVisible />
Expand Down Expand Up @@ -770,6 +764,7 @@ describe('WeekPicker', () => {
it('should render with defaultValue', () => {
cy.mount(<WeekPicker defaultValue={startWeek} />);
cy.get('.next-week-picker-input input').should('have.value', '2018-2nd');
cy.get('i.next-input-clear-icon').should('exist');
});

it('should set hasClear to false', () => {
Expand Down Expand Up @@ -809,7 +804,6 @@ describe('WeekPicker', () => {

describe('action', () => {
it('should select', () => {
// const onChange = cy.spy();
const onChange = cy.spy();
cy.mount(
<WeekPicker
Expand Down Expand Up @@ -1263,7 +1257,6 @@ describe('RangePicker', () => {

cy.get('.next-calendar-cell[title="2017-11-09"] .next-calendar-date').click();
cy.get('.next-calendar-cell[title="2017-12-09"] .next-calendar-date').eq(1).click();
// assert(ret[1].format('YYYY-MM-DD HH:mm:ss') === '2017-12-09 09:00:00');
cy.wrap(onChange).should('be.calledWith', [
'2017-11-09 09:00:00',
'2017-12-09 09:00:00',
Expand All @@ -1288,7 +1281,6 @@ describe('RangePicker', () => {
);
cy.get('.next-calendar-cell[title="2017-11-09"] .next-calendar-date').click();
cy.get('.next-calendar-cell[title="2017-12-09"] .next-calendar-date').eq(1).click();
// assert(ret[1].format('YYYY-MM-DD HH:mm:ss') === '2017-12-09 23:59:59');
});

it('should resetTime', () => {
Expand Down Expand Up @@ -1551,6 +1543,7 @@ describe('RangePicker', () => {
]}
popupProps={{
animation: false,
autoFocus: false,
}}
/>
);
Expand Down
4 changes: 2 additions & 2 deletions components/date-picker/date-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class DatePicker extends Component<DatePickerProps, DatePickerState> {
super(props);
const { format, timeFormat, dateTimeFormat } = getDateTimeFormat(
props.format,
props.showTime!
props.showTime
);

this.state = {
Expand Down Expand Up @@ -352,7 +352,7 @@ class DatePicker extends Component<DatePickerProps, DatePickerState> {
});
};

onOk = (value: undefined) => {
onOk = (value?: Moment | null) => {
this.onVisibleChange(false, 'okBtnClick');
this.onValueChange(value || this.state.value, 'onOk');
};
Expand Down
4 changes: 3 additions & 1 deletion components/date-picker/module/panel-footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { func } from '../../util';
import { PANEL } from '../util';
import type { PanelFooterProps } from '../types';

class PanelFooter extends React.PureComponent<PanelFooterProps> {
class PanelFooter<isRange extends boolean = false> extends React.PureComponent<
isRange extends true ? PanelFooterProps : Omit<PanelFooterProps, 'onOk'> & { onOk: () => void }
> {
static displayName = 'PanelFooter';
static defaultProps = {
onOk: func.noop,
Expand Down
14 changes: 7 additions & 7 deletions components/date-picker/range-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function mapTimeToValue(name: string) {
)[name];
}

function getFormatValues(values: RangePickerProps['value'] | null | undefined, format?: string) {
function getFormatValues(values: RangePickerProps['value'] | null, format?: string) {
if (!Array.isArray(values)) {
return [null, null];
}
Expand Down Expand Up @@ -152,7 +152,7 @@ class RangePicker extends Component<RangePickerProps, RangePickerState> {
super(props);
const { format, timeFormat, dateTimeFormat } = getDateTimeFormat(
props.format,
props.showTime!,
props.showTime,
props.type
);

Expand Down Expand Up @@ -209,10 +209,10 @@ class RangePicker extends Component<RangePickerProps, RangePickerState> {
}

onValueChange = (
values: [start?: Moment | null, end?: Moment | null],
values: (Moment | undefined | null)[],
handler: 'onOk' | 'onChange' = 'onChange'
) => {
let ret: [start?: Moment | string | null, end?: Moment | string | null];
let ret: (Moment | string | undefined | null)[];
if (!values.length || !this.state.inputAsString) {
ret = values;
} else {
Expand Down Expand Up @@ -583,7 +583,7 @@ class RangePicker extends Component<RangePickerProps, RangePickerState> {
});
};

onOk = (value?: [start?: Moment | null, end?: Moment | null]) => {
onOk = (value?: (Moment | null | undefined)[]) => {
this.onVisibleChange(false, 'okBtnClick');
this.onValueChange(value || [this.state.startValue, this.state.endValue], 'onOk');
};
Expand Down Expand Up @@ -986,7 +986,7 @@ class RangePicker extends Component<RangePickerProps, RangePickerState> {
}

panelFooter = panelFooter || (
<PanelFooter
<PanelFooter<true>
prefix={prefix}
value={state.startValue || state.endValue}
ranges={Object.keys(ranges).map(key => ({
Expand All @@ -1008,7 +1008,7 @@ class RangePicker extends Component<RangePickerProps, RangePickerState> {
locale={locale}
panel={state.panel!}
onPanelChange={showTime ? this.changePanel : null}
onOk={this.onOk as PanelFooterProps['onOk']}
onOk={this.onOk}
/>
);

Expand Down

0 comments on commit 4c37bf5

Please sign in to comment.