|
@@ -1,5 +1,5 @@
|
1
|
1
|
import React, { useState } from 'react';
|
2
|
|
-import { Card, DatePicker, Button } from 'antd';
|
|
2
|
+import { Radio, DatePicker, Button } from 'antd';
|
3
|
3
|
import * as dayjs from 'dayjs';
|
4
|
4
|
// import * as isLeapYear from 'dayjs/plugin/isLeapYear' // 导入插件
|
5
|
5
|
// import 'dayjs/locale/zh-cn' // 导入本地化语言
|
|
@@ -9,53 +9,60 @@ export default (props) => {
|
9
|
9
|
const { onChange } = props;
|
10
|
10
|
|
11
|
11
|
const { RangePicker } = DatePicker;
|
12
|
|
- const [date, setDate] = useState([]);
|
|
12
|
+ const [pickerValue, setPickerValue] = useState([]);
|
|
13
|
+ const [dtType, setDtType] = useState('2');
|
|
14
|
+ const dtTypeRef = React.useRef(dtType);
|
13
|
15
|
|
14
|
|
- const updateDate = (dt, typ) => {
|
15
|
|
- if (onChange) {
|
16
|
|
- onChange(dt, typ);
|
17
|
|
- }
|
18
|
|
-
|
19
|
|
- setDate(dt);
|
|
16
|
+ const onRadioChange = e => {
|
|
17
|
+ const value = e.target.value;
|
|
18
|
+ setDtType(value);
|
|
19
|
+ dtTypeRef.current = value;
|
|
20
|
+ console.log('dtTypeRef.current', dtTypeRef.current)
|
20
|
21
|
}
|
21
|
22
|
|
22
|
|
- const onWeek = () => {
|
23
|
|
- updateDate(
|
24
|
|
- [
|
25
|
|
- dayjs().startOf('week').add(1, 'day'),
|
26
|
|
- dayjs().endOf('week').add(1, 'day')
|
27
|
|
- ],
|
28
|
|
- 1
|
29
|
|
- );
|
30
|
|
- }
|
31
|
23
|
|
32
|
|
- const onMonth = () => {
|
33
|
|
- updateDate([
|
34
|
|
- dayjs().startOf('month'),
|
35
|
|
- dayjs().endOf('month')
|
36
|
|
- ], 2)
|
37
|
|
- // console.log('fangfang', dayjs().endOf('month'));
|
|
24
|
+ const onPickerChange = e => {
|
|
25
|
+ setPickerValue(e);
|
38
|
26
|
}
|
39
|
27
|
|
40
|
|
- const onYear = () => {
|
41
|
|
- updateDate([
|
42
|
|
- dayjs().startOf('year'),
|
43
|
|
- dayjs().endOf('year')
|
44
|
|
- ], 3)
|
45
|
|
- // console.log('fangfang', dayjs().endOf('year'));
|
46
|
|
- }
|
|
28
|
+ React.useEffect(() => {
|
|
29
|
+ if (dtType == 1) {
|
|
30
|
+ setPickerValue(
|
|
31
|
+ [
|
|
32
|
+ dayjs().startOf('week').add(1, 'day'),
|
|
33
|
+ dayjs().endOf('week').add(1, 'day')
|
|
34
|
+ ]
|
|
35
|
+ );
|
|
36
|
+ } else if (dtType == 2) {
|
|
37
|
+ setPickerValue([
|
|
38
|
+ dayjs().startOf('month'),
|
|
39
|
+ dayjs().endOf('month')
|
|
40
|
+ ])
|
|
41
|
+ } else if (dtType == 3) {
|
|
42
|
+ setPickerValue([
|
|
43
|
+ dayjs().startOf('year'),
|
|
44
|
+ dayjs().endOf('year')
|
|
45
|
+ ])
|
|
46
|
+ }
|
|
47
|
+ }, [dtType]);
|
47
|
48
|
|
48
|
|
- const onRange = (e) => {
|
49
|
|
- updateDate(e, 0);
|
50
|
|
- }
|
|
49
|
+ React.useEffect(() => {
|
|
50
|
+ if (pickerValue && pickerValue.length > 1 && onChange) {
|
|
51
|
+ onChange(pickerValue, dtTypeRef.current);
|
|
52
|
+ }
|
|
53
|
+ }, [pickerValue]);
|
51
|
54
|
|
52
|
|
- // console.log('data', data)
|
53
|
55
|
return (
|
54
|
56
|
<div>
|
55
|
|
- <Button onClick={onWeek} type="text">本周</Button>
|
56
|
|
- <Button onClick={onMonth} type="text">本月</Button>
|
57
|
|
- <Button onClick={onYear} type="text">本年</Button>
|
58
|
|
- <RangePicker onChange={onRange} value={date} />
|
|
57
|
+ <Radio.Group value={dtType} onChange={onRadioChange} style={{ marginRight: '20px' }}>
|
|
58
|
+ <Radio.Button value="1" type="text">本周</Radio.Button>
|
|
59
|
+ <Radio.Button value="2" type="text">本月</Radio.Button>
|
|
60
|
+ <Radio.Button value="3" type="text">本年</Radio.Button>
|
|
61
|
+ </Radio.Group>
|
|
62
|
+ <RangePicker
|
|
63
|
+ value={pickerValue}
|
|
64
|
+ onChange={onPickerChange}
|
|
65
|
+ />
|
59
|
66
|
</div>
|
60
|
67
|
)
|
61
|
68
|
}
|