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

refactor: multiple default use !needConfirm & fix min-max limitation check logic #747

Merged
merged 8 commits into from
Feb 1, 2024
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
8 changes: 8 additions & 0 deletions docs/demo/limitation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: limitation
nav:
title: Demo
path: /demo
---

<code src="../examples/limitation.tsx"></code>
8 changes: 8 additions & 0 deletions docs/demo/multiple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: multiple
nav:
title: Demo
path: /demo
---

<code src="../examples/multiple.tsx"></code>
52 changes: 52 additions & 0 deletions docs/examples/limitation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as React from 'react';
import '../../assets/index.less';
import SinglePicker from '../../src/PickerInput/SinglePicker';

import dayjs from 'dayjs';
import 'dayjs/locale/ar';
import 'dayjs/locale/zh-cn';
import LocalizedFormat from 'dayjs/plugin/localizedFormat';
import dayjsGenerateConfig from '../../src/generate/dayjs';
import zhCN from '../../src/locale/zh_CN';

dayjs.locale('zh-cn');
dayjs.extend(LocalizedFormat);

console.clear();

(window as any).dayjs = dayjs;

const sharedLocale = {
locale: zhCN,
generateConfig: dayjsGenerateConfig,
style: { width: 300 },
};

export default () => {
return (
<div>
<SinglePicker
{...sharedLocale}
// defaultPickerValue={dayjs('2019-09-15')}
defaultValue={dayjs('2020-09-03')}
minDate={dayjs('2019-08-01')}
maxDate={dayjs('2020-10-31')}
// open
/>
<SinglePicker
{...sharedLocale}
defaultValue={dayjs('2020-09-03')}
minDate={dayjs('2019-08-01')}
maxDate={dayjs('2020-10-31')}
picker="month"
/>
<SinglePicker
{...sharedLocale}
defaultValue={dayjs('2020-09-03')}
minDate={dayjs('2019-08-01')}
maxDate={dayjs('2020-10-31')}
picker="quarter"
/>
</div>
);
};
35 changes: 35 additions & 0 deletions docs/examples/multiple.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as React from 'react';
import '../../assets/index.less';
import type { PickerRef } from '../../src/interface';
import SinglePicker from '../../src/PickerInput/SinglePicker';

import dayjs from 'dayjs';
import 'dayjs/locale/ar';
import 'dayjs/locale/zh-cn';
import LocalizedFormat from 'dayjs/plugin/localizedFormat';
import dayjsGenerateConfig from '../../src/generate/dayjs';
import zhCN from '../../src/locale/zh_CN';

dayjs.locale('zh-cn');
dayjs.extend(LocalizedFormat);

console.clear();

(window as any).dayjs = dayjs;

const sharedLocale = {
locale: zhCN,
generateConfig: dayjsGenerateConfig,
style: { width: 300 },
};

export default () => {
const singleRef = React.useRef<PickerRef>(null);

return (
<div>
<SinglePicker {...sharedLocale} multiple ref={singleRef} onOpenChange={console.error} />
<SinglePicker {...sharedLocale} multiple ref={singleRef} needConfirm />
</div>
);
};
56 changes: 10 additions & 46 deletions src/PickerInput/Popup/PopupPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export type PopupPanelProps<DateType extends object = any> = MustProp<DateType>
Omit<PickerPanelProps<DateType>, 'onPickerValueChange' | 'showTime'> &
FooterProps<DateType> & {
multiplePanel?: boolean;
minDate?: DateType;
maxDate?: DateType;
range?: boolean;

onPickerValueChange: (date: DateType) => void;
Expand All @@ -23,17 +21,8 @@ export type PopupPanelProps<DateType extends object = any> = MustProp<DateType>
export default function PopupPanel<DateType extends object = any>(
props: PopupPanelProps<DateType>,
) {
const {
picker,
multiplePanel,
pickerValue,
onPickerValueChange,
onSubmit,
minDate,
maxDate,
range,
hoverValue,
} = props;
const { picker, multiplePanel, pickerValue, onPickerValueChange, onSubmit, range, hoverValue } =
props;
const { prefixCls, generateConfig } = React.useContext(PickerContext);

// ======================== Offset ========================
Expand Down Expand Up @@ -63,36 +52,6 @@ export default function PopupPanel<DateType extends object = any>(

const hideHeader = picker === 'time';

// ====================== Limitation ======================
const needLimit = React.useCallback(
(currentPickerValue: DateType) => {
let hidePrev = false;
let hideNext = false;

const dateBeforePickerValue = internalOffsetDate(currentPickerValue, -1);
if (minDate && generateConfig.isAfter(minDate, dateBeforePickerValue)) {
hidePrev = true;
}

const dateAfterPickerValue = internalOffsetDate(currentPickerValue, 1);
if (maxDate && generateConfig.isAfter(dateAfterPickerValue, maxDate)) {
hideNext = true;
}

return {
hidePrev,
hideNext,
};
},
[minDate, maxDate, internalOffsetDate, generateConfig],
);

const firstPanelNeedLimit = React.useMemo(() => needLimit(pickerValue), [pickerValue, needLimit]);
const secondPanelNeedLimit = React.useMemo(
() => needLimit(nextPickerValue),
[nextPickerValue, needLimit],
);

// ======================== Props =========================
const pickerProps = {
...props,
Expand All @@ -113,12 +72,18 @@ export default function PopupPanel<DateType extends object = any>(
return (
<div className={`${prefixCls}-panels`}>
<PickerHackContext.Provider
value={{ ...sharedContext, hideNext: true, hidePrev: firstPanelNeedLimit.hidePrev }}
value={{
...sharedContext,
hideNext: true,
}}
>
<PickerPanel<DateType> {...pickerProps} />
</PickerHackContext.Provider>
<PickerHackContext.Provider
value={{ ...sharedContext, hidePrev: true, hideNext: secondPanelNeedLimit.hideNext }}
value={{
...sharedContext,
hidePrev: true,
}}
>
<PickerPanel<DateType>
{...pickerProps}
Expand All @@ -135,7 +100,6 @@ export default function PopupPanel<DateType extends object = any>(
<PickerHackContext.Provider
value={{
...sharedContext,
...firstPanelNeedLimit,
}}
>
<PickerPanel {...pickerProps} />
Expand Down
5 changes: 3 additions & 2 deletions src/PickerInput/hooks/useFilledProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ export default function useFilledProps<
const internalPicker: InternalMode = picker === 'date' && showTime ? 'datetime' : picker;

/** The picker is `datetime` or `time` */
const complexPicker = internalPicker === 'time' || internalPicker === 'datetime' || multiple;
const mergedNeedConfirm = needConfirm ?? complexPicker;
const multipleInteractivePicker = internalPicker === 'time' || internalPicker === 'datetime';
const complexPicker = multipleInteractivePicker || multiple;
const mergedNeedConfirm = needConfirm ?? multipleInteractivePicker;

// ========================== Time ==========================
// Auto `format` need to check `showTime.showXXX` first.
Expand Down
2 changes: 1 addition & 1 deletion src/PickerInput/hooks/useRangeValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function useInnerValue<ValueType extends DateType[], DateType extends obj
clone[i] = clone[i] || null;
}
} else if (order) {
clone = orderDates(clone, generateConfig);
clone = orderDates(clone.filter((date) => date) as ValueType, generateConfig);
}

// Update merged value
Expand Down
19 changes: 12 additions & 7 deletions src/PickerPanel/DatePanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export default function DatePanel<DateType extends object = any>(props: DatePane
// ========================== Base ==========================
const [info, now] = useInfo(props, mode);
const weekFirstDay = generateConfig.locale.getWeekFirstDay(locale.locale);
const baseDate = getWeekStartDate(locale.locale, generateConfig, pickerValue);
const monthStartDate = generateConfig.setDate(pickerValue, 1);
const baseDate = getWeekStartDate(locale.locale, generateConfig, monthStartDate);
const month = generateConfig.getMonth(pickerValue);

// =========================== PrefixColumn ===========================
Expand Down Expand Up @@ -176,12 +177,16 @@ export default function DatePanel<DateType extends object = any>(props: DatePane
<PanelContext.Provider value={info}>
<div className={classNames(panelPrefixCls, showWeek && `${panelPrefixCls}-show-week`)}>
{/* Header */}
<PanelHeader
onOffset={(offset) => {
onPickerValueChange(generateConfig.addMonth(pickerValue, offset));
}}
onSuperOffset={(offset) => {
onPickerValueChange(generateConfig.addYear(pickerValue, offset));
<PanelHeader<DateType>
offset={(distance) => generateConfig.addMonth(pickerValue, distance)}
superOffset={(distance) => generateConfig.addYear(pickerValue, distance)}
onChange={onPickerValueChange}
// Limitation
getStart={(date) => generateConfig.setDate(date, 1)}
getEnd={(date) => {
let clone = generateConfig.setDate(date, 1);
clone = generateConfig.addMonth(clone, 1);
return generateConfig.addDate(clone, -1);
}}
>
{monthYearNodes}
Expand Down
33 changes: 22 additions & 11 deletions src/PickerPanel/DecadePanel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import type { DisabledDate, SharedPanelProps } from '../../interface';
import { formatValue } from '../../utils/dateUtil';
import { formatValue, isInRange, isSameDecade } from '../../utils/dateUtil';
import { PanelContext, useInfo } from '../context';
import PanelBody from '../PanelBody';
import PanelHeader from '../PanelHeader';
Expand All @@ -15,13 +15,20 @@ export default function DecadePanel<DateType extends object = any>(

// ========================== Base ==========================
const [info] = useInfo(props, 'decade');
const startYear = Math.floor(generateConfig.getYear(pickerValue) / 100) * 100;
const endYear = startYear + 99;

const baseDate = generateConfig.setYear(pickerValue, startYear - 10);
const getStartYear = (date: DateType) => {
const startYear = Math.floor(generateConfig.getYear(pickerValue) / 100) * 100;
return generateConfig.setYear(date, startYear);
};
const getEndYear = (date: DateType) => {
const startYear = getStartYear(date);
return generateConfig.addYear(startYear, 99);
};

const startYearDate = getStartYear(pickerValue);
const endYearDate = getEndYear(pickerValue);

const startYearDate = generateConfig.setYear(baseDate, startYear);
const endYearDate = generateConfig.setYear(startYearDate, endYear);
const baseDate = generateConfig.addYear(startYearDate, -10);

// ========================= Cells ==========================
const getCellDate = (date: DateType, offset: number) => {
Expand All @@ -46,9 +53,11 @@ export default function DecadePanel<DateType extends object = any>(
};

const getCellClassName = (date: DateType) => {
const dateYear = generateConfig.getYear(date);
return {
[`${prefixCls}-cell-in-view`]: startYear <= dateYear && dateYear <= endYear,
[`${prefixCls}-cell-in-view`]:
isSameDecade(generateConfig, date, startYearDate) ||
isSameDecade(generateConfig, date, endYearDate) ||
isInRange(generateConfig, startYearDate, endYearDate, date),
};
};

Expand Down Expand Up @@ -88,9 +97,11 @@ export default function DecadePanel<DateType extends object = any>(
<div className={panelPrefixCls}>
{/* Header */}
<PanelHeader
onSuperOffset={(offset) => {
onPickerValueChange(generateConfig.addYear(pickerValue, offset * 100));
}}
superOffset={(distance) => generateConfig.addYear(pickerValue, distance * 100)}
onChange={onPickerValueChange}
// Limitation
getStart={getStartYear}
getEnd={getEndYear}
>
{yearNode}
</PanelHeader>
Expand Down
8 changes: 5 additions & 3 deletions src/PickerPanel/MonthPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@
<div className={panelPrefixCls}>
{/* Header */}
<PanelHeader
onSuperOffset={(offset) => {
onPickerValueChange(generateConfig.addYear(pickerValue, offset));
}}
superOffset={(distance) => generateConfig.addYear(pickerValue, distance)}
onChange={onPickerValueChange}
// Limitation
getStart={(date) => generateConfig.setMonth(date, 0)}
getEnd={(date) => generateConfig.setMonth(date, 11)}

Check warning on line 98 in src/PickerPanel/MonthPanel/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/PickerPanel/MonthPanel/index.tsx#L97-L98

Added lines #L97 - L98 were not covered by tests
>
{yearNode}
</PanelHeader>
Expand Down
Loading
Loading