All files / src/calendar CalendarTable.tsx

28.38% Statements 21/74
20.43% Branches 19/93
53.85% Functions 7/13
28.38% Lines 21/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288                                                                                                                                                                  4x 4x                 3x 3x                                               70x 3x           3x     3x 3x 3x 3x     3x               3x     12x 12x               3x                                                                                                             105x             105x                                     105x 105x     105x                                                                                                    
import React, { useRef } from "react";
import moment, { Moment, isMoment } from "moment";
import { DateTable, DateTableProps } from "./tables/DateTable";
import { ControlledProps } from "../form/controlled";
import { CellStatus } from "./tables/BaseTable";
import { MonthTable } from "./tables/MonthTable";
import { ScaleTransition } from "../transition/ScaleTransition";
import { YearTable } from "./tables/YearTable";
import { TimeDisabledProps } from "../timepicker/TimeProps";
import { TimeTable } from "../timepicker/TimeTable";
import { useTranslation } from "../i18n";
import {
  RangeDateType,
  showTimeType,
  CalendarTableType,
  DateChangeContext,
} from "./DateProps";
 
export interface CalendarTableProps
  extends ControlledProps<
    Moment | RangeDateType,
    React.SyntheticEvent,
    DateChangeContext
  > {
  /**
   * 允许选择的时间范围限制
   */
  range?: RangeDateType;
 
  /**
   * 不可选的日期
   */
  disabledDate?: (date: Moment, startDate?: Moment) => boolean;
 
  /**
   * 不可选的时间
   */
  disabledTime?: (
    date: Moment | RangeDateType,
    partial?: "start" | "end"
  ) => TimeDisabledProps;
 
  /**
   * 是否开启时间选择
   */
  showTime?: showTimeType<Moment>;
 
  /**
   * 当前展示日期
   */
  curViewMoment?: Moment;
 
  /**
   * 当前展示日期改变回调
   */
  onCurViewMomentChange?: DateTableProps["onCurrentChange"];
 
  /**
   * 当前展示的日历类型
   */
  type: CalendarTableType;
 
  /**
   * 日历类型改变回调
   */
  onTypeChange?: (types: CalendarTableType) => void;
 
  /**
   * 作为范围选择部分时的位置
   */
  rangeType?: "start" | "end";
 
  /**
   * 是否只选择到月份
   */
  monthOnly?: boolean;
}
 
/**
 * 动画参数
 */
const ZOMM_OUT = 1.04;
const ZOOM_IN = 1 / ZOMM_OUT;
 
/**
 * 获取当前时间选择范围
 */
export function getTimeRange(
  value: Moment,
  range: RangeDateType = [null, null]
): RangeDateType {
  Eif (!Array.isArray(range) || !value) {
    return undefined;
  }
  const [min, max] = range;
  const timeRange: RangeDateType = [
    moment().startOf("day"),
    moment().endOf("day"),
  ];
  if (isMoment(min) && value.isSame(min, "day")) {
    timeRange[0] = min;
  }
  if (isMoment(max) && value.isSame(max, "day")) {
    timeRange[1] = max;
  }
  return timeRange;
}
 
export function CalendarTable({
  value,
  onChange,
  type,
  onTypeChange,
  curViewMoment = moment(),
  onCurViewMomentChange,
  range,
  disabledDate = () => true,
  disabledTime = () => ({}),
  showTime,
  rangeType,
  monthOnly,
  ...props
}: CalendarTableProps) {
  const t = useTranslation();
 
  // 记录 Table 切换
  const prevTypeRef = useRef<CalendarTableType>(null);
  const prevType = prevTypeRef.current;
  Eif (type === "month" || type === "date") {
    prevTypeRef.current = type;
  }
 
  const tableProps = {
    ...props,
    range,
    onTypeChange,
    current: curViewMoment,
    onCurrentChange: onCurViewMomentChange,
  };
 
  const timeProps = typeof showTime === "object" ? showTime : {};
 
  function getValue(): Moment {
    Eif (!Array.isArray(value)) {
      return value;
    }
    if (rangeType === "start") {
      return value[0];
    }
    return value[1];
  }
 
  return (
    <>
      {/* Year */}
      <ScaleTransition in={type === "year"} exit={false} from={ZOMM_OUT}>
        <YearTable
          {...tableProps}
          onSelect={value => {
            onCurViewMomentChange(moment(curViewMoment).year(value.year()));
            onTypeChange(prevType || (monthOnly ? "date" : "month"));
          }}
          cellStatus={date => {
            if (!Array.isArray(value)) {
              if (isMoment(value) && date.isSame(value, "year")) {
                return CellStatus.Selected;
              }
            }
            return CellStatus.Common;
          }}
        />
      </ScaleTransition>
 
      {/* Month */}
      <ScaleTransition
        in={type === "month"}
        exit={false}
        from={prevType !== "date" ? ZOOM_IN : ZOMM_OUT}
      >
        <MonthTable
          {...tableProps}
          onSelect={(value, context) => {
            onCurViewMomentChange(moment(curViewMoment).month(value.month()));
            if (monthOnly) {
              onChange(value, context);
              return;
            }
            onTypeChange("date");
          }}
          cellStatus={date => {
            if (!Array.isArray(value)) {
              if (isMoment(value) && date.isSame(value, "month")) {
                return CellStatus.Selected;
              }
            }
            return CellStatus.Common;
          }}
        />
      </ScaleTransition>
 
      {/* Date */}
      <ScaleTransition in={type === "date"} exit={false} from={ZOOM_IN}>
        <DateTable
          {...tableProps}
          value={getValue()}
          disabledDate={(date: Moment) => {
            // 范围选择已选择 start,未选择 end
            Iif (
              Array.isArray(value) &&
              isMoment(value[0]) &&
              !isMoment(value[1])
            ) {
              return disabledDate(date, value[0]);
            }
            return disabledDate(date);
          }}
          onSelect={(v, context) => {
            // 单日选择
            context.type = "date";
            if (!Array.isArray(value)) {
              return onChange(v, context);
            }
            // 范围选择
            if (isMoment(value[0]) && !isMoment(value[1])) {
              if (value[0].isBefore(v)) {
                return onChange([value[0], v], context);
              }
              return onChange([v, value[0]], context);
            }
            return onChange([v, null], context);
          }}
          cellStatus={date => {
            // 单日选择
            Eif (!Array.isArray(value)) {
              Iif (isMoment(value) && date.isSame(value, "day")) {
                return CellStatus.Selected;
              }
              return CellStatus.Common;
            }
            // 范围选择
            if (isMoment(value[0]) && date.isSame(value[0], "day")) {
              // 只选中了开始或开始结束在同一天返回 Selected
              if (!isMoment(value[1])) {
                return CellStatus.Selected;
              }
              if (value[0].isSame(value[1], "day")) {
                return CellStatus.Selected;
              }
              return CellStatus.RangeStart;
            }
            if (isMoment(value[1]) && date.isSame(value[1], "day")) {
              return CellStatus.RangeEnd;
            }
            if (isMoment(value[0]) && isMoment(value[1])) {
              if (value[0].isBefore(date) && value[1].isAfter(date)) {
                return CellStatus.InRange;
              }
            }
            return CellStatus.Common;
          }}
        />
      </ScaleTransition>
 
      {/* Time */}
      <ScaleTransition in={type === "time"} exit={false} from={ZOMM_OUT}>
        <TimeTable
          caption={isMoment(getValue()) ? getValue().format("L") : t.selectTime}
          value={getValue()}
          onChange={(v, context) => {
            context.type = "time";
            // 日期选择完才可选择时间,所以 value 必定合法
            if (!Array.isArray(value)) {
              return onChange(v, context);
            }
            if (rangeType === "start") {
              return onChange([v, value[1]], context);
            }
            return onChange([value[0], v], context);
          }}
          range={getTimeRange(getValue(), range)}
          {...disabledTime(value, rangeType) || {}}
          {...timeProps}
        />
      </ScaleTransition>
    </>
  );
}