159 lines
5.2 KiB
Vue
159 lines
5.2 KiB
Vue
<script lang="ts">
|
|
import {
|
|
computed,
|
|
defineComponent,
|
|
h,
|
|
mergeProps,
|
|
provide,
|
|
ref,
|
|
watch,
|
|
} from "vue";
|
|
import { PopoverAnchor, injectPopoverRootContext } from "reka-ui";
|
|
import OverlayAction from "../overlay/OverlayAction";
|
|
import { usePopover } from "../popover/context";
|
|
import { mergeIds } from "../field/context";
|
|
import { selectTriggerDisabledKey, useSelect } from "./context";
|
|
export default defineComponent({
|
|
name: "SelectTrigger",
|
|
inheritAttrs: false,
|
|
props: { as: { type: String, default: "button" }, isDisabled: Boolean },
|
|
setup(props, { attrs, slots, expose }) {
|
|
const ctx = useSelect();
|
|
const popup = usePopover(),
|
|
root = injectPopoverRootContext();
|
|
watch(
|
|
ctx.trigger,
|
|
(element) => {
|
|
root.triggerElement.value = element;
|
|
},
|
|
{ immediate: true },
|
|
);
|
|
root.triggerId = ctx.field.field.controlId.value;
|
|
const disabled = computed(
|
|
() =>
|
|
props.isDisabled ||
|
|
ctx.field.field.disabled.value ||
|
|
attrs.disabled === true ||
|
|
attrs.disabled === "",
|
|
);
|
|
provide(selectTriggerDisabledKey, disabled);
|
|
const focused = ref(false),
|
|
keyboard = ref(true);
|
|
function keydown(event: KeyboardEvent) {
|
|
if (
|
|
event.defaultPrevented ||
|
|
event.isComposing ||
|
|
event.keyCode === 229 ||
|
|
disabled.value
|
|
)
|
|
return;
|
|
keyboard.value = true;
|
|
if (
|
|
!ctx.open.value &&
|
|
["Backspace", "Delete"].includes(event.key) &&
|
|
ctx.clearCount.value &&
|
|
ctx.keys.value.length
|
|
) {
|
|
event.preventDefault();
|
|
ctx.clear();
|
|
return;
|
|
}
|
|
if (
|
|
!ctx.open.value &&
|
|
event.key.length === 1 &&
|
|
event.key !== " " &&
|
|
!event.ctrlKey &&
|
|
!event.metaKey &&
|
|
!event.altKey
|
|
) {
|
|
event.preventDefault();
|
|
ctx.search(event.key);
|
|
return;
|
|
}
|
|
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
event.preventDefault();
|
|
ctx.intent.value = ctx.keys.value.length
|
|
? "selected"
|
|
: event.key === "ArrowUp"
|
|
? "last"
|
|
: "first";
|
|
if (ctx.open.value) void ctx.focusCollection();
|
|
else ctx.setOpen(true);
|
|
}
|
|
}
|
|
expose({ focus: () => ctx.trigger.value?.focus(), element: ctx.trigger });
|
|
return () =>
|
|
h(
|
|
PopoverAnchor,
|
|
{ asChild: true },
|
|
{
|
|
default: () =>
|
|
h(
|
|
OverlayAction,
|
|
{
|
|
asChild: true,
|
|
isDisabled: disabled.value,
|
|
activate: popup.state.toggle,
|
|
},
|
|
{
|
|
default: () =>
|
|
h(
|
|
props.as,
|
|
mergeProps(attrs, {
|
|
ref: ctx.trigger,
|
|
id: attrs.id ?? ctx.field.field.controlId.value,
|
|
type:
|
|
props.as === "button"
|
|
? (attrs.type ?? "button")
|
|
: undefined,
|
|
role: "combobox",
|
|
"aria-haspopup": "listbox",
|
|
"aria-expanded": ctx.open.value,
|
|
"aria-controls": ctx.open.value ? ctx.listId : undefined,
|
|
"aria-required":
|
|
ctx.field.field.required.value || undefined,
|
|
"aria-invalid":
|
|
ctx.field.field.invalid.value || undefined,
|
|
"aria-disabled": disabled.value || undefined,
|
|
"aria-label": attrs["aria-label"] ?? ctx.ariaLabel(),
|
|
"aria-labelledby":
|
|
attrs["aria-labelledby"] ??
|
|
((attrs["aria-label"] ?? ctx.ariaLabel())
|
|
? undefined
|
|
: mergeIds(ctx.field.labelledBy(), ctx.valueId)),
|
|
"aria-describedby": mergeIds(
|
|
attrs["aria-describedby"] as string | undefined,
|
|
ctx.field.describedBy(),
|
|
),
|
|
disabled: disabled.value || undefined,
|
|
class: ctx.slots.value.trigger(),
|
|
"data-slot": "select-trigger",
|
|
"data-disabled": disabled.value || undefined,
|
|
"data-focused": focused.value || undefined,
|
|
"data-focus-visible":
|
|
(focused.value &&
|
|
(keyboard.value ||
|
|
ctx.trigger.value?.matches(":focus-visible"))) ||
|
|
undefined,
|
|
onKeydown: keydown,
|
|
onPointerdown: () => {
|
|
keyboard.value = false;
|
|
ctx.intent.value = "selected";
|
|
},
|
|
onFocus: () => {
|
|
focused.value = true;
|
|
},
|
|
onBlur: () => {
|
|
focused.value = false;
|
|
},
|
|
}),
|
|
slots.default?.(),
|
|
),
|
|
},
|
|
),
|
|
},
|
|
);
|
|
},
|
|
});
|
|
</script>
|