Skip to content

A self-hosted media server and file management system

fix: Datepicker Reset Fixes #342 and add set to today button

aminnausin8102acd5759f
diff --git a/resources/js/components/cedar-ui/date-picker/DatePicker.vue b/resources/js/components/cedar-ui/date-picker/DatePicker.vue
index f467bc80..ce97335b 100644
--- a/resources/js/components/cedar-ui/date-picker/DatePicker.vue
+++ b/resources/js/components/cedar-ui/date-picker/DatePicker.vue
@@ -1,14 +1,13 @@
<script setup lang="ts">
import { CedarCalendar, CedarChevronLeft, CedarChevronRight } from '@/components/cedar-ui/icons';
-import { computed, nextTick, useTemplateRef, watch } from 'vue';
+import { nextTick, useTemplateRef, watch } from 'vue';
import { ButtonDatePicker, useDatePicker } from '.';
-import { toCalendarFormattedDate } from '@/service/util.ts';
import { ButtonIcon, ButtonText } from '@/components/cedar-ui/button';
import { OnClickOutside } from '@vueuse/components';
import { UseFocusTrap } from '@vueuse/integrations/useFocusTrap/component';
import { InputShell } from '@/components/cedar-ui/input';
-const props = defineProps<{ field: any; disabled?: boolean; useNativeUI?: boolean }>();
+const props = defineProps<{ field: any; disabled?: boolean; useNativeUi?: boolean }>();
const datePickerInput = useTemplateRef('datePickerInput');
const datePickerCalendar = useTemplateRef('datePickerCalendar');
@@ -36,6 +35,7 @@ const {
datePickerBlankDaysInMonth,
datePickerValueClicked,
showDatePickerPanel,
+ setDate,
} = useDatePicker({ model }, datePickerInput, datePickerCalendar);
async function focusInitialElement() {
@@ -63,14 +63,12 @@ watch(datePickerOpen, (open) => {
watch(datePickerPanel, () => {
focusInitialElement();
});
-
-const dateV = computed(() => toCalendarFormattedDate(model.value ?? '', { year: 'numeric', month: '2-digit', day: '2-digit' }));
</script>
<template>
- <InputShell v-if="useNativeUI">
+ <InputShell v-if="useNativeUi">
<template #input="{ class: inputClass }">
- <input type="date" :class="[inputClass, 'mt-2']" v-model="dateV" />
+ <input type="date" :class="[inputClass, 'mt-1', { 'button-disabled': disabled }, $attrs.class]" v-model="model" />
</template>
</InputShell>
<OnClickOutside v-else class="group relative text-sm" @trigger="toggleDatePicker(false)">
@@ -195,7 +193,14 @@ const dateV = computed(() => toCalendarFormattedDate(model.value ?? '', { year:
/>
</div>
- <ButtonText variant="ghost" @click="datePickerValueClicked()" class="hocus:bg-overlay-accent h-fit px-2 py-1" :title="'Month'"> Clear </ButtonText>
+ <div class="flex justify-between gap-2">
+ <ButtonText variant="ghost" @click="datePickerValueClicked()" class="hocus:bg-overlay-accent h-fit px-2 py-1" title="Clear selected date">
+ Clear
+ </ButtonText>
+ <ButtonText variant="ghost" @click="setDate(new Date(), true, true)" class="hocus:bg-overlay-accent h-fit px-2 py-1" title="Set date to today">
+ Today
+ </ButtonText>
+ </div>
</div>
</UseFocusTrap>
</Transition>
diff --git a/resources/js/components/cedar-ui/date-picker/useDatePicker.ts b/resources/js/components/cedar-ui/date-picker/useDatePicker.ts
index 4dbfa5a7..a1204341 100644
--- a/resources/js/components/cedar-ui/date-picker/useDatePicker.ts
+++ b/resources/js/components/cedar-ui/date-picker/useDatePicker.ts
@@ -1,4 +1,6 @@
-import { computed, nextTick, onMounted, ref, watch, type ModelRef, type Ref } from 'vue';
+import type { ModelRef, Ref } from 'vue';
+
+import { computed, nextTick, onMounted, ref, watch } from 'vue';
interface DatePickerProps {
model?: ModelRef<string | undefined | null>;
@@ -193,14 +195,21 @@ export default function useDatePicker(props: DatePickerProps, datePickerInput: R
datePickerPosition.value = bottomPos > window.innerHeight ? 'top' : 'bottom';
}
- function setDate(date: Date, setDate: boolean = true) {
+ function setDate(date: Date, setDate: boolean = true, setValue?: boolean) {
datePickerMonth.value = date.getMonth();
datePickerYear.value = date.getFullYear();
datePickerDay.value = date.getDate();
- if (setDate) datePickerValue.value = datePickerFormatDate(date);
+ if (setDate) {
+ datePickerValue.value = datePickerFormatDate(date);
+ }
calculateDays();
+
+ if (setValue && props.model) {
+ props.model.value = datePickerValue.value;
+ datePickerOpen.value = false;
+ }
}
function resetDate() {
@@ -252,5 +261,6 @@ export default function useDatePicker(props: DatePickerProps, datePickerInput: R
datePickerIsToday,
datePickerIsCurrentMonth,
showDatePickerPanel,
+ setDate,
};
}