85 lines
2.9 KiB
Plaintext
85 lines
2.9 KiB
Plaintext
<template>
|
|
<div class="aa-range" ref="root">
|
|
<div class="aa-range-trigger" :class="{ 'is-focus': open }" @click="open = !open">
|
|
<span v-if="modelValue.start && modelValue.end">{{ modelValue.start }} <span class="aa-range-sep">~</span> {{ modelValue.end }}</span>
|
|
<span v-else class="aa-range-placeholder">请选择时间范围</span>
|
|
</div>
|
|
<div v-show="open" class="aa-range-pop">
|
|
<div class="aa-range-presets">
|
|
<button
|
|
v-for="(p, i) in presets"
|
|
:key="i"
|
|
class="aa-range-preset"
|
|
:class="{ 'is-active': activeIndex === i }"
|
|
@click="applyPreset(i)"
|
|
>{{ p.label }}</button>
|
|
</div>
|
|
<div class="aa-range-custom">
|
|
<input type="date" v-model="start" />
|
|
<span class="aa-range-tilde">~</span>
|
|
<input type="date" v-model="end" />
|
|
</div>
|
|
<div class="aa-range-actions">
|
|
<button class="aa-range-cancel" @click="close">取消</button>
|
|
<button class="aa-range-ok" @click="confirm">确定</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { ref, onMounted, onBeforeUnmount } from 'vue';
|
|
|
|
function fmt(d) {
|
|
return d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
|
|
}
|
|
function presetRange(p) {
|
|
const end = new Date(); end.setHours(0, 0, 0, 0);
|
|
const start = new Date(end);
|
|
if (p.year === 'this') start.setMonth(0, 1);
|
|
else if (p.month === 'this') start.setDate(1);
|
|
else if (p.month === 'last') { start = new Date(end.getFullYear(), end.getMonth() - 1, 1); end.setDate(0); }
|
|
else { start.setDate(end.getDate() + (p.days || 0)); if (p.len) start.setDate(end.getDate() - (p.len - 1)); }
|
|
return { start: fmt(start), end: fmt(end) };
|
|
}
|
|
|
|
const props = defineProps({
|
|
modelValue: { type: Object, default: () => ({ start: '', end: '' }) },
|
|
presets: {
|
|
type: Array,
|
|
default: () => [
|
|
{ label: '今天', days: 0 },
|
|
{ label: '昨天', days: -1, len: 1 },
|
|
{ label: '近7天', days: -6 },
|
|
{ label: '近30天', days: -29 },
|
|
{ label: '本月', month: 'this' },
|
|
{ label: '上月', month: 'last' },
|
|
{ label: '本年', year: 'this' }
|
|
]
|
|
}
|
|
});
|
|
const emit = defineEmits(['update:modelValue']);
|
|
|
|
const root = ref(null);
|
|
const open = ref(false);
|
|
const start = ref(props.modelValue.start);
|
|
const end = ref(props.modelValue.end);
|
|
const activeIndex = ref(-1);
|
|
|
|
function applyPreset(i) {
|
|
const r = presetRange(props.presets[i]);
|
|
start.value = r.start; end.value = r.end; activeIndex.value = i;
|
|
}
|
|
function confirm() {
|
|
emit('update:modelValue', { start: start.value, end: end.value });
|
|
close();
|
|
}
|
|
function close() { open.value = false; activeIndex.value = -1; }
|
|
|
|
function onDoc(e) { if (root.value && !root.value.contains(e.target)) close(); }
|
|
onMounted(() => document.addEventListener('click', onDoc));
|
|
onBeforeUnmount(() => document.removeEventListener('click', onDoc));
|
|
</script>
|
|
|
|
<style src="./RangeQuickPicker.css"></style>
|