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

Commit f3b13ef

Browse files
committed
fix: Add missing AM/PM logic
1 parent 3dff975 commit f3b13ef

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

src/Vue3DatePicker/components/TimePicker/TimeInput.vue

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
<ChevronDownIcon />
2222
</div>
2323
</div>
24+
<div v-if="!is24">
25+
<button class="dp__pm_am_button" @click="setAmPm">{{ amPm }}</button>
26+
</div>
2427
<SelectionGrid
2528
v-if="hourOverlay"
2629
@update:modelValue="$emit('update:hours', $event)"
@@ -56,7 +59,7 @@
5659
import { computed, defineComponent, PropType, ref, toRef } from 'vue';
5760
import { ChevronUpIcon, ChevronDownIcon, ClockIcon } from '../Icons';
5861
import { IDateFilter, IDefaultSelect, ITimeRange, TimeInputProps } from '../../interfaces';
59-
import { getArrayInArray } from '../../utils/util';
62+
import { getArrayInArray, hoursToAmPmHours } from '../../utils/util';
6063
import SelectionGrid from '../SelectionGrid.vue';
6164
6265
export default defineComponent({
@@ -86,9 +89,11 @@
8689
const minuteOverlay = ref(false);
8790
const hours = toRef(props, 'hours');
8891
const minutes = toRef(props, 'minutes');
92+
const amPm = ref('AM');
8993
9094
const hourDisplay = computed((): string => {
91-
return hours.value < 10 ? `0${hours.value}` : `${hours.value}`;
95+
const hour = convert24ToAmPm(hours.value);
96+
return hour < 10 ? `0${hour}` : `${hour}`;
9297
});
9398
9499
const minuteDisplay = computed((): string => {
@@ -177,6 +182,28 @@
177182
}
178183
};
179184
185+
const convert24ToAmPm = (time: number): number => {
186+
if (props.is24) {
187+
return time;
188+
}
189+
if (time >= 12) {
190+
amPm.value = 'PM';
191+
} else {
192+
amPm.value = 'AM';
193+
}
194+
return hoursToAmPmHours(time);
195+
};
196+
197+
const setAmPm = () => {
198+
if (amPm.value === 'PM') {
199+
amPm.value = 'AM';
200+
emit('update:hours', hours.value - 12);
201+
} else {
202+
amPm.value = 'PM';
203+
emit('update:hours', hours.value + 12);
204+
}
205+
};
206+
180207
return {
181208
showTimePicker,
182209
hourOverlay,
@@ -189,6 +216,8 @@
189216
getMinutesGridItems,
190217
handleHours,
191218
handleMinutes,
219+
setAmPm,
220+
amPm,
192221
};
193222
},
194223
});

src/Vue3DatePicker/components/TimePicker/TimePicker.vue

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@
5050
:min-time="minTime"
5151
/>
5252
</template>
53-
<!-- todo - add this -->
54-
<!-- <div v-if="!is24" class="dp__pm_am_button">-->
55-
<!-- <div><button>PM</button></div>-->
56-
<!-- </div>-->
5753
</div>
5854
<div class="dp__button" @click="toggleTimePicker(false)"><CalendarIcon /></div>
5955
</div>

src/Vue3DatePicker/style/components/_TimeInput.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,12 @@
3737
color: var(--dp-primary-color);
3838
}
3939
}
40+
41+
.dp__pm_am_button {
42+
background: var(--dp-primary-color);
43+
color: var(--dp-primary-text-color);
44+
border: none;
45+
padding: 10px;
46+
border-radius: $dp__border_radius;
47+
cursor: pointer;
48+
}

src/Vue3DatePicker/utils/util.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,12 @@ export const getMonthNames = (locale: string): string[] => {
157157
});
158158
return months.map((date) => formatter.format(date).slice(0, 3));
159159
};
160+
161+
/**
162+
* Since internally watched values are in 24h mode, this will get am-pm value from set hour
163+
*/
164+
export const hoursToAmPmHours = (index: number): number => {
165+
const hoursValues = [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
166+
167+
return hoursValues[index];
168+
};

0 commit comments

Comments
 (0)