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

Commit d83550b

Browse files
committed
fix: Hide time input when fixed range is used (fixes #103)
- Component is refactored a little to remove redundant code
1 parent 79e7681 commit d83550b

File tree

2 files changed

+47
-46
lines changed

2 files changed

+47
-46
lines changed

src/Vue3DatePicker/components/TimePicker/TimeInput.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div class="dp__time_input">
2+
<div class="dp__time_input" v-if="!disabled">
33
<div :class="timeColClass">
44
<div
55
class="dp__inc_dec_button"
@@ -217,6 +217,7 @@
217217
noMinutesOverlay: { type: Boolean as PropType<boolean>, default: false },
218218
noSecondsOverlay: { type: Boolean as PropType<boolean>, default: false },
219219
enableSeconds: { type: Boolean as PropType<boolean>, default: false },
220+
disabled: { type: Boolean as PropType<boolean>, default: false },
220221
});
221222
222223
const hourOverlay = ref(false);

src/Vue3DatePicker/components/TimePicker/TimePicker.vue

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,51 +28,22 @@
2828
></slot>
2929
<template v-if="!$slots['time-picker-overlay']">
3030
<div class="dp__overlay_row">
31-
<template v-if="!range">
32-
<TimeInput
33-
:hours="hours"
34-
:minutes="minutes"
35-
:seconds="seconds"
36-
v-bind="timeInputProps"
37-
ref="timeInputRef"
38-
@update:hours="updateHours($event)"
39-
@update:minutes="updateMinutes($event)"
40-
@update:seconds="updateSeconds($event)"
41-
@reset-flow="$emit('reset-flow')"
42-
>
43-
<template v-for="(slot, i) in timeInputSlots" #[slot]="args" :key="i">
44-
<slot :name="slot" v-bind="args" />
45-
</template>
46-
</TimeInput>
47-
</template>
48-
<template v-if="range">
49-
<TimeInput
50-
:hours="hours[0]"
51-
:minutes="minutes[0]"
52-
:seconds="seconds[0]"
53-
v-bind="timeInputProps"
54-
@update:hours="updateHours([$event, hours[1]])"
55-
@update:minutes="updateMinutes([$event, minutes[1]])"
56-
@update:seconds="updateSeconds([$event, seconds[1]])"
57-
>
58-
<template v-for="(slot, i) in timeInputSlots" #[slot]="args" :key="i">
59-
<slot :name="slot" v-bind="args" />
60-
</template>
61-
</TimeInput>
62-
<TimeInput
63-
:hours="hours[1]"
64-
:minutes="minutes[1]"
65-
:seconds="seconds[1]"
66-
v-bind="timeInputProps"
67-
@update:hours="updateHours([hours[0], $event])"
68-
@update:minutes="updateMinutes([minutes[0], $event])"
69-
@update:seconds="updateSeconds([seconds[0], $event])"
70-
>
71-
<template v-for="(slot, i) in timeInputSlots" #[slot]="args" :key="i">
72-
<slot :name="slot" v-bind="args" />
73-
</template>
74-
</TimeInput>
75-
</template>
31+
<TimeInput
32+
v-for="(tInput, index) in timeInputs"
33+
:key="index"
34+
:disabled="index === 0 ? fixedStart : fixedEnd"
35+
:hours="tInput.hours"
36+
:minutes="tInput.minutes"
37+
:seconds="tInput.seconds"
38+
v-bind="timeInputProps"
39+
@update:hours="updateHours(getEvent($event, index, 'hours'))"
40+
@update:minutes="updateMinutes(getEvent($event, index, 'minutes'))"
41+
@update:seconds="updateSeconds(getEvent($event, index, 'seconds'))"
42+
>
43+
<template v-for="(slot, i) in timeInputSlots" #[slot]="args" :key="i">
44+
<slot :name="slot" v-bind="args" />
45+
</template>
46+
</TimeInput>
7647
</div>
7748
</template>
7849
<div
@@ -124,6 +95,8 @@
12495
noSecondsOverlay: { type: Boolean as PropType<boolean>, default: false },
12596
customProps: { type: Object as PropType<Record<string, unknown>>, default: null },
12697
enableSeconds: { type: Boolean as PropType<boolean>, default: false },
98+
fixedStart: { type: Boolean as PropType<boolean>, default: false },
99+
fixedEnd: { type: Boolean as PropType<boolean>, default: false },
127100
});
128101
const slots = useSlots();
129102
const autoApply = inject('autoApply', false);
@@ -137,6 +110,26 @@
137110
138111
const showTimePicker = ref(false);
139112
113+
const getTimeInput = (i: number) => {
114+
return {
115+
hours: Array.isArray(props.hours) ? props.hours[i] : props.hours,
116+
minutes: Array.isArray(props.minutes) ? props.minutes[i] : props.minutes,
117+
seconds: Array.isArray(props.seconds) ? props.seconds[i] : props.seconds,
118+
};
119+
};
120+
121+
const timeInputs = computed((): { hours: number; minutes: number; seconds: number }[] => {
122+
const arr = [];
123+
if (props.range) {
124+
for (let i = 0; i < 2; i++) {
125+
arr.push(getTimeInput(i));
126+
}
127+
} else {
128+
arr.push(getTimeInput(0));
129+
}
130+
return arr;
131+
});
132+
140133
const toggleTimePicker = (show: boolean, flow = false, childOpen = ''): void => {
141134
if (!flow) {
142135
emit('reset-flow');
@@ -172,6 +165,13 @@
172165
enableSeconds: props.enableSeconds,
173166
}));
174167
168+
const getEvent = (event: number, index: number, property: 'hours' | 'minutes' | 'seconds') => {
169+
if (index === 0) {
170+
return [event, timeInputs.value[1][property]];
171+
}
172+
return [timeInputs.value[0][property], event];
173+
};
174+
175175
const updateHours = (hours: number | number[]): void => {
176176
emit('update:hours', hours);
177177
};

0 commit comments

Comments
 (0)