Skip to content

Commit eae475c

Browse files
committed
feat: Add iso support in model-type (resolves #804)
1 parent 48115f4 commit eae475c

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ export interface VueDatePickerProps {
270270
*/
271271
disableTimeRangeValidation?: boolean;
272272
dayNames?: ((lang: string, weekStart: number) => string[]) | string[];
273-
modelType?: 'timestamp' | 'format' | string;
273+
modelType?: 'timestamp' | 'iso' | 'format' | string;
274274
modelAuto?: boolean;
275275
highlight?:
276276
| ((date: Date[], disabled?: boolean) => boolean)

src/VueDatePicker/composables/external-internal-mapper.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type { ModelValue, VueEmit, TimeModel, MonthModel, ModelTypeConverted } f
1919
import type { AllPropsType } from '@/props';
2020
import type { Ref } from 'vue';
2121
import { getTimezoneOffset, localToTz } from '@/utils/timezone';
22+
import { modelTypePredefined } from '@/constants';
2223

2324
/**
2425
* Handles values from external to internal and vise versa
@@ -310,12 +311,16 @@ export const useExternalInternalMapper = (emit: VueEmit, props: AllPropsType, is
310311
return props.utc === 'preserve' ? new Date(toDate.getTime() + toDate.getTimezoneOffset() * 60000) : toDate;
311312
}
312313
if (props.modelType) {
313-
if (props.modelType === 'date' || props.modelType === 'timestamp') return convertModelToTz(new Date(value));
314+
if (modelTypePredefined.includes(props.modelType)) return convertModelToTz(new Date(value));
314315

315316
if (props.modelType === 'format' && (typeof props.format === 'string' || !props.format))
316-
return convertModelToTz(parse(value as string, getDefaultPattern(), new Date(), { locale: formatLocale.value }));
317+
return convertModelToTz(
318+
parse(value as string, getDefaultPattern(), new Date(), { locale: formatLocale.value }),
319+
);
317320

318-
return convertModelToTz(parse(value as string, props.modelType, new Date(), { locale: formatLocale.value }));
321+
return convertModelToTz(
322+
parse(value as string, props.modelType, new Date(), { locale: formatLocale.value }),
323+
);
319324
}
320325

321326
return convertModelToTz(new Date(value));
@@ -328,6 +333,7 @@ export const useExternalInternalMapper = (emit: VueEmit, props: AllPropsType, is
328333
}
329334
if (props.modelType) {
330335
if (props.modelType === 'timestamp') return +convertZonedModelToLocal(val);
336+
if (props.modelType === 'iso') return convertZonedModelToLocal(val).toISOString();
331337

332338
if (props.modelType === 'format' && (typeof props.format === 'string' || !props.format))
333339
return formatDateFn(convertZonedModelToLocal(val));

src/VueDatePicker/constants/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export enum FlowStep {
2323
hours = 'hours',
2424
seconds = 'seconds',
2525
}
26+
27+
export const modelTypePredefined = ['timestamp', 'date', 'iso'];

tests/unit/v-model.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ describe('v-model mapping', () => {
249249
secondWrapper.vm.emitModelValue();
250250
expect(secondWrapper.emitted()).toHaveProperty('update:model-value');
251251
expect((secondWrapper.emitted()['update:model-value'][0] as any)[0]).toEqual(format(date, 'dd.mm.yyyy'));
252+
253+
const thirdWrapper = shallowMountDp({ modelType: 'iso' });
254+
thirdWrapper.vm.internalModelValue = date;
255+
thirdWrapper.vm.emitModelValue();
256+
257+
expect(thirdWrapper.emitted()).toHaveProperty('update:model-value');
258+
expect(thirdWrapper.emitted()['update:model-value']).toEqual([[date.toISOString()]]);
252259
});
253260

254261
it('Should emit single date on model-auto 1 selection', () => {

0 commit comments

Comments
 (0)