-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
defaults.ts
310 lines (288 loc) · 9.74 KB
/
defaults.ts
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import type {
AriaLabels,
IFormat,
Transition,
TextInputOptions,
DateFilter,
ActionRowData,
MultiCalendarsProp,
MultiCalendarsOptions,
OptionEnabled,
TextInputProp,
InlineProp,
InlineOptions,
Config,
HighlightProp,
Highlight,
HighlightFn,
WeekNumbersProp,
WeekNumbersOpts,
RangeProp,
RangeOpts,
RangeConfig,
TimeZoneProp,
TimeZoneConfig,
MaybeDate,
DisabledDatesProp,
IMarker,
PropDates,
MultiDatesProp,
MultiDatesDefault,
} from '@/interfaces';
import { getDate } from '@/utils/date-utils';
import { dateToTimezoneSafe, sanitizeDateToLocal } from '@/utils/timezone';
import { getMapKey, shouldMap } from '@/utils/util';
export const mergeDefaultTransitions = (conf: Partial<Transition>): Transition => ({
menuAppearTop: 'dp-menu-appear-top',
menuAppearBottom: 'dp-menu-appear-bottom',
open: 'dp-slide-down',
close: 'dp-slide-up',
next: 'calendar-next',
previous: 'calendar-prev',
vNext: 'dp-slide-up',
vPrevious: 'dp-slide-down',
...(conf ?? {}),
});
export const defaultAriaLabels = (labels: Partial<AriaLabels>): AriaLabels => {
return {
toggleOverlay: 'Toggle overlay',
menu: 'Datepicker menu',
input: 'Datepicker input',
calendarWrap: 'Calendar wrapper',
calendarDays: 'Calendar days',
openTimePicker: 'Open time picker',
closeTimePicker: 'Close time Picker',
incrementValue: (type: string) => `Increment ${type}`,
decrementValue: (type: string) => `Decrement ${type}`,
openTpOverlay: (type: string) => `Open ${type} overlay`,
amPmButton: 'Switch AM/PM mode',
openYearsOverlay: 'Open years overlay',
openMonthsOverlay: 'Open months overlay',
nextMonth: 'Next month',
prevMonth: 'Previous month',
nextYear: 'Next year',
prevYear: 'Previous year',
day: undefined,
weekDay: undefined,
...(labels ?? {}),
};
};
const getMultiCalendarsCount = (option?: OptionEnabled) => {
if (!option) return 0;
if (typeof option === 'boolean') return option ? 2 : 0;
return +option >= 2 ? +option : 2;
};
export const defaultMultiCalendars = (multiCalendars?: MultiCalendarsProp): MultiCalendarsOptions => {
const isConfig = typeof multiCalendars === 'object' && multiCalendars;
const defaultOptions = {
static: true,
solo: false,
};
if (!multiCalendars) return { ...defaultOptions, count: getMultiCalendarsCount(false) };
const addOptions = isConfig ? multiCalendars : ({} as MultiCalendarsOptions);
const option = isConfig ? addOptions.count ?? true : multiCalendars;
const count = getMultiCalendarsCount(option);
return Object.assign(defaultOptions, addOptions, { count });
};
export const defaultPreviewFormat = (
previewFormat: IFormat,
format: IFormat,
defaultPattern: string | ((val: Date) => string),
): IFormat => {
if (!previewFormat) {
return typeof defaultPattern === 'string' ? defaultPattern : format;
}
return previewFormat;
};
export const defaultTransitions = (transitions: boolean | Partial<Transition>): Transition => {
if (typeof transitions === 'boolean') {
return transitions ? mergeDefaultTransitions({}) : (false as unknown as Transition);
}
return mergeDefaultTransitions(transitions);
};
/**
* Default options to merge with user provided ones
*/
export const getDefaultTextInputOptions = (textInput: TextInputProp): TextInputOptions & { enabled: boolean } => {
const defaultOptions = {
enterSubmit: true,
tabSubmit: true,
openMenu: true,
selectOnFocus: false,
rangeSeparator: ' - ',
};
if (typeof textInput === 'object') {
return { ...defaultOptions, ...(textInput ?? {}), enabled: true };
}
return { ...defaultOptions, enabled: textInput };
};
/**
* Default filters to merge with user provided values
*/
export const getDefaultFilters = (filters: Partial<DateFilter>): DateFilter => ({
months: [],
years: [],
times: { hours: [], minutes: [], seconds: [] },
...(filters ?? {}),
});
export const getDefaultActionRowData = (actionRow: Partial<ActionRowData>): ActionRowData => ({
showSelect: true,
showCancel: true,
showNow: false,
showPreview: true,
...(actionRow ?? {}),
});
export const getDefaultInlineOptions = (inline: InlineProp): InlineOptions => {
const defaultOptions = { input: false };
if (typeof inline === 'object') {
return { ...defaultOptions, ...(inline ?? {}), enabled: true };
}
return {
enabled: inline,
...defaultOptions,
};
};
export const getDefaultConfig = (config?: Partial<Config>): Config => {
const defaultConfig = {
allowStopPropagation: true,
closeOnScroll: false,
modeHeight: 255,
allowPreventDefault: false,
closeOnClearValue: true,
closeOnAutoApply: true,
noSwipe: false,
keepActionRow: false,
onClickOutside: undefined,
tabOutClosesMenu: true,
arrowLeft: undefined,
};
return { ...defaultConfig, ...(config ?? {}) };
};
export const getDefaultHighlight = (highlight: HighlightProp): Highlight | HighlightFn => {
const defaultOptions = {
dates: Array.isArray(highlight) ? highlight.map((date) => getDate(date)) : [],
years: [],
months: [],
quarters: [],
weeks: [],
weekdays: [],
options: { highlightDisabled: false },
};
if (typeof highlight === 'function') return highlight;
return { ...defaultOptions, ...(highlight ?? {}) };
};
export const getDefaultWeekNumbers = (weekNumbers: WeekNumbersProp): WeekNumbersOpts => {
if (typeof weekNumbers === 'object') {
return {
type: weekNumbers?.type ?? 'local',
hideOnOffsetDates: weekNumbers?.hideOnOffsetDates ?? false,
};
}
return {
type: weekNumbers,
hideOnOffsetDates: false,
};
};
export const getDefaultRangeOptions = (config: RangeProp, deprecatedOpts: RangeOpts): RangeConfig => {
const defaultOptions = {
noDisabledRange: false,
showLastInRange: true,
minMaxRawRange: false,
partialRange: true,
disableTimeRangeValidation: false,
maxRange: undefined,
minRange: undefined,
autoRange: undefined,
fixedStart: false,
fixedEnd: false,
};
if (typeof config === 'object') {
return { enabled: true, ...defaultOptions, ...config };
}
return {
enabled: config,
noDisabledRange: deprecatedOpts.noDisabledRange,
showLastInRange: deprecatedOpts.showLastInRange,
minMaxRawRange: deprecatedOpts.minMaxRawRange,
partialRange: deprecatedOpts.partialRange,
disableTimeRangeValidation: deprecatedOpts.disableTimeRangeValidation,
maxRange: deprecatedOpts.maxRange,
minRange: deprecatedOpts.minRange,
autoRange: deprecatedOpts.autoRange,
fixedStart: deprecatedOpts.fixedStart,
fixedEnd: deprecatedOpts.fixedEnd,
};
};
export const getDefaultTimeZone = (timeZone: TimeZoneProp, emitTimezone?: string) => {
if (!timeZone) return { timezone: undefined, exactMatch: false, emitTimezone };
if (typeof timeZone === 'string') {
return { timezone: timeZone, exactMatch: false, dateInTz: undefined, emitTimezone, convertModel: true };
}
return {
timezone: timeZone.timezone,
exactMatch: timeZone.exactMatch ?? false,
dateInTz: timeZone.dateInTz ?? undefined,
emitTimezone: emitTimezone ?? timeZone.emitTimezone,
convertModel: timeZone.convertModel ?? true,
};
};
const datesArrToMap = (
datesArr: (Date | string | number)[],
timezone: TimeZoneConfig | undefined,
): Map<string, Date | null> => {
return new Map(
datesArr.map((date) => {
const d = dateToTimezoneSafe(date, timezone);
return [getMapKey(d), d];
}),
);
};
const mapMarkers = (markers: IMarker[], timezone: TimeZoneConfig | undefined) => {
if (markers.length) {
return new Map(
markers.map((marker) => {
const date = dateToTimezoneSafe(marker.date, timezone);
return [getMapKey(date), marker];
}),
);
}
return null;
};
/**
* Sync all props that rely on the date value to be in the same timezone
* All validation that is done from these props will now be in sync with provided timezone config
*/
export const mapPropDates = (
minDate: MaybeDate,
maxDate: MaybeDate,
disabledDates: DisabledDatesProp,
allowedDates: string[] | Date[],
highlight: HighlightFn | Highlight,
markers: IMarker[],
timezone: TimeZoneConfig | undefined,
): PropDates => {
return {
minDate: sanitizeDateToLocal(minDate, timezone),
maxDate: sanitizeDateToLocal(maxDate, timezone),
disabledDates: shouldMap(disabledDates) ? datesArrToMap(disabledDates, timezone) : disabledDates,
allowedDates: shouldMap(allowedDates) ? datesArrToMap(allowedDates, timezone) : null,
highlight:
typeof highlight === 'object' && shouldMap(highlight?.dates)
? datesArrToMap(highlight.dates, timezone)
: (highlight as HighlightFn),
markers: mapMarkers(markers, timezone),
};
};
export const getDefaultMultiDates = (
multiDates: MultiDatesProp,
multiDatesLimit: number | string,
): MultiDatesDefault => {
if (typeof multiDates === 'boolean') {
return { enabled: multiDates, dragSelect: true, limit: +multiDatesLimit };
}
return {
enabled: !!multiDates,
limit: multiDates.limit ? +multiDates.limit : null,
dragSelect: multiDates.dragSelect ?? true,
};
};