Skip to content

Commit

Permalink
chore: optimize logic of viewDate
Browse files Browse the repository at this point in the history
  • Loading branch information
kerm1it committed Feb 25, 2021
1 parent 3a86612 commit 03361d0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
1 change: 0 additions & 1 deletion examples/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export default () => {
<h3>Datetime</h3>
<Picker<Moment>
{...sharedProps}
value={undefined}
locale={zhCN}
defaultPickerValue={defaultValue.clone().subtract(1, 'month')}
showTime={{
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
7 changes: 1 addition & 6 deletions src/hooks/useHoverValue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState, useEffect, useRef } from 'react';
import { setDateTime } from '../utils/timeUtil';
import type { ValueTextConfig } from './useValueTexts';
import useValueTexts from './useValueTexts';

Expand Down Expand Up @@ -28,11 +27,7 @@ export default function useHoverValue<DateType>(
});

function onEnter(date: DateType) {
let hoverDate = date;
if (!valueText) {
hoverDate = setDateTime(generateConfig, date, generateConfig.getNow())
}
setValue(hoverDate);
setValue(date);
}

function onLeave(immediately: boolean = false) {
Expand Down

0 comments on commit 03361d0

Please sign in to comment.