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>
|
||||
|
||||
@@ -51,6 +51,8 @@ export default defineComponent({
|
||||
tabindex: attrs.tabindex ?? -1,
|
||||
class: [ctx?.slots.value.cell(), attrs.class],
|
||||
"data-slot": "table-cell",
|
||||
"data-tree-column": isTreeColumn.value || undefined,
|
||||
"data-level": row?.level,
|
||||
colspan: props.colspan,
|
||||
},
|
||||
slots.default?.({
|
||||
|
||||
@@ -69,8 +69,15 @@ export default defineComponent({
|
||||
width,
|
||||
setWidth,
|
||||
});
|
||||
const cssWidth = (value: string | number | undefined) =>
|
||||
typeof value === "number" ? `${value}px` : value;
|
||||
const cssWidth = (value: string | number | undefined) => {
|
||||
if (value === undefined) return undefined;
|
||||
if (typeof value === "number") return `${value}px`;
|
||||
// CSS Grid fr units are not valid table widths. Treat Nfr as a flexible
|
||||
// column (native width:auto sharing remaining space). This does not
|
||||
// implement RAC fr weight proportions across columns.
|
||||
if (/^\d+(\.\d+)?fr$/.test(value)) return undefined;
|
||||
return value;
|
||||
};
|
||||
const sortDirection = computed<TableSortDirection | undefined>(() => {
|
||||
if (!props.allowsSorting || !props.id) return undefined;
|
||||
const d = ctx?.sortDescriptor.value;
|
||||
@@ -99,6 +106,10 @@ export default defineComponent({
|
||||
maxWidth: cssWidth(props.maxWidth),
|
||||
},
|
||||
"data-column-id": props.id,
|
||||
"data-default-width":
|
||||
typeof (props.width ?? width.value) === "string"
|
||||
? String(props.width ?? width.value)
|
||||
: undefined,
|
||||
"data-row-header": props.isRowHeader || undefined,
|
||||
"data-slot": "table-column",
|
||||
"data-allows-sorting": props.allowsSorting || undefined,
|
||||
|
||||
@@ -13,7 +13,16 @@ import {
|
||||
onUpdated,
|
||||
type PropType,
|
||||
} from "vue";
|
||||
import { tableContextKey, tableRowContextKey, type TableKey } from "./context";
|
||||
import {
|
||||
tableBodyContextKey,
|
||||
tableContextKey,
|
||||
tableRowContextKey,
|
||||
type TableKey,
|
||||
} from "./context";
|
||||
|
||||
/** Nested controls own their own activation; row click must not also toggle selection. */
|
||||
const INTERACTIVE_SELECTOR =
|
||||
"button, a, input, select, textarea, label, [role='button'], [role='link'], [contenteditable='true'], [data-choice-input], [data-slot='checkbox-content'], [data-slot='checkbox-control'], [data-slot='checkbox-indicator'], [data-slot='radio-content'], [data-slot='radio-control'], [data-slot='radio-indicator']";
|
||||
|
||||
export default defineComponent({
|
||||
name: "TableRow",
|
||||
@@ -28,17 +37,26 @@ export default defineComponent({
|
||||
setup(props, { slots }) {
|
||||
const attrs = useAttrs();
|
||||
const ctx = inject(tableContextKey, null);
|
||||
const bodyCtx = inject(tableBodyContextKey, null);
|
||||
const element = ref<HTMLElement | null>(null);
|
||||
let unregisterBodyRow: (() => void) | undefined;
|
||||
watch(
|
||||
[() => props.rowKey, element],
|
||||
([key, node], previous) => {
|
||||
if (previous) ctx?.navigation?.register(previous[0], null);
|
||||
if (!node?.closest("thead"))
|
||||
if (previous?.[0] != null) ctx?.navigation?.register(previous[0], null);
|
||||
if (!node?.closest("thead")) {
|
||||
ctx?.navigation?.register(key, node, () => props.isDisabled);
|
||||
if (!unregisterBodyRow && bodyCtx && node)
|
||||
unregisterBodyRow = bodyCtx.registerRow();
|
||||
}
|
||||
},
|
||||
{ flush: "sync" },
|
||||
{ flush: "sync", immediate: true },
|
||||
);
|
||||
onBeforeUnmount(() => ctx?.navigation?.register(props.rowKey, null));
|
||||
onBeforeUnmount(() => {
|
||||
ctx?.navigation?.register(props.rowKey, null);
|
||||
unregisterBodyRow?.();
|
||||
unregisterBodyRow = undefined;
|
||||
});
|
||||
onUpdated(() => ctx?.navigation?.refresh());
|
||||
const isSelected = computed(
|
||||
() => ctx?.isRowSelected(props.rowKey) ?? false,
|
||||
@@ -61,6 +79,9 @@ export default defineComponent({
|
||||
get hasChildItems() {
|
||||
return props.hasChildItems;
|
||||
},
|
||||
get level() {
|
||||
return props.level;
|
||||
},
|
||||
});
|
||||
return () => {
|
||||
// Vue DOM merge: user onClick is first in the array, so
|
||||
@@ -75,8 +96,17 @@ export default defineComponent({
|
||||
"data-selected": isSelected.value || undefined,
|
||||
"data-disabled": isDisabled.value || undefined,
|
||||
"data-expanded": isExpanded.value || undefined,
|
||||
"data-level": props.level,
|
||||
"aria-expanded": props.hasChildItems ? isExpanded.value : undefined,
|
||||
"aria-level": props.level,
|
||||
// 1-based level for CSS indent (matches upstream --table-row-level).
|
||||
style:
|
||||
props.level != null
|
||||
? ({ "--table-row-level": String(props.level) } as Record<
|
||||
string,
|
||||
string
|
||||
>)
|
||||
: undefined,
|
||||
"aria-selected":
|
||||
ctx && ctx.selectionMode.value !== "none"
|
||||
? isSelected.value
|
||||
@@ -95,11 +125,7 @@ export default defineComponent({
|
||||
if (event.defaultPrevented) return;
|
||||
if (isDisabled.value) return;
|
||||
const target = event.target as HTMLElement | null;
|
||||
if (
|
||||
target?.closest(
|
||||
"button, a, input, select, textarea, [role='button'], [role='link']",
|
||||
)
|
||||
) {
|
||||
if (target?.closest(INTERACTIVE_SELECTOR)) {
|
||||
return;
|
||||
}
|
||||
ctx?.toggleSelect(props.rowKey, event);
|
||||
@@ -108,11 +134,7 @@ export default defineComponent({
|
||||
if (event.defaultPrevented || isDisabled.value) return;
|
||||
if (event.key !== " " && event.key !== "Spacebar") return;
|
||||
const target = event.target as HTMLElement | null;
|
||||
if (
|
||||
target?.closest(
|
||||
"input, select, textarea, button, a, [role='button'], [role='link'], [contenteditable='true']",
|
||||
)
|
||||
) {
|
||||
if (target?.closest(INTERACTIVE_SELECTOR)) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
|
||||
@@ -42,11 +42,21 @@ export interface TableRowContextValue {
|
||||
isDisabled: ComputedRef<boolean>;
|
||||
isExpanded: ComputedRef<boolean>;
|
||||
hasChildItems: boolean;
|
||||
/** 1-based tree level when provided by TableRow. */
|
||||
level?: number;
|
||||
}
|
||||
|
||||
export const tableRowContextKey: InjectionKey<TableRowContextValue> =
|
||||
Symbol("hv-table-row");
|
||||
|
||||
export interface TableBodyContextValue {
|
||||
/** Register a mounted body row; returns an unregister function. */
|
||||
registerRow: () => () => void;
|
||||
}
|
||||
|
||||
export const tableBodyContextKey: InjectionKey<TableBodyContextValue> =
|
||||
Symbol("hv-table-body");
|
||||
|
||||
export interface TableColumnContextValue {
|
||||
key: ComputedRef<string | undefined>;
|
||||
allowsSorting: ComputedRef<boolean>;
|
||||
|
||||
Reference in New Issue
Block a user