56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import type { Ref } from 'vue'
|
|
|
|
export type DateShortcutType = 'all' | 'today' | 'tomorrow' | 'yesterday' | 'week'
|
|
|
|
function formatYmd(date: Date): string {
|
|
const year = date.getFullYear()
|
|
const month = String(date.getMonth() + 1).padStart(2, '0')
|
|
const day = String(date.getDate()).padStart(2, '0')
|
|
return `${year}-${month}-${day}`
|
|
}
|
|
|
|
/**
|
|
* 列表工具栏计划日期快捷筛选(全部 / 昨日 / 今日 / 明日 / 本周)
|
|
*/
|
|
export function useListDateShortcut(dateRange: Ref<string[]>, onSearch: () => void) {
|
|
const activeDateShortcut = ref<DateShortcutType>('all')
|
|
|
|
function handleDateShortcut(type: DateShortcutType) {
|
|
activeDateShortcut.value = type
|
|
const today = new Date()
|
|
|
|
if (type === 'all') {
|
|
dateRange.value = []
|
|
} else if (type === 'today') {
|
|
const d = formatYmd(today)
|
|
dateRange.value = [d, d]
|
|
} else if (type === 'tomorrow') {
|
|
const t = new Date(today)
|
|
t.setDate(t.getDate() + 1)
|
|
const d = formatYmd(t)
|
|
dateRange.value = [d, d]
|
|
} else if (type === 'yesterday') {
|
|
const y = new Date(today)
|
|
y.setDate(y.getDate() - 1)
|
|
const d = formatYmd(y)
|
|
dateRange.value = [d, d]
|
|
} else if (type === 'week') {
|
|
const start = new Date(today)
|
|
const day = start.getDay() || 7
|
|
start.setDate(start.getDate() - day + 1)
|
|
const end = new Date(start)
|
|
end.setDate(start.getDate() + 6)
|
|
dateRange.value = [formatYmd(start), formatYmd(end)]
|
|
}
|
|
|
|
onSearch()
|
|
}
|
|
|
|
function resetDateShortcut() {
|
|
activeDateShortcut.value = 'all'
|
|
dateRange.value = []
|
|
}
|
|
|
|
return { activeDateShortcut, handleDateShortcut, resetDateShortcut }
|
|
}
|