forked from techniq/svelte-ux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateRangeDisplay.svelte
More file actions
78 lines (68 loc) · 2.18 KB
/
DateRangeDisplay.svelte
File metadata and controls
78 lines (68 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<script lang="ts">
import { PeriodType, getDateFuncsByPeriodType, type FormatDateOptions } from '../utils/date';
import type { DateRange } from '../utils/dateRange';
import { getSettings } from './settings';
export let value: DateRange | null | undefined;
const { format: format_ux } = getSettings();
let showToValue = false;
$: if (value?.to) {
if (value?.from && value?.periodType) {
const { isSame } = getDateFuncsByPeriodType(value.periodType);
switch (value.periodType) {
case PeriodType.Day:
case PeriodType.Month:
case PeriodType.CalendarYear:
case PeriodType.FiscalYearOctober:
showToValue = !isSame(value.from, value.to);
break;
default:
// Always show "to" value for week and quarter
showToValue = true;
}
} else {
showToValue = true;
}
}
const getPeriodType = (value: DateRange | null | undefined) => {
let periodType = value?.periodType ?? PeriodType.Day;
// Override periodTypes that show ranges for individual values
switch (periodType) {
case PeriodType.WeekSun:
case PeriodType.WeekMon:
case PeriodType.WeekTue:
case PeriodType.WeekWed:
case PeriodType.WeekThu:
case PeriodType.WeekFri:
case PeriodType.WeekSat:
case PeriodType.BiWeek1Sun:
case PeriodType.BiWeek1Mon:
case PeriodType.BiWeek1Tue:
case PeriodType.BiWeek1Wed:
case PeriodType.BiWeek1Thu:
case PeriodType.BiWeek1Fri:
case PeriodType.BiWeek1Sat:
case PeriodType.BiWeek2Sun:
case PeriodType.BiWeek2Mon:
case PeriodType.BiWeek2Tue:
case PeriodType.BiWeek2Wed:
case PeriodType.BiWeek2Thu:
case PeriodType.BiWeek2Fri:
case PeriodType.BiWeek2Sat:
periodType = PeriodType.Day;
break;
case PeriodType.Quarter:
periodType = PeriodType.Month;
break;
}
return periodType;
};
</script>
{#if value?.from}
{$format_ux(value.from, getPeriodType(value), { variant: 'long' })}
{:else}
<div> </div>
{/if}
{#if value?.to && showToValue}
<span> - </span>
{$format_ux(value.to, getPeriodType(value), { variant: 'long' })}
{/if}