Skip to content

Commit 11bd8bc

Browse files
committed
allow presetDate.value to be functions
1 parent a4b050b commit 11bd8bc

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"date-fns": "^4.1.0"
8585
},
8686
"peerDependencies": {
87-
"vue": ">=3.2.0"
87+
"vue": ">=3.3.0"
8888
},
8989
"engines": {
9090
"node": ">=18.12.0"

src/VueDatePicker/components/DatepickerMenu.vue

+4-4
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
</template>
121121

122122
<script lang="ts" setup>
123-
import { computed, onMounted, onUnmounted, ref, useSlots } from 'vue';
123+
import { computed, onMounted, onUnmounted, ref, toValue, useSlots } from 'vue';
124124
125125
import ActionRow from '@/components/ActionRow.vue';
126126
@@ -135,7 +135,7 @@
135135
import QuarterPicker from '@/components/QuarterPicker/QuarterPicker.vue';
136136
137137
import type { DynamicClass, MenuView, InternalModuleValue, MenuExposedFn, MonthModel } from '@/interfaces';
138-
import type { PropType } from 'vue';
138+
import type { MaybeRefOrGetter, PropType } from 'vue';
139139
import { ArrowDirection, EventKey } from '@/constants';
140140
import { useResponsive } from '@/composables/responsive';
141141
@@ -368,8 +368,8 @@
368368
callChildFn('selectCurrentDate');
369369
};
370370
371-
const presetDate = (value: Date[] | string[] | string | Date, noTz?: boolean) => {
372-
callChildFn('presetDate', value, noTz);
371+
const presetDate = (value: MaybeRefOrGetter<Date[] | string[] | string | Date>, noTz?: boolean) => {
372+
callChildFn('presetDate', toValue(value), noTz);
373373
};
374374
375375
const clearHoverDate = () => {

src/VueDatePicker/interfaces.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ComponentPublicInstance, Ref } from 'vue';
1+
import type { ComponentPublicInstance, Ref, MaybeRefOrGetter } from 'vue';
22
import type { HeaderPicker } from '@/constants';
33
import DatepickerMenu from '@/components/DatepickerMenu.vue';
44
import type DatepickerInput from '@/components/DatepickerInput.vue';
@@ -122,7 +122,7 @@ export type DisabledDatesProp = Date[] | string[] | IDisableDates;
122122

123123
export type PresetDate = {
124124
label: string;
125-
value: Date[] | string[] | Date | string;
125+
value: MaybeRefOrGetter<Date[] | string[] | Date | string>;
126126
style?: Record<string, string>;
127127
slot?: string;
128128
noTz?: boolean;

tests/unit/components/Datepicker.spec.ts

+44-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { flushPromises, mount, VueWrapper } from '@vue/test-utils';
2-
import { describe, it, expect, afterEach } from 'vitest';
2+
import { describe, it, expect, afterEach, vi } from 'vitest';
33
import { nextTick } from 'vue';
44
import {
55
addMonths,
@@ -12,6 +12,8 @@ import {
1212
addDays,
1313
getHours,
1414
startOfMonth,
15+
startOfDay,
16+
endOfDay,
1517
} from 'date-fns';
1618
import { ja } from 'date-fns/locale';
1719

@@ -274,16 +276,55 @@ describe('Logic connection', () => {
274276
const { dp, menu } = await mountDatepicker({
275277
modeValue: null,
276278
range: true,
277-
presetDates: [{ label: 'Today', range }],
279+
presetDates: [{ label: 'Today', value: range, testId: 'TodayPresetBtn' }],
278280
});
279281

280-
menu.vm.presetDate(range);
282+
await menu.find('[data-test-id="TodayPresetBtn"]').trigger('click');
281283

282284
expect(dp.vm.internalModelValue).toHaveLength(2);
283285
expect(dp.vm.internalModelValue).toEqual(range);
284286
dp.unmount();
285287
});
286288

289+
it('Should preset range from preset-dates when range its a function', async () => {
290+
const range = [new Date('2000-01-01T00:00:00'), new Date('2000-12-31T23:59:59')];
291+
const { dp, menu } = await mountDatepicker({
292+
modeValue: null,
293+
range: true,
294+
presetDates: [{ label: 'This Year', value: () => range, testId: 'ThisYearPresetBtn' }],
295+
});
296+
297+
await menu.find('[data-test-id="ThisYearPresetBtn"]').trigger('click');
298+
299+
expect(dp.vm.internalModelValue).toHaveLength(2);
300+
expect(dp.vm.internalModelValue).toEqual(range);
301+
dp.unmount();
302+
});
303+
304+
it('Should correctly set today when preset-dates its a function and selected 2 days later', async () => {
305+
vi.useFakeTimers()
306+
vi.setSystemTime(new Date('2000-01-01T00:00:00.000Z'))
307+
308+
const { dp, menu } = await mountDatepicker({
309+
modeValue: null,
310+
range: true,
311+
presetDates: [{ label: 'Today', value: () => [startOfDay(new Date()), endOfDay(new Date())], testId: 'TodayPresetBtn' }],
312+
});
313+
314+
await menu.find('[data-test-id="TodayPresetBtn"]').trigger('click');
315+
316+
expect(dp.vm.internalModelValue).toEqual([startOfDay(new Date('2000-01-01T00:00:00.000Z')), endOfDay(new Date('2000-01-01T00:00:00.000Z'))]);
317+
318+
setTimeout(async () => await menu.find('[data-test-id="TodayPresetBtn"]').trigger('click'), 1000 * 60 * 60 * 48) //
319+
320+
vi.runAllTimers()
321+
322+
expect(dp.vm.internalModelValue).toEqual([startOfDay(new Date('2000-01-03T00:00:00.000Z')), endOfDay(new Date('2000-01-03T00:00:00.000Z'))]);
323+
324+
dp.unmount();
325+
vi.useRealTimers()
326+
});
327+
287328
it('Should select week', async () => {
288329
const start = startOfMonth(new Date());
289330

0 commit comments

Comments
 (0)