封裝日期組件

import React from "react";
import { DatePicker, Input } from "antd";
import styles from "./index.scss";
import moment from "moment";

class MonthPick extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      inputText: "",
      value: "",
    };
  }

  handleOpenChange = (open) => {
    const { value } = this.props;
    if (open) {
      this.setState(
        {
          inputText: value ? moment(value).format("YYYY-MM") : "",
        },
        () => {
          setTimeout(() => {
            document.getElementById("inputBox").focus();
          }, 0);
        }
      );
    }
  };

  // 月份發(fā)生變化傳值出去
  handleChange = (changedValue) => {
    const { onChange } = this.props;
    let data = changedValue;
    this.setState({ inputText: moment(changedValue).format("YYYY-MM") });
    const dataStr = changedValue ? moment(changedValue).format("YYYY-MM") : "";
    if (onChange) {
      onChange(data, dataStr);
    }
  };

  inputTextChangeFn = (e) => {
    const { disabledDate } = this.props;
    this.setState({
      inputText: e.target.value,
    });
    if (!e.target.value) {
      this.handleChange("");
      this.setState({ inputText: "" });
      return;
    }
    let changedValue = moment(e.target.value);
    // 數(shù)據(jù)格式是否正確
    if (/^[1-9]\d{3}-(0[1-9]|1[0-2])$/.test(e.target.value)) {
      // 是否有不可選數(shù)據(jù)
      if (disabledDate) {
        this.isDisabledDateFn(changedValue);
      } else {
        this.handleChange(changedValue);
      }
    }
  };

  isDisabledDateFn = (val) => {
    const { disabledDate } = this.props;
    if (!disabledDate(val)) {
      this.handleChange(val);
    }
  };

  // 處理人員詳情頁面點擊月份組件按鈕有時候沒有反應的bug
  changeYearFn = (e) => {
    if (e.target.getAttribute("class") == "ant-calendar-month-panel-next-year-btn") {
      e.stopPropagation();
    }
    if (e.target.getAttribute("class") == "ant-calendar-month-panel-prev-year-btn") {
      e.stopPropagation();
    }
    if (e.target.getAttribute("class") == "ant-calendar-year-panel-year") {
      e.stopPropagation();
    }
    if (e.target.getAttribute("class") == "ant-calendar-decade-panel-decade") {
      e.stopPropagation();
    }
    if (e.target.getAttribute("class") == "ant-calendar-picker-clear") {
      e.stopPropagation();
    }
  };

  render() {
    const { inputText } = this.state;
    const { value, placeholder } = this.props;
    return (
      <div className={styles.monthBox} id="monthBox" onClick={(e) => this.changeYearFn(e)}>
        <DatePicker.MonthPicker
          {...this.props}
          dropdownClassName="monthPickBox"
          renderExtraFooter={() => (
            <Input
              id="inputBox"
              style={{ width: "280px", height: "32px", border: "none" }}
              value={inputText}
              onChange={this.inputTextChangeFn}
              placeholder={placeholder ? placeholder : "請選擇日期"}
            />
          )}
          // 解決上下滾動月份彈窗沒有跟著移動的bug
          // getCalendarContainer={triggerNode => {
          //   console.log(triggerNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode);
          //   if(moreLevel) {
          //     return triggerNode.parentNode
          //   }
          //   return triggerNode.parentNode
          // }}
          format="YYYY-MM"
          onOpenChange={this.handleOpenChange}
          value={value ? moment(value) : undefined}
          onChange={this.handleChange}
        />
      </div>
    );
  }
}

export default MonthPick;

效果


image.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容