Basic.tsx 5.21 KB
Newer Older
1 2 3 4 5
import React, { useState } from 'react';
import TimeRangePicker from '../index';
import moment from 'moment';

const Demo = () => {
6 7
  const [dateValue, setDateValue] = useState(null);
  const [timeValue, setTimeValue] = useState(null);
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
  const [data, setData] = useState({ startDate: '', endDate: '' });
  const [time, setTime] = useState({ startTime: '', endTime: '' });

  const onDateChange = (key, data = []) => {
    let start = '',
      end = '';
    switch (key) {
      case 'all':
        break;
      case 'yesterday':
        start = moment().subtract(1, 'days').format(startFormat);
        end = moment().subtract(1, 'days').format(endFormat);
        break;
      case 'today':
        start = moment().format(startFormat);
        end = moment().format(endFormat);
        break;
      case 'thisWeek':
涂茜's avatar
涂茜 committed
26 27
        start = moment().startOf('week').format(startFormat);
        end = moment().endOf('week').format(endFormat);
28 29
        break;
      case 'lastWeek':
涂茜's avatar
涂茜 committed
30 31
        start = moment().startOf('week').subtract(1, 'week').format(startFormat);
        end = moment().endOf('week').subtract(1, 'week').format(endFormat);
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
        break;
      case 'thisMonth':
        start = moment().startOf('month').format(startFormat);
        end = moment().endOf('month').format(endFormat);
        break;
      case 'lastMonth':
        start = moment()
          .month(moment().month() - 1)
          .startOf('month')
          .format(startFormat);
        end = moment()
          .month(moment().month() - 1)
          .endOf('month')
          .format(endFormat);
        break;
      case 'thisYear':
        start = moment().startOf('year').format(startFormat);
        end = moment().endOf('year').format(endFormat);
        break;
      case 'lastYear':
        start = moment()
          .year(moment().year() - 1)
          .startOf('year')
          .format(startFormat);
        end = moment()
          .year(moment().year() - 1)
          .endOf('year')
          .format(endFormat);
        break;
      case 'customer':
        if (data && data.length > 0) {
          start = moment(data[0]).format(startFormat);
          end = moment(data[1]).format(endFormat);
        }
        break;
    }
68
    setDateValue(key);
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
    setData({ startDate: start, endDate: end });
  };

  const onTimeChange = (key, data = []) => {
    let start = '',
      end = '';
    switch (key) {
      case 'oneHour':
        start = moment().subtract(1, 'hour').format(timeFormat);
        end = moment().format(timeFormat);
        break;
      case 'fourHour':
        start = moment().subtract(4, 'hour').format(timeFormat);
        end = moment().format(timeFormat);
        break;
      case 'twelveHours':
        start = moment().subtract(12, 'hour').format(timeFormat);
        end = moment().format(timeFormat);
        break;
      case 'roundClock':
        start = moment().subtract(24, 'hour').format(timeFormat);
        end = moment().format(timeFormat);
        break;
      case 'yesterday':
        start = moment().subtract(1, 'days').format(startFormat);
        end = moment().subtract(1, 'days').format(endFormat);
        break;
      case 'customer':
        if (data && data.length > 0) {
          start = moment(data[0]).format(timeFormat);
          end = moment(data[1]).format(timeFormat);
        }
        break;
    }
103
    setTimeValue(key);
104 105 106 107 108 109
    setTime({ startTime: start, endTime: end });
  };

  return (
    <>
      <h3>horizontal</h3>
110 111 112 113 114 115 116
      <TimeRangePicker
        value={dateValue}
        onChange={onDateChange}
        defaultValue={dateList[0].key}
        dataSource={dateList}
        layout={'horizontal'}
      />
117 118
      <br />
      <h3>vertical</h3>
119 120 121 122 123 124 125
      <TimeRangePicker
        value={dateValue}
        onChange={onDateChange}
        defaultValue={dateList[0].key}
        dataSource={dateList}
        layout={'vertical'}
      />
126 127 128 129 130 131 132 133
      <br />
      <div>
        startDate:{data.startDate}
        <br />
        endDate:{data.endDate}
      </div>
      <br />
      <TimeRangePicker
134
        value={timeValue}
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        onChange={onTimeChange}
        dataSource={timeList}
        timeProps={{ showTime: true }}
      />
      <br />
      <div>
        startTime:{time.startTime}
        <br />
        endTime:{time.endTime}
      </div>
    </>
  );
};

export default Demo;

const startFormat = 'YYYY-MM-DD 00:00:00';
const endFormat = 'YYYY-MM-DD 23:59:59';

涂茜's avatar
涂茜 committed
154
const timeFormat = 'YYYY-MM-DD hh:mm:ss';
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

const dateList = [
  {
    key: 'all',
    name: '全部',
  },
  {
    key: 'yesterday',
    name: '昨天',
  },
  {
    key: 'today',
    name: '今天',
  },
  {
    key: 'thisWeek',
    name: '本周',
  },
  {
    key: 'lastWeek',
    name: '上周',
  },
  {
    key: 'thisMonth',
    name: '本月',
  },
  {
    key: 'lastMonth',
    name: '上月',
  },
  {
    key: 'thisYear',
    name: '今年',
  },
  {
    key: 'lastYear',
    name: '去年',
  },
  {
    key: 'customer',
    name: '自定义',
  },
];

const timeList = [
  {
    key: 'oneHour',
    name: '近1小时',
  },
  {
    key: 'fourHour',
    name: '近4小时',
  },
  {
    key: 'twelveHours',
    name: '近12小时',
  },
  {
    key: 'roundClock',
    name: '近24小时',
  },
  {
    key: 'yesterday',
    name: '昨天',
  },
  {
    key: 'customer',
    name: '自定义',
  },
];