MobileDate.jsx 4.4 KB
Newer Older
徐立's avatar
徐立 committed
1 2 3 4
/**
 * 2019年10月23日
 * 移动端日期选择封装
 */
5 6 7 8
/*
*  2022年8月29日 29143 h5/优秀学生干部管理/优秀学生干部申请,时间只要年/月;更改时间排版(20192601007,bdk@2022*$)
*
 */
9 10
import React, { Component } from 'react';
import { DatePicker, List, Toast } from 'antd-mobile';
11
import moment from 'moment';
12

徐立's avatar
徐立 committed
13
export default class MobileDate extends Component {
14 15 16 17 18 19 20 21 22
  constructor(props) {
    super(props);
    const value = props.value || [];

    this.state = {
      dates: value,
    };
  }

23
  triggerChange = changedValue => {
钟是志's avatar
钟是志 committed
24
    console.log(changedValue);
25 26
    // Should provide an event to pass value to Form.
    const onChange = this.props.onChange;
27
    // console.log(changedValue);
28
    if (onChange) {
29
      onChange([...changedValue]);
30 31 32 33 34 35 36
    }
  };

  componentWillReceiveProps(nextProps) {
    // Should be a controlled component.
    if ('value' in nextProps) {
      const value = nextProps.value;
37
      this.setState({ dates: value });
38 39 40
    }
  }

41 42
  handelStartChange = date => {
    const { dates } = this.state;
43 44 45 46

    if (dates.length === 0) {
      dates.push(moment(date.valueOf()));
    } else {
47
      if (dates.length === 2) {
48 49 50 51 52 53 54 55 56
        if (moment(date.valueOf()).isAfter(dates[1], 'second')) {
          Toast.fail('结束时间必须在开始时间之后', 1);
          return false;
        }
      }
      dates[0] = moment(date.valueOf());
    }

    if (!('value' in this.props)) {
57
      this.setState({ dates });
58 59 60
    }
    this.triggerChange(dates);
  };
钟是志's avatar
钟是志 committed
61

62 63
  handelEndChange = date => {
    const { dates } = this.state;
64 65 66 67 68 69 70 71 72 73 74 75 76 77
    if (dates.length === 0) {
      dates.push(null);
      dates.push(moment(date.valueOf()));
    } else if (dates.length === 1) {
      if (dates[0].isAfter(moment(date.valueOf()), 'second')) {
        Toast.fail('结束时间必须在开始时间之后', 1);
        return false;
      }
      dates.push(moment(date.valueOf()));
    } else {
      if (dates[0].isAfter(moment(date.valueOf()), 'second')) {
        Toast.fail('结束时间必须在开始时间之后', 1);
        return false;
      }
78

79 80 81
      dates[1] = moment(date.valueOf());
    }
    if (!('value' in this.props)) {
82
      this.setState({ dates });
83 84 85
    }
    this.triggerChange(dates);
  };
86

87 88
  MustSpan = () => {
    const { required } = this.props;
89 90 91
    if (required) {
      return <span style={{ color: 'red' }}>* </span>;
    } else {
92 93 94 95
      return null;
    }
  };

96 97
  formatDateShow = (v) => {

钟是志's avatar
钟是志 committed
98
  };
99

100
  render() {
101
    let { dates } = this.state;
102 103 104 105
    const { format, showTime, otherProps,
     } = this.props;
    const mobileProps = otherProps?.mobileProps || {};
    const formatDiy = mobileProps.format || format || 'YYYY-MM-DD HH:mm:ss';
钟是志's avatar
钟是志 committed
106
    let mode = 'datetime';
107
    if (!showTime) {
钟是志's avatar
钟是志 committed
108 109
      mode = 'date';
    }
110
    const startTime = dates.length > 0 ? dates[0] && dates[0].valueOf() : null;
111
    const endTime = dates.length > 1 ? dates[1] && dates[1].valueOf() : null;
112
    const MustSpan = this.MustSpan();
钟是志's avatar
钟是志 committed
113 114

    // console.log(startTime, endTime);
115

116 117 118 119 120
    return (
      <div>
        <DatePicker
          // value={this.state.date}
          extra={
121 122
            <span style={{ fontSize: 14 }}>
              {!!startTime
123
                ? moment(startTime).format(formatDiy)
124
                : '请选择时间'}
125
            </span>
126
          }
127 128 129 130
          locale={{
            okText: '确定',
            dismissText: '取消',
          }}
131
          value={!!startTime ? new Date(startTime) : null}
钟是志's avatar
钟是志 committed
132
          mode={mode}
133
          onChange={date => this.handelStartChange(date)}
134 135 136 137
          {...mobileProps}
          format={(value) => {
            return moment(value).format(formatDiy);
          }}
138
        >
139
          <List.Item arrow="horizontal">{MustSpan}开始</List.Item>
140 141 142 143
        </DatePicker>
        <DatePicker
          // value={this.state.date}
          extra={
144 145
            <span style={{ fontSize: 14 }}>
              {!!endTime
146
                ? moment(endTime).format(formatDiy)
147
                : '请选择时间'}
148
            </span>
149 150
          }
          value={!!endTime ? new Date(endTime) : null}
151 152 153 154
          locale={{
            okText: '确定',
            dismissText: '取消',
          }}
钟是志's avatar
钟是志 committed
155
          mode={mode}
156
          onChange={date => this.handelEndChange(date)}
157 158 159 160
          {...mobileProps}
          format={(value) => {
            return moment(value).format(formatDiy);
          }}
161
        >
162
          <List.Item arrow="horizontal">{MustSpan}结束</List.Item>
163 164 165 166
        </DatePicker>
      </div>
    );
  }
徐立's avatar
徐立 committed
167
}