adminforth 4.7.5 → 4.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/modules/styles.js +4 -4
- package/dist/spa/package.json +1 -0
- package/dist/spa/src/afcl/Calendar.vue +211 -0
- package/dist/spa/src/afcl/DateField.vue +257 -0
- package/dist/spa/src/afcl/DatePicker.vue +103 -245
- package/dist/spa/src/afcl/TimeField.vue +52 -0
- package/dist/spa/src/afcl/pickerStyles.ts +25 -0
- package/dist/spa/src/afcl/shadowRoot.ts +1 -2
- package/dist/spa/src/components/ColumnValueInput.vue +9 -7
- package/dist/spa/src/components/ColumnValueInputWrapper.vue +1 -1
- package/dist/spa/src/components/CustomDateRangePicker.vue +47 -165
- package/dist/spa/src/components/Filters.vue +7 -4
- package/dist/spa/src/components/GroupsTable.vue +23 -43
- package/dist/spa/src/components/ShowTable.vue +21 -31
- package/dist/spa/src/utils/dateUtils.ts +33 -21
- package/dist/spa/src/views/CreateEditSkeleton.vue +6 -17
- package/dist/spa/src/views/ShowView.vue +14 -24
- package/dist/spa/tailwind.config.js +1 -1
- package/package.json +1 -1
package/dist/modules/styles.js
CHANGED
|
@@ -78,11 +78,11 @@ export const styles = () => ({
|
|
|
78
78
|
/* Show View Table */
|
|
79
79
|
/*----------------------------------------*/
|
|
80
80
|
lightShowTableHeadingBackground: "#FAFAFA",
|
|
81
|
-
lightShowTableHeadingText: "#
|
|
81
|
+
lightShowTableHeadingText: "#18181B",
|
|
82
82
|
lightShowTableUnderHeadingBackground: "#FAFAFA",
|
|
83
83
|
lightShowTableUnderHeadingText: "#696973",
|
|
84
84
|
lightShowTablesBodyBackground: "alias:lightForm",
|
|
85
|
-
lightShowTableBodyText: "#
|
|
85
|
+
lightShowTableBodyText: "#18181B",
|
|
86
86
|
lightShowTableBodyBorder: "#F0F0F2",
|
|
87
87
|
/* Show View buttons*/
|
|
88
88
|
lightShowViewButtonBackground: "#FFFFFF",
|
|
@@ -396,11 +396,11 @@ export const styles = () => ({
|
|
|
396
396
|
/* Show View Table */
|
|
397
397
|
/*----------------------------------------*/
|
|
398
398
|
darkShowTableHeadingBackground: "alias:darkFormHeading",
|
|
399
|
-
darkShowTableHeadingText: "#
|
|
399
|
+
darkShowTableHeadingText: "#F4F4F5",
|
|
400
400
|
darkShowTableUnderHeadingBackground: "alias:darkFormHeading",
|
|
401
401
|
darkShowTableUnderHeadingText: "#A1A1AA",
|
|
402
402
|
darkShowTablesBodyBackground: "alias:darkForm",
|
|
403
|
-
darkShowTableBodyText: "#
|
|
403
|
+
darkShowTableBodyText: "#F4F4F5",
|
|
404
404
|
darkShowTableBodyBorder: "#202024",
|
|
405
405
|
/* Show View buttons*/
|
|
406
406
|
darkShowViewButtonBackground: "#1F2937",
|
package/dist/spa/package.json
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
|
|
13
13
|
"i18n:extract": "echo {} > i18n-empty.json && vue-i18n-extract report --vueFiles \"./src/**/*.{js,vue,ts}\" --output ./i18n-messages.json --languageFiles \"i18n-empty.json\" --add"
|
|
14
14
|
},
|
|
15
|
+
"//": "TODO next major: remove flowbite-datepicker (with it flowbite), core does not import it, only @adminforth/i18n up to 3.1.7 does",
|
|
15
16
|
"dependencies": {
|
|
16
17
|
"@fontsource-variable/geist": "^5.3.0",
|
|
17
18
|
"@fontsource-variable/geist-mono": "^5.3.0",
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div class="afcl-calendar w-[16.5rem] p-3 text-lightDatePickerCalendarText dark:text-darkDatePickerCalendarMainText">
|
|
3
|
+
<div class="afcl-calendar-header flex items-center justify-between gap-1 mb-2">
|
|
4
|
+
<button type="button" :class="NAV_BUTTON_CLASSES" :aria-label="$t(VIEW_STEPS[view].previousLabel)" @click="turnPage(-1)">
|
|
5
|
+
<IconChevronLeft class="w-4 h-4 rtl:rotate-180" aria-hidden="true" />
|
|
6
|
+
</button>
|
|
7
|
+
<button
|
|
8
|
+
type="button"
|
|
9
|
+
class="afcl-calendar-title h-7 px-2 rounded-md text-sm font-semibold capitalize tabular-nums focus:outline-none focus-visible:ring-[3px] focus-visible:ring-lightFocusRing dark:focus-visible:ring-darkFocusRing enabled:hover:bg-lightDatePickerCalendarArrowButtonBackgroundHover dark:enabled:hover:bg-darkDatePickerCalendarArrowButtonBackgroundHover"
|
|
10
|
+
:disabled="view === 'years'"
|
|
11
|
+
@click="view = view === 'days' ? 'months' : 'years'"
|
|
12
|
+
>
|
|
13
|
+
{{ title }}
|
|
14
|
+
</button>
|
|
15
|
+
<button type="button" :class="NAV_BUTTON_CLASSES" :aria-label="$t(VIEW_STEPS[view].nextLabel)" @click="turnPage(1)">
|
|
16
|
+
<IconChevronRight class="w-4 h-4 rtl:rotate-180" aria-hidden="true" />
|
|
17
|
+
</button>
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
<div
|
|
21
|
+
ref="gridEl"
|
|
22
|
+
class="afcl-calendar-grid grid gap-0.5 text-center"
|
|
23
|
+
:class="view === 'days' ? 'grid-cols-7' : MONTHS_GRID_CLASSES"
|
|
24
|
+
@keydown="onGridKeydown"
|
|
25
|
+
>
|
|
26
|
+
<template v-if="view === 'days'">
|
|
27
|
+
<span
|
|
28
|
+
v-for="name in weekdayNames"
|
|
29
|
+
:key="name"
|
|
30
|
+
class="h-6 leading-6 text-[11px] font-medium capitalize text-lightDatePickerCalendarDaysOfWeekText dark:text-darkDatePickerCalendarDaysOfWeekText"
|
|
31
|
+
>{{ name }}</span>
|
|
32
|
+
<span v-for="n in leadingBlanks" :key="`lead-${n}`" class="h-7" aria-hidden="true"></span>
|
|
33
|
+
</template>
|
|
34
|
+
<button
|
|
35
|
+
v-for="cell in cells"
|
|
36
|
+
:key="cell.value"
|
|
37
|
+
type="button"
|
|
38
|
+
:tabindex="cell.value === activeValue ? 0 : -1"
|
|
39
|
+
:aria-label="cell.ariaLabel"
|
|
40
|
+
:aria-pressed="cell.value === selectedValue"
|
|
41
|
+
:aria-current="cell.value === todayValue ? 'date' : undefined"
|
|
42
|
+
:class="[
|
|
43
|
+
'afcl-calendar-cell rounded-md tabular-nums capitalize focus:outline-none focus-visible:relative focus-visible:ring-[3px] focus-visible:ring-lightFocusRing dark:focus-visible:ring-darkFocusRing',
|
|
44
|
+
view === 'days' ? 'h-7 text-[12.5px]' : 'h-full text-sm',
|
|
45
|
+
cell.value === selectedValue
|
|
46
|
+
? 'font-medium bg-lightDatePickerCalendarDateActiveButtonBackground text-lightDatePickerCalendarDateActiveButtonText dark:bg-darkDatePickerCalendarDateActiveButtonBackground dark:text-darkDatePickerCalendarDateActiveButtonText'
|
|
47
|
+
: 'text-lightDatePickerCalendarDateButtonText hover:bg-lightDatePickerCalendarDateButtonBackgroundHover dark:text-darkDatePickerCalendarDateButtonText dark:hover:bg-darkDatePickerCalendarDateButtonBackgroundHover',
|
|
48
|
+
cell.value === todayValue && cell.value !== selectedValue && 'ring-1 ring-inset ring-lightInputBorder dark:ring-darkInputBorder',
|
|
49
|
+
]"
|
|
50
|
+
@click="pick(cell.date)"
|
|
51
|
+
>{{ cell.label }}</button>
|
|
52
|
+
<span v-for="n in trailingBlanks" :key="`trail-${n}`" class="h-7" aria-hidden="true"></span>
|
|
53
|
+
</div>
|
|
54
|
+
|
|
55
|
+
<div class="afcl-calendar-footer flex justify-between mt-2.5 pt-2.5 border-t border-lightDialogBreakLine dark:border-darkDialogBreakLine">
|
|
56
|
+
<Button type="button" variant="ghost" size="sm" :shadow="false" @click="emit('update:modelValue', null)">{{ $t('Clear') }}</Button>
|
|
57
|
+
<Button type="button" variant="secondary" size="sm" @click="emit('update:modelValue', todayValue)">{{ $t('Today') }}</Button>
|
|
58
|
+
</div>
|
|
59
|
+
</div>
|
|
60
|
+
</template>
|
|
61
|
+
|
|
62
|
+
<script setup lang="ts">
|
|
63
|
+
import { computed, nextTick, ref, watch } from 'vue';
|
|
64
|
+
import dayjs, { type Dayjs, type ManipulateType } from 'dayjs';
|
|
65
|
+
import { IconChevronLeft, IconChevronRight } from '@iconify-prerendered/vue-lucide';
|
|
66
|
+
import { PICKER_DAY_FORMAT } from '@/utils/dateUtils';
|
|
67
|
+
import { activeElementIn } from './shadowRoot';
|
|
68
|
+
import Button from './Button.vue';
|
|
69
|
+
|
|
70
|
+
type CalendarView = 'days' | 'months' | 'years';
|
|
71
|
+
|
|
72
|
+
const props = defineProps<{
|
|
73
|
+
// the picked day in PICKER_DAY_FORMAT
|
|
74
|
+
modelValue: string | null,
|
|
75
|
+
}>();
|
|
76
|
+
|
|
77
|
+
const emit = defineEmits<{
|
|
78
|
+
'update:modelValue': [day: string | null],
|
|
79
|
+
}>();
|
|
80
|
+
|
|
81
|
+
const NAV_BUTTON_CLASSES = `flex items-center justify-center w-7 h-7 rounded-md focus:outline-none focus-visible:ring-[3px]
|
|
82
|
+
bg-lightDatePickerCalendarArrowButtonBackground hover:bg-lightDatePickerCalendarArrowButtonBackgroundHover focus-visible:ring-lightFocusRing
|
|
83
|
+
dark:bg-darkDatePickerCalendarArrowButtonBackground dark:hover:bg-darkDatePickerCalendarArrowButtonBackgroundHover dark:focus-visible:ring-darkFocusRing`;
|
|
84
|
+
|
|
85
|
+
// months and years fill the height of six weeks of days (a row of names, six rows of days, gaps), so the calendar
|
|
86
|
+
// does not jump when its view changes
|
|
87
|
+
const MONTHS_GRID_CLASSES = 'grid-cols-3 grid-rows-4 h-[12.75rem]';
|
|
88
|
+
const YEARS_PER_PAGE = 12;
|
|
89
|
+
// six weeks, so the calendar keeps its height while paging through months
|
|
90
|
+
const DAY_CELLS = 42;
|
|
91
|
+
// Shift+PageUp/PageDown jump by this many pages
|
|
92
|
+
const BIG_PAGE = 12;
|
|
93
|
+
|
|
94
|
+
interface ViewSteps {
|
|
95
|
+
unit: ManipulateType, // what one cell is
|
|
96
|
+
row: number, // cells in a row, the step of ArrowUp and ArrowDown
|
|
97
|
+
pageUnit: ManipulateType, // what the prev and next buttons and PageUp/PageDown turn
|
|
98
|
+
pageSize: number,
|
|
99
|
+
key: string, // format which tells cells of the view apart
|
|
100
|
+
cellLabel: string,
|
|
101
|
+
cellAriaLabel: string,
|
|
102
|
+
previousLabel: string,
|
|
103
|
+
nextLabel: string,
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const VIEW_STEPS: Record<CalendarView, ViewSteps> = {
|
|
107
|
+
days: {
|
|
108
|
+
unit: 'day', row: 7, pageUnit: 'month', pageSize: 1, key: PICKER_DAY_FORMAT,
|
|
109
|
+
cellLabel: 'D', cellAriaLabel: 'D MMMM YYYY', previousLabel: 'Previous month', nextLabel: 'Next month',
|
|
110
|
+
},
|
|
111
|
+
months: {
|
|
112
|
+
unit: 'month', row: 3, pageUnit: 'year', pageSize: 1, key: 'YYYY-MM',
|
|
113
|
+
cellLabel: 'MMM', cellAriaLabel: 'MMMM YYYY', previousLabel: 'Previous year', nextLabel: 'Next year',
|
|
114
|
+
},
|
|
115
|
+
years: {
|
|
116
|
+
unit: 'year', row: 3, pageUnit: 'year', pageSize: YEARS_PER_PAGE, key: 'YYYY',
|
|
117
|
+
cellLabel: 'YYYY', cellAriaLabel: 'YYYY', previousLabel: 'Previous years', nextLabel: 'Next years',
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const view = ref<CalendarView>('days');
|
|
122
|
+
// the cell which takes keyboard focus, the shown month, year or block of years follows it
|
|
123
|
+
const cursor = ref(dayjs(props.modelValue ?? undefined).startOf('day'));
|
|
124
|
+
const gridEl = ref<HTMLElement>();
|
|
125
|
+
|
|
126
|
+
watch(() => props.modelValue, (day) => {
|
|
127
|
+
if (day) {
|
|
128
|
+
cursor.value = dayjs(day);
|
|
129
|
+
view.value = 'days';
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const todayValue = dayjs().format(PICKER_DAY_FORMAT);
|
|
134
|
+
const steps = computed(() => VIEW_STEPS[view.value]);
|
|
135
|
+
const activeValue = computed(() => cursor.value.format(steps.value.key));
|
|
136
|
+
const selectedValue = computed(() => props.modelValue && dayjs(props.modelValue).format(steps.value.key));
|
|
137
|
+
const yearsStart = computed(() => Math.floor(cursor.value.year() / YEARS_PER_PAGE) * YEARS_PER_PAGE);
|
|
138
|
+
|
|
139
|
+
const title = computed(() => {
|
|
140
|
+
if (view.value === 'days') {
|
|
141
|
+
return cursor.value.format('MMMM YYYY');
|
|
142
|
+
}
|
|
143
|
+
if (view.value === 'months') {
|
|
144
|
+
return cursor.value.format('YYYY');
|
|
145
|
+
}
|
|
146
|
+
return `${yearsStart.value} – ${yearsStart.value + YEARS_PER_PAGE - 1}`;
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const weekdayNames = computed(() => dayjs.weekdaysMin(true));
|
|
150
|
+
const leadingBlanks = computed(() => (cursor.value.startOf('month').day() - dayjs.localeData().firstDayOfWeek() + 7) % 7);
|
|
151
|
+
const trailingBlanks = computed(() => view.value === 'days' ? DAY_CELLS - leadingBlanks.value - cursor.value.daysInMonth() : 0);
|
|
152
|
+
|
|
153
|
+
const cells = computed(() => {
|
|
154
|
+
const firstByView: Record<CalendarView, Dayjs> = {
|
|
155
|
+
days: cursor.value.startOf('month'),
|
|
156
|
+
months: cursor.value.startOf('year'),
|
|
157
|
+
years: cursor.value.year(yearsStart.value).startOf('year'),
|
|
158
|
+
};
|
|
159
|
+
const count = view.value === 'days' ? cursor.value.daysInMonth() : 12;
|
|
160
|
+
const { unit, key, cellLabel, cellAriaLabel } = steps.value;
|
|
161
|
+
return Array.from({ length: count }, (_, i) => {
|
|
162
|
+
const date = firstByView[view.value].add(i, unit);
|
|
163
|
+
return { date, value: date.format(key), label: date.format(cellLabel), ariaLabel: date.format(cellAriaLabel) };
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
function turnPage(direction: number) {
|
|
168
|
+
const { pageUnit, pageSize } = steps.value;
|
|
169
|
+
cursor.value = cursor.value.add(direction * pageSize, pageUnit);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function pick(date: Dayjs) {
|
|
173
|
+
if (view.value === 'days') {
|
|
174
|
+
emit('update:modelValue', date.format(PICKER_DAY_FORMAT));
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
cursor.value = view.value === 'months' ? cursor.value.month(date.month()) : cursor.value.year(date.year());
|
|
178
|
+
view.value = view.value === 'months' ? 'days' : 'months';
|
|
179
|
+
// a click keeps the focus in the field, only a keyboard user is already inside the calendar
|
|
180
|
+
if (gridEl.value!.contains(activeElementIn(gridEl.value!))) {
|
|
181
|
+
focus();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function onGridKeydown(e: KeyboardEvent) {
|
|
186
|
+
const { unit, row, pageUnit, pageSize } = steps.value;
|
|
187
|
+
const page = (e.shiftKey ? BIG_PAGE : 1) * pageSize;
|
|
188
|
+
const moves: Record<string, () => Dayjs> = {
|
|
189
|
+
ArrowLeft: () => cursor.value.add(-1, unit),
|
|
190
|
+
ArrowRight: () => cursor.value.add(1, unit),
|
|
191
|
+
ArrowUp: () => cursor.value.add(-row, unit),
|
|
192
|
+
ArrowDown: () => cursor.value.add(row, unit),
|
|
193
|
+
PageUp: () => cursor.value.add(-page, pageUnit),
|
|
194
|
+
PageDown: () => cursor.value.add(page, pageUnit),
|
|
195
|
+
};
|
|
196
|
+
if (!moves[e.key]) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
e.preventDefault();
|
|
200
|
+
cursor.value = moves[e.key]();
|
|
201
|
+
focus();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// moves the keyboard focus to the active cell
|
|
205
|
+
async function focus() {
|
|
206
|
+
await nextTick();
|
|
207
|
+
gridEl.value!.querySelector<HTMLButtonElement>('[tabindex="0"]')!.focus();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
defineExpose({ focus });
|
|
211
|
+
</script>
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div ref="rootEl" class="afcl-date-field relative" :style="{ anchorName }">
|
|
3
|
+
<IconCalendar v-if="!bare" :class="PICKER_ICON_CLASSES" aria-hidden="true" />
|
|
4
|
+
<input
|
|
5
|
+
ref="inputEl"
|
|
6
|
+
v-model="text"
|
|
7
|
+
:id="inputId"
|
|
8
|
+
type="text"
|
|
9
|
+
autocomplete="off"
|
|
10
|
+
aria-haspopup="dialog"
|
|
11
|
+
:aria-expanded="isOpen"
|
|
12
|
+
:placeholder="placeholder"
|
|
13
|
+
:disabled="disabled"
|
|
14
|
+
:class="[
|
|
15
|
+
'w-full',
|
|
16
|
+
PICKER_TEXT_CLASSES,
|
|
17
|
+
bare ? 'h-full px-2.5 bg-transparent border-0' : ['ps-8 pe-2.5', PICKER_BOX_CLASSES, isOpen && PICKER_BOX_OPEN_CLASSES],
|
|
18
|
+
inputClass,
|
|
19
|
+
]"
|
|
20
|
+
@focus="onFocus"
|
|
21
|
+
@click="open"
|
|
22
|
+
@input="onInput"
|
|
23
|
+
@keydown="onInputKeydown"
|
|
24
|
+
@blur="commit"
|
|
25
|
+
/>
|
|
26
|
+
<teleport :to="teleportTarget" v-if="isOpen">
|
|
27
|
+
<!-- mousedown keeps the focus in the field, so a click on a day does not blur it and typing goes on -->
|
|
28
|
+
<div
|
|
29
|
+
ref="popoverEl"
|
|
30
|
+
popover="manual"
|
|
31
|
+
role="dialog"
|
|
32
|
+
:style="{ positionAnchor: anchorName }"
|
|
33
|
+
class="afcl-date-field-popover p-0 rounded-lg bg-lightDatePickerCalendarBackground shadow-popover dark:bg-darkDatePickerCalendarBackground dark:shadow-darkPopover"
|
|
34
|
+
@mousedown.prevent
|
|
35
|
+
@keydown="onPopoverKeydown"
|
|
36
|
+
>
|
|
37
|
+
<Calendar ref="calendarRef" :modelValue="previewDay" @update:modelValue="select" />
|
|
38
|
+
</div>
|
|
39
|
+
</teleport>
|
|
40
|
+
</div>
|
|
41
|
+
</template>
|
|
42
|
+
|
|
43
|
+
<script setup lang="ts">
|
|
44
|
+
import { nextTick, onBeforeUnmount, ref, useId, watch } from 'vue';
|
|
45
|
+
import { useI18n } from 'vue-i18n';
|
|
46
|
+
import { IconCalendar } from '@iconify-prerendered/vue-lucide';
|
|
47
|
+
import { formatPickerDay, parseFlexibleDate } from '@/utils/dateUtils';
|
|
48
|
+
import { activeElementIn, useTeleportTarget } from './shadowRoot';
|
|
49
|
+
import Calendar from './Calendar.vue';
|
|
50
|
+
import { PICKER_BOX_CLASSES, PICKER_BOX_OPEN_CLASSES, PICKER_ICON_CLASSES, PICKER_TEXT_CLASSES } from './pickerStyles';
|
|
51
|
+
|
|
52
|
+
const props = defineProps<{
|
|
53
|
+
// the picked day in PICKER_DAY_FORMAT
|
|
54
|
+
modelValue: string | null,
|
|
55
|
+
placeholder?: string,
|
|
56
|
+
disabled?: boolean,
|
|
57
|
+
// closes the calendar once a day is picked
|
|
58
|
+
autoHide?: boolean,
|
|
59
|
+
// no border, icon and background: the field sits inside a box of the parent, like the two halves of a range
|
|
60
|
+
bare?: boolean,
|
|
61
|
+
inputId?: string,
|
|
62
|
+
inputClass?: string,
|
|
63
|
+
}>();
|
|
64
|
+
|
|
65
|
+
const emit = defineEmits<{
|
|
66
|
+
'update:modelValue': [day: string | null],
|
|
67
|
+
}>();
|
|
68
|
+
|
|
69
|
+
const OFFSET = 6;
|
|
70
|
+
const SUPPORTS_ANCHOR = CSS.supports('anchor-name: --a');
|
|
71
|
+
|
|
72
|
+
const { locale } = useI18n();
|
|
73
|
+
const teleportTarget = useTeleportTarget();
|
|
74
|
+
const anchorName = `--afcl-date-field-${useId()}`;
|
|
75
|
+
|
|
76
|
+
const rootEl = ref<HTMLElement>();
|
|
77
|
+
const inputEl = ref<HTMLInputElement>();
|
|
78
|
+
const popoverEl = ref<HTMLElement>();
|
|
79
|
+
const calendarRef = ref<InstanceType<typeof Calendar>>();
|
|
80
|
+
|
|
81
|
+
const isOpen = ref(false);
|
|
82
|
+
const text = ref('');
|
|
83
|
+
// the day the calendar shows while the user types, it becomes the value on Enter or blur
|
|
84
|
+
const previewDay = ref<string | null>(null);
|
|
85
|
+
// focus which the field takes back from its own calendar must not open it again
|
|
86
|
+
let skipFocusOpen = false;
|
|
87
|
+
|
|
88
|
+
function showValue() {
|
|
89
|
+
text.value = props.modelValue ? formatPickerDay(props.modelValue) : '';
|
|
90
|
+
previewDay.value = props.modelValue;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
watch(() => props.modelValue, showValue, { immediate: true });
|
|
94
|
+
// month names follow the language
|
|
95
|
+
watch(locale, showValue);
|
|
96
|
+
|
|
97
|
+
function open() {
|
|
98
|
+
if (!props.disabled) {
|
|
99
|
+
isOpen.value = true;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function close() {
|
|
104
|
+
isOpen.value = false;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function onFocus() {
|
|
108
|
+
if (skipFocusOpen) {
|
|
109
|
+
skipFocusOpen = false;
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
open();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function focusField() {
|
|
116
|
+
skipFocusOpen = true;
|
|
117
|
+
inputEl.value!.focus();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function onInput() {
|
|
121
|
+
open();
|
|
122
|
+
const typed = text.value.trim();
|
|
123
|
+
previewDay.value = typed ? parseFlexibleDate(typed) ?? previewDay.value : null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function commit() {
|
|
127
|
+
const typed = text.value.trim();
|
|
128
|
+
const day = typed ? parseFlexibleDate(typed) : null;
|
|
129
|
+
if (typed && !day) {
|
|
130
|
+
// not a date: the field returns to the value it had
|
|
131
|
+
showValue();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
text.value = day ? formatPickerDay(day) : '';
|
|
135
|
+
previewDay.value = day;
|
|
136
|
+
if (day !== props.modelValue) {
|
|
137
|
+
emit('update:modelValue', day);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function select(day: string | null) {
|
|
142
|
+
const focusInCalendar = popoverEl.value!.contains(activeElementIn(popoverEl.value!));
|
|
143
|
+
text.value = day ? formatPickerDay(day) : '';
|
|
144
|
+
previewDay.value = day;
|
|
145
|
+
if (day !== props.modelValue) {
|
|
146
|
+
emit('update:modelValue', day);
|
|
147
|
+
}
|
|
148
|
+
if (props.autoHide) {
|
|
149
|
+
close();
|
|
150
|
+
if (focusInCalendar) {
|
|
151
|
+
focusField();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function onInputKeydown(e: KeyboardEvent) {
|
|
157
|
+
if (e.key === 'ArrowDown') {
|
|
158
|
+
e.preventDefault();
|
|
159
|
+
open();
|
|
160
|
+
nextTick(() => calendarRef.value!.focus());
|
|
161
|
+
} else if (e.key === 'Escape' && isOpen.value) {
|
|
162
|
+
// only the calendar closes, not a dialog or drawer around the field
|
|
163
|
+
e.stopPropagation();
|
|
164
|
+
close();
|
|
165
|
+
} else if (e.key === 'Enter' && isOpen.value) {
|
|
166
|
+
e.preventDefault();
|
|
167
|
+
commit();
|
|
168
|
+
if (props.autoHide) {
|
|
169
|
+
close();
|
|
170
|
+
}
|
|
171
|
+
} else if (e.key === 'Tab') {
|
|
172
|
+
close();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function onPopoverKeydown(e: KeyboardEvent) {
|
|
177
|
+
if (e.key === 'Escape') {
|
|
178
|
+
e.stopPropagation();
|
|
179
|
+
close();
|
|
180
|
+
focusField();
|
|
181
|
+
} else if (e.key === 'Tab') {
|
|
182
|
+
// the calendar is teleported to the end of the page, Tab cycles inside it like in a dialog
|
|
183
|
+
const focusable = [...popoverEl.value!.querySelectorAll<HTMLElement>('button:not([disabled]):not([tabindex="-1"])')];
|
|
184
|
+
const current = focusable.indexOf(activeElementIn(popoverEl.value!) as HTMLElement);
|
|
185
|
+
e.preventDefault();
|
|
186
|
+
focusable[(current + (e.shiftKey ? -1 : 1) + focusable.length) % focusable.length].focus();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function onDocumentMousedown(e: MouseEvent) {
|
|
191
|
+
// inside a shadow root e.target is the host, composedPath() starts at the clicked element
|
|
192
|
+
const target = e.composedPath()[0] as Node;
|
|
193
|
+
if (!rootEl.value!.contains(target) && !popoverEl.value?.contains(target)) {
|
|
194
|
+
close();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Safari before 26 and Firefox before 147 have no CSS anchor positioning
|
|
199
|
+
function placeByRect() {
|
|
200
|
+
const field = rootEl.value!.getBoundingClientRect();
|
|
201
|
+
const el = popoverEl.value!;
|
|
202
|
+
const { width, height } = el.getBoundingClientRect();
|
|
203
|
+
const fitsBelow = field.bottom + OFFSET + height <= window.innerHeight;
|
|
204
|
+
el.style.top = `${fitsBelow ? field.bottom + OFFSET : field.top - OFFSET - height}px`;
|
|
205
|
+
el.style.left = `${Math.min(Math.max(field.left, 0), window.innerWidth - width)}px`;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function addListeners() {
|
|
209
|
+
document.addEventListener('mousedown', onDocumentMousedown);
|
|
210
|
+
if (!SUPPORTS_ANCHOR) {
|
|
211
|
+
window.addEventListener('scroll', placeByRect, true);
|
|
212
|
+
window.addEventListener('resize', placeByRect);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function removeListeners() {
|
|
217
|
+
document.removeEventListener('mousedown', onDocumentMousedown);
|
|
218
|
+
window.removeEventListener('scroll', placeByRect, true);
|
|
219
|
+
window.removeEventListener('resize', placeByRect);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
watch(isOpen, async (opened) => {
|
|
223
|
+
if (!opened) {
|
|
224
|
+
removeListeners();
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
await nextTick();
|
|
228
|
+
popoverEl.value!.showPopover();
|
|
229
|
+
if (!SUPPORTS_ANCHOR) {
|
|
230
|
+
placeByRect();
|
|
231
|
+
}
|
|
232
|
+
addListeners();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
onBeforeUnmount(removeListeners);
|
|
236
|
+
|
|
237
|
+
defineExpose({
|
|
238
|
+
focus: () => inputEl.value!.focus(),
|
|
239
|
+
});
|
|
240
|
+
</script>
|
|
241
|
+
|
|
242
|
+
<style>
|
|
243
|
+
/* popover puts the calendar into the top layer, so no z-index and no clipping by overflow of the parents */
|
|
244
|
+
.afcl-date-field-popover {
|
|
245
|
+
inset: auto;
|
|
246
|
+
margin: 0;
|
|
247
|
+
overflow: visible;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
@supports (anchor-name: --a) {
|
|
251
|
+
.afcl-date-field-popover {
|
|
252
|
+
position-area: bottom span-right;
|
|
253
|
+
position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
|
|
254
|
+
margin-top: 6px;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
</style>
|