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

chore: optimize experience of hover value #220

Merged
merged 2 commits into from
Feb 25, 2021
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
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