index.tsx
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
68
69
70
71
import React from 'react';
import { FormItem, DatePicker } from '@formily/antd-v5';
import { Field } from '@formily/react';
import cx from 'classnames';
import './index.less';
import { FieldProps } from '../../typings';
import moment from 'moment';
const { RangePicker } = DatePicker;
const RangePickerWrap = (props: any) => {
const { value, onChange, unix } = props;
const [stateValue, setStateValue] = React.useState<any>([]);
React.useEffect(() => {
if (value && value.length && unix) {
setStateValue([moment(value[0] * 1000), moment(value[1] * 1000)]);
return;
}
setStateValue(value);
}, [value]);
const handleChange = (value: any) => {
const [start, end] = value || [];
if (!value) {
onChange([]);
return;
}
if (unix) {
onChange([moment(start).unix(), moment(end).unix()]);
} else {
onChange(value);
}
};
return <RangePicker {...props} onChange={handleChange} value={stateValue} />;
};
const RangeDate: React.FC<FieldProps> = (props) => {
const { name, title, validator = [], format, decoratorProps, componentProps } = props;
return (
<div className={cx('global_rangedate')}>
<Field
{...props}
name={name}
title={title}
decorator={[
FormItem,
{
...decoratorProps,
},
]}
component={[
RangePickerWrap,
{
allowClear: true,
componenttypename: 'DatePicker.RangePicker',
format: format || ['YYYY/MM/DD', 'YYYY/MM/DD'],
...componentProps,
},
]}
validator={validator}
/>
</div>
);
};
export default RangeDate;