import moment, { Moment } from "moment"; import { useOutsideClick } from "../_util/use-outside-click"; import { TriggerProps } from "../popover/trigger"; import { isValidRange } from "../timepicker/util"; import { DatePickerProps } from "./DatePicker"; import { RangePickerProps } from "./RangePicker"; import { MonthPickerProps } from "./MonthPicker"; export function getNow(value: Moment) { // eslint-disable-next-line dot-notation return value["tz"] && value["tz"]() ? // eslint-disable-next-line dot-notation moment()["tz"](value["tz"]()) : moment(); } export function getYearMonthDate(date: Moment) { return { year: date.year(), month: date.month(), date: date.date(), }; } /** * 以下为不关注时区的比较方法 */ type GranularityType = | "year" | "quarter" | "month" | "date" | "day" | "hour" | "minute" | "second"; const granularityList: GranularityType[] = [ "year", "quarter", "month", // "week", "date", "hour", "minute", "second", // "millisecond", ]; // moment("2021-03-25 08:00:00").tz("Pacific/Niue").isSame(moment("2021-03-25 08:00:00")) // true // // isSame(moment("2021-03-25 08:00:00").tz("Pacific/Niue"), moment("2021-03-24 13:00:00")) // true export function isSame( a: Moment, b: Moment, granularity: GranularityType = "second" ): boolean { if (!moment.isMoment(a) || !moment.isMoment(b)) { return false; } if (granularity === "day") { // eslint-disable-next-line no-param-reassign granularity = "date"; } let result = true; for (const g of granularityList) { result = result && a[g]() === b[g](); if (granularity === g) { return result; } } return result; } export function isBefore( a: Moment, b: Moment, granularity: GranularityType = "second" ): boolean { if (!moment.isMoment(a) || !moment.isMoment(b)) { return false; } if (granularity === "day") { // eslint-disable-next-line no-param-reassign granularity = "date"; } for (const g of granularityList) { if (a[g]() < b[g]()) { return true; } if (a[g]() > b[g]()) { return false; } if (granularity === g) { return false; } } return false; } export function isAfter( a: Moment, b: Moment, granularity: GranularityType = "second" ): boolean { return isBefore(b, a, granularity); } /** * 选择器弹出 Trigger */ export const DatePickerTrigger = ({ childrenElementRef, overlayElementRef, visible, render, onOpen, onClose, }: TriggerProps & { onOpen: () => void; onClose: () => void }) => { const { listen } = useOutsideClick([childrenElementRef, overlayElementRef]); listen(() => { if (visible) { onClose(); } }); return render({ overlayProps: {}, childrenProps: { onClick: (event: React.MouseEvent) => { event.stopPropagation(); if (!visible) { onOpen(); } else { onClose(); } }, }, }); }; /** * 当前时间是否为可选 */ export function isValidDate( date: Moment, { range, disabledDate = () => true, }: Partial | Partial, startValue? ): boolean { const [rangeMin, rangeMax] = range || [null, null]; if (moment.isMoment(rangeMin) && isAfter(rangeMin, date, "day")) { return false; } if (moment.isMoment(rangeMax) && isBefore(rangeMax, date, "day")) { return false; } return disabledDate(date, startValue); } export function isValidMonth( date: Moment, { range, disabledMonth = () => true, }: Partial | Partial ) { const [rangeMin, rangeMax] = range || [null, null]; if (moment.isMoment(rangeMin) && isAfter(rangeMin, date, "month")) { return false; } if (moment.isMoment(rangeMax) && isBefore(rangeMax, date, "month")) { return false; } return disabledMonth(date); }