160 lines
5.5 KiB
Vue
160 lines
5.5 KiB
Vue
<script lang="ts">
|
|
import {
|
|
computed,
|
|
defineComponent,
|
|
h,
|
|
inject,
|
|
mergeProps,
|
|
provide,
|
|
useAttrs,
|
|
ref,
|
|
watch,
|
|
onBeforeUnmount,
|
|
onUpdated,
|
|
type PropType,
|
|
} from "vue";
|
|
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",
|
|
inheritAttrs: false,
|
|
props: {
|
|
level: { type: Number, default: undefined },
|
|
rowKey: { type: [String, Number] as PropType<TableKey>, required: true },
|
|
isDisabled: { type: Boolean, default: false },
|
|
/** Exact virtual-row extent; omitted rows retain natural table sizing. */
|
|
height: { type: Number, default: undefined },
|
|
hasChildItems: { type: Boolean, default: false },
|
|
as: { type: String, default: "tr" },
|
|
},
|
|
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?.[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", immediate: true },
|
|
);
|
|
onBeforeUnmount(() => {
|
|
ctx?.navigation?.register(props.rowKey, null);
|
|
unregisterBodyRow?.();
|
|
unregisterBodyRow = undefined;
|
|
});
|
|
onUpdated(() => ctx?.navigation?.refresh());
|
|
const isSelected = computed(
|
|
() => ctx?.isRowSelected(props.rowKey) ?? false,
|
|
);
|
|
const isDisabled = computed(
|
|
() => props.isDisabled || (ctx?.isRowDisabled(props.rowKey) ?? false),
|
|
);
|
|
const isExpanded = computed(
|
|
() => ctx?.isRowExpanded(props.rowKey) ?? false,
|
|
);
|
|
let cellIndex = 0;
|
|
provide(tableRowContextKey, {
|
|
nextCellIndex: () => cellIndex++,
|
|
get key() {
|
|
return props.rowKey;
|
|
},
|
|
isSelected,
|
|
isDisabled,
|
|
isExpanded,
|
|
height: computed(() => props.height),
|
|
get hasChildItems() {
|
|
return props.hasChildItems;
|
|
},
|
|
get level() {
|
|
return props.level;
|
|
},
|
|
});
|
|
return () => {
|
|
// Vue DOM merge: user onClick is first in the array, so
|
|
// stopImmediatePropagation / preventDefault gate the internal handler.
|
|
return h(
|
|
props.as,
|
|
mergeProps(attrs as Record<string, unknown>, {
|
|
ref: element,
|
|
"aria-disabled": isDisabled.value || undefined,
|
|
class: [ctx?.slots.value.row(), attrs.class],
|
|
"data-slot": "table-row",
|
|
"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: {
|
|
height: props.height != null ? `${props.height}px` : undefined,
|
|
"--table-row-level":
|
|
props.level != null ? String(props.level) : undefined,
|
|
},
|
|
"aria-selected":
|
|
ctx && ctx.selectionMode.value !== "none"
|
|
? isSelected.value
|
|
: undefined,
|
|
// Selectable rows get a default keyboard entry when the caller
|
|
// does not set tabindex explicitly.
|
|
tabindex:
|
|
attrs.tabindex !== undefined
|
|
? attrs.tabindex
|
|
: ctx?.navigation
|
|
? isDisabled.value
|
|
? -1
|
|
: ctx.navigation.tabIndex(props.rowKey)
|
|
: undefined,
|
|
onClick: (event: MouseEvent) => {
|
|
if (event.defaultPrevented) return;
|
|
if (isDisabled.value) return;
|
|
const target = event.target as HTMLElement | null;
|
|
if (target?.closest(INTERACTIVE_SELECTOR)) {
|
|
return;
|
|
}
|
|
ctx?.toggleSelect(props.rowKey, event);
|
|
},
|
|
onKeydown: (event: KeyboardEvent) => {
|
|
if (event.defaultPrevented || isDisabled.value) return;
|
|
if (event.key !== " " && event.key !== "Spacebar") return;
|
|
const target = event.target as HTMLElement | null;
|
|
if (target?.closest(INTERACTIVE_SELECTOR)) {
|
|
return;
|
|
}
|
|
event.preventDefault();
|
|
ctx?.toggleSelect(props.rowKey, event);
|
|
},
|
|
}),
|
|
slots.default?.({
|
|
isSelected: isSelected.value,
|
|
isExpanded: isExpanded.value,
|
|
hasChildItems: props.hasChildItems,
|
|
toggleExpanded: () => {
|
|
if (!isDisabled.value && props.hasChildItems)
|
|
ctx?.toggleExpand(props.rowKey);
|
|
},
|
|
isDisabled: isDisabled.value,
|
|
}),
|
|
);
|
|
};
|
|
},
|
|
});
|
|
</script>
|