@dvrd/dvr-controls 1.0.49 → 1.0.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +0 -1
- package/package.json +2 -2
- package/src/js/date/dvrdDatePicker.tsx +26 -27
- package/src/js/input/date/dateField.tsx +42 -33
- package/src/js/input/date/dateFieldController.tsx +16 -24
- package/src/js/input/date/datePicker/datePicker.tsx +16 -15
- package/src/js/input/date/input/dateInput.tsx +7 -7
- package/src/js/util/momentUtil.ts +0 -54
package/index.ts
CHANGED
|
@@ -124,7 +124,6 @@ export * from './src/js/util/miscUtil';
|
|
|
124
124
|
export * from './src/js/util/componentUtil';
|
|
125
125
|
export * from './src/js/util/eventUtil';
|
|
126
126
|
export * from './src/js/util/jwtUtil';
|
|
127
|
-
export * from './src/js/util/momentUtil';
|
|
128
127
|
export * from './src/js/util/responsiveUtil';
|
|
129
128
|
export * from './src/js/util/validationUtil';
|
|
130
129
|
export * from './src/js/util/interfaces';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dvrd/dvr-controls",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.51",
|
|
4
4
|
"description": "Custom web controls",
|
|
5
5
|
"main": "index.ts",
|
|
6
6
|
"files": [
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"react-router-dom": "6.15.0"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
+
"@dvrd/idate": "^1.6.1",
|
|
33
34
|
"@fortawesome/fontawesome-svg-core": "6.3.0",
|
|
34
35
|
"@fortawesome/free-brands-svg-icons": "6.3.0",
|
|
35
36
|
"@fortawesome/free-regular-svg-icons": "6.3.0",
|
|
@@ -55,7 +56,6 @@
|
|
|
55
56
|
"lodash.isequal": "^4.5.0",
|
|
56
57
|
"lodash.merge": "^4.6.2",
|
|
57
58
|
"lodash.mergewith": "^4.6.2",
|
|
58
|
-
"moment": "2.29.4",
|
|
59
59
|
"react-color": "2.19.3",
|
|
60
60
|
"react-rnd": "10.4.1",
|
|
61
61
|
"typescript": "4.9.5",
|
|
@@ -4,19 +4,18 @@
|
|
|
4
4
|
import './style/dvrdDatePicker.scss';
|
|
5
5
|
|
|
6
6
|
import classNames from 'classnames';
|
|
7
|
-
import {Moment} from 'moment';
|
|
8
7
|
import React, {MouseEventHandler, useEffect, useMemo, useRef, useState} from 'react';
|
|
9
8
|
import {ChangeFunction, ErrorType} from '../util/interfaces';
|
|
10
9
|
import AwesomeIcon from '../icon/awesomeIcon';
|
|
11
|
-
import {toMoment} from '../util/momentUtil';
|
|
12
10
|
import WithBackground from '../popup/withBackground';
|
|
13
11
|
import DvrdButton from "../button/dvrdButton";
|
|
14
12
|
import {generateComponentId} from "../util/componentUtil";
|
|
13
|
+
import IDate from '@dvrd/idate';
|
|
15
14
|
|
|
16
15
|
interface Props {
|
|
17
|
-
onChange: ChangeFunction<
|
|
16
|
+
onChange: ChangeFunction<IDate>;
|
|
18
17
|
closeOnChange?: boolean;
|
|
19
|
-
value:
|
|
18
|
+
value: IDate | null;
|
|
20
19
|
label: string;
|
|
21
20
|
error?: ErrorType;
|
|
22
21
|
className?: string;
|
|
@@ -26,8 +25,8 @@ interface Props {
|
|
|
26
25
|
alwaysShowArrows?: boolean;
|
|
27
26
|
useMobileNative?: boolean;
|
|
28
27
|
id?: string;
|
|
29
|
-
max?:
|
|
30
|
-
min?:
|
|
28
|
+
max?: IDate | string;
|
|
29
|
+
min?: IDate | string;
|
|
31
30
|
}
|
|
32
31
|
|
|
33
32
|
export default function DvrdDatePicker(props: Props) {
|
|
@@ -44,7 +43,7 @@ export default function DvrdDatePicker(props: Props) {
|
|
|
44
43
|
}, [props.id]);
|
|
45
44
|
|
|
46
45
|
function onChangeNative(evt: React.ChangeEvent<HTMLInputElement>) {
|
|
47
|
-
onChange(
|
|
46
|
+
onChange(new IDate(evt.target.value), evt);
|
|
48
47
|
}
|
|
49
48
|
|
|
50
49
|
function onClickContainer() {
|
|
@@ -52,7 +51,7 @@ export default function DvrdDatePicker(props: Props) {
|
|
|
52
51
|
setPickerOpen(true);
|
|
53
52
|
}
|
|
54
53
|
|
|
55
|
-
const onChangePicker = (daySelected: boolean) => (value:
|
|
54
|
+
const onChangePicker = (daySelected: boolean) => (value: IDate) => {
|
|
56
55
|
onChange(value.clone());
|
|
57
56
|
if (closeOnChange && daySelected) onClosePicker();
|
|
58
57
|
};
|
|
@@ -89,33 +88,33 @@ export default function DvrdDatePicker(props: Props) {
|
|
|
89
88
|
}
|
|
90
89
|
|
|
91
90
|
interface PickerProps {
|
|
92
|
-
onChange: (daySelected: boolean) => ChangeFunction<
|
|
91
|
+
onChange: (daySelected: boolean) => ChangeFunction<IDate>;
|
|
93
92
|
onClose: VoidFunction;
|
|
94
|
-
value:
|
|
93
|
+
value: IDate | null;
|
|
95
94
|
open: boolean;
|
|
96
95
|
alwaysShowArrows?: boolean;
|
|
97
|
-
max?:
|
|
98
|
-
min?:
|
|
96
|
+
max?: IDate | string;
|
|
97
|
+
min?: IDate | string;
|
|
99
98
|
}
|
|
100
99
|
|
|
101
100
|
function Picker(props: PickerProps) {
|
|
102
101
|
const {min, max, alwaysShowArrows, open, onClose, value, onChange} = props;
|
|
103
102
|
const divRef = useRef<HTMLDivElement>(null);
|
|
104
|
-
const pickerValue = useMemo(() =>
|
|
103
|
+
const pickerValue = useMemo(() => new IDate(value), [value]);
|
|
105
104
|
const [padBeforeDays, days, padAfterDays] = useMemo(() => {
|
|
106
105
|
const daysInMonth = pickerValue.daysInMonth();
|
|
107
106
|
const days: number[] = [];
|
|
108
107
|
const padBeforeDays: number[] = [];
|
|
109
108
|
const padAfterDays: number[] = [];
|
|
110
109
|
for (let i = 1; i <= daysInMonth; i++) days.push(i);
|
|
111
|
-
let firstMonthDay = pickerValue.clone().
|
|
110
|
+
let firstMonthDay = pickerValue.clone().day(1).weekday();
|
|
112
111
|
if (!firstMonthDay) firstMonthDay = 7;
|
|
113
112
|
const padDaysBefore = firstMonthDay - 1;
|
|
114
113
|
let dayBefore = pickerValue.clone().add(-1, 'month').daysInMonth()
|
|
115
114
|
for (let i = 0; i < padDaysBefore; i++)
|
|
116
115
|
padBeforeDays.splice(0, 0, dayBefore--);
|
|
117
116
|
|
|
118
|
-
const lastWeekDay = pickerValue.clone().
|
|
117
|
+
const lastWeekDay = pickerValue.clone().day(daysInMonth).weekday();
|
|
119
118
|
const padDaysAfter = 7 - lastWeekDay;
|
|
120
119
|
for (let i = 0; i < padDaysAfter; i++)
|
|
121
120
|
padAfterDays.push(i + 1);
|
|
@@ -132,7 +131,7 @@ function Picker(props: PickerProps) {
|
|
|
132
131
|
return function () {
|
|
133
132
|
const value = pickerValue.clone();
|
|
134
133
|
value.add(-1, key);
|
|
135
|
-
value.
|
|
134
|
+
value.day(Math.min(value.daysInMonth(), value.day()))
|
|
136
135
|
onChange(false)(value);
|
|
137
136
|
}
|
|
138
137
|
}
|
|
@@ -141,7 +140,7 @@ function Picker(props: PickerProps) {
|
|
|
141
140
|
return function () {
|
|
142
141
|
const value = pickerValue.clone();
|
|
143
142
|
value.add(1, key);
|
|
144
|
-
value.
|
|
143
|
+
value.day(Math.min(value.daysInMonth(), value.day()))
|
|
145
144
|
onChange(false)(value);
|
|
146
145
|
}
|
|
147
146
|
}
|
|
@@ -149,22 +148,22 @@ function Picker(props: PickerProps) {
|
|
|
149
148
|
function onResetSwitcher(key: 'year' | 'month') {
|
|
150
149
|
return function () {
|
|
151
150
|
const value = pickerValue.clone();
|
|
152
|
-
if (key === 'year') value.year(
|
|
153
|
-
else value.month(
|
|
154
|
-
value.
|
|
151
|
+
if (key === 'year') value.year(IDate.now().year());
|
|
152
|
+
else value.month(IDate.now().month());
|
|
153
|
+
value.day(Math.min(value.daysInMonth(), value.day()))
|
|
155
154
|
onChange(false)(value);
|
|
156
155
|
}
|
|
157
156
|
}
|
|
158
157
|
|
|
159
158
|
function onClickToday() {
|
|
160
|
-
onChange(true)(
|
|
159
|
+
onChange(true)(IDate.now());
|
|
161
160
|
}
|
|
162
161
|
|
|
163
162
|
function onSelectDay(day: number, month?: number, year?: number) {
|
|
164
163
|
return function () {
|
|
165
164
|
const pickValue = pickerValue.clone();
|
|
166
165
|
if (day || value === null) {
|
|
167
|
-
pickValue.
|
|
166
|
+
pickValue.day(day);
|
|
168
167
|
if (month !== undefined) pickValue.month(month);
|
|
169
168
|
if (year !== undefined) pickValue.year(year);
|
|
170
169
|
onChange(true)(pickValue);
|
|
@@ -177,7 +176,7 @@ function Picker(props: PickerProps) {
|
|
|
177
176
|
const value = pickerValue.clone();
|
|
178
177
|
if (kind === 'prev') value.add(-1, 'month');
|
|
179
178
|
else value.add(1, 'month');
|
|
180
|
-
value.
|
|
179
|
+
value.day(day);
|
|
181
180
|
onChange(true)(value);
|
|
182
181
|
}
|
|
183
182
|
}
|
|
@@ -234,11 +233,11 @@ function Picker(props: PickerProps) {
|
|
|
234
233
|
function dateIsDisabled(day: number, month?: number, year?: number) {
|
|
235
234
|
if (!min && !max) return false;
|
|
236
235
|
const pickValue = pickerValue.clone();
|
|
237
|
-
pickValue.
|
|
236
|
+
pickValue.day(day);
|
|
238
237
|
if (month !== undefined) pickValue.month(month);
|
|
239
238
|
if (year !== undefined) pickValue.year(year);
|
|
240
|
-
if (min &&
|
|
241
|
-
else if (max &&
|
|
239
|
+
if (min && new IDate(min).isAfter(pickValue, 'day')) return true;
|
|
240
|
+
else if (max && new IDate(max).isBefore(pickValue, 'day')) return true;
|
|
242
241
|
return false;
|
|
243
242
|
}
|
|
244
243
|
|
|
@@ -387,7 +386,7 @@ function Picker(props: PickerProps) {
|
|
|
387
386
|
return renderDay(day, index, onSelectPadDay(day, 'prev'), 'pad', 'prev');
|
|
388
387
|
})}
|
|
389
388
|
{days.map((day: number, index: number) => {
|
|
390
|
-
const isSelected = day === pickerValue.
|
|
389
|
+
const isSelected = day === pickerValue.day();
|
|
391
390
|
return renderDay(day, index, onSelectDay(day),
|
|
392
391
|
classNames(isSelected && 'selected', day === 0 && 'hide'));
|
|
393
392
|
})}
|
|
@@ -8,7 +8,6 @@ import React, {ChangeEvent, CSSProperties, KeyboardEventHandler, PureComponent}
|
|
|
8
8
|
import {ChangeFunction, ControlVariant} from "../../util/interfaces";
|
|
9
9
|
import {DateTimeParts, DateType} from "./dateFieldController";
|
|
10
10
|
import classNames from "classnames";
|
|
11
|
-
import {Moment} from 'moment';
|
|
12
11
|
import {ControlContext} from "../../util/controlContext";
|
|
13
12
|
import {BACKSPACE_CODE} from "../../util/constants";
|
|
14
13
|
import DatePicker from "./datePicker/datePicker";
|
|
@@ -17,11 +16,11 @@ import DateInput from "./input/dateInput";
|
|
|
17
16
|
import {DVRInputControllerProps} from "../v2/inputController_v2";
|
|
18
17
|
import mergeWith from 'lodash.mergewith';
|
|
19
18
|
import {colorIsWhite, convertColor, editColor} from '../../util/colorUtil';
|
|
20
|
-
import {toMoment} from '../../util/momentUtil';
|
|
21
19
|
import {pad} from '../../util/controlUtil';
|
|
22
20
|
import AwesomeIcon from "../../icon/awesomeIcon";
|
|
23
21
|
import WithBackground from '../../popup/withBackground';
|
|
24
22
|
import ButtonController from "../../button/buttonController";
|
|
23
|
+
import IDate, {IDateValue} from '@dvrd/idate';
|
|
25
24
|
|
|
26
25
|
export interface DateNumParts {
|
|
27
26
|
day: number;
|
|
@@ -40,8 +39,8 @@ interface Props extends DVRInputControllerProps {
|
|
|
40
39
|
pickerClass: string;
|
|
41
40
|
disableFuture: boolean;
|
|
42
41
|
disablePast: boolean;
|
|
43
|
-
disableAfter?:
|
|
44
|
-
disableBefore?:
|
|
42
|
+
disableAfter?: IDateValue;
|
|
43
|
+
disableBefore?: IDateValue;
|
|
45
44
|
value: string;
|
|
46
45
|
mask: string;
|
|
47
46
|
}
|
|
@@ -102,12 +101,12 @@ export default class DateField extends PureComponent<Props, State> {
|
|
|
102
101
|
day: Math.min(pickerValue.day, currentMoment.daysInMonth()),
|
|
103
102
|
month: currentMoment.month(),
|
|
104
103
|
year: currentMoment.year(),
|
|
105
|
-
hour: currentMoment.
|
|
106
|
-
minute: currentMoment.
|
|
104
|
+
hour: currentMoment.hours(),
|
|
105
|
+
minute: currentMoment.minutes(),
|
|
107
106
|
};
|
|
108
107
|
} else {
|
|
109
108
|
nextValue = Object.assign({}, this.state.pickerValue, {[key]: value});
|
|
110
|
-
const nextMoment =
|
|
109
|
+
const nextMoment = IDate.now().month(nextValue.month).year(nextValue.year).day(1),
|
|
111
110
|
maxDays = nextMoment.daysInMonth();
|
|
112
111
|
if (nextValue.day > maxDays)
|
|
113
112
|
nextValue.day = maxDays;
|
|
@@ -202,13 +201,13 @@ export default class DateField extends PureComponent<Props, State> {
|
|
|
202
201
|
};
|
|
203
202
|
|
|
204
203
|
onClickToday = () => {
|
|
205
|
-
const today =
|
|
204
|
+
const today = IDate.now();
|
|
206
205
|
this.setState({
|
|
207
206
|
pickerValue: {
|
|
208
207
|
month: today.month(),
|
|
209
|
-
day: today.
|
|
210
|
-
minute: today.
|
|
211
|
-
hour: today.
|
|
208
|
+
day: today.day(),
|
|
209
|
+
minute: today.minutes(),
|
|
210
|
+
hour: today.hours(),
|
|
212
211
|
year: today.year(),
|
|
213
212
|
}
|
|
214
213
|
}, () => {
|
|
@@ -223,12 +222,12 @@ export default class DateField extends PureComponent<Props, State> {
|
|
|
223
222
|
this.setState({pickerState: nextState});
|
|
224
223
|
};
|
|
225
224
|
|
|
226
|
-
onChangeInput = (value:
|
|
225
|
+
onChangeInput = (value: IDate) => {
|
|
227
226
|
this.setState({
|
|
228
227
|
pickerValue: Object.assign({}, this.state.pickerValue, {
|
|
229
|
-
day: value.
|
|
230
|
-
hour: value.
|
|
231
|
-
minute: value.
|
|
228
|
+
day: value.day(),
|
|
229
|
+
hour: value.hours(),
|
|
230
|
+
minute: value.minutes(),
|
|
232
231
|
month: value.month(),
|
|
233
232
|
year: value.year()
|
|
234
233
|
})
|
|
@@ -244,14 +243,15 @@ export default class DateField extends PureComponent<Props, State> {
|
|
|
244
243
|
};
|
|
245
244
|
|
|
246
245
|
getInitPickerValue = () => {
|
|
247
|
-
const dateParts = Object.assign({}, this.props.dateParts)
|
|
246
|
+
const dateParts = Object.assign({}, this.props.dateParts);
|
|
247
|
+
const now = IDate.now();
|
|
248
248
|
if (!isNaN(Number(dateParts.month))) dateParts.month = (Number(dateParts.month) - 1).toString();
|
|
249
249
|
return mergeWith(dateParts, {
|
|
250
|
-
day: now.
|
|
250
|
+
day: now.day(),
|
|
251
251
|
month: now.month(),
|
|
252
252
|
year: now.year(),
|
|
253
|
-
hour: now.
|
|
254
|
-
minute: now.
|
|
253
|
+
hour: now.hours(),
|
|
254
|
+
minute: now.minutes(),
|
|
255
255
|
}, (source1: string, source2: number) => {
|
|
256
256
|
if (!source1 || source1.length === 0 || ['dd', 'mm', 'yyyy', 'hh'].includes(source1)) return source2;
|
|
257
257
|
return Number(source1);
|
|
@@ -259,7 +259,9 @@ export default class DateField extends PureComponent<Props, State> {
|
|
|
259
259
|
};
|
|
260
260
|
|
|
261
261
|
scrollToCurrent = () => {
|
|
262
|
-
const currentMoment = this.getCurrentMoment()
|
|
262
|
+
const currentMoment = this.getCurrentMoment();
|
|
263
|
+
const hour = currentMoment.hours();
|
|
264
|
+
const minute = currentMoment.minutes();
|
|
263
265
|
this.scrollTime('hour', hour);
|
|
264
266
|
this.scrollTime('minute', minute);
|
|
265
267
|
};
|
|
@@ -276,19 +278,26 @@ export default class DateField extends PureComponent<Props, State> {
|
|
|
276
278
|
return color;
|
|
277
279
|
};
|
|
278
280
|
|
|
279
|
-
getCurrentMoment = (value?: DateNumParts):
|
|
280
|
-
const {pickerValue} = this.state
|
|
281
|
-
|
|
281
|
+
getCurrentMoment = (value?: DateNumParts): IDate => {
|
|
282
|
+
const {pickerValue} = this.state;
|
|
283
|
+
const toUse: DateNumParts = value !== undefined ? value : pickerValue;
|
|
284
|
+
return IDate.now().set({
|
|
285
|
+
year: toUse.year,
|
|
286
|
+
month: toUse.month,
|
|
287
|
+
day: toUse.day,
|
|
288
|
+
hours: toUse.hour,
|
|
289
|
+
minutes: toUse.minute
|
|
290
|
+
});
|
|
282
291
|
};
|
|
283
292
|
|
|
284
|
-
getInputValue = ():
|
|
293
|
+
getInputValue = (): IDate | null => {
|
|
285
294
|
const {value, dateType} = this.props;
|
|
286
295
|
if (value.length) {
|
|
287
296
|
if (dateType === DateType.DATE)
|
|
288
|
-
return
|
|
297
|
+
return new IDate(value, 'DD-MM-YYYY');
|
|
289
298
|
if (dateType === DateType.DATETIME)
|
|
290
|
-
return
|
|
291
|
-
return
|
|
299
|
+
return new IDate(value, 'DD-MM-YYYY HH:mm');
|
|
300
|
+
return new IDate(value, 'HH:mm');
|
|
292
301
|
}
|
|
293
302
|
return null;
|
|
294
303
|
};
|
|
@@ -306,19 +315,19 @@ export default class DateField extends PureComponent<Props, State> {
|
|
|
306
315
|
return (
|
|
307
316
|
<div className={classNames('dvr-datepicker-container', pickerClass)}>
|
|
308
317
|
{pickerState === PickerState.DATE &&
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
318
|
+
<DatePicker onChange={this.onChangeInternalValue} getCurrentMoment={this.getCurrentMoment}
|
|
319
|
+
pickerValue={pickerValue} dateParts={dateParts} disableAfter={disableAfter}
|
|
320
|
+
disableBefore={disableBefore} disableFuture={disableFuture} disablePast={disablePast}/>}
|
|
312
321
|
{pickerState === PickerState.TIME &&
|
|
313
|
-
|
|
322
|
+
<TimePicker pickerValue={pickerValue} dateType={dateType} onChange={this.onChangeTime}/>}
|
|
314
323
|
{dateType === DateType.DATETIME && this.renderStateSelector()}
|
|
315
324
|
<div className='dvr-datepicker-footer'>
|
|
316
325
|
<ButtonController onClick={this.onClickToday} label={this.getTodayText()}
|
|
317
326
|
type={ControlVariant.SIMPLE}
|
|
318
327
|
primary={false}/>
|
|
319
328
|
{dateType != DateType.DATE &&
|
|
320
|
-
|
|
321
|
-
|
|
329
|
+
<ButtonController onClick={this.onSubmit} label='Oké' type={ControlVariant.SIMPLE}
|
|
330
|
+
primary={false}/>}
|
|
322
331
|
</div>
|
|
323
332
|
</div>
|
|
324
333
|
)
|
|
@@ -3,17 +3,16 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import React, {PureComponent} from 'react';
|
|
6
|
-
import moment, {Moment} from 'moment';
|
|
7
6
|
import {ControlVariant} from "../../util/interfaces";
|
|
8
7
|
import DateField from "./dateField";
|
|
9
8
|
import {DefaultInputProps, DVRInputControllerProps} from "../v2/inputController_v2";
|
|
10
|
-
import {
|
|
9
|
+
import IDate, {IDateValue} from '@dvrd/idate';
|
|
11
10
|
|
|
12
11
|
export enum DateType {DATETIME, DATE, TIME}
|
|
13
12
|
|
|
14
|
-
export enum DateReturnType {DATE,
|
|
13
|
+
export enum DateReturnType {DATE, IDATE, STRING}
|
|
15
14
|
|
|
16
|
-
interface Props<T extends
|
|
15
|
+
interface Props<T extends IDateValue = string> extends Omit<DVRInputControllerProps, 'value' | 'onChange'> {
|
|
17
16
|
onChange: (value: T) => void;
|
|
18
17
|
dateType: DateType;
|
|
19
18
|
disableFuture: boolean;
|
|
@@ -37,7 +36,7 @@ export interface DateTimeParts {
|
|
|
37
36
|
minute: string;
|
|
38
37
|
}
|
|
39
38
|
|
|
40
|
-
export default class DateFieldController<T extends
|
|
39
|
+
export default class DateFieldController<T extends IDateValue> extends PureComponent<Props<T>, State> {
|
|
41
40
|
static defaultProps = Object.assign(DefaultInputProps, {
|
|
42
41
|
dateType: DateType.DATE,
|
|
43
42
|
disableFuture: false,
|
|
@@ -76,20 +75,13 @@ export default class DateFieldController<T extends string | Moment | Date> exten
|
|
|
76
75
|
}
|
|
77
76
|
|
|
78
77
|
private getInitValue = (props: Props<T>): string => {
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
if (!value
|
|
78
|
+
const {dateType} = props;
|
|
79
|
+
const value = props.value;
|
|
80
|
+
if (!value) return '';
|
|
81
|
+
if (typeof value === 'string' && !value.length) {
|
|
82
82
|
return '';
|
|
83
83
|
}
|
|
84
|
-
|
|
85
|
-
if (moment.isMoment(value)) momentValue = value;
|
|
86
|
-
else {
|
|
87
|
-
if (value instanceof Date) momentValue = moment(value);
|
|
88
|
-
else {
|
|
89
|
-
momentValue = toMoment(value);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
} else momentValue = moment();
|
|
84
|
+
const momentValue = new IDate(value);
|
|
93
85
|
let format: string;
|
|
94
86
|
if (dateType === DateType.DATE) format = 'DD-MM-YYYY';
|
|
95
87
|
else if (dateType === DateType.DATETIME) format = 'DD-MM-YYYY HH:mm';
|
|
@@ -159,10 +151,10 @@ export default class DateFieldController<T extends string | Moment | Date> exten
|
|
|
159
151
|
this.setState({value});
|
|
160
152
|
// Only call onChange when the date is validly (only containing numbers)
|
|
161
153
|
if (!/[a-zA-Z]/.test(value)) {
|
|
162
|
-
let momentValue:
|
|
163
|
-
if (dateType === DateType.DATE) momentValue =
|
|
164
|
-
else if (dateType === DateType.TIME) momentValue =
|
|
165
|
-
else momentValue =
|
|
154
|
+
let momentValue: IDate;
|
|
155
|
+
if (dateType === DateType.DATE) momentValue = new IDate(value, 'DD-MM-YYYY');
|
|
156
|
+
else if (dateType === DateType.TIME) momentValue = new IDate(value, 'HH:mm');
|
|
157
|
+
else momentValue = new IDate(value, 'DD-MM-YYYY HH:mm');
|
|
166
158
|
switch (returnType) {
|
|
167
159
|
case DateReturnType.STRING:
|
|
168
160
|
switch (dateType) {
|
|
@@ -170,7 +162,7 @@ export default class DateFieldController<T extends string | Moment | Date> exten
|
|
|
170
162
|
onChange(momentValue.format('HH:mm') as T);
|
|
171
163
|
break;
|
|
172
164
|
case DateType.DATETIME:
|
|
173
|
-
onChange(momentValue.toISOString(
|
|
165
|
+
onChange(momentValue.toISOString() as T);
|
|
174
166
|
break;
|
|
175
167
|
case DateType.DATE:
|
|
176
168
|
onChange(momentValue.format('YYYY-MM-DD') as T);
|
|
@@ -178,9 +170,9 @@ export default class DateFieldController<T extends string | Moment | Date> exten
|
|
|
178
170
|
}
|
|
179
171
|
break;
|
|
180
172
|
case DateReturnType.DATE:
|
|
181
|
-
onChange(momentValue.
|
|
173
|
+
onChange(momentValue.date as T);
|
|
182
174
|
break;
|
|
183
|
-
case DateReturnType.
|
|
175
|
+
case DateReturnType.IDATE:
|
|
184
176
|
onChange(momentValue as T);
|
|
185
177
|
break;
|
|
186
178
|
}
|
|
@@ -10,18 +10,17 @@ import {ControlContext} from "../../../util/controlContext";
|
|
|
10
10
|
import {colorIsWhite, convertColor, editColor} from "../../../util/colorUtil";
|
|
11
11
|
import {voidFunction} from "../../../util/controlUtil";
|
|
12
12
|
import classNames from "classnames";
|
|
13
|
-
import {Moment, unitOfTime} from 'moment';
|
|
14
13
|
import {DateTimeParts} from "../dateFieldController";
|
|
15
|
-
import {toMoment} from '../../../util/momentUtil';
|
|
16
14
|
import AwesomeIcon from '../../../icon/awesomeIcon';
|
|
15
|
+
import IDate, {IDateValue, MutateKey} from '@dvrd/idate';
|
|
17
16
|
|
|
18
17
|
interface Props {
|
|
19
18
|
onChange: (key: PickerKey, value: number, add?: boolean) => VoidFunction;
|
|
20
|
-
getCurrentMoment: (value?: DateNumParts) =>
|
|
19
|
+
getCurrentMoment: (value?: DateNumParts) => IDate;
|
|
21
20
|
pickerValue: DateNumParts;
|
|
22
21
|
dateParts: DateTimeParts;
|
|
23
|
-
disableAfter?:
|
|
24
|
-
disableBefore?:
|
|
22
|
+
disableAfter?: IDateValue;
|
|
23
|
+
disableBefore?: IDateValue;
|
|
25
24
|
disableFuture?: boolean;
|
|
26
25
|
disablePast?: boolean;
|
|
27
26
|
}
|
|
@@ -31,7 +30,8 @@ export default class DatePicker extends PureComponent<Props> {
|
|
|
31
30
|
static contextType = ControlContext;
|
|
32
31
|
|
|
33
32
|
onClickReset = (type: 'year' | 'month') => () => {
|
|
34
|
-
const {onChange} = this.props
|
|
33
|
+
const {onChange} = this.props;
|
|
34
|
+
const today = IDate.now();
|
|
35
35
|
let key: PickerKey, value: number;
|
|
36
36
|
if (type === 'year') {
|
|
37
37
|
key = PickerKey.YEAR;
|
|
@@ -74,11 +74,11 @@ export default class DatePicker extends PureComponent<Props> {
|
|
|
74
74
|
};
|
|
75
75
|
|
|
76
76
|
getMonthLabel = (index: number): string =>
|
|
77
|
-
|
|
77
|
+
IDate.now().month(index).format('MMMM');
|
|
78
78
|
|
|
79
79
|
getDaysToRender = (): { day: number, isCurrentMonth: boolean }[] => {
|
|
80
80
|
const daysToRender: { day: number, isCurrentMonth: boolean }[] = [],
|
|
81
|
-
currentMoment = this.props.getCurrentMoment(), dayPadding = currentMoment.
|
|
81
|
+
currentMoment = this.props.getCurrentMoment(), dayPadding = currentMoment.day(1).weekday() - 1,
|
|
82
82
|
days = currentMoment.daysInMonth(), maxPrevMonth = currentMoment.subtract(1, 'month').daysInMonth();
|
|
83
83
|
|
|
84
84
|
// Add days to pad begin weekdays
|
|
@@ -94,17 +94,18 @@ export default class DatePicker extends PureComponent<Props> {
|
|
|
94
94
|
return daysToRender;
|
|
95
95
|
};
|
|
96
96
|
|
|
97
|
-
isDateDisabled = (date:
|
|
98
|
-
const {disableAfter, disableBefore, disableFuture, disablePast} = this.props
|
|
97
|
+
isDateDisabled = (date: IDate, granularity: MutateKey = 'd'): boolean => {
|
|
98
|
+
const {disableAfter, disableBefore, disableFuture, disablePast} = this.props;
|
|
99
|
+
const now = IDate.now();
|
|
99
100
|
if (disablePast && date.isBefore(now, granularity)) return true;
|
|
100
101
|
if (disableFuture && date.isAfter(now, granularity)) return true;
|
|
101
|
-
if (disableAfter && date.isAfter(
|
|
102
|
-
return !!(disableBefore && date.isBefore(
|
|
102
|
+
if (disableAfter && date.isAfter(new IDate(disableAfter), granularity)) return true;
|
|
103
|
+
return !!(disableBefore && date.isBefore(new IDate(disableBefore), granularity));
|
|
103
104
|
};
|
|
104
105
|
|
|
105
106
|
isDayCurrent = (day: number) => {
|
|
106
107
|
const {pickerValue, dateParts, getCurrentMoment} = this.props, pickerCurrent = getCurrentMoment(),
|
|
107
|
-
currentMonth = dateParts.month.includes('m') ?
|
|
108
|
+
currentMonth = dateParts.month.includes('m') ? IDate.now().month() : Number(dateParts.month) - 1;
|
|
108
109
|
return pickerCurrent.month() === currentMonth && day === Number(pickerValue.day);
|
|
109
110
|
};
|
|
110
111
|
|
|
@@ -139,7 +140,7 @@ export default class DatePicker extends PureComponent<Props> {
|
|
|
139
140
|
renderDaysContainer = () => {
|
|
140
141
|
const daysToRender = this.getDaysToRender(), weekDays: string[] = [];
|
|
141
142
|
for (let i = 1; i <= 7; i++)
|
|
142
|
-
weekDays.push(
|
|
143
|
+
weekDays.push(IDate.now().day(i).format('dd'));
|
|
143
144
|
return (
|
|
144
145
|
<div className='dvr-days-container'>
|
|
145
146
|
{weekDays.map((day: string, index: number) => (
|
|
@@ -154,7 +155,7 @@ export default class DatePicker extends PureComponent<Props> {
|
|
|
154
155
|
|
|
155
156
|
renderDay = (dayData: { day: number, isCurrentMonth: boolean }, index: number) => {
|
|
156
157
|
const {day, isCurrentMonth} = dayData, {onChange} = this.props,
|
|
157
|
-
dayMoment = this.props.getCurrentMoment().
|
|
158
|
+
dayMoment = this.props.getCurrentMoment().day(day),
|
|
158
159
|
isDisabled = this.isDateDisabled(dayMoment) || !isCurrentMonth,
|
|
159
160
|
isCurrent = this.isDayCurrent(day) && isCurrentMonth, isHoverAble = !isCurrent && isCurrentMonth,
|
|
160
161
|
key: string = isCurrentMonth ? `day-${day}-${index}` : `other-day-${day}-${index}`;
|
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
* Copyright (c) 2021. Dave van Rijn Development
|
|
3
3
|
*/
|
|
4
4
|
import React, {KeyboardEventHandler, MouseEventHandler, PureComponent} from 'react';
|
|
5
|
-
import moment, {Moment} from 'moment';
|
|
6
5
|
import {DateType} from "../dateFieldController";
|
|
7
6
|
import {AwesomeIcon, ElementPosition, OrnamentShape, TextField, generateComponentId} from "../../../../../";
|
|
8
7
|
import {voidFunction} from "../../../util/controlUtil";
|
|
9
8
|
import {ErrorType} from "../../../util/interfaces";
|
|
10
9
|
import {DefaultInputProps} from "../../v2/inputController_v2";
|
|
11
10
|
import { IconName } from '@fortawesome/fontawesome-svg-core';
|
|
11
|
+
import IDate from '@dvrd/idate';
|
|
12
12
|
|
|
13
13
|
type DateIndex = {
|
|
14
14
|
index: number;
|
|
@@ -19,10 +19,10 @@ type DateIndex = {
|
|
|
19
19
|
|
|
20
20
|
interface Props {
|
|
21
21
|
onClickOrnament?: MouseEventHandler;
|
|
22
|
-
onChange: (value:
|
|
22
|
+
onChange: (value: IDate) => void;
|
|
23
23
|
onEnter?: KeyboardEventHandler;
|
|
24
24
|
dateType: DateType;
|
|
25
|
-
value:
|
|
25
|
+
value: IDate | null;
|
|
26
26
|
error?: ErrorType;
|
|
27
27
|
label?: string | number;
|
|
28
28
|
fullWidth?: boolean;
|
|
@@ -137,7 +137,7 @@ export default class DateInput extends PureComponent<Props, State> {
|
|
|
137
137
|
}
|
|
138
138
|
};
|
|
139
139
|
|
|
140
|
-
parseValue = (value:
|
|
140
|
+
parseValue = (value: IDate | null): DateIndex[] => {
|
|
141
141
|
const indexes: DateIndex[] = [], {dateType} = this.props;
|
|
142
142
|
let chars: string, defaultChars: string;
|
|
143
143
|
if (dateType === DateType.DATE) {
|
|
@@ -168,12 +168,12 @@ export default class DateInput extends PureComponent<Props, State> {
|
|
|
168
168
|
|
|
169
169
|
tryOnChange = (indexes: DateIndex[]) => {
|
|
170
170
|
const {dateType, onChange} = this.props;
|
|
171
|
-
let momentValue:
|
|
171
|
+
let momentValue: IDate,
|
|
172
172
|
format: string = dateType === DateType.DATE ? 'DD-MM-YYYY' : dateType === DateType.DATETIME ?
|
|
173
173
|
'DD-MM-YYYY HH:mm' : 'HH:mm';
|
|
174
174
|
if (indexes.length >= format.length) {
|
|
175
175
|
const stringValue = this.getStringValue(format.length);
|
|
176
|
-
momentValue =
|
|
176
|
+
momentValue = new IDate(stringValue, format);
|
|
177
177
|
if (!/[a-zA-Z]/.test(stringValue) && momentValue.isValid()) onChange(momentValue);
|
|
178
178
|
}
|
|
179
179
|
};
|
|
@@ -199,7 +199,7 @@ export default class DateInput extends PureComponent<Props, State> {
|
|
|
199
199
|
const {indexes} = this.state, prevIndexes = prevState.indexes, {value} = this.props,
|
|
200
200
|
prevValue = prevProps.value;
|
|
201
201
|
if (this.indexesAreDifferent(indexes, prevIndexes)) this.tryOnChange(indexes);
|
|
202
|
-
if ((!prevValue && value) || (!value && prevValue) || (value && prevValue && !value.
|
|
202
|
+
if ((!prevValue && value) || (!value && prevValue) || (value && prevValue && !value.isAt(prevValue)))
|
|
203
203
|
this.setState({indexes: this.parseValue(value)});
|
|
204
204
|
};
|
|
205
205
|
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2021. Dave van Rijn Development
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import moment, {Moment} from "moment";
|
|
6
|
-
|
|
7
|
-
export const MOMENT_FORMATS: string[] = [
|
|
8
|
-
'MM/DD/YYYY HH:mm:ssZ',
|
|
9
|
-
'MM-DD-YYYY HH:mm:ssZ',
|
|
10
|
-
'DD/MM/YYYY HH:mm:ssZ',
|
|
11
|
-
'DD-MM-YYYY HH:mm:ssZ',
|
|
12
|
-
'YYYY-MM-DD HH:mm:ssZ',
|
|
13
|
-
'YYYY/MM/DD HH:mm:ssZ',
|
|
14
|
-
'DD/MM/YYYYTHH:mm:ssZ',
|
|
15
|
-
'DD-MM-YYYYTHH:mm:ssZ',
|
|
16
|
-
'YYYY-MM-DDTHH:mm:ssZ',
|
|
17
|
-
'MM/DD/YYYY HH:mm:ss',
|
|
18
|
-
'MM-DD-YYYY HH:mm:ss',
|
|
19
|
-
'DD/MM/YYYY HH:mm:ss',
|
|
20
|
-
'DD-MM-YYYY HH:mm:ss',
|
|
21
|
-
'YYYY-MM-DD HH:mm:ss',
|
|
22
|
-
'YYYY/MM/DD HH:mm:ss',
|
|
23
|
-
'DD/MM/YYYYTHH:mm:ss',
|
|
24
|
-
'DD-MM-YYYYTHH:mm:ss',
|
|
25
|
-
'YYYY-MM-DDTHH:mm:ss',
|
|
26
|
-
'MM/DD/YYYY HH:mm',
|
|
27
|
-
'MM-DD-YYYY HH:mm',
|
|
28
|
-
'DD/MM/YYYY HH:mm',
|
|
29
|
-
'DD-MM-YYYY HH:mm',
|
|
30
|
-
'YYYY-MM-DD HH:mm',
|
|
31
|
-
'YYYY/MM/DD HH:mm',
|
|
32
|
-
'DD/MM/YYYYTHH:mm',
|
|
33
|
-
'DD-MM-YYYYTHH:mm',
|
|
34
|
-
'YYYY-MM-DDTHH:mm',
|
|
35
|
-
'YYYY/MM/DDTHH:mm',
|
|
36
|
-
'YYYY-MM-DD',
|
|
37
|
-
'YYYY/MM/DD',
|
|
38
|
-
'MM-DD-YYYY',
|
|
39
|
-
'MM/DD/YYYY',
|
|
40
|
-
'DD-MM-YYYY',
|
|
41
|
-
'DD/MM/YYYY',
|
|
42
|
-
];
|
|
43
|
-
|
|
44
|
-
export const toMoment = (dateValue?: string | Moment | null | Date, formats: string[] | string = MOMENT_FORMATS): Moment => {
|
|
45
|
-
if (moment.isMoment(dateValue)) return dateValue;
|
|
46
|
-
if(dateValue instanceof Date) return moment(dateValue);
|
|
47
|
-
if (!dateValue || !dateValue.length) return moment();
|
|
48
|
-
return moment(dateValue, formats);
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export const formatMoment = (date?: Moment, format: string = 'YYYY-MM-DDTHH:mm:ss'): string => {
|
|
52
|
-
if (!date) date = toMoment();
|
|
53
|
-
return date.format(format);
|
|
54
|
-
};
|