/** * @Author: ?? * @Date: ?? */ import React, { ForwardedRef, forwardRef, useEffect, useImperativeHandle, useState } from "react"; import { DatePickerProps, DatePickerRefFuncProps } from "./interface"; import { default as RCDatePicker } from "react-datepicker"; import "react-datepicker/dist/react-datepicker.css"; import "./index.css"; import { CustomDpInput } from "./CustomDpInput"; import { timeValue, YMD, YMDHms } from "@utils/time"; import dayjs from "dayjs"; /** * ### 引用方法 * ```tsx * import { DatePicker } from "@atomintl/exchange-ui"; * ``` * ##### 更多react-datepicker props详见 https://reactdatepicker.com/#example-custom-header-with-two-months-displayed */ export const DatePicker: React.FC = props => { const { onDateStrChange, timeQuantum, resetButton, queryButton, className, initSinceWhen, onRef } = props; const [[startDate, endDate], setDateRange] = useState<[Date | null, Date | null]>([null, null]); const [sinceWhen, setSince] = useState<"d" | "w" | "m" | "3m" | "">(initSinceWhen || ""); const start_end = startDate && endDate; const CustomInput = forwardRef(({ value, onClick }: any, ref: ForwardedRef) => { return ; }); useEffect(() => { onDateStrChange?.([ { startV: startDate ? timeValue(startDate) : "", endV: startDate ? timeValue(endDate) : "" }, { startDate: startDate ? YMDHms(startDate) : "", endDate: endDate ? YMDHms(endDate) : "" } ]); }, [startDate, endDate]); const renderDayContents = (day: any, date: Date) => { const tooltipText = YMD(date); return {day}; }; useEffect(() => { if (sinceWhen) { let today = dayjs(), targetDay = undefined; switch (sinceWhen) { case "d": targetDay = dayjs().add(1, "day"); break; case "w": targetDay = dayjs().add(1, "w"); break; case "m": targetDay = dayjs().add(1, "M"); break; case "3m": targetDay = dayjs().add(3, "M"); break; } setDateRange([new Date(today.toString()), new Date(targetDay?.toString())]); } }, [sinceWhen]); const TimeList = React.memo(() => { return timeQuantum ? ( <>
setSince("d")} > 一天
setSince("w")} > 一周
setSince("m")} > 一个月
setSince("3m")} > 三个月
时间
) : null; }); const CheckWidget = () => { return queryButton ?
查询
: null; }; const ResetWidget = () => { return resetButton ? (
setDateRange([null, null])} > 重置
) : null; }; useImperativeHandle(onRef, () => ({ setDateRange, setSince })); return (
{ console.log(e); setDateRange(e); }} monthsShown={2} showPopperArrow={false} dateFormat={"yyyy-MM-dd"} customInput={} renderDayContents={renderDayContents} // renderCustomHeader={({ // date, // changeYear, // changeMonth, // decreaseMonth, // increaseMonth, // prevMonthButtonDisabled, // nextMonthButtonDisabled, // }) => (
)} {...props} />
); }; DatePicker.defaultProps = {}; export type { DatePickerProps, DatePickerRefFuncProps }; export default DatePicker;