Skip to content

Commit adbd72f

Browse files
committed
refactor: Code cleanup
1 parent 937f1b3 commit adbd72f

File tree

7 files changed

+67
-52
lines changed

7 files changed

+67
-52
lines changed

src/VueDatePicker/VueDatePicker.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
DatepickerInputRef,
106106
ModelValue,
107107
MenuView,
108+
MaybeElementRef,
108109
} from '@/interfaces';
109110
import { useDefaults } from '@/composables/defaults';
110111
@@ -518,7 +519,7 @@
518519
dpMenuRef.value?.handleFlow(skipStep);
519520
};
520521
521-
onClickOutside(dpWrapMenuRef, inputRef, () => clickOutside(validateBeforeEmit));
522+
onClickOutside(dpWrapMenuRef, inputRef as unknown as MaybeElementRef, () => clickOutside(validateBeforeEmit));
522523
523524
defineExpose({
524525
closeMenu,

src/VueDatePicker/composables/defaults.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ export const useDefaults = (props: AllPropsType | PickerBasePropsType) => {
8282
const defaultedMultiDates = computed(() => getDefaultMultiDates(props.multiDates, props.multiDatesLimit));
8383

8484
const propDates = computed(() =>
85-
mapPropDates(
86-
props.minDate,
87-
props.maxDate,
88-
props.disabledDates,
89-
props.allowedDates,
90-
defaultedHighlight.value,
91-
props.markers,
92-
defaultedTz.value,
93-
props.monthPicker || props.yearPicker || props.quarterPicker,
94-
),
85+
mapPropDates({
86+
minDate: props.minDate,
87+
maxDate: props.maxDate,
88+
disabledDates: props.disabledDates,
89+
allowedDates: props.allowedDates,
90+
highlight: defaultedHighlight.value,
91+
markers: props.markers,
92+
timezone: defaultedTz.value,
93+
isSpecific: props.monthPicker || props.yearPicker || props.quarterPicker,
94+
}),
9595
);
9696

9797
const defaultedRange = computed(() =>

src/VueDatePicker/composables/position.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { h, ref, render, toRef, watch } from 'vue';
2-
import { type DatepickerInputRef, type DatepickerMenuRef, OpenPosition } from '@/interfaces';
2+
import {
3+
type DatepickerInputRef,
4+
type DatepickerMenuRef,
5+
type InlineOptions,
6+
type VueEmit,
7+
OpenPosition,
8+
type MaybeElementRef,
9+
} from '@/interfaces';
310

411
import { unrefElement } from '@/utils/util';
512
import { MenuPlacement } from '@/constants';
613

714
import type { Component, ComputedRef, Ref, Slots } from 'vue';
8-
import type { InlineOptions, VueEmit } from '@/interfaces';
915
import type { AllPropsType } from '@/props';
1016

1117
/**
@@ -95,7 +101,7 @@ export const usePosition = ({
95101
};
96102

97103
const customAltPosition = () => {
98-
const el = unrefElement(inputRef);
104+
const el = unrefElement(inputRef as MaybeElementRef);
99105
const { top, left, transform } = props.altPosition(el);
100106
menuStyle.value = { top: `${top}px`, left: `${left}px`, transform: transform ?? '' };
101107
};
@@ -172,7 +178,7 @@ export const usePosition = ({
172178
};
173179

174180
const getMenuPlacement = (): MenuPlacement => {
175-
const inputEl = unrefElement(inputRef);
181+
const inputEl = unrefElement(inputRef as MaybeElementRef);
176182
if (inputEl) {
177183
const { height: menuHeight } = menuRect.value;
178184
const { top: inputTop, height: inputHeight } = inputEl.getBoundingClientRect();
@@ -201,7 +207,7 @@ export const usePosition = ({
201207

202208
// Parent function that will perform check on which calculation function to invoke
203209
const calculateMenuPosition = () => {
204-
const inputEl = unrefElement(inputRef);
210+
const inputEl = unrefElement(inputRef as MaybeElementRef);
205211
if (inputEl) {
206212
if (props.autoPosition) {
207213
return setAutoPosition(inputEl);
@@ -245,7 +251,7 @@ export const usePosition = ({
245251
// Renders invisible menu on open to determine the menu dimensions
246252
const shadowRender = (DPMenu: Component, props: AllPropsType) => {
247253
const container = document.createElement('div');
248-
const input = unrefElement(inputRef)?.getBoundingClientRect();
254+
const input = unrefElement(inputRef as MaybeElementRef)?.getBoundingClientRect();
249255
container.setAttribute('id', 'dp--temp-container');
250256
const wrap = pickerWrapperRef.value?.clientWidth ? pickerWrapperRef.value : document.body;
251257
wrap.append(container);

src/VueDatePicker/interfaces.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ export interface ConfigurableWindow {
8787
window?: Window;
8888
}
8989

90-
export type MaybeElementRef = MaybeRef<
91-
HTMLElement | SVGElement | ComponentPublicInstance | undefined | null | Element | any
92-
>;
90+
export type MaybeElementRef = MaybeRef<HTMLElement | SVGElement | ComponentPublicInstance | undefined | null | Element>;
9391
export type OnClickOutsideEvents = Pick<
9492
WindowEventMap,
9593
'click' | 'mousedown' | 'mouseup' | 'touchstart' | 'touchend' | 'pointerdown' | 'pointerup'
@@ -371,3 +369,14 @@ export interface TimeInput {
371369
type: TimeType;
372370
separator?: boolean;
373371
}
372+
373+
export interface MapPropDatesOpts {
374+
minDate: MaybeDate;
375+
maxDate: MaybeDate;
376+
disabledDates: DisabledDatesProp;
377+
allowedDates: string[] | Date[];
378+
highlight: HighlightFn | Highlight;
379+
markers: IMarker[];
380+
timezone: TimeZoneConfig | undefined;
381+
isSpecific?: boolean;
382+
}

src/VueDatePicker/utils/date-utils.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,7 @@ export const dateToUtc = (date: Date, preserve: boolean, enableSeconds: boolean)
123123
export const resetDateTime = (value: Date | string, beginning?: boolean): Date => {
124124
const dateParse = getDate(JSON.parse(JSON.stringify(value)));
125125
const timeReset = set(dateParse, { hours: 0, minutes: 0, seconds: 0, milliseconds: 0 });
126-
if (beginning) {
127-
return startOfMonth(timeReset);
128-
}
129-
return timeReset;
130-
131-
// return set(dateParse, { hours: 0, minutes: 0, seconds: 0, milliseconds: 0 });
126+
return beginning ? startOfMonth(timeReset) : timeReset;
132127
};
133128

134129
export const setDateTime = (

src/VueDatePicker/utils/defaults.ts

+14-18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type {
2828
PropDates,
2929
MultiDatesProp,
3030
MultiDatesDefault,
31+
MapPropDatesOpts,
3132
} from '@/interfaces';
3233
import { getDate } from '@/utils/date-utils';
3334
import { dateToTimezoneSafe, sanitizeDateToLocal } from '@/utils/timezone';
@@ -276,26 +277,21 @@ const mapMarkers = (markers: IMarker[], timezone: TimeZoneConfig | undefined) =>
276277
* Sync all props that rely on the date value to be in the same timezone
277278
* All validation that is done from these props will now be in sync with provided timezone config
278279
*/
279-
export const mapPropDates = (
280-
minDate: MaybeDate,
281-
maxDate: MaybeDate,
282-
disabledDates: DisabledDatesProp,
283-
allowedDates: string[] | Date[],
284-
highlight: HighlightFn | Highlight,
285-
markers: IMarker[],
286-
timezone: TimeZoneConfig | undefined,
287-
isSpecific: boolean,
288-
): PropDates => {
280+
export const mapPropDates = (opts: MapPropDatesOpts): PropDates => {
289281
return {
290-
minDate: sanitizeDateToLocal(minDate, timezone, isSpecific),
291-
maxDate: sanitizeDateToLocal(maxDate, timezone, isSpecific),
292-
disabledDates: shouldMap(disabledDates) ? datesArrToMap(disabledDates, timezone, isSpecific) : disabledDates,
293-
allowedDates: shouldMap(allowedDates) ? datesArrToMap(allowedDates, timezone, isSpecific) : null,
282+
minDate: sanitizeDateToLocal(opts.minDate, opts.timezone, opts.isSpecific),
283+
maxDate: sanitizeDateToLocal(opts.maxDate, opts.timezone, opts.isSpecific),
284+
disabledDates: shouldMap(opts.disabledDates)
285+
? datesArrToMap(opts.disabledDates, opts.timezone, opts.isSpecific)
286+
: opts.disabledDates,
287+
allowedDates: shouldMap(opts.allowedDates)
288+
? datesArrToMap(opts.allowedDates, opts.timezone, opts.isSpecific)
289+
: null,
294290
highlight:
295-
typeof highlight === 'object' && shouldMap(highlight?.dates)
296-
? datesArrToMap(highlight.dates, timezone)
297-
: (highlight as HighlightFn),
298-
markers: mapMarkers(markers, timezone),
291+
typeof opts.highlight === 'object' && shouldMap(opts.highlight?.dates)
292+
? datesArrToMap(opts.highlight.dates, opts.timezone)
293+
: (opts.highlight as HighlightFn),
294+
markers: mapMarkers(opts.markers, opts.timezone),
299295
};
300296
};
301297

tests/unit/utils.spec.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import type { AllPropsType } from '@/props';
5757
import { defaultMultiCalendars, defaultTransitions, mapPropDates } from '@/utils/defaults';
5858
import { de } from 'date-fns/locale';
5959
import { localToTz } from '@/utils/timezone';
60+
import type { TimeZoneConfig } from '@/interfaces';
6061

6162
const getCurrentTime = () => {
6263
return {
@@ -72,6 +73,19 @@ const getMinMax = () => {
7273
return { minDate, maxDate };
7374
};
7475

76+
const getMapDatesOpts = (date: Date, timezone: TimeZoneConfig) => {
77+
const highlightFn = (date: any) => !!date;
78+
return {
79+
minDate: date,
80+
maxDate: date,
81+
disabledDates: [date],
82+
allowedDates: [date],
83+
markers: [],
84+
highlight: highlightFn,
85+
timezone,
86+
};
87+
};
88+
7589
describe('Utils and date utils formatting', () => {
7690
it('Should group array by 3', () => {
7791
const items = getArrayInArray(Array.from(Array(10).keys()));
@@ -434,10 +448,7 @@ describe('Utils and date utils formatting', () => {
434448
it('Should map propDates value with and without timezone', async () => {
435449
const today = resetDateTime(new Date());
436450
const highlightFn = (date: any) => !!date;
437-
const mappedDates = mapPropDates(today, today, [today], [today], highlightFn, [], {
438-
timezone: undefined,
439-
exactMatch: false,
440-
});
451+
const mappedDates = mapPropDates(getMapDatesOpts(today, { timezone: undefined, exactMatch: false }));
441452

442453
expect(mappedDates.maxDate).toEqual(today);
443454
expect(mappedDates.minDate).toEqual(today);
@@ -446,10 +457,7 @@ describe('Utils and date utils formatting', () => {
446457
expect(mappedDates.highlight).toEqual(highlightFn);
447458

448459
const todayInTz = localToTz(today, 'UTC');
449-
const mappedDatesInTimezone = mapPropDates(today, today, [today], [today], highlightFn, [], {
450-
timezone: 'UTC',
451-
exactMatch: false,
452-
});
460+
const mappedDatesInTimezone = mapPropDates(getMapDatesOpts(today, { timezone: 'UTC', exactMatch: false }));
453461

454462
expect(mappedDatesInTimezone.maxDate).toEqual(todayInTz);
455463
expect(mappedDatesInTimezone.minDate).toEqual(todayInTz);

0 commit comments

Comments
 (0)