Skip to content

Commit

Permalink
fix(date-picker): date panel is not destroyed when it is hidden (#1864)
Browse files Browse the repository at this point in the history
Co-authored-by: maxin <maxin@growingio.com>
  • Loading branch information
nnmax and maxin committed Feb 25, 2022
1 parent db27fa7 commit 9bfcf4f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/date-picker/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const DatePicker: React.FC<DatePickerProps> = (props: DatePickerProps) =>
onSelect?.(currentValue, formatDate(currentValue));
};

const content = <StaticDatePicker onSelect={handleOnSelect} disabledDate={disabledDate} />;
const content = <StaticDatePicker onSelect={handleOnSelect} disabledDate={disabledDate} value={controlledValue} />;

function renderTrigger() {
if (trigger) {
Expand Down
6 changes: 5 additions & 1 deletion src/static-date-picker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ const OmittedCell: React.FC = () => {
const DatePicker: React.FC<StaticDatePickerProps> = ({
viewDate: viewDateProp,
disabledDate: disabledDateProp,
value,
defaultValue,
...restProps
}) => {
const locale = useLocale<Locale>('DatePicker') || defaultLocale;
const [viewDate, setViewDate] = useControlledState(viewDateProp, new Date());
const [viewDate, setViewDate] = useControlledState(viewDateProp, value ?? defaultValue ?? new Date());

const prefixCls = usePrefixCls('picker');

Expand Down Expand Up @@ -60,6 +62,8 @@ const DatePicker: React.FC<StaticDatePickerProps> = ({
data-testid="static-date-picker"
dateRender={omitOtherDate}
disabledDate={disabledDate}
value={value}
defaultValue={defaultValue}
{...restProps}
pickerValue={viewDate}
onPickerValueChange={(date) => setViewDate(date)}
Expand Down
5 changes: 4 additions & 1 deletion src/static-date-range-picker/StaticDateRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ function StaticDateRangePicker({
locale,
...rest
}: StaticDateRangePickerProps) {
const [viewDates, setViewDates] = React.useState<[Date, Date]>(defaultViewDates ?? getDefaultViewDates());
const [hoveredDates, setHoveredDates] = React.useState<RangeValue<Date>>();
const [dateIndex, setDateIndex] = React.useState<number>(0);
// @ts-ignore
const [selectedValue, setSelectedValue] = useControlledState<RangeValue<Date>>(value, defaultValue);

const [viewDates, setViewDates] = React.useState<[Date, Date]>(
defaultViewDates ?? getDefaultViewDates(selectedValue?.[0] ?? new Date())
);
const prefixCls = usePrefixCls('date-range-picker');

function renderPicker(position: 'left' | 'right') {
Expand Down
6 changes: 3 additions & 3 deletions src/static-date-range-picker/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { RangeValue } from 'rc-picker/lib/interface';
import generateDateFns from 'rc-picker/lib/generate/dateFns';
import { getClosingViewDate } from 'rc-picker/lib/utils/dateUtil';
import { add, startOfToday } from 'date-fns';
import { add, startOfDay } from 'date-fns';

export const getDefaultViewDates = () => [startOfToday(), add(startOfToday(), { months: 1 })] as [Date, Date];
export const getDefaultViewDates = (defaultDate: Date = new Date()) =>
[startOfDay(defaultDate), add(startOfDay(defaultDate), { months: 1 })] as [Date, Date];

export const calcClosingViewDate = (currentDate: Date, offset?: number) =>
getClosingViewDate(currentDate, 'date', generateDateFns, offset);
Expand Down

1 comment on commit 9bfcf4f

@vercel
Copy link

@vercel vercel bot commented on 9bfcf4f Feb 25, 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.