fix: retain pending Toast and Table fidelity corrections
This commit is contained in:
@@ -1,24 +1,187 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, h, inject, useAttrs } from "vue";
|
||||
import { tableContextKey } from "./context";
|
||||
import {
|
||||
Comment,
|
||||
Fragment,
|
||||
defineComponent,
|
||||
h,
|
||||
inject,
|
||||
isVNode,
|
||||
provide,
|
||||
onMounted,
|
||||
onUpdated,
|
||||
onBeforeUnmount,
|
||||
ref,
|
||||
useAttrs,
|
||||
type PropType,
|
||||
type VNodeChild,
|
||||
} from "vue";
|
||||
import {
|
||||
tableBodyContextKey,
|
||||
tableContextKey,
|
||||
type TableBodyContextValue,
|
||||
} from "./context";
|
||||
|
||||
function flattenChildren(
|
||||
nodes: VNodeChild[],
|
||||
out: VNodeChild[] = [],
|
||||
): VNodeChild[] {
|
||||
for (const child of nodes) {
|
||||
if (Array.isArray(child)) {
|
||||
flattenChildren(child, out);
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
isVNode(child) &&
|
||||
child.type === Fragment &&
|
||||
Array.isArray(child.children)
|
||||
) {
|
||||
flattenChildren(child.children as VNodeChild[], out);
|
||||
continue;
|
||||
}
|
||||
out.push(child);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function componentTypeName(node: VNodeChild): string | undefined {
|
||||
if (!isVNode(node)) return undefined;
|
||||
const type = node.type;
|
||||
if (typeof type === "string") return type;
|
||||
if (typeof type === "object" && type !== null && "name" in type) {
|
||||
return (type as { name?: string }).name;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vnode-level populated check that does not execute child setup/render.
|
||||
* Covers TableRow/native tr and TableCollection via its `items` prop so a
|
||||
* populated collection is not swallowed by the empty slot.
|
||||
*/
|
||||
function slotLooksPopulated(children: VNodeChild[]): boolean {
|
||||
for (const node of flattenChildren(children)) {
|
||||
if (!isVNode(node)) {
|
||||
if (typeof node === "string" && node.trim() !== "") return true;
|
||||
if (typeof node === "number") return true;
|
||||
continue;
|
||||
}
|
||||
if (node.type === Comment) continue;
|
||||
const name = componentTypeName(node);
|
||||
if (name === "TableRow" || name === "tr") return true;
|
||||
if (name === "TableCollection") {
|
||||
const items = (node.props as { items?: unknown[] } | null)?.items;
|
||||
if (Array.isArray(items) && items.length > 0) return true;
|
||||
continue;
|
||||
}
|
||||
// Unknown components stay mounted below; their actual rows are observed
|
||||
// after rendering instead of treating the component itself as a row.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
name: "TableBody",
|
||||
inheritAttrs: false,
|
||||
props: { as: { type: String, default: "tbody" } },
|
||||
props: {
|
||||
as: { type: String, default: "tbody" },
|
||||
/**
|
||||
* Force the empty state even if default slot content is present.
|
||||
* Omit to auto-detect: empty when the default slot looks unpopulated
|
||||
* (TableRow/native tr, TableCollection.items) and no rows registered.
|
||||
*/
|
||||
empty: { type: Boolean, default: undefined },
|
||||
/** Colspan of the empty-state cell. */
|
||||
colspan: {
|
||||
type: [Number, String] as PropType<number | string>,
|
||||
default: 1,
|
||||
},
|
||||
},
|
||||
setup(props, { slots }) {
|
||||
const attrs = useAttrs();
|
||||
const ctx = inject(tableContextKey, null);
|
||||
return () =>
|
||||
h(
|
||||
const rowRefCount = ref(0);
|
||||
const bodyElement = ref<HTMLElement | null>(null);
|
||||
const hasRenderedRows = ref(false);
|
||||
let observer: MutationObserver | undefined;
|
||||
function syncRenderedRows() {
|
||||
hasRenderedRows.value = Array.from(
|
||||
bodyElement.value?.children ?? [],
|
||||
).some(
|
||||
(node) =>
|
||||
node.tagName === "TR" &&
|
||||
node.getAttribute("data-slot") !== "table-empty-row",
|
||||
);
|
||||
}
|
||||
onMounted(() => {
|
||||
syncRenderedRows();
|
||||
if (typeof MutationObserver !== "undefined" && bodyElement.value) {
|
||||
observer = new MutationObserver(syncRenderedRows);
|
||||
observer.observe(bodyElement.value, { childList: true });
|
||||
}
|
||||
});
|
||||
onUpdated(syncRenderedRows);
|
||||
onBeforeUnmount(() => observer?.disconnect());
|
||||
|
||||
provide(tableBodyContextKey, {
|
||||
registerRow() {
|
||||
rowRefCount.value += 1;
|
||||
return () => {
|
||||
rowRefCount.value = Math.max(0, rowRefCount.value - 1);
|
||||
};
|
||||
},
|
||||
} satisfies TableBodyContextValue);
|
||||
|
||||
function renderEmptyRow() {
|
||||
const content = slots.empty?.();
|
||||
if (!content) return null;
|
||||
return h(
|
||||
"tr",
|
||||
{
|
||||
"data-slot": "table-empty-row",
|
||||
role: "presentation",
|
||||
style: "pointer-events: auto",
|
||||
},
|
||||
[
|
||||
h(
|
||||
"td",
|
||||
{
|
||||
class: "hv-table__empty-cell",
|
||||
"data-slot": "table-empty-cell",
|
||||
colspan: props.colspan,
|
||||
role: "presentation",
|
||||
},
|
||||
content,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
return () => {
|
||||
const children = flattenChildren(slots.default?.() ?? []);
|
||||
const looksPopulated = slotLooksPopulated(children);
|
||||
const hasRows = rowRefCount.value > 0 || hasRenderedRows.value;
|
||||
const isEmpty =
|
||||
props.empty === true ||
|
||||
(props.empty !== false && !!slots.empty && !hasRows && !looksPopulated);
|
||||
|
||||
return h(
|
||||
props.as,
|
||||
{
|
||||
...attrs,
|
||||
ref: bodyElement,
|
||||
class: [ctx?.slots.value.body(), attrs.class],
|
||||
"data-slot": "table-body",
|
||||
"data-empty": isEmpty || undefined,
|
||||
},
|
||||
slots.default?.(),
|
||||
// Automatic detection keeps wrappers mounted so later data can appear.
|
||||
// Explicit empty suppresses data rows, their selection and navigation.
|
||||
props.empty === true
|
||||
? [renderEmptyRow()]
|
||||
: isEmpty
|
||||
? [...children, renderEmptyRow()]
|
||||
: children,
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user