feat: migrate public DatePicker with shared field and calendar owners
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, useAttrs, watch } from "vue";
|
||||
import { PopoverAnchor, useForwardExpose } from "reka-ui";
|
||||
import { datePickerVariants } from "@heroui-vue/styles";
|
||||
import OverlayAction from "../overlay/OverlayAction";
|
||||
import { usePopover } from "../popover/context";
|
||||
import { useDatePicker } from "./context";
|
||||
defineOptions({ name: "DatePickerTrigger", inheritAttrs: false });
|
||||
const props = withDefaults(
|
||||
defineProps<{ as?: string; asChild?: boolean; isDisabled?: boolean }>(),
|
||||
{ as: "button" },
|
||||
);
|
||||
const popover = usePopover();
|
||||
const picker = useDatePicker();
|
||||
const attrs = useAttrs();
|
||||
const { forwardRef, currentElement } = useForwardExpose();
|
||||
const focusVisible = ref(false);
|
||||
const disabled = computed(
|
||||
() => props.isDisabled || picker.disabled.value || picker.readOnly.value,
|
||||
);
|
||||
const defaultLabelledBy = computed(() => {
|
||||
if (attrs["aria-label"] || attrs["aria-labelledby"]) return undefined;
|
||||
const ids = [popover.triggerId, picker.labelledBy()].filter(Boolean);
|
||||
return ids.length ? ids.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,
|
||||
(element) => {
|
||||
picker.trigger.value = element;
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
</script>
|
||||
<template>
|
||||
<PopoverAnchor as-child>
|
||||
<OverlayAction
|
||||
:ref="forwardRef"
|
||||
v-bind="$attrs"
|
||||
:as="as"
|
||||
:as-child="asChild"
|
||||
:is-disabled="disabled"
|
||||
:activate="popover.state.toggle"
|
||||
:class="datePickerVariants().trigger()"
|
||||
data-slot="date-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 = true"
|
||||
@blur="focusVisible = false"
|
||||
>
|
||||
<slot />
|
||||
</OverlayAction>
|
||||
</PopoverAnchor>
|
||||
</template>
|
||||
Reference in New Issue
Block a user