forked from shesha-io/shesha-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatePickerWrapper.tsx
More file actions
227 lines (189 loc) · 7.44 KB
/
datePickerWrapper.tsx
File metadata and controls
227 lines (189 loc) · 7.44 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
225
226
227
import { DatePicker } from '@/components/antd';
import moment, { isMoment, Moment } from 'moment';
import React, { CSSProperties, FC, useMemo, useRef } from 'react';
import ReadOnlyDisplayFormItem from '@/components/readOnlyDisplayFormItem';
import { useForm, useGlobalState, useMetadata } from '@/providers';
import { getMoment, getRangeMoment } from '@/utils/date';
import { getDataProperty } from '@/utils/metadata';
import { IDateFieldProps, RangePickerChangeEvent, TimePickerChangeEvent } from './interfaces';
import { DATE_TIME_FORMATS, disabledDate, disabledTime, getFormat } from './utils';
import { asPropertiesArray } from '@/interfaces/metadata';
import { useStyles } from './style';
const MIDNIGHT_MOMENT = moment('00:00:00', 'HH:mm:ss');
const { RangePicker } = DatePicker;
export const DatePickerWrapper: FC<IDateFieldProps> = (props) => {
const { properties: metaProperties } = useMetadata(false)?.metadata ?? {};
const properties = asPropertiesArray(metaProperties, []);
const { globalState } = useGlobalState();
const {
propertyName: name,
hideBorder,
range,
value,
showTime,
showNow,
onChange,
picker = 'date',
readOnly,
additionalStyles,
defaultToMidnight,
resolveToUTC,
} = props;
const dateFormat = props?.dateFormat || getDataProperty(properties, name, 'dataFormat') || DATE_TIME_FORMATS.date;
const timeFormat = props?.timeFormat || DATE_TIME_FORMATS.time;
const { styles } = useStyles({ fullStyles: additionalStyles });
const finalStyles: CSSProperties = { ...additionalStyles };
const { formData } = useForm();
const pickerFormat = getFormat(props, properties);
const convertValue = (localValue: any): string => {
const newValue = isMoment(localValue) ? localValue : getMoment(localValue, pickerFormat);
const val =
picker === 'week'
? newValue.startOf('week')
: picker === 'month'
? newValue.startOf('month')
: picker === 'quarter'
? newValue.startOf('quarter')
: picker === 'year'
? newValue.startOf('year')
: !showTime
? newValue.startOf('day')
: newValue;
const finalMoment = resolveToUTC ? val.clone().utc() : val.clone().local();
return resolveToUTC ? finalMoment.toISOString() : finalMoment.format('YYYY-MM-DDTHH:mm:ss.SSS');
};
const handleDatePickerChange = (localValue: any | null, dateString: string): void => {
if (!dateString?.trim()) {
(onChange as TimePickerChangeEvent)(null, '');
return;
}
const newValue = convertValue(localValue);
(onChange as TimePickerChangeEvent)(newValue, dateString);
};
const handleRangePicker = (values: any[], formatString: [string, string]): void => {
if (formatString?.includes('')) {
(onChange as RangePickerChangeEvent)(null, null);
return;
}
const dates = (values as []).map((val: any) => convertValue(val));
(onChange as RangePickerChangeEvent)(dates, formatString);
};
const prevDatePartRef = useRef(null);
const handleCalendarDatePickerChange = (dates: Moment | Moment[]): void => {
if (!dates || Array.isArray(dates)) return;
const getDatePart = (date: Moment): string => date.format('YYYY-MM-DD');
const newDatePart = getDatePart(dates);
const prevDatePart = prevDatePartRef.current;
let newDate;
if (newDatePart !== prevDatePart) {
// Date part changed — override time with current system time
const now = moment();
newDate = dates.clone().set({
hour: now.hour(),
minute: now.minute(),
second: now.second(),
});
} else {
// Date part did not change — user changed the time, keep it as is
newDate = dates;
}
prevDatePartRef.current = newDatePart;
handleDatePickerChange(newDate, newDate.format(pickerFormat));
};
const momentValue = useMemo(() => getMoment(value, pickerFormat), [value, pickerFormat]);
const rangeMomentValue = useMemo(() => getRangeMoment(value, pickerFormat), [value, pickerFormat]);
const prevStartDatePartRef = useRef(null);
const prevEndDatePartRef = useRef(null);
const handleCalendarRangeChange = (dates: Moment[]): void => {
if (!dates) return;
const [start, end] = dates;
const getDatePart = (date: Moment | undefined): string => date?.format('YYYY-MM-DD');
const startDatePart = getDatePart(start);
const endDatePart = getDatePart(end);
let newStart = start;
let newEnd = end;
/* start and end date parts are used to determine if the user has changed the date part of the date
if the date part has changed, we override the time with the current system time
if the date part has not changed, we keep the time as it is */
if (start) {
const prevStartDatePart = prevStartDatePartRef.current;
if (startDatePart !== prevStartDatePart) {
const nowForStart = moment();
newStart = start.clone().set({
hour: nowForStart.hour(),
minute: nowForStart.minute(),
second: nowForStart.second(),
});
}
prevStartDatePartRef.current = startDatePart;
}
if (end) {
const prevEndDatePart = prevEndDatePartRef.current;
if (endDatePart !== prevEndDatePart) {
const nowForEnd = moment();
newEnd = end.clone().set({
hour: nowForEnd.hour(),
minute: nowForEnd.minute(),
second: nowForEnd.second(),
});
}
prevEndDatePartRef.current = endDatePart;
}
const newDates = [newStart, newEnd];
handleRangePicker(
newDates,
[
newStart?.format(pickerFormat),
newEnd?.format(pickerFormat),
],
);
};
if (range) {
return (
<div style={{ marginRight: 1 }}>
<RangePicker
onCalendarChange={(dates) => {
if (dates && showTime && !defaultToMidnight) handleCalendarRangeChange(dates);
}}
className="sha-range-picker"
disabledDate={(e) => disabledDate(props, e, formData, globalState)}
disabledTime={disabledTime(props, formData, globalState)}
onChange={handleRangePicker}
format={pickerFormat}
value={rangeMomentValue}
picker={picker}
showTime={showTime ? (defaultToMidnight ? { defaultValue: [MIDNIGHT_MOMENT, MIDNIGHT_MOMENT] } : true) : false}
disabled={readOnly}
style={finalStyles}
allowClear
variant={hideBorder ? 'borderless' : undefined}
/>
</div>
);
}
if (readOnly) {
const format = showTime ? `${dateFormat} ${timeFormat}` : dateFormat;
return <ReadOnlyDisplayFormItem value={momentValue} type="datetime" dateFormat={format} timeFormat={timeFormat} style={finalStyles} />;
}
return (
<div style={{ marginRight: 1 }}>
<DatePicker
className={styles.dateField}
disabledDate={(e) => disabledDate(props, e, formData, globalState)}
disabledTime={disabledTime(props, formData, globalState)}
onChange={handleDatePickerChange}
variant={hideBorder ? 'borderless' : undefined}
showTime={showTime ? (defaultToMidnight ? { defaultValue: MIDNIGHT_MOMENT } : true) : false}
showNow={showNow}
picker={picker}
format={pickerFormat}
style={{ ...finalStyles }}
onCalendarChange={(dates) => {
if (dates && showTime && !defaultToMidnight) handleCalendarDatePickerChange(dates);
}}
value={momentValue}
allowClear
/>
</div>
);
};