feat: migrate public DatePicker with shared field and calendar owners
This commit is contained in:
@@ -1,5 +1,13 @@
|
||||
<script setup lang="ts" generic="M extends CalendarSelectionMode = 'single'">
|
||||
import { computed, getCurrentInstance, useAttrs } from "vue";
|
||||
import {
|
||||
computed,
|
||||
getCurrentInstance,
|
||||
inject,
|
||||
nextTick,
|
||||
onMounted,
|
||||
useAttrs,
|
||||
watch,
|
||||
} from "vue";
|
||||
import {
|
||||
GregorianCalendar,
|
||||
toCalendar,
|
||||
@@ -10,6 +18,7 @@ import {
|
||||
import { useChoiceValue } from "../choice/shared";
|
||||
import { copyDateValue, copyDateValues } from "../date/value";
|
||||
import { calendarHeading } from "../date/calendar";
|
||||
import { datePickerKey } from "../date-picker/context";
|
||||
import { useCalendarView } from "./view";
|
||||
import type {
|
||||
CalendarModel,
|
||||
@@ -39,10 +48,19 @@ defineSlots<{
|
||||
}>();
|
||||
const attrs = useAttrs(),
|
||||
instance = getCurrentInstance()!;
|
||||
const picker = inject(datePickerKey, undefined);
|
||||
type Value = DateValue | DateValue[] | null;
|
||||
const copy = (value: Value | undefined): Value =>
|
||||
Array.isArray(value) ? copyDateValues(value) : copyDateValue(value ?? null);
|
||||
const has = (key: string) => Object.hasOwn(instance.vnode.props ?? {}, key);
|
||||
const inheritsValue = computed(
|
||||
() =>
|
||||
!!picker &&
|
||||
!has("modelValue") &&
|
||||
!has("model-value") &&
|
||||
!has("value") &&
|
||||
!has("defaultValue"),
|
||||
);
|
||||
const model = useChoiceValue<Value>(
|
||||
() =>
|
||||
copy(
|
||||
@@ -56,6 +74,10 @@ const model = useChoiceValue<Value>(
|
||||
},
|
||||
);
|
||||
const selectedValues = computed(() => {
|
||||
if (inheritsValue.value) {
|
||||
const value = picker!.value.value;
|
||||
return value ? [value] : [];
|
||||
}
|
||||
const value = model.value.value;
|
||||
return Array.isArray(value) ? value : value ? [value] : [];
|
||||
});
|
||||
@@ -67,10 +89,26 @@ const view = useCalendarView(
|
||||
dates: selectedValues,
|
||||
selected,
|
||||
request,
|
||||
unavailable: (date) => !!props.isDateUnavailable?.(date.copy()),
|
||||
unavailable: (date) =>
|
||||
!!(props.isDateUnavailable ?? picker?.isDateUnavailable)?.(date),
|
||||
multiple: () => props.selectionMode === "multiple",
|
||||
},
|
||||
emit,
|
||||
"calendar",
|
||||
picker
|
||||
? {
|
||||
minValue: () => picker.minValue.value,
|
||||
maxValue: () => picker.maxValue.value,
|
||||
defaultFocusedValue: () => picker.placeholder.value,
|
||||
locale: () => picker.locale.value,
|
||||
dir: () => picker.dir.value,
|
||||
firstDayOfWeek: () => picker.firstDayOfWeek,
|
||||
pageBehavior: () => picker.pageBehavior,
|
||||
isDisabled: () => picker.disabled.value,
|
||||
isReadOnly: () => picker.readOnly.value,
|
||||
isInvalid: () => picker.invalid.value,
|
||||
}
|
||||
: undefined,
|
||||
);
|
||||
const {
|
||||
root,
|
||||
@@ -89,6 +127,10 @@ const {
|
||||
function request(value: DateValue) {
|
||||
if (view.disabledDay(value) || view.unavailable(value) || view.readOnly.value)
|
||||
return;
|
||||
if (inheritsValue.value) {
|
||||
picker!.requestDate(value);
|
||||
return;
|
||||
}
|
||||
const fallback = Array.isArray(props.defaultValue)
|
||||
? props.defaultValue[0]
|
||||
: props.defaultValue;
|
||||
@@ -113,6 +155,19 @@ function request(value: DateValue) {
|
||||
);
|
||||
} else if (!selected(value)) model.request(next.copy());
|
||||
}
|
||||
function focusForPicker(open: boolean) {
|
||||
if (!picker || !open) return;
|
||||
void nextTick(() => {
|
||||
if (picker.state.isOpen.value && tabStop.value)
|
||||
focusDate(tabStop.value, true);
|
||||
});
|
||||
}
|
||||
onMounted(() => focusForPicker(!!picker?.state.isOpen.value));
|
||||
watch(
|
||||
() => picker?.state.isOpen.value,
|
||||
(open) => focusForPicker(!!open),
|
||||
{ flush: "post" },
|
||||
);
|
||||
defineExpose({
|
||||
get element() {
|
||||
return root.value;
|
||||
|
||||
@@ -43,6 +43,19 @@ export type CalendarViewProps = Omit<
|
||||
| "selectionMode"
|
||||
| "isDateUnavailable"
|
||||
>;
|
||||
/** Constraints a DatePicker owner passes to an otherwise unbound Calendar. */
|
||||
export interface CalendarInheritedConstraints {
|
||||
minValue?: () => DateValue | undefined;
|
||||
maxValue?: () => DateValue | undefined;
|
||||
defaultFocusedValue?: () => DateValue | undefined;
|
||||
locale?: () => string;
|
||||
dir?: () => "ltr" | "rtl";
|
||||
firstDayOfWeek?: () => import("../date/calendar").CalendarWeekday | undefined;
|
||||
pageBehavior?: () => "visible" | "single" | undefined;
|
||||
isDisabled?: () => boolean;
|
||||
isReadOnly?: () => boolean;
|
||||
isInvalid?: () => boolean;
|
||||
}
|
||||
export interface CalendarSelectionAdapter {
|
||||
dates: ComputedRef<DateValue[]>;
|
||||
selected: (date: DateValue) => boolean;
|
||||
@@ -70,6 +83,7 @@ export function useCalendarView(
|
||||
selection: CalendarSelectionAdapter,
|
||||
emit: ViewEmit,
|
||||
prefix: "calendar" | "range-calendar" = "calendar",
|
||||
inherited?: CalendarInheritedConstraints,
|
||||
) {
|
||||
const instance = getCurrentInstance()!,
|
||||
config = useConfig();
|
||||
@@ -78,25 +92,40 @@ export function useCalendarView(
|
||||
trigger = shallowRef<HTMLElement>();
|
||||
const headingId = `hv-calendar-${useId()}-heading`;
|
||||
const locale = computed(
|
||||
() => props.locale ?? config?.locale.value ?? "en-US",
|
||||
() =>
|
||||
props.locale ?? inherited?.locale?.() ?? config?.locale.value ?? "en-US",
|
||||
);
|
||||
const dir = computed(
|
||||
() => props.dir ?? inherited?.dir?.() ?? config?.dir.value ?? "ltr",
|
||||
);
|
||||
const dir = computed(() => props.dir ?? config?.dir.value ?? "ltr");
|
||||
const timeZone = computed(() => props.timeZone ?? getLocalTimeZone());
|
||||
const disabled = useInheritedDisabled(
|
||||
() => props.isDisabled || props.disabled,
|
||||
() =>
|
||||
props.isDisabled || props.disabled || Boolean(inherited?.isDisabled?.()),
|
||||
);
|
||||
const readOnly = computed(
|
||||
() => !!props.isReadOnly || Boolean(inherited?.isReadOnly?.()),
|
||||
);
|
||||
const readOnly = computed(() => !!props.isReadOnly);
|
||||
const multiple = computed(() => selection.multiple?.() ?? false);
|
||||
const invalid = computed(() => !!props.isInvalid || !!selection.invalid?.());
|
||||
const invalid = computed(
|
||||
() =>
|
||||
!!props.isInvalid ||
|
||||
Boolean(inherited?.isInvalid?.()) ||
|
||||
!!selection.invalid?.(),
|
||||
);
|
||||
const options = computed(() => ({
|
||||
locale: locale.value,
|
||||
firstDayOfWeek: props.firstDayOfWeek,
|
||||
firstDayOfWeek: props.firstDayOfWeek ?? inherited?.firstDayOfWeek?.(),
|
||||
visibleDuration: props.visibleDuration,
|
||||
weeksInMonth: props.weeksInMonth,
|
||||
}));
|
||||
const pageBehavior = computed(
|
||||
() => props.pageBehavior ?? inherited?.pageBehavior?.(),
|
||||
);
|
||||
const initial = copyDateValue(
|
||||
props.defaultFocusedValue ??
|
||||
selection.dates.value[0] ??
|
||||
inherited?.defaultFocusedValue?.() ??
|
||||
today(timeZone.value),
|
||||
);
|
||||
const focusModel = useChoiceValue<DateValue>(
|
||||
@@ -114,7 +143,9 @@ export function useCalendarView(
|
||||
const min = computed(() =>
|
||||
toCalendarDate(
|
||||
dateForLocale(
|
||||
props.minValue ?? new CalendarDate(1900, 1, 1),
|
||||
props.minValue ??
|
||||
inherited?.minValue?.() ??
|
||||
new CalendarDate(1900, 1, 1),
|
||||
locale.value,
|
||||
),
|
||||
),
|
||||
@@ -122,7 +153,9 @@ export function useCalendarView(
|
||||
const max = computed(() =>
|
||||
toCalendarDate(
|
||||
dateForLocale(
|
||||
props.maxValue ?? new CalendarDate(2099, 12, 31),
|
||||
props.maxValue ??
|
||||
inherited?.maxValue?.() ??
|
||||
new CalendarDate(2099, 12, 31),
|
||||
locale.value,
|
||||
),
|
||||
),
|
||||
@@ -226,7 +259,7 @@ export function useCalendarView(
|
||||
{ flush: "sync" },
|
||||
);
|
||||
watch(
|
||||
[locale, () => props.visibleDuration, () => props.firstDayOfWeek],
|
||||
[locale, () => props.visibleDuration, () => options.value.firstDayOfWeek],
|
||||
() => {
|
||||
anchor.value = aligned(focus.value);
|
||||
},
|
||||
@@ -247,7 +280,7 @@ export function useCalendarView(
|
||||
function pageStep(direction: -1 | 1) {
|
||||
const { unit, count } = calendarDuration(props.visibleDuration);
|
||||
return {
|
||||
[unit]: direction * (props.pageBehavior === "single" ? 1 : count),
|
||||
[unit]: direction * (pageBehavior.value === "single" ? 1 : count),
|
||||
};
|
||||
}
|
||||
function pageDisabled(direction: -1 | 1) {
|
||||
|
||||
Reference in New Issue
Block a user