All files / src/datepicker MonthPicker.tsx

57.14% Statements 16/28
66.67% Branches 10/15
37.5% Functions 3/8
57.14% Lines 16/28

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                                                    2x   2x 2x                                     2x     2x     2x         2x     2x     2x 2x   4x       2x       2x 2x 2x                                             2x                                                                                                          
import React, { useState, useEffect, useRef, useCallback } from "react";
import classNames from "classnames";
import moment, { Moment, isMoment } from "moment";
import CalendarPart from "../calendar/CalendarPart";
import { ControlledProps, useDefaultValue } from "../form/controlled";
import { CalendarTable } from "../calendar/CalendarTable";
import { Combine } from "../_type";
import { Input } from "../input/Input";
import { DropdownBox } from "../dropdown";
import { CommonDatePickerProps } from "./DatePickerProps";
import { useTranslation } from "../i18n";
import { CalendarTableType, DateChangeContext } from "../calendar/DateProps";
import { withStatics } from "../_util/with-statics";
import { Popover } from "../popover/Popover";
import { DatePickerTrigger } from "./util";
import { useDefault } from "../_util/use-default";
import { useConfig } from "../_util/config-context";
 
export interface MonthPickerProps
  extends Combine<CommonDatePickerProps, ControlledProps<Moment>> {
  /**
   * 不可选的日期
   */
  // disabledMonth?: (date: Moment) => boolean;
}
 
export const MonthPicker = withStatics(
  function MonthPicker(props: MonthPickerProps) {
    const t = useTranslation(moment);
    const { classPrefix } = useConfig();
    const {
      header,
      className,
      style,
      value,
      onChange,
      disabled,
      format = "YYYY-MM",
      placeholder = t.selectDate,
      defaultOpen = false,
      open,
      onOpenChange = () => null,
      placement = "bottom-start",
      placementOffset = 5,
      closeOnScroll = true,
      escapeWithReference,
      overlayClassName,
      overlayStyle,
    } = useDefaultValue(props);
 
    // 当前面板类型
    const [type, setType] = useState<CalendarTableType>("month");
 
    // 当前面板展示时间
    const [curViewMoment, setCurViewMoment] = useState<Moment>(
      isMoment(value) ? value : moment()
    );
 
    // 当前选中日期
    const [curValue, setCurValue] = useState<Moment>(value);
 
    // 选择器是否展开
    const [active, setActive] = useDefault(open, defaultOpen, onOpenChange);
 
    // 输入框显示值
    const inputRef = useRef<HTMLInputElement>(null);
    const getInputValue = useCallback(
      value => {
        return isMoment(value) ? value.locale(t.locale).format(format) : "";
      },
      [t.locale, format]
    );
    const [inputValue, setInputValue] = useState<string>(
      getInputValue(curValue)
    );
 
    useEffect(() => {
      setCurValue(value);
      setInputValue(getInputValue(value));
    }, [format, value, getInputValue]);
 
    function handleChange(value: Moment, context: DateChangeContext) {
      setCurValue(value);
      setInputValue(getInputValue(value));
      onChange(value, context);
      handleClose();
    }
 
    function handleOpen() {
      if (disabled) {
        return;
      }
      setActive(true);
      setType("month");
    }
 
    function handleClose() {
      setInputValue(getInputValue(value));
      setActive(false);
    }
 
    return (
      <Popover
        trigger={[
          DatePickerTrigger,
          { onOpen: handleOpen, onClose: handleClose },
        ]}
        visible={active}
        onVisibleChange={setActive}
        placement={placement}
        placementOffset={placementOffset}
        closeOnScroll={closeOnScroll}
        escapeWithReference={escapeWithReference}
        overlayClassName={overlayClassName}
        overlayStyle={overlayStyle}
        overlay={
          <DropdownBox>
            <CalendarPart.Panel>
              {!!header && <CalendarPart.Header>{header}</CalendarPart.Header>}
              <CalendarTable
                {...props}
                monthOnly
                value={curValue}
                onChange={handleChange}
                type={type}
                onTypeChange={setType}
                curViewMoment={curViewMoment}
                onCurViewMomentChange={setCurViewMoment}
              />
            </CalendarPart.Panel>
          </DropdownBox>
        }
      >
        <div
          className={classNames(`${classPrefix}-monthpicker`, className)}
          style={style}
        >
          <div className={`${classPrefix}-monthpicker__input`}>
            <Input
              ref={inputRef}
              disabled={disabled}
              placeholder={placeholder}
              value={inputValue}
              onFocus={() => inputRef.current.blur()}
            />
          </div>
        </div>
      </Popover>
    );
  },
  {
    defaultLabelAlign: "middle",
  }
);