91 lines
2.9 KiB
Vue
91 lines
2.9 KiB
Vue
<script setup lang="ts">
|
|
import { computed, ref, useAttrs, watch } from "vue";
|
|
import {
|
|
injectPopoverRootContext,
|
|
PopoverAnchor,
|
|
useForwardExpose,
|
|
} from "reka-ui";
|
|
import { dateRangePickerVariants } from "@heroui-vue/styles";
|
|
import OverlayAction from "../overlay/OverlayAction";
|
|
import { usePopover } from "../popover/context";
|
|
import { useDateRangePicker } from "./context";
|
|
defineOptions({ name: "DateRangePickerTrigger", inheritAttrs: false });
|
|
const props = withDefaults(
|
|
defineProps<{ as?: string; asChild?: boolean; isDisabled?: boolean }>(),
|
|
{ as: "button" },
|
|
);
|
|
const picker = useDateRangePicker();
|
|
const popover = usePopover();
|
|
const root = injectPopoverRootContext();
|
|
const attrs = useAttrs();
|
|
const { forwardRef, currentElement } = useForwardExpose();
|
|
const focusVisible = ref(false);
|
|
const triggerId = computed(
|
|
() => (attrs.id as string | undefined) ?? popover.triggerId,
|
|
);
|
|
const disabled = computed(
|
|
() => props.isDisabled || picker.disabled.value || picker.readOnly.value,
|
|
);
|
|
const defaultLabelledBy = computed(() => {
|
|
if (attrs["aria-label"] || attrs["aria-labelledby"]) return undefined;
|
|
const label = picker.labelledBy();
|
|
return label ? [triggerId.value, label].join(" ") : undefined;
|
|
});
|
|
const defaultLabel = computed(() => {
|
|
if (attrs["aria-label"] || attrs["aria-labelledby"]) return undefined;
|
|
if (defaultLabelledBy.value) return undefined;
|
|
return picker.locale.value.toLowerCase().startsWith("zh")
|
|
? "日历"
|
|
: "Calendar";
|
|
});
|
|
watch(
|
|
[currentElement, triggerId],
|
|
([element, id]) => {
|
|
picker.trigger.value = element;
|
|
picker.triggerId.value = id;
|
|
root.triggerElement.value = element;
|
|
root.triggerId = id;
|
|
},
|
|
{ immediate: true },
|
|
);
|
|
</script>
|
|
<template>
|
|
<PopoverAnchor as-child>
|
|
<OverlayAction
|
|
:ref="forwardRef"
|
|
v-bind="$attrs"
|
|
:id="triggerId"
|
|
:as="as"
|
|
:as-child="asChild"
|
|
:is-disabled="disabled"
|
|
:activate="popover.state.toggle"
|
|
:class="dateRangePickerVariants().trigger()"
|
|
data-slot="date-range-picker-trigger"
|
|
aria-haspopup="dialog"
|
|
:aria-expanded="popover.state.isOpen.value"
|
|
:aria-controls="
|
|
popover.state.isOpen.value ? popover.contentId.value : undefined
|
|
"
|
|
:aria-label="($attrs['aria-label'] as string | undefined) ?? defaultLabel"
|
|
:aria-labelledby="
|
|
($attrs['aria-labelledby'] as string | undefined) ?? defaultLabelledBy
|
|
"
|
|
:aria-describedby="
|
|
($attrs['aria-describedby'] as string | undefined) ??
|
|
picker.describedBy()
|
|
"
|
|
:data-open="popover.state.isOpen.value || undefined"
|
|
:data-disabled="disabled || undefined"
|
|
:data-focus-visible="focusVisible || undefined"
|
|
@focus="
|
|
focusVisible = ($event.currentTarget as HTMLElement).matches(
|
|
':focus-visible',
|
|
)
|
|
"
|
|
@blur="focusVisible = false"
|
|
>
|
|
<slot />
|
|
</OverlayAction>
|
|
</PopoverAnchor>
|
|
</template>
|