Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/components/misc/date-range.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { DatePicker } from "antd";6import dayjs from "dayjs";7import { CSSProperties, useState } from "react";8import { DateRangeType, Date0 } from "@cocalc/util/types/store";910interface Props {11onChange?: (x: DateRangeType) => void;12style?: CSSProperties;13noPast?: boolean; // if true, don't allow dates in the past14maxDaysInFuture?: number; // don't allow dates this far in the future from now15disabled?: boolean;16initialValues?: DateRangeType;17suffix?: string;18}1920export default function DateRange(props: Props) {21const {22onChange,23style,24noPast,25maxDaysInFuture,26disabled = false,27initialValues = [undefined, undefined],28suffix,29} = props;3031const [dateRange, setDateRange] = useState<DateRangeType>(initialValues);3233const presets = [34{ label: "Day", value: [dayjs(), dayjs().add(1, "day")] },35{ label: "Week", value: [dayjs(), dayjs().add(1, "week")] },36{ label: "Month", value: [dayjs(), dayjs().add(1, "month")] },37{ label: "Year", value: [dayjs(), dayjs().add(1, "year")] },38{39label: "+ Hour",40value: [dayjs(dateRange[0]), dayjs(dateRange[0]).add(1, "hour")],41},42{43label: "+ Day",44value: [dayjs(dateRange[0]), dayjs(dateRange[0]).add(1, "day")],45},46{47label: "+ Week",48value: [dayjs(dateRange[0]), dayjs(dateRange[0]).add(1, "week")],49},50{51label: "+ Month",52value: [dayjs(dateRange[0]), dayjs(dateRange[0]).add(1, "month")],53},54{55label: "+ Three Months",56value: [dayjs(dateRange[0]), dayjs(dateRange[0]).add(3, "months")],57},58{59label: "+ Four Months",60value: [dayjs(dateRange[0]), dayjs(dateRange[0]).add(4, "months")],61},62];6364return (65<div style={style}>66<DatePicker.RangePicker67changeOnBlur68disabled={disabled}69allowEmpty={[true, true]}70renderExtraFooter={() => (71<div style={{ marginBottom: "-15px" }}>72<div>73Select start and end dates above, with the help of the presets74below:75</div>76<ul>77<li style={{ marginTop: "-15px" }}>78Week = one week starting today79</li>80<li style={{ marginTop: "-15px" }}>81+Week = one week, starting from the selected start date82</li>83</ul>84</div>85)}86presets={presets as any}87value={88[89dateRange[0] ? dayjs(dateRange[0]) : undefined,90dateRange[1] ? dayjs(dateRange[1]) : undefined,91] as any92}93onChange={(value) => {94const now = dayjs();95// Ensure start is the later of now or the start of the selected day96const start = value?.[0]97? dayjs(value[0]).isBefore(now)98? now.toDate()99: dayjs(value[0]).startOf("day").toDate()100: undefined;101// Set end of day, but only modify if there's a value102const end = value?.[1]103? dayjs(value[1]).endOf("day").subtract(1, "minute").toDate()104: undefined;105const x: [Date0, Date0] = [start, end];106setDateRange(x);107onChange?.(x);108}}109disabledDate={110noPast || maxDaysInFuture111? (date) => {112if (!date) return false;113if (noPast && date <= dayjs().subtract(1, "day")) return true;114if (115maxDaysInFuture &&116date >= dayjs().add(maxDaysInFuture, "days")117) {118return true;119}120return false;121}122: undefined123}124/>125{suffix && <span style={{ marginLeft: "5px" }}>{suffix}</span>}126</div>127);128}129130131