add new datepicker to time entry picker, add am/pm support

This commit is contained in:
Gregor Vostrak
2025-05-20 12:53:51 +02:00
parent 15411ec0c8
commit 0bd32dee39
5 changed files with 189 additions and 137 deletions

View File

@@ -119,7 +119,8 @@ function deleteSelected() {
</script> </script>
<template> <template>
<TimeEntryCreateModal <AppLayout title="Dashboard" data-testid="time_view">
<TimeEntryCreateModal
v-model:show="showManualTimeEntryModal" v-model:show="showManualTimeEntryModal"
:enable-estimated-time="isAllowedToPerformPremiumAction()" :enable-estimated-time="isAllowedToPerformPremiumAction()"
:create-project="createProject" :create-project="createProject"
@@ -130,7 +131,6 @@ function deleteSelected() {
:tasks :tasks
:tags :tags
:clients></TimeEntryCreateModal> :clients></TimeEntryCreateModal>
<AppLayout title="Dashboard" data-testid="time_view">
<MainContainer <MainContainer
class="pt-5 lg:pt-8 pb-4 lg:pb-6"> class="pt-5 lg:pt-8 pb-4 lg:pb-6">
<div <div

View File

@@ -1,76 +1,84 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, watch } from 'vue';
import { import {
getDayJsInstance, Popover,
getLocalizedDayJs, PopoverContent,
} from '@/packages/ui/src/utils/time'; PopoverTrigger,
import { twMerge } from 'tailwind-merge'; } from '@/Components/ui/popover';
import { Button } from '@/Components/ui/button';
import { Calendar } from '@/Components/ui/calendar';
import { CalendarIcon } from 'lucide-vue-next';
import { formatDateLocalized } from '@/packages/ui/src/utils/time';
import { parseDate, type DateValue } from '@internationalized/date';
import { computed, inject, type ComputedRef } from 'vue';
import { type Organization } from '@/packages/api/src';
import { getLocalizedDayJs } from '@/packages/ui/src/utils/time';
const props = defineProps<{ const props = defineProps<{
class?: string; class?: string;
tabindex?: string; tabindex?: string;
}>(); }>();
// This has to be a localized timestamp, not UTC const model = defineModel<string | null>();
const model = defineModel<string | null>({ const emit = defineEmits<{
default: null, changed: [string];
}); }>();
const tempDate = ref(getLocalizedDayJs(model.value).format('YYYY-MM-DD')); const handleChange = (date: DateValue | undefined) => {
if (!date) {
watch(model, (value) => { model.value = null;
tempDate.value = getLocalizedDayJs(value).format('YYYY-MM-DD'); return;
});
function updateDate(event: Event) {
const target = event.target as HTMLInputElement;
const newValue = target.value;
const newDate = getDayJsInstance()(newValue);
if (newDate.isValid()) {
model.value = getLocalizedDayJs(model.value)
.set('year', newDate.year())
.set('month', newDate.month())
.set('date', newDate.date())
.format();
emit('changed', model.value);
} }
}
const datePicker = ref<HTMLInputElement | null>(null); const dayjs = getLocalizedDayJs(model.value);
model.value = dayjs
.year(date.year)
.month(date.month - 1) // CalendarDate uses 1-based months
.date(date.day)
.format();
emit('changed', model.value);
};
function updateTempValue(event: Event) { const date = computed(() => {
const target = event.target as HTMLInputElement; return model.value
tempDate.value = target.value; ? parseDate(getLocalizedDayJs(model.value).format('YYYY-MM-DD'))
} : undefined;
});
const emit = defineEmits(['changed']); const organization = inject<ComputedRef<Organization>>('organization');
</script> </script>
<template> <template>
<div class="flex items-center text-text-secondary"> <Popover>
<input <PopoverTrigger as-child>
id="start" <Button
ref="datePicker" variant="input"
:tabindex="tabindex" size="sm"
:class=" :class="[
twMerge( 'w-full gap-1.5 justify-center text-left font-normal',
'bg-input-background border text-text-primary border-input-border focus-visible:outline-0 focus-visible:border-input-border-active focus-visible:ring-0 rounded-md', !model && 'text-muted-foreground',
props.class props.class,
) ]"
" :tabindex="tabindex">
type="date" <CalendarIcon class="h-3 w-3" />
name="trip-start" <span class="text-center">
:value="tempDate" {{
@change="updateTempValue" model
@blur="updateDate" ? formatDateLocalized(
@keydown.enter="updateDate" /> model,
</div> organization?.date_format
)
: 'Pick a date'
}}
</span>
</Button>
</PopoverTrigger>
<PopoverContent class="w-auto p-0">
<Calendar
mode="single"
:model-value="date"
:initial-focus="true"
@update:model-value="handleChange"
/>
</PopoverContent>
</Popover>
</template> </template>
<style scoped>
input::-webkit-calendar-picker-indicator {
filter: invert(1);
opacity: 0.2;
}
</style>

View File

@@ -1,15 +1,18 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, watch } from 'vue'; import { ref, watch, inject, type ComputedRef } from 'vue';
import { getLocalizedDayJs } from '@/packages/ui/src/utils/time'; import { getLocalizedDayJs, formatTime } from '@/packages/ui/src/utils/time';
import { useFocus } from '@vueuse/core'; import { useFocus } from '@vueuse/core';
import { TextInput } from '@/packages/ui/src'; import { TextInput } from '@/packages/ui/src';
import { twMerge } from 'tailwind-merge'; import { twMerge } from 'tailwind-merge';
import type { Organization } from '@/packages/api/src';
// This has to be a localized timestamp, not UTC // This has to be a localized timestamp, not UTC
const model = defineModel<string | null>({ const model = defineModel<string | null>({
default: null, default: null,
}); });
const organization = inject<ComputedRef<Organization>>('organization');
const props = withDefaults( const props = withDefaults(
defineProps<{ defineProps<{
size?: 'base' | 'large'; size?: 'base' | 'large';
@@ -24,62 +27,95 @@ const props = withDefaults(
function updateTime(event: Event) { function updateTime(event: Event) {
const target = event.target as HTMLInputElement; const target = event.target as HTMLInputElement;
const newValue = target.value.trim(); const newValue = target.value.trim();
// Get current hours and minutes for comparison
const currentTime = model.value ? getLocalizedDayJs(model.value) : null;
const currentHours = currentTime?.hour() ?? 0;
const currentMinutes = currentTime?.minute() ?? 0;
// Handle AM/PM format
const amPmMatch = newValue.match(/^(\d{1,2}):?(\d{2})?\s*(AM|PM|am|pm)$/);
if (amPmMatch) {
let hours = amPmMatch[1];
const minutes = amPmMatch[2] ?? '00';
const period = amPmMatch[3];
hours = parseInt(hours).toString();
if (period.toUpperCase() === 'PM' && hours !== '12') {
hours = (parseInt(hours) + 12).toString();
} else if (period.toUpperCase() === 'AM' && hours === '12') {
hours = '0';
}
const newHours = parseInt(hours);
const newMinutes = parseInt(minutes);
if (newHours !== currentHours || newMinutes !== currentMinutes) {
model.value = getLocalizedDayJs(model.value)
.set('hours', newHours)
.set('minutes', newMinutes)
.set('seconds', 0)
.format();
emit('changed', model.value);
}
return;
}
// Handle existing formats
if (newValue.split(':').length === 2) { if (newValue.split(':').length === 2) {
const [hours, minutes] = newValue.split(':'); const [hours, minutes] = newValue.split(':');
if (!isNaN(parseInt(hours)) && !isNaN(parseInt(minutes))) { if (!isNaN(parseInt(hours)) && !isNaN(parseInt(minutes))) {
model.value = getLocalizedDayJs(model.value) const newHours = Math.min(parseInt(hours), 23);
.set('hours', Math.min(parseInt(hours), 23)) const newMinutes = Math.min(parseInt(minutes), 59);
.set('minutes', Math.min(parseInt(minutes), 59))
.format(); if (newHours !== currentHours || newMinutes !== currentMinutes) {
emit('changed', model.value); model.value = getLocalizedDayJs(model.value)
.set('hours', newHours)
.set('minutes', newMinutes)
.set('seconds', 0)
.format();
emit('changed', model.value);
}
} }
} }
// check if input is only numbers // check if input is only numbers
else if (/^\d+$/.test(newValue)) { else if (/^\d+$/.test(newValue)) {
let newHours = currentHours;
let newMinutes = currentMinutes;
if (newValue.length === 4) { if (newValue.length === 4) {
// parse 1300 to 13:00 // parse 1300 to 13:00
const [hours, minutes] = [ newHours = Math.min(parseInt(newValue.slice(0, 2)), 23);
newValue.slice(0, 2), newMinutes = Math.min(parseInt(newValue.slice(2, 4)), 59);
newValue.slice(2, 4),
];
model.value = getLocalizedDayJs(model.value)
.set('hours', Math.min(parseInt(hours), 23))
.set('minutes', Math.min(parseInt(minutes), 59))
.format();
emit('changed', model.value);
} else if (newValue.length === 3) { } else if (newValue.length === 3) {
// parse 130 to 01:30 // parse 130 to 01:30
const [hours, minutes] = [ newHours = Math.min(parseInt(newValue.slice(0, 1)), 23);
newValue.slice(0, 1), newMinutes = Math.min(parseInt(newValue.slice(1, 3)), 59);
newValue.slice(1, 3),
];
model.value = getLocalizedDayJs(model.value)
.set('hours', Math.min(parseInt(hours), 23))
.set('minutes', Math.min(parseInt(minutes), 59))
.format();
emit('changed', model.value);
} else if (newValue.length === 2) { } else if (newValue.length === 2) {
// parse 13 to 13:00 // parse 13 to 13:00
model.value = getLocalizedDayJs(model.value) newHours = Math.min(parseInt(newValue), 23);
.set('hours', Math.min(parseInt(newValue), 23)) newMinutes = 0;
.set('minutes', 0)
.format();
emit('changed', model.value);
} else if (newValue.length === 1) { } else if (newValue.length === 1) {
// parse 1 to 01:00 // parse 1 to 01:00
newHours = Math.min(parseInt(newValue), 23);
newMinutes = 0;
}
if (newHours !== currentHours || newMinutes !== currentMinutes) {
model.value = getLocalizedDayJs(model.value) model.value = getLocalizedDayJs(model.value)
.set('hours', Math.min(parseInt(newValue), 23)) .set('hours', newHours)
.set('minutes', 0) .set('minutes', newMinutes)
.set('seconds', 0)
.format(); .format();
emit('changed', model.value); emit('changed', model.value);
} }
} }
inputValue.value = getLocalizedDayJs(model.value).format('HH:mm');
} }
watch(model, (value) => { watch(model, (value) => {
inputValue.value = value ? getLocalizedDayJs(value).format('HH:mm') : null; inputValue.value = value
? formatTime(value, organization?.value?.time_format || '24-hours')
: null;
}); });
const timeInput = ref<HTMLInputElement | null>(null); const timeInput = ref<HTMLInputElement | null>(null);
@@ -88,7 +124,12 @@ const emit = defineEmits(['changed']);
useFocus(timeInput, { initialValue: props.focus }); useFocus(timeInput, { initialValue: props.focus });
const inputValue = ref( const inputValue = ref(
model.value ? getLocalizedDayJs(model.value).format('HH:mm') : null model.value
? formatTime(
model.value,
organization?.value?.time_format || '24-hours'
)
: null
); );
</script> </script>
@@ -98,7 +139,7 @@ const inputValue = ref(
ref="timeInput" ref="timeInput"
v-model="inputValue" v-model="inputValue"
:class=" :class="
twMerge('text-center w-24 px-3 py-2', size === 'large' && 'w-28') twMerge('text-center w-28 px-3 py-2', size === 'large' && 'w-28')
" "
data-testid="time_picker_input" data-testid="time_picker_input"
type="text" type="text"

View File

@@ -52,26 +52,22 @@ watch(focused, (newValue, oldValue) => {
</script> </script>
<template> <template>
<div <form
ref="dropdownContent" ref="dropdownContent"
class="grid grid-cols-2 divide-x divide-card-background-separator text-center py-2"> class="grid grid-cols-2 divide-x divide-card-background-separator text-center py-2">
<div <div class="px-2">
class="px-2" <div class="font-semibold text-text-primary text-sm pb-2">
@keydown.enter.prevent="nextTick(() => emit('close'))"> Start
<div class="font-semibold text-text-primary text-sm pb-2">Start</div> </div>
<div class="space-y-2"> <div class="space-y-2">
<TimePickerSimple <TimePickerSimple
v-model="tempStart" v-model="tempStart"
data-testid="time_entry_range_start" data-testid="time_entry_range_start"
tabindex="0" tabindex="0"
:focus :focus
@keydown.enter.prevent="nextTick(() => emit('close'))"
@keydown.exact.tab.shift.stop.prevent="emit('close')" @keydown.exact.tab.shift.stop.prevent="emit('close')"
@changed="updateTimeEntry"></TimePickerSimple> @changed="updateTimeEntry"></TimePickerSimple>
<DatePicker
v-model="tempStart"
class="text-xs text-text-tertiary max-w-24 px-1.5 py-1.5"
@changed="updateTimeEntry"
@blur.stop.prevent="emit('close')"></DatePicker>
</div> </div>
</div> </div>
<div class="px-2"> <div class="px-2">
@@ -80,16 +76,29 @@ watch(focused, (newValue, oldValue) => {
<TimePickerSimple <TimePickerSimple
v-model="tempEnd" v-model="tempEnd"
data-testid="time_entry_range_end" data-testid="time_entry_range_end"
@keydown.enter.prevent="nextTick(() => emit('close'))"
@changed="updateTimeEntry"></TimePickerSimple> @changed="updateTimeEntry"></TimePickerSimple>
<DatePicker
v-model="tempEnd"
class="text-xs text-text-tertiary max-w-24 px-1.5 py-1.5"
@changed="updateTimeEntry"></DatePicker>
</div> </div>
<div v-else class="text-text-secondary">-- : --</div> <div v-else class="text-text-secondary">-- : --</div>
<div tabindex="0" @focusin="emit('close')"></div>
</div> </div>
</div> <div class="px-2 pt-2">
<DatePicker
v-model="tempStart"
class="text-xs text-text-tertiary max-w-28 px-1.5 py-1.5"
@changed="updateTimeEntry"></DatePicker>
</div>
<div class="px-2 pt-2">
<DatePicker
v-if="tempEnd !== null"
v-model="tempEnd"
class="text-xs text-text-tertiary max-w-28 px-1.5 py-1.5"
@changed="updateTimeEntry"></DatePicker>
</div>
<div
tabindex="0"
class="focus-visible:outline-none"
@focusin="emit('close')"></div>
</form>
</template> </template>
<style></style> <style></style>

View File

@@ -29,7 +29,7 @@ import DurationHumanInput from '@/packages/ui/src/Input/DurationHumanInput.vue';
import { InformationCircleIcon } from '@heroicons/vue/20/solid'; import { InformationCircleIcon } from '@heroicons/vue/20/solid';
import type { Tag, Task } from '@/packages/api/src'; import type { Tag, Task } from '@/packages/api/src';
import TimePickerSimple from "@/packages/ui/src/Input/TimePickerSimple.vue"; import TimePickerSimple from '@/packages/ui/src/Input/TimePickerSimple.vue';
const show = defineModel('show', { default: false }); const show = defineModel('show', { default: false });
const saving = ref(false); const saving = ref(false);
@@ -148,9 +148,7 @@ type BillableOption = {
<div class="flex-1 min-w-0"> <div class="flex-1 min-w-0">
<TimeTrackerProjectTaskDropdown <TimeTrackerProjectTaskDropdown
v-model:project="timeEntry.project_id" v-model:project="timeEntry.project_id"
v-model:task=" v-model:task="timeEntry.task_id"
timeEntry.task_id
"
:clients :clients
:create-project :create-project
:create-client :create-client
@@ -160,7 +158,9 @@ type BillableOption = {
class="bg-input-background" class="bg-input-background"
:projects="projects" :projects="projects"
:tasks="tasks" :tasks="tasks"
:enable-estimated-time="enableEstimatedTime"></TimeTrackerProjectTaskDropdown> :enable-estimated-time="
enableEstimatedTime
"></TimeTrackerProjectTaskDropdown>
</div> </div>
<div class="flex items-center space-x-2"> <div class="flex items-center space-x-2">
<div class="flex-col"> <div class="flex-col">
@@ -242,37 +242,31 @@ type BillableOption = {
</div> </div>
</div> </div>
</div> </div>
<div class=""> <div class="grid gap-2 grid-cols-2">
<InputLabel>Start</InputLabel> <div class="space-y-1">
<div class="flex flex-col items-center space-y-2 mt-1"> <InputLabel>Start</InputLabel>
<TimePickerSimple <TimePickerSimple
v-model="localStart" v-model="localStart"
size="large"></TimePickerSimple> size="large"></TimePickerSimple>
<DatePicker
v-model="localStart"
tabindex="1"
class="text-xs text-text-tertiary max-w-28 px-1.5 py-1.5"></DatePicker>
</div> </div>
</div> <div class="space-y-1">
<div class=""> <InputLabel>End</InputLabel>
<InputLabel>End</InputLabel>
<div class="flex flex-col items-center space-y-2 mt-1">
<TimePickerSimple <TimePickerSimple
v-model="localEnd" v-model="localEnd"
size="large"></TimePickerSimple> size="large"></TimePickerSimple>
<DatePicker
v-model="localEnd"
tabindex="1"
class="text-xs text-text-tertiary max-w-28 px-1.5 py-1.5"></DatePicker>
</div> </div>
<DatePicker
v-model="localStart"
class="text-xs text-text-tertiary max-w-28 px-1.5 py-1.5"></DatePicker>
<DatePicker
v-model="localEnd"
class="text-xs text-text-tertiary max-w-28 px-1.5 py-1.5"></DatePicker>
</div> </div>
</div> </div>
</template> </template>
<template #footer> <template #footer>
<SecondaryButton tabindex="2" @click="show = false"> Cancel</SecondaryButton> <SecondaryButton @click="show = false">Cancel</SecondaryButton>
<PrimaryButton <PrimaryButton
tabindex="2"
class="ms-3" class="ms-3"
:class="{ 'opacity-25': saving }" :class="{ 'opacity-25': saving }"
:disabled="saving" :disabled="saving"