Skip to content

Commit

Permalink
fix(static-date-picker): fix some bug (#1898)
Browse files Browse the repository at this point in the history
1. 解决了 #1863 引入的 `disabledDate` 判断错误,导致将月份和年份也错误 disabled 的问题;
2. 解决了过去动态时段-回显数据时,时间选择器面板未定位到已选定日期的问题;

fix #1888

Co-authored-by: maxin <maxin@growingio.com>
  • Loading branch information
nnmax and maxin committed Mar 10, 2022
1 parent ae3f606 commit f053caf
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
55 changes: 42 additions & 13 deletions src/static-date-picker/DatePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
import React, { useLayoutEffect, useRef } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { useControlledState, useLocale, usePrefixCls } from '@gio-design/utils';
import { LeftDoubleOutlined, LeftOutlined, RightOutlined, RightDoubleOutlined } from '@gio-design/icons';
import PickerPanel from 'rc-picker/lib/PickerPanel';
import generateDateFns from 'rc-picker/lib/generate/dateFns';
import { Locale } from 'rc-picker/lib/interface';
import { Locale, PickerMode } from 'rc-picker/lib/interface';
import { isEqual } from 'date-fns';
import defaultLocale from './locales/zh-CN';
import { StaticDatePickerProps } from './interfaces';

const OmittedCell: React.FC = () => {
const spanRef = useRef<HTMLSpanElement>(null);
useLayoutEffect(() => {
// Remove title prop
if (spanRef.current) {
const parent = spanRef.current.parentElement;
if (parent?.hasAttribute('title')) {
parent.removeAttribute('title');
}
useEffect(() => {
const parent = spanRef.current?.parentElement;

const observer = new MutationObserver((mutationList) => {
mutationList.forEach((mutation) => {
if (
// prettier-disabled
mutation.type === 'attributes' &&
mutation.attributeName &&
parent?.hasAttribute(mutation.attributeName)
) {
parent?.removeAttribute(mutation.attributeName);
}
});
});

if (parent) {
const attributes = ['title', 'class'];
// 移除已经初始化的 `title` 和 `class` 属性
attributes.forEach((attribute) => parent.removeAttribute(attribute));
// 监听后续的 `title` 和 `class` 属性变化,然后移除
observer.observe(parent, {
attributeFilter: attributes,
attributes: true,
childList: true,
});
}
return () => observer.disconnect();
}, []);

return <span ref={spanRef} />;
};

Expand All @@ -27,11 +48,14 @@ const DatePicker: React.FC<StaticDatePickerProps> = ({
disabledDate: disabledDateProp,
value,
defaultValue,
onPanelChange,
...restProps
}) => {
const locale = useLocale<Locale>('DatePicker') || defaultLocale;
const [viewDate, setViewDate] = useControlledState(viewDateProp, value ?? defaultValue ?? new Date());

const [mode, setMode] = useState<PickerMode>('date');

const prefixCls = usePrefixCls('picker');

const isSameYearAndDay = (currentDate: Date) =>
Expand All @@ -46,10 +70,11 @@ const DatePicker: React.FC<StaticDatePickerProps> = ({
}
return <OmittedCell />;
};

const disabledDate = (currentDate: Date) => {
if (!isSameYearAndDay(currentDate)) {
return true;
if (mode === 'date') {
if (!isSameYearAndDay(currentDate)) {
return true;
}
}
if (typeof disabledDateProp === 'function') {
return disabledDateProp(currentDate);
Expand All @@ -69,7 +94,11 @@ const DatePicker: React.FC<StaticDatePickerProps> = ({
onPickerValueChange={(date) => setViewDate(date)}
locale={locale}
prefixCls={prefixCls}
picker="date"
onPanelChange={(changedValue, changedMode: PickerMode) => {
setMode(changedMode);
onPanelChange?.(changedValue, changedMode);
}}
picker={mode}
generateConfig={generateDateFns}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down
6 changes: 3 additions & 3 deletions src/static-date-picker/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PanelMode, Locale } from 'rc-picker/lib/interface';
import { Locale, PickerMode } from 'rc-picker/lib/interface';
import { CommonProps } from '@gio-design/utils';

export type DatePickerLocale = Omit<Locale, 'locale'>;
Expand All @@ -23,9 +23,9 @@ export interface StaticDatePickerProps extends CommonProps {
* 日历面板切换的回调
*
* @param value - 当前日期 `Date`
* @param mode - 当前模式,目前只会是 `date` 模式
* @param mode - 当前模式
*/
onPanelChange?: (value: Date, mode: PanelMode) => void;
onPanelChange?: (value: Date, mode: PickerMode) => void;
/**
* 日期发生变化时的回调
*
Expand Down
7 changes: 6 additions & 1 deletion src/static-date-picker/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
cursor: pointer;
}

&-start,
&-end {
color: @gray-3;
}

&::before {
position: absolute;
right: 0;
Expand Down Expand Up @@ -179,7 +184,7 @@
background: @blue-3;
}

&-in-view&-disabled {
&-disabled {
color: @gray-3;
cursor: not-allowed;

Expand Down
11 changes: 2 additions & 9 deletions src/static-past-time-picker/RelativeRangeBody.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { startOfDay, startOfToday, startOfYesterday, subMonths } from 'date-fns';
import { startOfDay, startOfYesterday } from 'date-fns';
import DatePicker from '../static-date-picker';
import DateRangePicker from '../static-date-range-picker';
import { RelativeRangeBodyProps } from './interfaces';
Expand All @@ -11,14 +11,7 @@ function RelativeRangeBody({ dateRange, fixedMode, onRangeChange, disabledDate }
};
return <DatePicker disabledDate={disabledDate} value={dateRange[0]} onSelect={handleOnSelect} />;
}
return (
<DateRangePicker
defaultViewDates={[subMonths(startOfToday(), 1), startOfYesterday()]}
disabledDate={disabledDate}
onSelect={onRangeChange}
value={dateRange as [Date, Date]}
/>
);
return <DateRangePicker disabledDate={disabledDate} onSelect={onRangeChange} value={dateRange as [Date, Date]} />;
}

export default RelativeRangeBody;

1 comment on commit f053caf

@vercel
Copy link

@vercel vercel bot commented on f053caf Mar 10, 2022

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.