Skip to content

Commit

Permalink
feat: Add iso support in model-type (resolves #804)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasenkoo committed Apr 4, 2024
1 parent 48115f4 commit eae475c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export interface VueDatePickerProps {
*/
disableTimeRangeValidation?: boolean;
dayNames?: ((lang: string, weekStart: number) => string[]) | string[];
modelType?: 'timestamp' | 'format' | string;
modelType?: 'timestamp' | 'iso' | 'format' | string;
modelAuto?: boolean;
highlight?:
| ((date: Date[], disabled?: boolean) => boolean)
Expand Down
12 changes: 9 additions & 3 deletions src/VueDatePicker/composables/external-internal-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { ModelValue, VueEmit, TimeModel, MonthModel, ModelTypeConverted } f
import type { AllPropsType } from '@/props';
import type { Ref } from 'vue';
import { getTimezoneOffset, localToTz } from '@/utils/timezone';
import { modelTypePredefined } from '@/constants';

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

if (props.modelType === 'format' && (typeof props.format === 'string' || !props.format))
return convertModelToTz(parse(value as string, getDefaultPattern(), new Date(), { locale: formatLocale.value }));
return convertModelToTz(
parse(value as string, getDefaultPattern(), new Date(), { locale: formatLocale.value }),
);

return convertModelToTz(parse(value as string, props.modelType, new Date(), { locale: formatLocale.value }));
return convertModelToTz(
parse(value as string, props.modelType, new Date(), { locale: formatLocale.value }),
);
}

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

if (props.modelType === 'format' && (typeof props.format === 'string' || !props.format))
return formatDateFn(convertZonedModelToLocal(val));
Expand Down
2 changes: 2 additions & 0 deletions src/VueDatePicker/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ export enum FlowStep {
hours = 'hours',
seconds = 'seconds',
}

export const modelTypePredefined = ['timestamp', 'date', 'iso'];
7 changes: 7 additions & 0 deletions tests/unit/v-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ describe('v-model mapping', () => {
secondWrapper.vm.emitModelValue();
expect(secondWrapper.emitted()).toHaveProperty('update:model-value');
expect((secondWrapper.emitted()['update:model-value'][0] as any)[0]).toEqual(format(date, 'dd.mm.yyyy'));

const thirdWrapper = shallowMountDp({ modelType: 'iso' });
thirdWrapper.vm.internalModelValue = date;
thirdWrapper.vm.emitModelValue();

expect(thirdWrapper.emitted()).toHaveProperty('update:model-value');
expect(thirdWrapper.emitted()['update:model-value']).toEqual([[date.toISOString()]]);
});

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

0 comments on commit eae475c

Please sign in to comment.