feat: migrate Select with field, collection and SSR contracts
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
import { Fragment, Text, isVNode, type VNode, type VNodeChild } from "vue";
|
||||
import { assertKey, type CollectionKey } from "../collection/keys";
|
||||
import type { CollectionRecord } from "../collection/owner";
|
||||
function name(node: VNode) {
|
||||
const type = node.type as { name?: string; __name?: string };
|
||||
return type.name ?? type.__name;
|
||||
}
|
||||
function children(node: VNode): VNodeChild[] {
|
||||
if (Array.isArray(node.children)) return node.children as VNodeChild[];
|
||||
if (node.children && typeof node.children === "object") {
|
||||
const result = (
|
||||
node.children as { default?: () => VNodeChild }
|
||||
).default?.();
|
||||
return Array.isArray(result) ? result : result == null ? [] : [result];
|
||||
}
|
||||
return typeof node.children === "string" ? [node.children] : [];
|
||||
}
|
||||
function text(nodes: VNodeChild[]): string {
|
||||
return nodes
|
||||
.map((node) => {
|
||||
if (typeof node === "string" || typeof node === "number")
|
||||
return String(node);
|
||||
if (Array.isArray(node)) return text(node);
|
||||
if (!isVNode(node)) return "";
|
||||
if (node.type === Text) return String(node.children);
|
||||
// Descriptions/icons and arbitrary components are not option name sources.
|
||||
if (
|
||||
node.type === Fragment ||
|
||||
typeof node.type === "string" ||
|
||||
name(node) === "Label"
|
||||
)
|
||||
return text(children(node));
|
||||
return "";
|
||||
})
|
||||
.join("")
|
||||
.trim();
|
||||
}
|
||||
/** Inspect known collection slots only; never execute arbitrary setup/render. */
|
||||
export function planSelectItems(nodes: VNodeChild[]) {
|
||||
const records: CollectionRecord[] = [];
|
||||
let found = false,
|
||||
opaque = false;
|
||||
function add(record: CollectionRecord) {
|
||||
assertKey(record.key);
|
||||
if (records.some((item) => item.key === record.key))
|
||||
throw new Error(`Duplicate collection key: ${record.key}`);
|
||||
records.push(record);
|
||||
}
|
||||
function walk(
|
||||
nodes: VNodeChild[],
|
||||
inside = false,
|
||||
blocked: readonly CollectionKey[] = [],
|
||||
) {
|
||||
for (const node of nodes) {
|
||||
if (Array.isArray(node)) {
|
||||
walk(node, inside, blocked);
|
||||
continue;
|
||||
}
|
||||
if (!isVNode(node)) continue;
|
||||
const component = name(node);
|
||||
if (component === "ListBoxItem") {
|
||||
const key = node.props?.id;
|
||||
const disabled = node.props?.isDisabled ?? node.props?.["is-disabled"];
|
||||
add({
|
||||
key,
|
||||
text:
|
||||
node.props?.textValue ??
|
||||
node.props?.["text-value"] ??
|
||||
text(children(node)),
|
||||
disabled:
|
||||
disabled === true || disabled === "" || blocked.includes(key),
|
||||
});
|
||||
} else if (component === "ListBox") {
|
||||
found = true;
|
||||
const disabledKeys =
|
||||
node.props?.disabledKeys ?? node.props?.["disabled-keys"] ?? [];
|
||||
const keys = [...blocked, ...disabledKeys] as CollectionKey[];
|
||||
if (node.props?.items) {
|
||||
const slot = (
|
||||
node.children as { default?: (state: unknown) => VNodeChild[] }
|
||||
)?.default;
|
||||
const items = node.props.items as {
|
||||
id: CollectionKey;
|
||||
textValue?: string;
|
||||
isDisabled?: boolean;
|
||||
}[];
|
||||
items.forEach((item, index) => {
|
||||
const before = records.length;
|
||||
if (slot) {
|
||||
const rendered = slot({ item, index });
|
||||
walk(Array.isArray(rendered) ? rendered : [rendered], true, keys);
|
||||
}
|
||||
if (records.length === before)
|
||||
add({
|
||||
key: item.id,
|
||||
text: item.textValue ?? String(item.id),
|
||||
disabled: !!item.isDisabled || keys.includes(item.id),
|
||||
});
|
||||
else if (item.isDisabled)
|
||||
records.slice(before).forEach((record) => {
|
||||
record.disabled = true;
|
||||
});
|
||||
});
|
||||
} else walk(children(node), true, keys);
|
||||
} else if (
|
||||
node.type === Fragment ||
|
||||
typeof node.type === "string" ||
|
||||
["SelectPopover", "ListBoxSection"].includes(component ?? "")
|
||||
)
|
||||
walk(children(node), inside, blocked);
|
||||
else if (
|
||||
inside &&
|
||||
component &&
|
||||
!["Header", "Separator"].includes(component)
|
||||
)
|
||||
opaque = true;
|
||||
}
|
||||
}
|
||||
walk(nodes);
|
||||
return { records, complete: found && !opaque };
|
||||
}
|
||||
Reference in New Issue
Block a user