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

fix: 修复 datepicker 禁用样式问题 #1861

Merged
merged 2 commits into from
Jan 5, 2023
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
55 changes: 2 additions & 53 deletions src/date-picker/hooks/useDisableDate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dayjs from 'dayjs';
import isObject from 'lodash/isObject';
import type { TdDatePickerProps, TdDateRangePickerProps } from '../type';
import { isEnabledDate } from '../../_common/js/date-picker/utils';

export interface disableDateProps {
disableDate?: TdDatePickerProps['disableDate'] | TdDateRangePickerProps['disableDate'];
Expand All @@ -14,59 +14,8 @@ export default function useDisableDate(props: disableDateProps) {
const { disableDate, format, mode, start, end } = props;

return {
disableDate: (value: Date) => !isEnabled({ disableDate, format, mode, value }),
disableDate: (value: Date) => !isEnabledDate({ disableDate, format, mode, value }),
minDate: isObject(disableDate) && 'before' in disableDate ? new Date(disableDate.before) : start,
maxDate: isObject(disableDate) && 'after' in disableDate ? new Date(disableDate.after) : end,
};
}

function isEnabled(props): boolean {
const { disableDate, value, format, mode } = props;

if (!disableDate) return true;

let isEnabled = true;
// 值类型为 Function 则表示返回值为 true 的日期会被禁用
if (typeof disableDate === 'function') {
return !disableDate(value);
}

// 禁用日期,示例:['A', 'B'] 表示日期 A 和日期 B 会被禁用。
if (Array.isArray(disableDate)) {
let isIncludes = false;
const formatedDisabledDate = disableDate.map((item: string) => dayjs(item, format));
formatedDisabledDate.forEach((item) => {
if (item.isSame(dayjs(value))) {
isIncludes = true;
}
});
return !isIncludes;
}

// { from: 'A', to: 'B' } 表示在 A 到 B 之间的日期会被禁用。
const { from, to, before, after } = disableDate;
if (from && to) {
const compareMin = dayjs(new Date(from));
const compareMax = dayjs(new Date(to));

return !dayjs(value).isBetween(compareMin, compareMax, mode, '[]');
}

const min = before ? new Date(before) : null;
const max = after ? new Date(after) : null;

// { before: 'A', after: 'B' } 表示在 A 之前和在 B 之后的日期都会被禁用。
if (max && min) {
const compareMin = dayjs(new Date(min));
const compareMax = dayjs(new Date(max));

isEnabled = dayjs(value).isBetween(compareMin, compareMax, mode, '[]');
} else if (min) {
const compareMin = dayjs(new Date(min));
isEnabled = !dayjs(value).isBefore(compareMin, mode);
} else if (max) {
const compareMax = dayjs(new Date(max));
isEnabled = !dayjs(value).isAfter(compareMax, mode);
}
return isEnabled;
}