Skip to content

Commit 7354fc6

Browse files
committed
feat: Add model-value mapped value in internal-model-changed event (resolves #808)
1 parent f0f35c6 commit 7354fc6

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ export interface VueDatePickerProps {
323323
quarterPicker?: boolean;
324324
yearFirst?: boolean;
325325
loading?: boolean;
326+
onInternalModelChange?: (...args: any[]) => void;
326327
}
327328

328329
export type DatePickerInstance = ComponentPublicInstance<PublicMethods> | null;

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

+23-11
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ export const useExternalInternalMapper = (emit: VueEmit, props: AllPropsType, is
3737
watch(
3838
internalModelValue,
3939
() => {
40-
emit('internal-model-change', internalModelValue.value);
40+
if (typeof props.onInternalModelChange === 'function') {
41+
emit('internal-model-change', internalModelValue.value, emitModelValue(true));
42+
}
4143
},
4244
{ deep: true },
4345
);
@@ -228,8 +230,10 @@ export const useExternalInternalMapper = (emit: VueEmit, props: AllPropsType, is
228230
((internalModelValue.value as Date[]) || []).map((date) => toModelType(date) as string);
229231

230232
// Parent internal to external function mapper that will return proper date format based on provided config
231-
const mapInternalDatesToExternal = () => {
232-
sanitizeModelValue();
233+
const mapInternalDatesToExternal = (noSanitize: boolean = false) => {
234+
if (!noSanitize) {
235+
sanitizeModelValue();
236+
}
233237
if (props.modelAuto) return getModelAutoForExternal();
234238
if (defaultedMultiDates.value.enabled) return getMultiDatesForExternal();
235239
if (Array.isArray(internalModelValue.value)) {
@@ -343,7 +347,8 @@ export const useExternalInternalMapper = (emit: VueEmit, props: AllPropsType, is
343347
return convertZonedModelToLocal(val);
344348
};
345349

346-
const emitValue = (value: ModelValue, useTz = false): void => {
350+
const emitValue = (value: ModelValue, useTz = false, returnOnly = false) => {
351+
if (returnOnly) return value;
347352
emit('update:model-value', value);
348353
if (defaultedTz.value.emitTimezone && useTz) {
349354
const zonedValue = Array.isArray(value)
@@ -384,20 +389,27 @@ export const useExternalInternalMapper = (emit: VueEmit, props: AllPropsType, is
384389
};
385390

386391
// eslint-disable-next-line @typescript-eslint/no-explicit-any
387-
const modeEmitter = (mapper: any) => emitValue(convertType(mapInternalToSpecificExternal(mapper)));
392+
const modeEmitter = (mapper: any, returnOnly: boolean) =>
393+
emitValue(convertType(mapInternalToSpecificExternal(mapper)), false, returnOnly);
394+
395+
const emitWeekPicker = (returnOnly: boolean) => {
396+
const value = mapInternalWeekPickerToExternal();
397+
if (returnOnly) return value;
398+
return emit('update:model-value', mapInternalWeekPickerToExternal());
399+
};
388400

389401
/**
390402
* When date is selected, emit event to update modelValue on external,
391403
* and format input value
392404
*/
393-
const emitModelValue = (): void => {
405+
const emitModelValue = (returnOnly: boolean = false) => {
394406
formatInputValue();
395407

396-
if (props.monthPicker) return modeEmitter(getMonthVal);
397-
if (props.timePicker) return modeEmitter(getTimeVal);
398-
if (props.yearPicker) return modeEmitter(getYear);
399-
if (props.weekPicker) return emit('update:model-value', mapInternalWeekPickerToExternal());
400-
return emitValue(mapInternalDatesToExternal(), true);
408+
if (props.monthPicker) return modeEmitter(getMonthVal, returnOnly);
409+
if (props.timePicker) return modeEmitter(getTimeVal, returnOnly);
410+
if (props.yearPicker) return modeEmitter(getYear, returnOnly);
411+
if (props.weekPicker) return emitWeekPicker(returnOnly);
412+
return emitValue(mapInternalDatesToExternal(returnOnly), true, returnOnly);
401413
};
402414

403415
// Check if there is any selection before emitting value, to prevent null setting

src/VueDatePicker/props.ts

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export const AllProps = {
163163
quarterPicker: { type: Boolean as PropType<boolean>, default: false },
164164
yearFirst: { type: Boolean as PropType<boolean>, default: false },
165165
loading: { type: Boolean as PropType<boolean>, default: false },
166+
onInternalModelChange: { type: [Function, Object] as PropType<(...args: any[]) => void>, default: null },
166167
};
167168

168169
export const PickerBaseProps = {

0 commit comments

Comments
 (0)