Skip to content
This repository was archived by the owner on Apr 17, 2022. It is now read-only.

Commit 5e93bf6

Browse files
committed
refactor: Replace all duplicate wrapper functions
1 parent 18a06ce commit 5e93bf6

File tree

7 files changed

+68
-184
lines changed

7 files changed

+68
-184
lines changed

src/Vue3DatePicker/Vue3DatePicker.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
useSlots,
129129
watch,
130130
} from 'vue';
131+
import { getHours, getMinutes, getSeconds } from 'date-fns';
131132
132133
import DatepickerInput from './components/DatepickerInput.vue';
133134
import DatepickerMenu from './components/DatepickerMenu.vue';
@@ -146,7 +147,7 @@
146147
ITransition,
147148
IDisableDates,
148149
} from './interfaces';
149-
import { getDateHours, getDateMinutes, getDateSeconds, getDefaultPattern, isValidTime } from './utils/date-utils';
150+
import { getDefaultPattern, isValidTime } from './utils/date-utils';
150151
import { getDefaultTextInputOptions, getDefaultFilters, mergeDefaultTransitions } from './utils/util';
151152
import { usePosition } from './components/composition/position';
152153
import { useExternalInternalMapper } from './components/composition/external-internal-mapper';
@@ -355,7 +356,11 @@
355356
356357
const defaultStartTime = computed((): ITimeValue | ITimeValue[] | null => {
357358
const assignDefaultTime = (obj: ITimeValue): ITimeValue => {
358-
const defaultTime = { hours: getDateHours(), minutes: getDateMinutes(), seconds: getDateSeconds() };
359+
const defaultTime = {
360+
hours: getHours(new Date()),
361+
minutes: getMinutes(new Date()),
362+
seconds: getSeconds(new Date()),
363+
};
359364
return Object.assign(defaultTime, obj);
360365
};
361366
if (props.range) {

src/Vue3DatePicker/components/ActionRow.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<script lang="ts" setup>
3333
import { computed, PropType } from 'vue';
3434
import { IFormat, InternalModuleValue, ITimeValue } from '../interfaces';
35-
import { formatDate, getMonthForExternal, getTImeForExternal, isValidTime } from '../utils/date-utils';
35+
import { formatDate, getMonthVal, getTImeForExternal, isValidTime } from '../utils/date-utils';
3636
import { isModelValueRange } from '../utils/type-guard';
3737
3838
const emit = defineEmits(['closePicker', 'selectDate']);
@@ -95,7 +95,7 @@
9595
return props.previewFormat(getTImeForExternal(props.internalModelValue));
9696
}
9797
if (props.monthPicker) {
98-
return props.previewFormat(getMonthForExternal(props.internalModelValue as Date));
98+
return props.previewFormat(getMonthVal(props.internalModelValue as Date));
9999
}
100100
return props.previewFormat(props.internalModelValue);
101101
});

src/Vue3DatePicker/components/TimePicker/TimeInput.vue

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,15 @@
180180

181181
<script lang="ts" setup>
182182
import { computed, PropType, ref } from 'vue';
183+
import { getHours, getMinutes, getSeconds } from 'date-fns';
184+
183185
import { ChevronUpIcon, ChevronDownIcon, ClockIcon } from '../Icons';
186+
184187
import { DynamicClass, IDateFilter, IDefaultSelect, ITimeType } from '../../interfaces';
185188
import { getArrayInArray, hoursToAmPmHours } from '../../utils/util';
186189
import SelectionGrid from '../SelectionGrid.vue';
187-
import {
188-
addDateHours,
189-
addDateMinutes,
190-
addDateSeconds,
191-
subDateHours,
192-
subDateMinutes,
193-
subDateSeconds,
194-
} from '../../utils/date-utils';
195190
import { useTransitions } from '../composition/transition';
191+
import { addTime, subTime } from '../../utils/date-utils';
196192
197193
const emit = defineEmits(['setHours', 'setMinutes', 'update:hours', 'update:minutes', 'update:seconds']);
198194
const props = defineProps({
@@ -279,20 +275,20 @@
279275
hours:
280276
type === 'hours'
281277
? inc
282-
? addDateHours(props.hours, +props.hoursIncrement)
283-
: subDateHours(props.hours, +props.hoursIncrement)
278+
? getHours(addTime({ hours: +props.hours }, { hours: +props.hoursIncrement }))
279+
: getHours(subTime({ hours: +props.hours }, { hours: +props.hoursIncrement }))
284280
: props.hours,
285281
minutes:
286282
type === 'minutes'
287283
? inc
288-
? addDateMinutes(props.minutes, +props.minutesIncrement)
289-
: subDateMinutes(props.minutes, +props.minutesIncrement)
284+
? getMinutes(addTime({ minutes: props.minutes }, { minutes: +props.minutesIncrement }))
285+
: getMinutes(subTime({ minutes: props.minutes }, { minutes: +props.minutesIncrement }))
290286
: props.minutes,
291287
seconds:
292288
type === 'seconds'
293289
? inc
294-
? addDateSeconds(props.seconds, +props.secondsIncrement)
295-
: subDateSeconds(props.seconds, +props.secondsIncrement)
290+
? getSeconds(addTime({ seconds: props.seconds }, { seconds: +props.secondsIncrement }))
291+
: getSeconds(subTime({ seconds: props.seconds }, { seconds: +props.secondsIncrement }))
296292
: props.seconds,
297293
};
298294
};

src/Vue3DatePicker/components/composition/calendar.ts

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
import { computed, onMounted, Ref, ref, UnwrapRef, watch } from 'vue';
2+
import { addDays, addMonths, getDay, getHours, getISOWeek, getMinutes, getMonth, getSeconds, getYear } from 'date-fns';
3+
24
import { ICalendarDay, IMarker, InternalModuleValue, UseCalendar, VueEmit } from '../../interfaces';
35
import {
4-
addMonthsToDate,
5-
getAddedDays,
6-
getDateHours,
7-
getDateMinutes,
8-
getDateMonth,
9-
getDateSeconds,
10-
getDateYear,
116
getNextMonthYear,
127
getNextYearMonth,
138
getPreviousMonthYear,
14-
getWeekDay,
15-
getWeekNumber,
169
isDateAfter,
1710
isDateBefore,
1811
isDateEqual,
@@ -56,21 +49,25 @@ interface IUseCalendar {
5649
export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar => {
5750
const today = ref<Date>(new Date());
5851
const hoveredDate = ref<Date | null>();
59-
const month = ref<number>(getDateMonth(new Date()));
60-
const year = ref<number>(getDateYear(new Date()));
52+
const month = ref<number>(getMonth(new Date()));
53+
const year = ref<number>(getYear(new Date()));
6154
const monthNext = ref<number>(getNextMonthYear(new Date()).month);
6255
const yearNext = ref<number>(getNextMonthYear(new Date()).year);
63-
const hours = ref<number | number[]>(props.range ? [getDateHours(), getDateHours()] : getDateHours());
64-
const minutes = ref<number | number[]>(props.range ? [getDateMinutes(), getDateMinutes()] : getDateMinutes());
56+
const hours = ref<number | number[]>(
57+
props.range ? [getHours(new Date()), getHours(new Date())] : getHours(new Date()),
58+
);
59+
const minutes = ref<number | number[]>(
60+
props.range ? [getMinutes(new Date()), getMinutes(new Date())] : getMinutes(new Date()),
61+
);
6562
const seconds = ref<number | number[]>(props.range ? [0, 0] : 0);
6663

6764
onMounted(() => {
6865
mapInternalModuleValues();
6966

7067
if (!modelValue.value) {
7168
if (props.startDate) {
72-
month.value = getDateMonth(new Date(props.startDate));
73-
year.value = getDateYear(new Date(props.startDate));
69+
month.value = getMonth(new Date(props.startDate));
70+
year.value = getYear(new Date(props.startDate));
7471
if (props.twoCalendars) {
7572
monthNext.value = getNextMonthYear(new Date(props.startDate)).month;
7673
yearNext.value = getNextMonthYear(new Date(props.startDate)).year;
@@ -144,15 +141,15 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
144141
isDateEqual(sanitizeDate(new Date(disabledDate)), sanitizeDate(date)),
145142
);
146143
const disabledMonths = props.filters.months.length ? props.filters.months.map((month) => +month) : [];
147-
const inDisabledMonths = disabledMonths.includes(getDateMonth(date));
144+
const inDisabledMonths = disabledMonths.includes(getMonth(date));
148145
const weekDayDisabled = props.disabledWeekDays.length
149-
? props.disabledWeekDays.some((day) => +day === getWeekDay(date))
146+
? props.disabledWeekDays.some((day) => +day === getDay(date))
150147
: false;
151148
const notInSpecific = props.allowedDates.length
152149
? !props.allowedDates.some((dateVal) => isDateEqual(sanitizeDate(new Date(dateVal)), sanitizeDate(date)))
153150
: false;
154151

155-
const dateYear = getDateYear(date);
152+
const dateYear = getYear(date);
156153

157154
const outOfYearRange = dateYear < +props.yearRange[0] || dateYear > +props.yearRange[1];
158155

@@ -204,8 +201,8 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
204201
* Extracted method to map month and year
205202
*/
206203
const assignMonthAndYear = (date: Date): void => {
207-
month.value = getDateMonth(date);
208-
year.value = getDateYear(date);
204+
month.value = getMonth(date);
205+
year.value = getYear(date);
209206
};
210207

211208
/**
@@ -216,18 +213,18 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
216213
if (isModelValueRange(modelValue.value)) {
217214
if (modelValue.value.length === 2) {
218215
assignMonthAndYear(modelValue.value[0]);
219-
hours.value = [getDateHours(modelValue.value[0]), getDateHours(modelValue.value[1])];
220-
minutes.value = [getDateMinutes(modelValue.value[0]), getDateMinutes(modelValue.value[1])];
221-
seconds.value = [getDateSeconds(modelValue.value[0]), getDateSeconds(modelValue.value[1])];
216+
hours.value = [getHours(modelValue.value[0]), getHours(modelValue.value[1])];
217+
minutes.value = [getMinutes(modelValue.value[0]), getMinutes(modelValue.value[1])];
218+
seconds.value = [getSeconds(modelValue.value[0]), getSeconds(modelValue.value[1])];
222219
}
223220
if (props.twoCalendars) {
224221
handleNextMonthYear();
225222
}
226223
} else {
227224
assignMonthAndYear(modelValue.value);
228-
hours.value = getDateHours(modelValue.value);
229-
minutes.value = getDateMinutes(modelValue.value);
230-
seconds.value = getDateSeconds(modelValue.value);
225+
hours.value = getHours(modelValue.value);
226+
minutes.value = getMinutes(modelValue.value);
227+
seconds.value = getSeconds(modelValue.value);
231228
}
232229
} else {
233230
if (props.timePicker) {
@@ -255,8 +252,8 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
255252
* When using next calendar on auto range mode, adjust month and year for both calendars
256253
*/
257254
const handleNextCalendarAutoRange = (date: string | Date) => {
258-
const monthValue = getDateMonth(new Date(date));
259-
const yearValue = getDateYear(new Date(date));
255+
const monthValue = getMonth(new Date(date));
256+
const yearValue = getYear(new Date(date));
260257
const next = getNextMonthYear(new Date(date));
261258
month.value = monthValue;
262259
year.value = yearValue;
@@ -289,7 +286,7 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
289286
if (isNext) {
290287
handleNextCalendarAutoRange(day.value);
291288
}
292-
rangeDate = [new Date(day.value), getAddedDays(new Date(day.value), +props.autoRange)];
289+
rangeDate = [new Date(day.value), addDays(new Date(day.value), +props.autoRange)];
293290
} else {
294291
if (!rangeDate[0]) {
295292
rangeDate[0] = new Date(day.value);
@@ -320,7 +317,7 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
320317
const getWeekNum = (days: UnwrapRef<ICalendarDay[]>): string | number => {
321318
const firstCurrentData = days.find((day) => day.current);
322319
if (firstCurrentData) {
323-
return getWeekNumber(firstCurrentData.value);
320+
return getISOWeek(firstCurrentData.value);
324321
}
325322
return '';
326323
};
@@ -342,7 +339,7 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
342339
if (props.autoRange) {
343340
if (hoveredDate.value) {
344341
if (props.hideOffsetDates && !day.current) return false;
345-
const rangeEnd = getAddedDays(hoveredDate.value, +props.autoRange);
342+
const rangeEnd = addDays(hoveredDate.value, +props.autoRange);
346343
return isDateEqual(rangeEnd, new Date(day.value));
347344
}
348345
return false;
@@ -356,7 +353,7 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
356353
const isAutoRangeInBetween = (day: UnwrapRef<ICalendarDay>): boolean => {
357354
if (props.autoRange) {
358355
if (hoveredDate.value) {
359-
const rangeEnd = getAddedDays(hoveredDate.value, +props.autoRange);
356+
const rangeEnd = addDays(hoveredDate.value, +props.autoRange);
360357
if (props.hideOffsetDates && !day.current) return false;
361358
return isDateAfter(day.value, hoveredDate.value) && isDateBefore(day.value, rangeEnd);
362359
}
@@ -378,15 +375,15 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
378375

379376
const handleNextMonthYear = (): void => {
380377
if (Array.isArray(modelValue.value) && modelValue.value.length === 2) {
381-
const date = new Date(modelValue.value[1] ? modelValue.value[1] : addMonthsToDate(modelValue.value[0]));
378+
const date = new Date(modelValue.value[1] ? modelValue.value[1] : addMonths(modelValue.value[0], 1));
382379
if ((monthNext.value === month.value && yearNext.value === year.value) || !props.twoCalendarsSolo) {
383380
const date = getNextYearMonth(month.value, year.value);
384381
monthNext.value = date.month;
385382
yearNext.value = date.year;
386383
} else {
387-
if (getDateMonth(modelValue.value[0]) !== getDateMonth(modelValue.value[1])) {
388-
monthNext.value = getDateMonth(date);
389-
yearNext.value = getDateYear(date);
384+
if (getMonth(modelValue.value[0]) !== getMonth(modelValue.value[1])) {
385+
monthNext.value = getMonth(date);
386+
yearNext.value = getYear(date);
390387
}
391388
}
392389
}

src/Vue3DatePicker/components/composition/external-internal-mapper.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { Ref, ref, watch } from 'vue';
33
import {
44
formatDate,
55
getDefaultPattern,
6-
getMonthForExternal,
76
getTImeForExternal,
87
isValidDate,
9-
setDateTime,
108
setDateMonthOrYear,
9+
setDateTime,
1110
} from '../../utils/date-utils';
1211
import { IFormat, ModelValue, VueEmit } from '../../interfaces';
1312
import { isMonth, isRangeArray, isSingle, isTime, isTimeArray } from '../../utils/type-guard';
13+
import { getMonthVal } from '../../utils/date-utils';
1414

1515
interface IExternalInternalMapper {
1616
parseExternalModelValue: (value: ModelValue) => void;
@@ -95,7 +95,7 @@ export const useExternalInternalMapper = (
9595
} else if (timePicker) {
9696
inputValue.value = format(getTImeForExternal(internalModelValue.value));
9797
} else if (monthPicker) {
98-
inputValue.value = format(getMonthForExternal(internalModelValue.value as Date));
98+
inputValue.value = format(getMonthVal(internalModelValue.value as Date));
9999
} else {
100100
inputValue.value = format(internalModelValue.value);
101101
}
@@ -117,7 +117,7 @@ export const useExternalInternalMapper = (
117117
*/
118118
const emitModelValue = (): void => {
119119
if (monthPicker) {
120-
emit('update:modelValue', getMonthForExternal(internalModelValue.value as Date));
120+
emit('update:modelValue', getMonthVal(internalModelValue.value as Date));
121121
} else if (timePicker) {
122122
emit('update:modelValue', getTImeForExternal(internalModelValue.value));
123123
} else {

0 commit comments

Comments
 (0)