fix: anchor public pickers and preserve native Vue refs
This commit is contained in:
@@ -19,7 +19,7 @@ import {
|
||||
} from "vue";
|
||||
import { useChoiceValue } from "../choice/shared";
|
||||
import { useAutocomplete } from "./context";
|
||||
import { elementFromRef, mergedRef } from "../combo-box/dom";
|
||||
import { elementFromRef } from "../combo-box/dom";
|
||||
|
||||
function componentName(node: VNode): string | undefined {
|
||||
const type = node.type as Record<string, unknown> | undefined;
|
||||
@@ -83,11 +83,11 @@ export default defineComponent({
|
||||
composing.value = true;
|
||||
},
|
||||
onCompositionend: () => commitComposition(),
|
||||
// cloneVNode only honours a ref passed through its extra props.
|
||||
ref: mergedRef(child.ref, (instance: unknown) => {
|
||||
// Let Vue preserve the caller's normalized template ref.
|
||||
ref: (instance: unknown) => {
|
||||
ctx.filterInput.value = elementFromRef(instance) as
|
||||
HTMLInputElement | undefined;
|
||||
}),
|
||||
},
|
||||
};
|
||||
// The search input's own accessible name and description win over the
|
||||
// picker root's: the trigger name is not a name for this control.
|
||||
@@ -98,7 +98,7 @@ export default defineComponent({
|
||||
]) {
|
||||
if (own[key] !== undefined) delete injected[key];
|
||||
}
|
||||
return cloneVNode(child, injected);
|
||||
return cloneVNode(child, injected, true);
|
||||
}
|
||||
const children = child.children;
|
||||
if (Array.isArray(children)) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script lang="ts">
|
||||
/** Clickable value surface: opens the popover and is the single Tab entry. */
|
||||
import { defineComponent, h, mergeProps, ref, watch } from "vue";
|
||||
import { injectPopoverRootContext } from "reka-ui";
|
||||
import { PopoverAnchor, injectPopoverRootContext } from "reka-ui";
|
||||
import { useAutocomplete } from "./context";
|
||||
import { elementFromRef } from "../combo-box/dom";
|
||||
|
||||
@@ -13,7 +13,10 @@ export default defineComponent({
|
||||
const ctx = useAutocomplete();
|
||||
const element = ref<HTMLElement>();
|
||||
const popover = injectPopoverRootContext();
|
||||
const disabled = () => props.isDisabled || ctx.disabled.value;
|
||||
const disabled = () =>
|
||||
props.isDisabled ||
|
||||
ctx.disabled.value ||
|
||||
(attrs.disabled !== undefined && attrs.disabled !== false);
|
||||
|
||||
watch(
|
||||
element,
|
||||
@@ -79,7 +82,14 @@ export default defineComponent({
|
||||
injected["data-disabled"] = true;
|
||||
injected["aria-disabled"] = true;
|
||||
}
|
||||
return h("div", mergeProps(attrs, injected), slots.default?.());
|
||||
return h(
|
||||
PopoverAnchor,
|
||||
{ asChild: true },
|
||||
{
|
||||
default: () =>
|
||||
h("div", mergeProps(attrs, injected), slots.default?.()),
|
||||
},
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
/**
|
||||
* Case- and accent-insensitive substring match, the equivalent of ICU
|
||||
* "base" sensitivity for search: `Café` matches `cafe`, `STRASSE` matches
|
||||
* `strasse` after compatibility normalisation.
|
||||
*/
|
||||
const fold = (value: string) => value.normalize("NFKD").replace(/\p{Mn}/gu, "");
|
||||
|
||||
/** Locale-aware base-sensitivity substring matching; no hand-written accent folding. */
|
||||
export function createBaseContains(locale?: string) {
|
||||
const collator = new Intl.Collator(locale, {
|
||||
usage: "search",
|
||||
sensitivity: "base",
|
||||
});
|
||||
return (text: string, query: string) => {
|
||||
if (!query) return true;
|
||||
return fold(text)
|
||||
.toLocaleLowerCase(locale)
|
||||
.includes(fold(query).toLocaleLowerCase(locale));
|
||||
const characters = Array.from(text.normalize("NFC"));
|
||||
const needle = query.normalize("NFC");
|
||||
// Collation expansions (ß/ss, ligatures) need not have equal code-point lengths.
|
||||
for (let start = 0; start < characters.length; start++) {
|
||||
let candidate = "";
|
||||
for (let end = start; end < characters.length; end++) {
|
||||
candidate += characters[end];
|
||||
if (collator.compare(candidate, needle) === 0) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -18,9 +18,9 @@ import {
|
||||
type VNode,
|
||||
type VNodeChild,
|
||||
} from "vue";
|
||||
import { injectPopoverRootContext } from "reka-ui";
|
||||
import { PopoverAnchor, injectPopoverRootContext } from "reka-ui";
|
||||
import { useComboBox } from "./context";
|
||||
import { elementFromRef, mergedRef } from "./dom";
|
||||
import { elementFromRef } from "./dom";
|
||||
|
||||
function componentName(node: VNode): string | undefined {
|
||||
const type = node.type as Record<string, unknown> | undefined;
|
||||
@@ -67,16 +67,15 @@ export default defineComponent({
|
||||
"onUpdate:modelValue": (next: string) => ctx.setInputValue(next),
|
||||
// The root variant is inherited; an explicit Input variant still wins.
|
||||
variant: own.variant ?? ctx.variant(),
|
||||
// cloneVNode only honours a ref passed through its extra props, so the
|
||||
// caller's template ref and ours are merged here.
|
||||
ref: mergedRef(node.ref, (instance: unknown) => {
|
||||
// Vue merges normalized refs, including string/useTemplateRef/ref_for.
|
||||
ref: (instance: unknown) => {
|
||||
ctx.inputElement.value = elementFromRef(instance) as
|
||||
HTMLInputElement | undefined;
|
||||
}),
|
||||
},
|
||||
};
|
||||
for (const key of Object.keys(injected))
|
||||
if (injected[key] === undefined) delete injected[key];
|
||||
return cloneVNode(node, injected);
|
||||
return cloneVNode(node, injected, true);
|
||||
}
|
||||
|
||||
return () => {
|
||||
@@ -85,14 +84,21 @@ export default defineComponent({
|
||||
Array.isArray(rendered) ? rendered : [rendered]
|
||||
) as VNodeChild[];
|
||||
return h(
|
||||
"div",
|
||||
mergeProps(attrs, {
|
||||
ref: root,
|
||||
class: ctx.slots.value.inputGroup(),
|
||||
"data-slot": "combo-box-input-group",
|
||||
"data-disabled": ctx.disabled.value || undefined,
|
||||
}),
|
||||
children.map(compose),
|
||||
PopoverAnchor,
|
||||
{ asChild: true },
|
||||
{
|
||||
default: () =>
|
||||
h(
|
||||
"div",
|
||||
mergeProps(attrs, {
|
||||
ref: root,
|
||||
class: ctx.slots.value.inputGroup(),
|
||||
"data-slot": "combo-box-input-group",
|
||||
"data-disabled": ctx.disabled.value || undefined,
|
||||
}),
|
||||
children.map(compose),
|
||||
),
|
||||
},
|
||||
);
|
||||
};
|
||||
},
|
||||
|
||||
@@ -6,31 +6,6 @@ export function elementFromRef(value: unknown): HTMLElement | undefined {
|
||||
return element instanceof Element ? (element as HTMLElement) : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep both the caller's template ref and our internal one. Vue replaces a
|
||||
* cloned vnode's ref, so the merge has to happen here; Vue 3.5 also stores
|
||||
* component refs inside a `{ i, r }` holder that must be unwrapped.
|
||||
*/
|
||||
export function mergedRef(...refs: unknown[]) {
|
||||
const assign = (ref: unknown, instance: unknown) => {
|
||||
if (!ref) return;
|
||||
if (typeof ref === "function") {
|
||||
ref(instance);
|
||||
return;
|
||||
}
|
||||
if (typeof ref !== "object") return;
|
||||
if ("value" in (ref as object)) {
|
||||
(ref as { value: unknown }).value = instance;
|
||||
return;
|
||||
}
|
||||
const inner = (ref as { r?: unknown }).r;
|
||||
if (inner !== undefined) assign(inner, instance);
|
||||
};
|
||||
return (instance: unknown) => {
|
||||
for (const ref of refs) assign(ref, instance);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Vue vnode shape bits (not exported by the package). `cloneVNode` copies
|
||||
* `shapeFlag`, so replacing an element's children needs these to stay coherent.
|
||||
|
||||
Reference in New Issue
Block a user