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

Commit dea2423

Browse files
committed
refactor: Additional validation for min and max time
- Time will be preset to current time
1 parent bd828d2 commit dea2423

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/Vue3DatePicker/components/Calendar.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,22 @@
181181
}
182182
183183
weekDays.value = getDayNames(props.locale, +props.weekStart);
184+
const date = new Date();
184185
185186
if (singleModelValue.value) {
186187
hoursSingle.value = singleModelValue.value.getHours();
187188
minutesSingle.value = singleModelValue.value.getMinutes();
189+
} else {
190+
hoursSingle.value = date.getHours();
191+
minutesSingle.value = date.getMinutes();
188192
}
189193
190194
if (rangeModelValue.value && rangeModelValue.value[0] && rangeModelValue.value[1]) {
191195
hoursRange.value = [rangeModelValue.value[0].getHours(), rangeModelValue.value[1].getHours()];
192196
minutesRange.value = [rangeModelValue.value[0].getMinutes(), rangeModelValue.value[1].getMinutes()];
197+
} else {
198+
hoursRange.value = [date.getHours(), date.getHours()];
199+
minutesRange.value = [date.getMinutes(), date.getMinutes()];
193200
}
194201
});
195202

src/Vue3DatePicker/components/TimePicker/TimeInput.vue

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
</template>
5757

5858
<script lang="ts">
59-
import { computed, defineComponent, PropType, ref, toRef } from 'vue';
59+
import { computed, defineComponent, onMounted, PropType, ref, toRef } from 'vue';
6060
import { ChevronUpIcon, ChevronDownIcon, ClockIcon } from '../Icons';
6161
import { IDateFilter, IDefaultSelect, ITimeRange, TimeInputProps } from '../../interfaces';
6262
import { getArrayInArray, hoursToAmPmHours } from '../../utils/util';
@@ -91,6 +91,10 @@
9191
const minutes = toRef(props, 'minutes');
9292
const amPm = ref('AM');
9393
94+
onMounted(() => {
95+
checkMinMaxHours();
96+
});
97+
9498
const hourDisplay = computed((): string => {
9599
const hour = convert24ToAmPm(hours.value);
96100
return hour < 10 ? `0${hour}` : `${hour}`;
@@ -127,13 +131,37 @@
127131
minuteOverlay.value = !minuteOverlay.value;
128132
};
129133
134+
const checkMinMaxHours = (): void => {
135+
if (Object.keys(props.maxTime).length) {
136+
if (props.maxTime.hours && hours.value > props.maxTime.hours) {
137+
emit('update:hours', +props.maxTime.hours);
138+
}
139+
if (props.maxTime.minutes && minutes.value > props.maxTime.minutes) {
140+
emit('update:minutes', +props.maxTime.minutes);
141+
}
142+
}
143+
if (Object.keys(props.minTime).length) {
144+
if (props.minTime.hours && hours.value < props.minTime.hours) {
145+
emit('update:hours', +props.minTime.hours);
146+
}
147+
if (props.minTime.minutes && minutes.value < props.minTime.minutes) {
148+
emit('update:minutes', +props.minTime.minutes);
149+
}
150+
}
151+
};
152+
130153
const handleHours = (type: string): void => {
131154
if (type === 'increment') {
132155
if (props.maxTime.hours) {
133156
if (hours.value + +props.hoursIncrement > +props.maxTime.hours) {
134157
return;
135158
}
136159
}
160+
if (props.minTime.hours) {
161+
if (hours.value + +props.hoursIncrement < +props.minTime.hours) {
162+
return;
163+
}
164+
}
137165
if (
138166
(props.is24 && hours.value + +props.hoursIncrement >= 24) ||
139167
(!props.is24 && hours.value + +props.hoursIncrement >= 12)
@@ -148,6 +176,11 @@
148176
return;
149177
}
150178
}
179+
if (props.maxTime.hours) {
180+
if (hours.value - +props.hoursIncrement > +props.maxTime.hours) {
181+
return;
182+
}
183+
}
151184
if (hours.value - +props.hoursIncrement < 0) {
152185
emit('update:hours', props.is24 ? 24 - +props.hoursIncrement : 12 - -+props.hoursIncrement);
153186
} else {
@@ -163,6 +196,11 @@
163196
return;
164197
}
165198
}
199+
if (props.minTime.minutes) {
200+
if (minutes.value + +props.minutesIncrement < +props.minTime.minutes) {
201+
return;
202+
}
203+
}
166204
if (minutes.value + +props.minutesIncrement >= 60) {
167205
emit('update:minutes', 0);
168206
} else {
@@ -174,6 +212,11 @@
174212
return;
175213
}
176214
}
215+
if (props.maxTime.minutes) {
216+
if (minutes.value + +props.minutesIncrement > +props.maxTime.minutes) {
217+
return;
218+
}
219+
}
177220
if (minutes.value - +props.minutesIncrement < 0) {
178221
emit('update:minutes', 60 - +props.minutesIncrement);
179222
} else {

0 commit comments

Comments
 (0)