feat: establish shared calendar range selection foundation
This commit is contained in:
@@ -73,14 +73,14 @@ function staticPart(
|
||||
inheritAttrs: false,
|
||||
props: { as: { type: String, default: tag } },
|
||||
setup(props, { attrs, slots }) {
|
||||
useCalendar();
|
||||
const ctx = useCalendar();
|
||||
return () =>
|
||||
h(
|
||||
props.as,
|
||||
mergeProps(
|
||||
{
|
||||
class: className,
|
||||
"data-slot": slot,
|
||||
class: className.replace("hv-calendar__", `hv-${ctx.prefix}__`),
|
||||
"data-slot": slot.replace("calendar-", `${ctx.prefix}-`),
|
||||
"aria-hidden":
|
||||
slot === "calendar-cell-indicator" ? "true" : undefined,
|
||||
},
|
||||
@@ -126,8 +126,8 @@ export const CalendarHeading = defineComponent({
|
||||
mergeProps(
|
||||
{
|
||||
id,
|
||||
class: "hv-calendar__heading",
|
||||
"data-slot": "calendar-heading",
|
||||
class: `hv-${ctx.prefix}__heading`,
|
||||
"data-slot": `${ctx.prefix}-heading`,
|
||||
"aria-live": "polite",
|
||||
"aria-atomic": "true",
|
||||
},
|
||||
@@ -162,12 +162,12 @@ export const CalendarNavButton = defineComponent({
|
||||
mergeProps(
|
||||
{
|
||||
type: "button",
|
||||
class: "hv-calendar__nav-button",
|
||||
class: `hv-${ctx.prefix}__nav-button`,
|
||||
"aria-label": direction === -1 ? "Previous" : "Next",
|
||||
},
|
||||
attrs,
|
||||
{
|
||||
"data-slot": "calendar-nav-button",
|
||||
"data-slot": `${ctx.prefix}-nav-button`,
|
||||
disabled,
|
||||
tabindex: ctx.yearOpen.value ? -1 : attrs.tabindex,
|
||||
"data-disabled": disabled || undefined,
|
||||
@@ -176,7 +176,12 @@ export const CalendarNavButton = defineComponent({
|
||||
},
|
||||
},
|
||||
),
|
||||
slots.default?.() ?? [calendarChevron(direction === -1)],
|
||||
slots.default?.() ?? [
|
||||
calendarChevron(
|
||||
direction === -1,
|
||||
`hv-${ctx.prefix}__nav-button-icon`,
|
||||
),
|
||||
],
|
||||
);
|
||||
};
|
||||
},
|
||||
@@ -207,7 +212,7 @@ export const CalendarGrid = defineComponent({
|
||||
"table",
|
||||
mergeProps(
|
||||
{
|
||||
class: "hv-calendar__grid",
|
||||
class: `hv-${ctx.prefix}__grid`,
|
||||
role: "grid",
|
||||
"aria-label": calendarHeading(
|
||||
ctx.anchor.value,
|
||||
@@ -220,7 +225,7 @@ export const CalendarGrid = defineComponent({
|
||||
},
|
||||
attrs,
|
||||
{
|
||||
"data-slot": "calendar-grid",
|
||||
"data-slot": `${ctx.prefix}-grid`,
|
||||
"aria-hidden": ctx.yearOpen.value || undefined,
|
||||
inert: ctx.yearOpen.value ? "" : undefined,
|
||||
},
|
||||
@@ -236,14 +241,15 @@ export const CalendarGridHeader = defineComponent({
|
||||
default?: (state: { day: string; index: number }) => VNodeChild;
|
||||
}>,
|
||||
setup(_props, { attrs, slots }) {
|
||||
const ctx = useCalendar();
|
||||
const grid = useCalendarGrid();
|
||||
return () =>
|
||||
h(
|
||||
"thead",
|
||||
mergeProps(
|
||||
{
|
||||
class: "hv-calendar__grid-header",
|
||||
"data-slot": "calendar-grid-header",
|
||||
class: `hv-${ctx.prefix}__grid-header`,
|
||||
"data-slot": `${ctx.prefix}-grid-header`,
|
||||
},
|
||||
attrs,
|
||||
),
|
||||
@@ -267,21 +273,22 @@ export const CalendarGridBody = defineComponent({
|
||||
default?: (state: { date: DateValue }) => VNodeChild;
|
||||
}>,
|
||||
setup(_props, { attrs, slots }) {
|
||||
const ctx = useCalendar();
|
||||
const grid = useCalendarGrid();
|
||||
return () =>
|
||||
h(
|
||||
"tbody",
|
||||
mergeProps(
|
||||
{
|
||||
class: "hv-calendar__grid-body",
|
||||
"data-slot": "calendar-grid-body",
|
||||
class: `hv-${ctx.prefix}__grid-body`,
|
||||
"data-slot": `${ctx.prefix}-grid-body`,
|
||||
},
|
||||
attrs,
|
||||
),
|
||||
grid.value.rows.map((row, index) =>
|
||||
h(
|
||||
"tr",
|
||||
{ key: index, class: "hv-calendar__grid-row" },
|
||||
{ key: index, class: `hv-${ctx.prefix}__grid-row` },
|
||||
row.map((date, index) =>
|
||||
date
|
||||
? (slots.default?.({ date: date.copy() }) ??
|
||||
@@ -334,6 +341,10 @@ export const CalendarCell = defineComponent({
|
||||
isHovered: hovered.value,
|
||||
isPressed: pressed.value,
|
||||
isFocusVisible: focusVisible.value,
|
||||
isSelectionStart: false,
|
||||
isSelectionEnd: false,
|
||||
isInvalid: ctx.invalid.value && ctx.selected(props.date),
|
||||
...ctx.range?.state(props.date),
|
||||
};
|
||||
});
|
||||
watch(
|
||||
@@ -357,6 +368,11 @@ export const CalendarCell = defineComponent({
|
||||
event.altKey
|
||||
)
|
||||
return;
|
||||
if (event.key === "Escape" && ctx.range?.cancel()) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
return;
|
||||
}
|
||||
const date = props.date;
|
||||
const sign = ctx.dir.value === "rtl" ? -1 : 1;
|
||||
let target: DateValue;
|
||||
@@ -404,7 +420,8 @@ export const CalendarCell = defineComponent({
|
||||
case " ":
|
||||
event.preventDefault();
|
||||
pressed.value = true;
|
||||
ctx.request(date);
|
||||
if (ctx.range) ctx.range.activate(date, "keyboard", event);
|
||||
else ctx.request(date);
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
@@ -413,6 +430,7 @@ export const CalendarCell = defineComponent({
|
||||
event.stopPropagation();
|
||||
if (target.compare(ctx.min.value) < 0) target = ctx.min.value;
|
||||
if (target.compare(ctx.max.value) > 0) target = ctx.max.value;
|
||||
target = ctx.range?.focusTarget(target) ?? target;
|
||||
// Bounds are finite and calendar arithmetic may saturate at year limits.
|
||||
while (
|
||||
target.compare(ctx.min.value) >= 0 &&
|
||||
@@ -422,7 +440,8 @@ export const CalendarCell = defineComponent({
|
||||
ctx.focusDate(target, true);
|
||||
return;
|
||||
}
|
||||
const next = target.add({ days: step });
|
||||
const candidate = target.add({ days: step });
|
||||
const next = ctx.range?.focusTarget(candidate) ?? candidate;
|
||||
if (isSameDay(next, target)) return;
|
||||
target = next;
|
||||
}
|
||||
@@ -438,6 +457,7 @@ export const CalendarCell = defineComponent({
|
||||
{
|
||||
role: "gridcell",
|
||||
"aria-selected": value.isSelected,
|
||||
"aria-invalid": value.isInvalid || undefined,
|
||||
"aria-hidden": value.isOutsideMonth || undefined,
|
||||
"aria-disabled": value.isDisabled || value.isUnavailable || undefined,
|
||||
},
|
||||
@@ -447,7 +467,7 @@ export const CalendarCell = defineComponent({
|
||||
mergeProps(
|
||||
{
|
||||
type: "button",
|
||||
class: "hv-calendar__cell",
|
||||
class: `hv-${ctx.prefix}__cell`,
|
||||
tabindex:
|
||||
!ctx.yearOpen.value &&
|
||||
!value.isDisabled &&
|
||||
@@ -463,10 +483,14 @@ export const CalendarCell = defineComponent({
|
||||
ref: (node: unknown) => {
|
||||
element = node as HTMLButtonElement | undefined;
|
||||
},
|
||||
"data-slot": "calendar-cell",
|
||||
"data-slot": `${ctx.prefix}-cell`,
|
||||
"data-date": props.date.toString(),
|
||||
disabled: value.isDisabled,
|
||||
"data-selected": value.isSelected || undefined,
|
||||
"data-selection-start": value.isSelectionStart || undefined,
|
||||
"data-selection-end": value.isSelectionEnd || undefined,
|
||||
"data-invalid": value.isInvalid || undefined,
|
||||
"aria-invalid": value.isInvalid || undefined,
|
||||
"data-today": value.isToday || undefined,
|
||||
"data-disabled": value.isDisabled || undefined,
|
||||
"data-unavailable": value.isUnavailable || undefined,
|
||||
@@ -475,6 +499,11 @@ export const CalendarCell = defineComponent({
|
||||
value.isDisabled || value.isUnavailable || undefined,
|
||||
onKeydown: keydown,
|
||||
onClick: (event: MouseEvent) => {
|
||||
if (ctx.range) {
|
||||
if (value.isDisabled || ctx.yearOpen.value) return;
|
||||
ctx.range.click(props.date, event);
|
||||
return;
|
||||
}
|
||||
if (
|
||||
!event.defaultPrevented &&
|
||||
!ctx.yearOpen.value &&
|
||||
@@ -488,14 +517,19 @@ export const CalendarCell = defineComponent({
|
||||
"data-focus-visible": focusVisible.value || undefined,
|
||||
"data-hovered": hovered.value || undefined,
|
||||
"data-pressed": pressed.value || undefined,
|
||||
onPointerenter: () => {
|
||||
if (!value.isDisabled) hovered.value = true;
|
||||
onPointerenter: (event: PointerEvent) => {
|
||||
if (!event.defaultPrevented && !value.isDisabled)
|
||||
hovered.value = true;
|
||||
if (!value.isDisabled && !ctx.yearOpen.value)
|
||||
ctx.range?.pointerEnter(props.date, event);
|
||||
},
|
||||
onPointerleave: () => {
|
||||
hovered.value = false;
|
||||
pressed.value = false;
|
||||
},
|
||||
onPointerdown: (event: PointerEvent) => {
|
||||
if (!value.isDisabled && !ctx.yearOpen.value)
|
||||
ctx.range?.pointerDown(props.date, event);
|
||||
if (
|
||||
!event.defaultPrevented &&
|
||||
!value.isDisabled &&
|
||||
@@ -503,7 +537,9 @@ export const CalendarCell = defineComponent({
|
||||
)
|
||||
pressed.value = true;
|
||||
},
|
||||
onPointerup: () => {
|
||||
onPointerup: (event: PointerEvent) => {
|
||||
if (!value.isDisabled && !ctx.yearOpen.value)
|
||||
ctx.range?.pointerUp(props.date, event);
|
||||
pressed.value = false;
|
||||
},
|
||||
onKeyup: () => {
|
||||
@@ -524,7 +560,20 @@ export const CalendarCell = defineComponent({
|
||||
},
|
||||
},
|
||||
),
|
||||
slots.default?.(value) ?? value.formattedDate,
|
||||
ctx.range
|
||||
? h(
|
||||
"span",
|
||||
{
|
||||
class: `hv-${ctx.prefix}__cell-button`,
|
||||
"data-slot": `${ctx.prefix}-cell-button`,
|
||||
"data-selected":
|
||||
value.isSelectionStart ||
|
||||
value.isSelectionEnd ||
|
||||
undefined,
|
||||
},
|
||||
slots.default?.(value) ?? value.formattedDate,
|
||||
)
|
||||
: (slots.default?.(value) ?? value.formattedDate),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user