Skip to content

Commit 7f0ebb2

Browse files
authored
Merge pull request #132 from netervati/feat/date-picker
Support date picker for timestamp
2 parents 33275d6 + 6310083 commit 7f0ebb2

File tree

8 files changed

+244
-14
lines changed

8 files changed

+244
-14
lines changed

components/calendarPicker.vue

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<script setup lang="ts">
2+
import { CalendarDaysIcon } from '@heroicons/vue/24/solid';
3+
import { format } from 'date-fns';
4+
import { DatePicker as VCalendarDatePicker } from 'v-calendar';
5+
import 'v-calendar/dist/style.css';
6+
7+
const emit = defineEmits<{
8+
(e: 'change', value: string): void;
9+
}>();
10+
11+
const props = defineProps<{
12+
disabled: boolean;
13+
name: string;
14+
value?: string;
15+
}>();
16+
17+
const { value } = useField(() => props.name);
18+
19+
const curDate = new Date();
20+
21+
const range = ref({
22+
start: curDate,
23+
end: curDate,
24+
});
25+
26+
watch(range, (updated) => {
27+
value.value = updated;
28+
});
29+
30+
onMounted(() => {
31+
value.value = range.value;
32+
});
33+
34+
watchEffect(() => {
35+
if (props.value) {
36+
value.value = props.value;
37+
}
38+
});
39+
</script>
40+
41+
<template>
42+
<div tabindex="0" class="collapse bg-base-200">
43+
<input type="checkbox" />
44+
<div class="flex collapse-title gap-4 text-sm font-medium">
45+
<CalendarDaysIcon class="w-4 h-4" />
46+
{{ format(range.start, 'MMMM d, yyy') }} - {{ format(range.end, 'MMMM d, yyy') }}
47+
</div>
48+
<div class="collapse-content">
49+
<VCalendarDatePicker v-model.range="range" />
50+
</div>
51+
</div>
52+
</template>

components/modal/createModelData.vue

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
max: undefined,
2323
min: undefined,
2424
name: sch.name,
25+
range: undefined,
2526
type: sch.type,
2627
option: '',
2728
immutable: sch.name === 'id'
@@ -47,7 +48,7 @@
4748
// float: 'Float',
4849
number: 'Number',
4950
string: 'String',
50-
// timestamp: 'Timestamp',
51+
timestamp: 'Timestamp',
5152
uuid: 'UUID'
5253
};
5354
@@ -103,7 +104,7 @@
103104
/>
104105
</section>
105106
</div>
106-
<div class="flex gap-x-2" v-for="(sch, idx) in fields" :key="idx">
107+
<div class="grid grid-cols-4 gap-x-2" v-for="(sch, idx) in fields" :key="idx">
107108
<section class="form-control mt-2">
108109
<FormInput
109110
:disabled="true"
@@ -158,6 +159,17 @@
158159
/>
159160
</section>
160161
<!-- ================== -->
162+
<!-- ======== TIMESTAMP -->
163+
<!-- ================== -->
164+
<section
165+
v-show="sch.value.type === 'timestamp'"
166+
class="form-control mt-2 col-span-2"
167+
>
168+
<CalendarPicker
169+
:disabled="isDisabled"
170+
:name="`schema[${idx}].range`"
171+
/>
172+
</section>
161173
</div>
162174
<section class="mt-10">
163175
<Button :disabled="isDisabled" size="sm" @click="handleClose">

components/model/table.vue

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
import useModelData from '~~/stores/useModelData';
44
import ModalCreateModelData from '~~/components/modal/createModelData.vue';
55
import ModalConfirm from '~~/components/modal/confirm.vue';
6+
import format from 'date-fns/format';
7+
8+
type Schema = {
9+
name: string;
10+
type: string;
11+
}
612
713
defineProps<{
8-
schema: { name: string; type: string }[];
14+
schema: Schema[];
915
}>();
1016
1117
const modelData = useModelData();
@@ -25,6 +31,15 @@
2531
select.clear();
2632
},
2733
});
34+
35+
const render = (model: { schema: any }, schema: Schema) => {
36+
if (schema.type === 'timestamp') {
37+
const parsedDate = Date.parse(model.schema[schema.name]);
38+
return format(parsedDate, 'MMMM d, yyy');
39+
}
40+
41+
return model.schema[schema.name];
42+
};
2843
</script>
2944

3045
<template>
@@ -72,7 +87,7 @@
7287
</tr>
7388
</thead>
7489
<tbody>
75-
<TableLoader v-if="modelData.isLoading" :colspan="4" />
90+
<TableLoader v-if="modelData.isLoading" :colspan="schema.length + 2" />
7691
<tr v-for="md in modelData.list" v-else :key="md.id">
7792
<td colspan="2">
7893
<input
@@ -86,7 +101,7 @@
86101
v-for="sch in schema"
87102
class="font-normal normal-case text-base"
88103
>
89-
{{ md.schema[sch.name as any] }}
104+
{{ render(md, sch) }}
90105
</td>
91106
</tr>
92107
</tbody>

components/table/Loader.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
<template>
88
<tr>
9-
<td :colspan="colspan">
10-
<div class="flex h-full w-full">
9+
<td rowspan="4" :colspan="colspan">
10+
<figure class="flex mt-32 w-full">
1111
<LoaderSpinner />
12-
</div>
12+
</figure>
1313
</td>
1414
</tr>
1515
</template>

0 commit comments

Comments
 (0)