Skip to content

Commit

Permalink
chore: optimize experience of hover value (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
kerm1it authored Feb 25, 2021
1 parent 6af42f8 commit b2b8da3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
3 changes: 2 additions & 1 deletion examples/basic.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import moment, { Moment } from 'moment';
import type { Moment } from 'moment';
import moment from 'moment';
import Picker from '../src/Picker';
import momentGenerateConfig from '../src/generate/moment';
import zhCN from '../src/locale/zh_CN';
Expand Down
18 changes: 16 additions & 2 deletions src/PickerPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import type { MonthCellRender } from './panels/MonthPanel/MonthBody';
import RangeContext from './RangeContext';
import getExtraFooter from './utils/getExtraFooter';
import getRanges from './utils/getRanges';
import { getLowerBoundTime, setTime } from './utils/timeUtil';
import { getLowerBoundTime, setDateTime, setTime } from './utils/timeUtil';

export type PickerPanelSharedProps<DateType> = {
prefixCls?: string;
Expand Down Expand Up @@ -202,7 +202,21 @@ function PickerPanel<DateType>(props: PickerPanelProps<DateType>) {
const [viewDate, setInnerViewDate] = useMergedState<DateType | null, DateType>(null, {
value: pickerValue,
defaultValue: defaultPickerValue || mergedValue,
postState: (date) => date || generateConfig.getNow(),
postState: (date) => {
const now = generateConfig.getNow();
if (!date) return now;
// When value is null and set showTime
if (!mergedValue && showTime) {
if (typeof showTime === 'object') {
return setDateTime(generateConfig, date, showTime.defaultValue || now);
}
if (defaultValue) {
return setDateTime(generateConfig, date, defaultValue);
}
return setDateTime(generateConfig, date, now);
}
return date;
},
});

const setViewDate = (date: DateType) => {
Expand Down
29 changes: 2 additions & 27 deletions src/panels/DatetimePanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,8 @@ import DatePanel from '../DatePanel';
import type { SharedTimeProps } from '../TimePanel';
import TimePanel from '../TimePanel';
import { tuple } from '../../utils/miscUtil';
import type { PanelRefProps, DisabledTime, NullableDateType } from '../../interface';
import type { GenerateConfig } from '../../generate';

function setTime<DateType>(
generateConfig: GenerateConfig<DateType>,
date: DateType,
defaultDate: NullableDateType<DateType>,
) {
if (!defaultDate) {
return date;
}

let newDate = date;
newDate = generateConfig.setHour(
newDate,
generateConfig.getHour(defaultDate),
);
newDate = generateConfig.setMinute(
newDate,
generateConfig.getMinute(defaultDate),
);
newDate = generateConfig.setSecond(
newDate,
generateConfig.getSecond(defaultDate),
);
return newDate;
}
import { setDateTime as setTime } from '../../utils/timeUtil';
import type { PanelRefProps, DisabledTime } from '../../interface';

export type DatetimePanelProps<DateType> = {
disabledTime?: DisabledTime<DateType>;
Expand Down
27 changes: 27 additions & 0 deletions src/utils/timeUtil.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { NullableDateType } from '../interface';
import type { GenerateConfig } from '../generate';

export function setTime<DateType>(
Expand All @@ -13,6 +14,32 @@ export function setTime<DateType>(
return nextTime;
}

export function setDateTime<DateType>(
generateConfig: GenerateConfig<DateType>,
date: DateType,
defaultDate: NullableDateType<DateType>,
) {
if (!defaultDate) {
return date;
}

let newDate = date;
newDate = generateConfig.setHour(
newDate,
generateConfig.getHour(defaultDate),
);
newDate = generateConfig.setMinute(
newDate,
generateConfig.getMinute(defaultDate),
);
newDate = generateConfig.setSecond(
newDate,
generateConfig.getSecond(defaultDate),
);
return newDate;
}


export function getLowerBoundTime(
hour: number,
minute: number,
Expand Down

1 comment on commit b2b8da3

@vercel
Copy link

@vercel vercel bot commented on b2b8da3 Feb 25, 2021

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.