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

Commit 98fe5ae

Browse files
committed
feat: Allow partial range selection (#42)
1 parent 4c83702 commit 98fe5ae

File tree

6 files changed

+19
-6
lines changed

6 files changed

+19
-6
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ interface Vue3DatePicker {
138138
allowedDates?: string[] | Date[];
139139
showNowButton?: boolean;
140140
nowButtonLabel?: string;
141+
partialRange?: boolean;
141142
}
142143

143144
interface PublicMethods extends MethodOptions {

src/Vue3DatePicker/Vue3DatePicker.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
allowedDates: { type: Array as PropType<string[] | Date[]>, default: () => [] },
199199
showNowButton: { type: Boolean as PropType<boolean>, default: false },
200200
nowButtonLabel: { type: String as PropType<string>, default: 'Now' },
201+
partialRange: { type: Boolean as PropType<boolean>, default: true },
201202
});
202203
const slots = useSlots();
203204
const isOpen = ref(false);
@@ -248,6 +249,7 @@
248249
props.timePicker,
249250
props.monthPicker,
250251
props.range,
252+
props.partialRange,
251253
props.is24,
252254
props.enableTimePicker,
253255
emit,

src/Vue3DatePicker/components/ActionRow.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
if (!props.internalModelValue || !props.menuMount) return '';
5151
if (typeof props.previewFormat === 'string') {
5252
if (isModelValueRange(props.internalModelValue)) {
53-
if (props.internalModelValue.length === 2) {
53+
if (props.internalModelValue.length === 2 && props.internalModelValue[1]) {
5454
if (props.twoCalendars) {
5555
return `${formatDate(props.internalModelValue[0], props.previewFormat)} - ${formatDate(
5656
props.internalModelValue[1],

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
setDateMonthOrYear,
1111
} from '../../utils/date-utils';
1212
import { IFormat, ModelValue, VueEmit } from '../../interfaces';
13-
import { isMonth, isRange, isSingle, isTime, isTimeArray } from '../../utils/type-guard';
13+
import { isMonth, isRangeArray, isSingle, isTime, isTimeArray } from '../../utils/type-guard';
1414

1515
interface IExternalInternalMapper {
1616
parseExternalModelValue: (value: ModelValue) => void;
@@ -29,6 +29,7 @@ export const useExternalInternalMapper = (
2929
timePicker: boolean,
3030
monthPicker: boolean,
3131
range: boolean,
32+
partialRange: boolean,
3233
is24: boolean,
3334
enableTimePicker: boolean,
3435
emit: VueEmit,
@@ -58,8 +59,8 @@ export const useExternalInternalMapper = (
5859
mappedDate = setDateMonthOrYear(null, +value.month, +value.year);
5960
}
6061
} else if (range) {
61-
if (isRange(value)) {
62-
mappedDate = [new Date(value[0]), new Date(value[1])];
62+
if (isRangeArray(value, partialRange)) {
63+
mappedDate = [new Date(value[0]), value[1] ? new Date(value[1]) : (null as unknown as Date)];
6364
}
6465
} else if (isSingle(value)) {
6566
mappedDate = new Date(value);
@@ -96,6 +97,7 @@ export const useExternalInternalMapper = (
9697
const checkBeforeEmit = (): boolean => {
9798
if (internalModelValue.value) {
9899
if (range) {
100+
if (partialRange) return internalModelValue.value.length >= 1;
99101
return internalModelValue.value.length === 2;
100102
}
101103
return !!internalModelValue.value;
@@ -112,6 +114,9 @@ export const useExternalInternalMapper = (
112114
} else if (timePicker) {
113115
emit('update:modelValue', getTImeForExternal(internalModelValue.value));
114116
} else {
117+
if (range && partialRange && internalModelValue.value.length === 1) {
118+
internalModelValue.value.push(null);
119+
}
115120
emit('update:modelValue', internalModelValue.value);
116121
}
117122
formatInputValue();

src/Vue3DatePicker/utils/date-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const parseDate = (date: string, pattern: string): Date => {
6161
*/
6262
export const isValidDate = (value: Date | Date[] | null): boolean => {
6363
if (Array.isArray(value)) {
64-
return isValid(value[0]) && isValid(value[1]);
64+
return isValid(value[0]) && (value[1] ? isValid(value[1]) : true);
6565
}
6666
return value ? isValid(value) : false;
6767
};
@@ -157,7 +157,7 @@ export const getMonthForExternal = (date: Date): IMonthValue => {
157157
*/
158158
export const formatDate = (value: Date | Date[], pattern: string): string => {
159159
if (Array.isArray(value)) {
160-
return `${format(value[0], pattern)} - ${format(value[1], pattern)}`;
160+
return `${format(value[0], pattern)} - ${value[1] ? format(value[1], pattern) : ''}`;
161161
}
162162

163163
return format(value, pattern);

src/Vue3DatePicker/utils/type-guard.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export const isRange = (value: ModelValue): value is Date[] => {
3030
return Array.isArray(value) && value.length === 2;
3131
};
3232

33+
export const isRangeArray = (value: ModelValue, partialRange: boolean): value is Date[] => {
34+
if (partialRange) return Array.isArray(value);
35+
return isRange(value);
36+
};
37+
3338
export const isTimeArr = (value: ITimeValue | ITimeValue[]): value is ITimeValue[] => {
3439
return Array.isArray(value);
3540
};

0 commit comments

Comments
 (0)