RangePickerDiy.js 2.2 KB
Newer Older
1 2
import moment from 'moment';
import { DatePicker } from 'antd';
钟是志's avatar
钟是志 committed
3

4 5 6
const { RangePicker } = DatePicker;
import React, { useEffect, useState } from 'react';

钟是志's avatar
钟是志 committed
7 8 9 10 11 12 13
export function getMomentArr({
                               begin,
                               end,
                               initValue,
                               init,
                               obj,
                             }) {
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  let ivs = [];
  if (initValue != null && init != null) {
    ivs.push(moment(parseInt(initValue)));
    if (init && init[end.base52]) {
      ivs.push(moment(parseInt(init[end.base52])));
    } else if (obj && obj[end.base52]) {
      ivs.push(moment(parseInt(obj[end.base52])));
      // 钟是志 2021年9月28日13:59:48
      // 禅道bug http://scjoyedu.eicp.net:88/zentao/bug-view-21843.html
      // 不知道怎么改 init 为空对象 暂时处理为从obj里面拿
    } else {
      ivs.push(moment());
    }
  }

钟是志's avatar
钟是志 committed
29 30 31 32 33
  if (!ivs || !Array.isArray(ivs) || ivs.length !== 2) {
    ivs = [];
  } else {
    for (let i = 0; i < ivs.length; i++) {
      if (!moment.isMoment(ivs[i])) {
34 35 36 37 38 39 40
        ivs[i] = moment();
      }
    }
  }
  return ivs;
}

钟是志's avatar
钟是志 committed
41
export default function RangePickerDiy(props) {
42 43 44 45 46 47
  const {
    onChange,
    value,
    json,
    disabled,
  } = props;
钟是志's avatar
钟是志 committed
48
  const [bindValue, setBindValue] = useState([]);
49 50

  useEffect(() => {
51
    // console.log('RangePickerDiy组件Value', value);
钟是志's avatar
钟是志 committed
52 53
    if (!value || !Array.isArray(value) || value.length !== 2) {
      setBindValue([]);
54

钟是志's avatar
钟是志 committed
55 56 57
    } else {
      for (let i = 0; i < value.length; i++) {
        if (typeof value[i] === 'number' && value[i] > 10000) {
58
          bindValue[i] = moment(value[i]);
钟是志's avatar
钟是志 committed
59 60
        } else if (!moment.isMoment(value[i])) {
          // bindValue[i] = undefined;
61
          // console.log('RangePickerDiy组件没有获取到正确的值', value);
钟是志's avatar
钟是志 committed
62
        } else {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
          bindValue[i] = value[i];
        }
      }
      setBindValue(value);
    }
  }, [value]);
  return (
    <RangePicker
      value={bindValue}
      onChange={onChange}
      showTime={json.showTime != null ? json.showTime : true}
      format={json.format ? json.format : 'YYYY-MM-DD HH:mm:ss'}
      disabled={disabled}
      getCalendarContainer={() => {
        return document.body;
      }}
    />
钟是志's avatar
钟是志 committed
80
  );
81 82

}